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
pygame.mixer.quit()
_RELAUNCHED_ENV = "Q3A_LAUNCHED_IN_TERMINAL"
def has_terminal():
"""Check if stdout and stdin are attached to a terminal."""
try:
return sys.stdout.isatty() and sys.stdin.isatty()
return sys.stdin.isatty() and sys.stdout.isatty()
except Exception:
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 = [
["konsole", "--hold", "-e"], # KDE
["gnome-terminal", "--", "bash", "-c"], # GNOME
["xterm", "-hold", "-e"], # X11 fallback
["kitty", "-e"], # Kitty
["alacritty", "-e"] # Alacritty
("konsole", ["--hold", "-e"]),
("gnome-terminal", ["--", "bash", "-c"]),
("xterm", ["-hold", "-e"]),
("kitty", ["-e"]),
("alacritty", ["-e"]),
]
for term in terminals:
# Check if terminal exists
if shutil.which(term[0]):
# 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:
subprocess.Popen(cmd)
sys.exit(0)
except Exception:
continue
for term, args in terminals:
if not shutil.which(term):
continue
# If no terminal found, print a warning and exit
print("No terminal emulator found. Please run this program from a terminal.")
try:
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)
except Exception:
continue
# --- 3. Last-resort failure ---
sys.stderr.write(
"Unable to open a terminal window.\n"
"Please run this application from a terminal.\n"
)
sys.exit(1)
if __name__ == "__main__":