i3/.config/i3/rename_workspace.py

24 lines
867 B
Python
Executable File

#!/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']]
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'])
# 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
subprocess.call(['i3', "rename", "workspace", "to", newName])