another attempt at force open term in linux gui

This commit is contained in:
edschuy95 2026-01-22 11:11:47 -05:00
parent 257022ba6d
commit 21d6697963

View file

@ -713,44 +713,69 @@ def main():
pass pass
pygame.mixer.quit() pygame.mixer.quit()
_RELAUNCHED_ENV = "Q3A_LAUNCHED_IN_TERMINAL"
def has_terminal(): def has_terminal():
"""Check if stdout and stdin are attached to a terminal."""
try: try:
return sys.stdout.isatty() and sys.stdin.isatty() return sys.stdin.isatty() and sys.stdout.isatty()
except Exception: except Exception:
return False return False
def relaunch_in_terminal():
"""Relaunch this program inside a terminal emulator."""
# Full path to the current script or binary
script_path = os.path.abspath(sys.argv[0])
args = sys.argv[1:]
# Common terminal emulators to try def relaunch_in_terminal():
# Prevent infinite relaunch loops
if os.environ.get(_RELAUNCHED_ENV) == "1":
return
env = os.environ.copy()
env[_RELAUNCHED_ENV] = "1"
argv = [os.path.abspath(sys.argv[0])] + sys.argv[1:]
# --- 1. Preferred: xdg-terminal-exec (Bazzite / Fedora Atomic / Wayland) ---
if shutil.which("xdg-terminal-exec"):
try:
subprocess.Popen(
["xdg-terminal-exec"] + argv,
env=env
)
sys.exit(0)
except Exception:
pass # fall through to legacy terminals
# --- 2. Legacy fallback terminals ---
terminals = [ terminals = [
["konsole", "--hold", "-e"], # KDE ("konsole", ["--hold", "-e"]),
["gnome-terminal", "--", "bash", "-c"], # GNOME ("gnome-terminal", ["--", "bash", "-c"]),
["xterm", "-hold", "-e"], # X11 fallback ("xterm", ["-hold", "-e"]),
["kitty", "-e"], # Kitty ("kitty", ["-e"]),
["alacritty", "-e"] # Alacritty ("alacritty", ["-e"]),
] ]
for term in terminals: for term, args in terminals:
# Check if terminal exists if not shutil.which(term):
if shutil.which(term[0]): continue
# For GNOME Terminal, wrap command in bash -c
if "gnome-terminal" in term[0]:
cmd = term + [f'"{script_path}" {" ".join(args)}; exec bash"']
else:
cmd = term + [script_path] + args
try: try:
subprocess.Popen(cmd) if term == "gnome-terminal":
cmd = [term] + args + [
f'"{" ".join(argv)}"; exec bash'
]
else:
cmd = [term] + args + argv
subprocess.Popen(cmd, env=env)
sys.exit(0) sys.exit(0)
except Exception: except Exception:
continue continue
# If no terminal found, print a warning and exit # --- 3. Last-resort failure ---
print("No terminal emulator found. Please run this program from a terminal.") sys.stderr.write(
"Unable to open a terminal window.\n"
"Please run this application from a terminal.\n"
)
sys.exit(1) sys.exit(1)
if __name__ == "__main__": if __name__ == "__main__":