Brad Bishop | bec4ebc | 2022-08-03 09:55:16 -0400 | [diff] [blame] | 1 | import shutil |
| 2 | import collections |
| 3 | import pathlib |
| 4 | import os |
| 5 | |
| 6 | from typing import List, Optional |
| 7 | |
| 8 | |
| 9 | def 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 | |
| 16 | class 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 | |
| 53 | terminals = Terminals() |
| 54 | # TODO: option to switch between telnet and netcat |
| 55 | connect_command = "telnet localhost %port" |
| 56 | terminals.add_terminal(2, "tmux", f"tmux new-window -n \"%title\" \"{connect_command}\""), |
| 57 | terminals.add_terminal(2, "gnome-terminal", f"gnome-terminal --window --title \"%title\" --command \"{connect_command}\""), |
| 58 | terminals.add_terminal(1, "xterm", f"xterm -title \"%title\" -e {connect_command}"), |
| 59 | terminals.add_terminal(0, "none", None) |