aboutsummaryrefslogtreecommitdiff
path: root/i3/terminal.py
diff options
context:
space:
mode:
Diffstat (limited to 'i3/terminal.py')
-rw-r--r--i3/terminal.py54
1 files changed, 54 insertions, 0 deletions
diff --git a/i3/terminal.py b/i3/terminal.py
new file mode 100644
index 0000000..3993dcd
--- /dev/null
+++ b/i3/terminal.py
@@ -0,0 +1,54 @@
+import subprocess
+
+workspace_name = '9:term'
+instance_name = 'tmux_term'
+
+
+def is_term_running():
+ p = subprocess.run(['swaymsg', '-t', 'get_tree'],
+ capture_output=True,
+ check=True)
+ return f'"app_id": "{instance_name}"' in p.stdout.decode('utf-8')
+
+
+def run_term():
+ subprocess.Popen(['alacritty', '--class', instance_name,
+ '-e', 'tmux', 'new-session', '-A', '-s', 'default'])
+
+
+def get_focused(name):
+ p = subprocess.run(['swaymsg', '-p', '-t', f'get_{name}'],
+ capture_output=True,
+ check=True)
+ for line in p.stdout.decode('utf-8').strip().split('\n'):
+ if not line or line[0] == ' ':
+ continue
+ segments = line.split(' ')
+ if segments[-1] == '(focused)':
+ return segments[1]
+
+
+def hide_term():
+ subprocess.run(['swaymsg', 'workspace', 'back_and_forth'], check=True)
+
+
+def show_term():
+ output = get_focused('outputs')
+ subprocess.run(['swaymsg', 'workspace', workspace_name], check=True)
+ subprocess.run(['swaymsg', 'move', 'workspace', 'to', output], check=True)
+ subprocess.run(['swaymsg', 'workspace', workspace_name], check=True)
+
+
+def main():
+ if not is_term_running():
+ run_term()
+
+ focused_workspace = get_focused('workspaces')
+ if focused_workspace == workspace_name:
+ hide_term()
+ else:
+ show_term()
+
+
+if __name__ == '__main__':
+ main()