diff options
| author | bozo.kopic <bozo@kopic.xyz> | 2020-10-14 23:57:21 +0200 |
|---|---|---|
| committer | bozo.kopic <bozo@kopic.xyz> | 2020-10-14 23:57:21 +0200 |
| commit | a7f6bcbf2930fda0547136491bf1a67c3b337224 (patch) | |
| tree | ac912071501f523741c2629a5ab99d07384f1ded /i3/terminal.py | |
| parent | 2dcb41cd467d47a4c867b91cf5b26a4158937e7d (diff) | |
.
Diffstat (limited to 'i3/terminal.py')
| -rw-r--r-- | i3/terminal.py | 54 |
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() |
