Skip to content

Commit

Permalink
Issue #6: Restore window to "correct" workspace with custom name (#8)
Browse files Browse the repository at this point in the history
* moves window back to current workspace robustly using a JSON file to store original workspace

Close #6
  • Loading branch information
tcaxle authored Feb 2, 2020
1 parent 7b43606 commit dd529b1
Showing 1 changed file with 25 additions and 13 deletions.
38 changes: 25 additions & 13 deletions quiet-cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import json
import subprocess
import os

ZEN_NUMBER = 99
ZEN_NAME = 'zen'
Expand All @@ -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):
Expand All @@ -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])

0 comments on commit dd529b1

Please sign in to comment.