-
Notifications
You must be signed in to change notification settings - Fork 39
/
sway-sensible-terminal
executable file
·46 lines (38 loc) · 1.21 KB
/
sway-sensible-terminal
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#! /usr/bin/env python3
import os
import sys
import json
import subprocess
PATH = os.environ.get("PATH", "/usr/bin")
TERMINAL = os.environ.get("TERMINAL", "alacritty")
ARGS = sys.argv[1:]
def get_cwd(tree):
for node in tree.get("nodes", []):
if node["focused"]:
name = node["name"]
for part in name.split(":"):
if part.startswith("/") and os.path.exists(part):
return part
cwd = get_cwd(node)
if cwd is not None:
return cwd
if os.path.isfile("/usr/bin/swaymsg"):
prog = "swaymsg"
elif os.path.isfile("/usr/bin/i3-msg"):
prog = "i3-msg"
else:
raise Exception("Neither swaymsg or i3-msg was found in /usr/bin/")
cmd = subprocess.run(f"{prog} -t get_tree", shell=True, check=True, capture_output=True)
tree = json.loads(cmd.stdout)
cwd = get_cwd(tree)
if cwd is not None:
if "alacritty" in TERMINAL:
ARGS = ["--working-directory", cwd, *ARGS]
else:
ARGS = ["-d", cwd, *ARGS]
for d in PATH.split(":"):
path = os.path.join(d, TERMINAL)
if not os.path.isfile(path):
continue
os.execl(path, path, *ARGS)
raise Exception(f"Error: terminal '{TERMINAL}' was not found in $PATH ({PATH})")