Use i3ipc for Python scripts

This commit is contained in:
Adam Goldsmith 2022-03-10 11:40:34 -05:00
parent 0d9eece16d
commit cbdedf626d
2 changed files with 20 additions and 20 deletions

View File

@ -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)}")

View File

@ -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}")