summaryrefslogtreecommitdiff
path: root/scripts/Scripts/open_rel
blob: a71df0ad5df1e110144c57e039f6ae8306d8e7b4 (plain)
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
47
48
49
50
51
52
53
54
#!/usr/bin/env python3

import re
import subprocess as sp

import argparse


def get_window_title() -> str:
    command = ["xdotool", "getactivewindow", "getwindowname"]
    ret = sp.run(command, capture_output=True)

    return ret.stdout.decode("utf-8")[:-1]


def launch_term(command=None, directory=None):
    run = ["i3-sensible-terminal"]
    if command:
        run.extend(["-e", command])
    if directory:
        run.extend(["-d", directory])

    print("Running:", run)
    sp.run(run)


def decide_and_run(title: str, alt=False):
    if title.startswith("fish "):
        launch_term(directory=title[5:])
    elif (m := re.search(".*client\d*@\[(\d*)\] - Kakoune", title)):
        id = m.group(1)
        launch_term(command=f"kak -c {id}")
    elif (m := re.search("nix-shell ([^ ]*)( +([^ ]*))?", title)):
        shell_file = m.group(1)
        folder = m.group(3)
        if folder is None:
            folder = shell_file
            shell_file = None
        if alt:
            command = "nix-shell" + (f" {shell_file}" if shell_file else "")
        else:
            command = None

        launch_term(command=command,
                    directory=folder)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--alt", "-a", action="store_true", help="Alternative action")
    args = parser.parse_args()
    title = get_window_title()

    decide_and_run(title, args.alt)