From dd529b1a505e49bd77dbce76f3da9d79ad6a7851 Mon Sep 17 00:00:00 2001 From: Tom Crossley <38703509+tcacrossley@users.noreply.github.com> Date: Sun, 2 Feb 2020 20:53:02 +0000 Subject: [PATCH] Issue #6: Restore window to "correct" workspace with custom name (#8) * moves window back to current workspace robustly using a JSON file to store original workspace Close #6 --- quiet-cmd.py | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/quiet-cmd.py b/quiet-cmd.py index f24ff19..cf49117 100755 --- a/quiet-cmd.py +++ b/quiet-cmd.py @@ -3,6 +3,7 @@ import sys import json import subprocess +import os ZEN_NUMBER = 99 ZEN_NAME = 'zen' @@ -12,26 +13,33 @@ HEIGHT_PERC=90 MAX_HEIGHT=1024 -def enable_zen_mode(workspace_current, workspace_width, workspace_height): - width = round(min(workspace_width * WIDTH_PERC / 100, MAX_WIDTH)) - height = round(min(workspace_height * HEIGHT_PERC / 100, MAX_HEIGHT)) - workspace_name = '{}: {}'.format(ZEN_NUMBER, workspace_current) +ZEN_BORDER = 'none' +DEFAULT_BORDER = 'none' + +ZEN_FILE_PATH = os.path.join(os.environ['HOME'], '.config/i3/.i3-quiet.json') + +def enable_zen_mode(workspace): + width = round(min(workspace['rect']['width'] * WIDTH_PERC / 100, MAX_WIDTH)) + height = round(min(workspace['rect']['height'] * HEIGHT_PERC / 100, MAX_HEIGHT)) + workspace_name = '{}: {}'.format(ZEN_NUMBER, workspace['name']) + with open(ZEN_FILE_PATH, 'w') as zen_file: + json.dump(workspace, zen_file) return ';'.join([ 'move container to workspace {}'.format(workspace_name), 'workspace {}'.format(workspace_name), 'floating enable', - 'border none', + 'border {}'.format(ZEN_BORDER), 'resize set width {}'.format(width), 'resize set height {}'.format(height), 'move position center' ]) -def disable_zen_mode(workspace_previous): +def disable_zen_mode(workspace): return ';'.join([ - 'move container to workspace {}'.format(workspace_previous), - 'workspace {}'.format(workspace_previous), + 'move container to workspace number {}'.format(workspace['num']), + 'workspace number {}'.format(workspace['num']), 'floating disable', - 'border normal' + 'border {}'.format(DEFAULT_BORDER) ]) def goto_zen(workspace_name): @@ -40,19 +48,23 @@ def goto_zen(workspace_name): out = subprocess.run(['i3-msg', '-t', 'get_workspaces'], capture_output=True) workspaces = json.loads(out.stdout) +with open(ZEN_FILE_PATH, 'r') as zen_file: + try: + workspace_previous = json.load(zen_file) + except: + workspace_previous = None workspace_current = list(filter(lambda w: w['focused'], workspaces))[0] workspace_zen = list(filter(lambda w: w['num'] == ZEN_NUMBER, workspaces)) if __name__ == '__main__': if len(sys.argv) > 1: if len(workspace_zen): - msg = disable_zen_mode(workspace_zen[0]['name'].split(':')[1].strip()) + msg = disable_zen_mode(workspace_previous) else: if len(workspace_zen): msg = goto_zen(workspace_zen[0]['name']) else: - msg = enable_zen_mode(workspace_current['num'], - workspace_current['rect']['width'], - workspace_current['rect']['height']) + msg = enable_zen_mode(workspace_current) print(msg) subprocess.run(['i3-msg', msg]) +