diff --git a/.config/i3/compact_workspace_numbers.py b/.config/i3/compact_workspace_numbers.py index 629d6c9..5dd4b32 100755 --- a/.config/i3/compact_workspace_numbers.py +++ b/.config/i3/compact_workspace_numbers.py @@ -1,19 +1,18 @@ #!/usr/bin/env python3 -import subprocess -import json from itertools import groupby -workspaces = json.loads( - subprocess.check_output(["i3-msg", "-t", "get_workspaces"])) +import i3ipc -for tens, workspace_group in groupby(workspaces, lambda x: x['num'] // 10): +i3 = i3ipc.Connection() +workspaces = i3.get_workspaces() + +for tens, workspace_group in groupby(workspaces, lambda ws: ws.num // 10): workspaces = [ws for ws in workspace_group - if ws['num'] != -1 and ws['num'] % 10 != 0] + if ws.num != -1 and ws.num % 10 != 0] - workspaces.sort(key=lambda x: x['num']) + workspaces.sort(key=lambda ws: ws.num) for num, ws in enumerate(workspaces, 1): - name = ws['name'].split(":") + name = ws.name.split(":") name[0] = str(tens * 10 + num) - subprocess.run( - ["i3", "rename", "workspace", ws['name'], "to", ":".join(name)]) + i3.command(f"rename workspace {ws.name} to {':'.join(name)}") diff --git a/.config/i3/rename_workspace.py b/.config/i3/rename_workspace.py index de6e5e8..f40446c 100755 --- a/.config/i3/rename_workspace.py +++ b/.config/i3/rename_workspace.py @@ -1,23 +1,24 @@ #!/usr/bin/env python3 import subprocess -import json import re -workspaces = json.loads( - subprocess.check_output(["i3-msg", "-t", "get_workspaces"])) -focused = [ws for ws in workspaces if ws['focused']] +import i3ipc -if len(focused) == 1: # I have no idea when it wouldn't be, but whatever +i3 = i3ipc.Connection() +workspaces = i3.get_workspaces() +focused = [ws for ws in workspaces if ws.focused] + +if len(focused) == 1: # I have no idea when it wouldn't be, but whatever inputName = subprocess.check_output( - ['rofi', '-dmenu', '-l', '0', '-p', f'New name']).decode().strip() - oldNum = str(focused[0]['num']) + ['rofi', '-dmenu', '-l', '0', '-p', 'New name']).decode().strip() + oldNum = str(focused[0].num) # basically either take raw input, allow clearing text part of a name, # or add input to current number # TODO: handle renaming of workspace without number to another name without a number newName = inputName if re.match('^[0-9]+:', inputName) is not None else \ - oldNum if inputName == "" else \ - oldNum + ':' + inputName + oldNum if inputName == "" else \ + oldNum + ':' + inputName - subprocess.call(['i3', "rename", "workspace", "to", newName]) + i3.command(f"rename workspace to {newName}")