Replace rename_workspaces.sh by Python script which behaves better

also doesn't require jq
This commit is contained in:
Adam Goldsmith 2018-02-27 17:38:32 -05:00
parent 0c7f9234f3
commit 3ce43395a0
3 changed files with 23 additions and 14 deletions

View File

@ -54,7 +54,7 @@ bindsym $mod+space exec --no-startup-id "rofi $rofiOptions -show drun"
bindsym $mod+d exec --no-startup-id "rofi $rofiOptions -show run"
bindsym $mod+t exec --no-startup-id "rofi $rofiOptions -show sworkspace"
bindsym $mod+Shift+t exec --no-startup-id "rofi $rofiOptions -show mworkspace"
bindsym $mod+n exec $config_dir/rename_workspace.sh
bindsym $mod+n exec $config_dir/rename_workspace.py
bindsym $mod+c exec --no-startup-id rofi-pass

22
.config/i3/rename_workspace.py Executable file
View File

@ -0,0 +1,22 @@
#!/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
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])

View File

@ -1,13 +0,0 @@
#!/bin/bash
num=`i3-msg -t get_workspaces | jq 'map(select(.focused == true))[0].num'`
i3-input -F "rename workspace to \"$num:%s\"" -P "New name: $num:"
name=`i3-msg -t get_workspaces | jq 'map(select(.focused == true))[0].name'`
# If empty name was set
echo "$name"
if [[ "$name" =~ ^\"[0-9]+:\ *\"$ ]]
then
# Remove trailing comma and whitespace
i3-msg "rename workspace to \"$num\""
fi