forked from justbuchanan/i3scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added script to dynamically change window shortnames
- Loading branch information
1 parent
402da44
commit 59d70c9
Showing
3 changed files
with
86 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#!/usr/bin/env python3 | ||
|
||
## This script is used to dynamically rename workspace names in i3. | ||
# When run, it presents a text field popup using zenity, in which you can type a | ||
# new name for the workspace. It is compatible with the i3-autoname-workspaces | ||
# script and renames only the "shortname" of the workspace, keeping the number | ||
# and window icons in place. | ||
# | ||
# Note that this script can be used without i3-autoname-workspaces.py | ||
# | ||
# Dependencies: | ||
# * zenity - install with system package manager | ||
# * i3ipc - install with pip | ||
|
||
import i3ipc | ||
import logging | ||
import subprocess as proc | ||
import re | ||
import sys | ||
|
||
from util import * | ||
|
||
|
||
logging.basicConfig(level=logging.INFO) | ||
|
||
i3 = i3ipc.Connection() | ||
workspace = [w for w in i3.get_workspaces() if w.focused][0] | ||
name_parts = parse_workspace_name(workspace.name) | ||
logging.info("Current workspace shortname: '%s'" % name_parts['shortname']) | ||
|
||
try: | ||
# use zenity to show a text box asking the user for a new workspace name | ||
prompt_title = "Rename Workspace:" if name_parts['shortname'] == None else "Rename Workspace '%s':" % name_parts['shortname'] | ||
response = proc.check_output(['zenity', '--entry', "--text=%s" % prompt_title]) | ||
new_shortname = response.decode('utf-8').strip() | ||
logging.info("New name from user: '%s'" % new_shortname) | ||
|
||
if ' ' in new_shortname: | ||
msg = "No spaces allowed in workspace names" | ||
logging.error(msg) | ||
proc.check_call(['zenity', '--error', '--text=%s' % msg]) | ||
sys.exit(1) | ||
|
||
|
||
name_parts['shortname'] = new_shortname | ||
new_name = construct_workspace_name(name_parts) | ||
|
||
# get the current workspace and rename it | ||
workspace = [w for w in i3.get_workspaces() if w.focused][0] | ||
res = i3.command('rename workspace "%s" to "%s"' % (workspace.name, new_name)) | ||
except proc.CalledProcessError as e: | ||
logging.info("Cancelled by user, exiting...") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import re | ||
|
||
# Used so that we can keep the workspace's name when we add icons to it. | ||
# Returns a dictionary with the following keys: 'num', 'shortname', and 'icons' | ||
# Any field that's missing in @name will be None in the returned dict | ||
def parse_workspace_name(name): | ||
return re.match('(?P<num>\d+):?(?P<shortname>\w+)? ?(?P<icons>.+)?', name).groupdict() | ||
|
||
# Given a dictionary with 'num', 'shortname', 'icons', return the formatted name | ||
# by concatenating them together. | ||
def construct_workspace_name(parts): | ||
new_name = str(parts['num']) | ||
if parts['shortname'] or parts['icons']: | ||
new_name += ':' | ||
|
||
if parts['shortname']: | ||
new_name += parts['shortname'] | ||
|
||
if parts['icons']: | ||
new_name += ' ' + parts['icons'] | ||
|
||
return new_name |