diff --git a/RA3MP3Playback.py b/RA3MP3Playback.py index 9c27785..43866c1 100644 --- a/RA3MP3Playback.py +++ b/RA3MP3Playback.py @@ -11,6 +11,7 @@ import tempfile import psutil import subprocess import getpass +import shutil if (os.name != "nt"): import pty @@ -713,28 +714,44 @@ def main(): pygame.mixer.quit() def has_terminal(): - return sys.stdout.isatty() and sys.stdin.isatty() + """Check if stdout and stdin are attached to a terminal.""" + try: + return sys.stdout.isatty() and sys.stdin.isatty() + except Exception: + return False def relaunch_in_terminal(): - # Detect the current folder and script/binary path - script = os.path.abspath(sys.argv[0]) - - # Choose a terminal emulator (Konsole, GNOME Terminal, xterm) + """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 terminals = [ - ["konsole", "--hold", "-e"], - ["gnome-terminal", "--", "bash", "-c"], - ["xterm", "-hold", "-e"] + ["konsole", "--hold", "-e"], # KDE + ["gnome-terminal", "--", "bash", "-c"], # GNOME + ["xterm", "-hold", "-e"], # X11 fallback + ["kitty", "-e"], # Kitty + ["alacritty", "-e"] # Alacritty ] - + for term in terminals: - try: - subprocess.Popen(term + [script] + sys.argv[1:]) - sys.exit(0) # exit the current GUI-launched process - except FileNotFoundError: - continue - - # If no terminal found, just print a warning - print("No terminal emulator found. Please run from a terminal.") + # 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 + + # If no terminal found, print a warning and exit + print("No terminal emulator found. Please run this program from a terminal.") + sys.exit(1) if __name__ == "__main__": if (os.name != "nt"):