1import shutil
2import collections
3import pathlib
4import os
5
6from typing import List, Optional
7
8
9def get_config_dir() -> pathlib.Path:
10    value = os.environ.get("XDG_CONFIG_HOME")
11    if value and os.path.isabs(value):
12        return pathlib.Path(value)
13    else:
14        return pathlib.Path.home() / ".config"
15
16class Terminals:
17    Terminal = collections.namedtuple("Terminal", ["priority", "name", "command"])
18
19    def __init__(self):
20        self.terminals = []
21
22    def add_terminal(self, priority, name, command):
23        self.terminals.append(Terminals.Terminal(priority, name, command))
24        # Keep this list sorted by priority
25        self.terminals.sort(reverse=True, key=lambda t: t.priority)
26        self.name_map = {t.name: t for t in self.terminals}
27
28    def configured_terminal(self) -> Optional[str]:
29        import configparser
30
31        config = configparser.ConfigParser()
32        config.read(get_config_dir() / "runfvp.conf")
33        return config.get("RunFVP", "Terminal", fallback=None)
34
35    def preferred_terminal(self) -> str:
36        import shlex
37
38        preferred = self.configured_terminal()
39        if preferred:
40            return preferred
41
42        for t in self.terminals:
43            if t.command and shutil.which(shlex.split(t.command)[0]):
44                return t.name
45        return self.terminals[-1].name
46
47    def all_terminals(self) -> List[str]:
48        return self.name_map.keys()
49
50    def __getitem__(self, name: str):
51        return self.name_map[name]
52
53terminals = Terminals()
54# TODO: option to switch between telnet and netcat
55connect_command = "telnet localhost %port"
56terminals.add_terminal(2, "tmux", f"tmux new-window -n \"%title\" \"{connect_command}\"")
57terminals.add_terminal(2, "gnome-terminal", f"gnome-terminal --window --title \"%title\" --command \"{connect_command}\"")
58terminals.add_terminal(1, "xterm", f"xterm -title \"%title\" -e {connect_command}")
59terminals.add_terminal(0, "none", None)
60