hypr: script to organize workspaces

This commit is contained in:
2025-08-02 21:27:45 +02:00
parent 2b576d429b
commit ee4663da4d
3 changed files with 59 additions and 1 deletions

0
.scripts/modify-psname.py Normal file → Executable file
View File

58
.scripts/workspace-org.py Executable file
View File

@@ -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())

View File

@@ -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