|
16 | 16 | import os |
17 | 17 | import re |
18 | 18 | import shutil |
| 19 | +import shlex |
19 | 20 | import subprocess |
20 | 21 | import sys |
21 | 22 | import tempfile |
@@ -58,6 +59,47 @@ def ensure_default_files(): |
58 | 59 | "SNOWFLAKE_ROLE", |
59 | 60 | ] |
60 | 61 |
|
| 62 | + |
| 63 | +def relaunch_in_terminal() -> bool: |
| 64 | + """Relaunch this script in a terminal window. |
| 65 | +
|
| 66 | + Prefers Ghostty when available on macOS or Linux. Falls back to the |
| 67 | + default system terminal when Ghostty isn't installed. Returns True if a |
| 68 | + new terminal was spawned and the caller should exit.""" |
| 69 | + |
| 70 | + if os.environ.get("VICTORIA_IN_TERMINAL"): |
| 71 | + return False |
| 72 | + |
| 73 | + cmd: List[str] = [sys.executable, str(Path(__file__).resolve())] + sys.argv[1:] |
| 74 | + env = os.environ.copy() |
| 75 | + env["VICTORIA_IN_TERMINAL"] = "1" |
| 76 | + |
| 77 | + ghostty = shutil.which("ghostty") |
| 78 | + if ghostty and sys.platform != "win32": |
| 79 | + try: |
| 80 | + subprocess.Popen([ghostty, "-e", *cmd], env=env) |
| 81 | + return True |
| 82 | + except Exception: |
| 83 | + pass |
| 84 | + |
| 85 | + if sys.platform == "darwin": |
| 86 | + try: |
| 87 | + osa_cmd = f'tell application "Terminal" to do script "{shlex.join(cmd)}"' |
| 88 | + subprocess.Popen(["osascript", "-e", osa_cmd], env=env) |
| 89 | + return True |
| 90 | + except Exception: |
| 91 | + pass |
| 92 | + elif sys.platform.startswith("linux"): |
| 93 | + term = shutil.which("x-terminal-emulator") |
| 94 | + if term: |
| 95 | + try: |
| 96 | + subprocess.Popen([term, "-e", *cmd], env=env) |
| 97 | + return True |
| 98 | + except Exception: |
| 99 | + pass |
| 100 | + |
| 101 | + return False |
| 102 | + |
61 | 103 | # ------------------ Terminal Capability Detection ------------------ |
62 | 104 | def _enable_windows_ansi(): |
63 | 105 | """Best-effort enable ANSI on Windows without external deps.""" |
@@ -777,6 +819,8 @@ def main(): |
777 | 819 | launch_tool() |
778 | 820 |
|
779 | 821 | if __name__ == "__main__": |
| 822 | + if relaunch_in_terminal(): |
| 823 | + sys.exit(0) |
780 | 824 | try: |
781 | 825 | main() |
782 | 826 | except KeyboardInterrupt: |
|
0 commit comments