From ee4663da4dfd925fadc0dc7ff186a2a05716ad6f Mon Sep 17 00:00:00 2001 From: Uyanide Date: Sat, 2 Aug 2025 21:27:45 +0200 Subject: [PATCH] hypr: script to organize workspaces --- .scripts/modify-psname.py | 0 .scripts/workspace-org.py | 58 +++++++++++++++++++++++++++++++++++++ hypr/hyprland/keybinds.conf | 2 +- 3 files changed, 59 insertions(+), 1 deletion(-) mode change 100644 => 100755 .scripts/modify-psname.py create mode 100755 .scripts/workspace-org.py diff --git a/.scripts/modify-psname.py b/.scripts/modify-psname.py old mode 100644 new mode 100755 diff --git a/.scripts/workspace-org.py b/.scripts/workspace-org.py new file mode 100755 index 0000000..f16445e --- /dev/null +++ b/.scripts/workspace-org.py @@ -0,0 +1,58 @@ +#!/bin/env python3 +''' +Author: Uyanide pywang0608@foxmail.com +Date: 2025-08-02 20:56:46 +LastEditTime: 2025-08-02 21:27:07 +Discription: Organize Hyprland workspaces from 1 to N, where N is the number of workspaces with windows, + e.g. 3 5 6 -> 1 2 3. + One must have xtra-dispatchers plugin installed and enabled to use this script. +''' + +import subprocess +import re + + +def get_active_workspace_ids() -> list[int]: + """Get workspace IDs that have windows > 0""" + try: + result = subprocess.run(['hyprctl', 'workspaces'], capture_output=True, text=True, check=True) + output = result.stdout + except subprocess.CalledProcessError: + return [] + + workspace_ids = [] + current_id : int | None = None + + for line in output.split('\n'): + # Match workspace ID line + workspace_match = re.match(r'^workspace ID (\d+)', line) + if workspace_match: + current_id = int(workspace_match.group(1)) + continue + + # Match windows count line + # Only consider workspaces with at least one window + windows_match = re.match(r'\s+windows: (\d+)', line) + if windows_match and current_id is not None: + windows_count = int(windows_match.group(1)) + if windows_count > 0: + workspace_ids.append(current_id) + current_id = None + + return sorted(workspace_ids) + + +def organize_workspaces(ids: list[int]) -> None: + """Reorganize workspaces to consecutive numbers starting from 1""" + if not ids: + return + moveto = 1 + for workspace_id in ids: + if moveto != workspace_id: + subprocess.run(['hyprctl', 'dispatch', 'workspace', str(moveto)], check=False) + subprocess.run(['hyprctl', 'dispatch', 'plugin:xtd:bringallfrom', str(workspace_id)], check=False) + moveto += 1 + + +if __name__ == '__main__': + organize_workspaces(get_active_workspace_ids()) diff --git a/hypr/hyprland/keybinds.conf b/hypr/hyprland/keybinds.conf index ee98001..68555d1 100755 --- a/hypr/hyprland/keybinds.conf +++ b/hypr/hyprland/keybinds.conf @@ -103,7 +103,7 @@ bind = Super+Alt, F, fullscreenstate, 0 3 # Toggle fake fullscreen bind = Super, F, fullscreen, 0 bind = Super, D, fullscreen, 1 bind = Super, J, togglesplit -# bind = Super, P, pseudo +bind = Super, O, exec, workspace-org.py # Organize windows in workspace #! ##! Workspace navigation