|
| 1 | +import subprocess |
| 2 | +import sys |
| 3 | +import os |
| 4 | +from colorama import init, Fore, Style |
| 5 | + |
| 6 | +# Initialize colorama |
| 7 | +init(autoreset=True) |
| 8 | + |
| 9 | +def resource_path(relative_path): |
| 10 | + """Get the absolute path to a resource, works for PyInstaller""" |
| 11 | + if getattr(sys, '_MEIPASS', False): # If running in a PyInstaller bundle |
| 12 | + return os.path.join(sys._MEIPASS, relative_path) |
| 13 | + return os.path.join(os.path.abspath("."), relative_path) |
| 14 | + |
| 15 | +def print_ascii_banner(): |
| 16 | + banner = """ |
| 17 | +{} |
| 18 | + :: |
| 19 | + %% |
| 20 | + %% |
| 21 | + #*:*##%#+. -*###%#= -*%###+:%% .=#####+: .+#%%%#+: =*%%%%#+. .=*%%%%#=. *#+=#%%%#=. :+#%%%#+: |
| 22 | + %%#. -%# #%- :#%. +%+ -%%% :%# *%- .%%%:.-**= .%%%#)%%%. -%%% %%%: #%%%*==*%%%: =%%# *%%= |
| 23 | + %% #% *%- .%# :%+ .%% %%=::::::%% #%%+-:. *%%: .%%%: .%%# #%%. :%%% :%%%=====#%% |
| 24 | + %# *% %%. #% =%- %% .%%========= -+*#%%%+ #%% .%%# *%% #%* #%%.-%%. |
| 25 | + %# *% =%+ -%+ .%# =%% *%- -. .-+: +%%- =%%+. =+-. #%%+. .=%%* #%%= .+%%* %%%: ==. |
| 26 | + %# *% :##=--=#%= :#%+--=#*%% =%*=--+%#: .*%%%##%%* =#%%%%%%%+ +%%%%%%%%= #%%#%%%%%%= .*%%%#%%%#: |
| 27 | + -- :- :-===: .===-. -- :===-. .-===-. :-===- :====: #%# :===- -===-. |
| 28 | + #%# |
| 29 | + #%# |
| 30 | + |
| 31 | + Nodescope: an XML Editor and Visualizer |
| 32 | +{}""".format(Fore.CYAN, Style.RESET_ALL) |
| 33 | + print(banner) |
| 34 | + |
| 35 | +def interactive_loop(): |
| 36 | + print_ascii_banner() |
| 37 | + try: |
| 38 | + from src.cli.cli_handler import main as cli_main # Import the CLI handler's main function |
| 39 | + except ImportError as e: |
| 40 | + print(f"{Fore.RED}Error importing CLI handler: {e}{Style.RESET_ALL}") |
| 41 | + sys.exit(1) |
| 42 | + |
| 43 | + while True: |
| 44 | + try: |
| 45 | + command = input(f"{Fore.GREEN}>> {Style.RESET_ALL}").strip() |
| 46 | + if command.lower() in ["exit", "quit"]: |
| 47 | + print(f"{Fore.LIGHTRED_EX}Exiting CLI mode. Goodbye!{Style.RESET_ALL}") |
| 48 | + sys.exit(0) |
| 49 | + elif not command: |
| 50 | + continue # Skip empty commands |
| 51 | + |
| 52 | + # Split the command into arguments |
| 53 | + args = command.split() |
| 54 | + |
| 55 | + # Backup original sys.argv |
| 56 | + original_argv = sys.argv.copy() |
| 57 | + |
| 58 | + # Set sys.argv to mimic command-line arguments |
| 59 | + sys.argv = [sys.executable, *args] |
| 60 | + |
| 61 | + try: |
| 62 | + cli_main() # Call the CLI handler's main function |
| 63 | + except SystemExit: |
| 64 | + # argparse may call sys.exit(), which raises SystemExit |
| 65 | + pass |
| 66 | + finally: |
| 67 | + # Restore original sys.argv |
| 68 | + sys.argv = original_argv |
| 69 | + except KeyboardInterrupt: |
| 70 | + print(f"\n{Fore.LIGHTRED_EX}Exiting CLI mode. Goodbye!{Style.RESET_ALL}") |
| 71 | + sys.exit(0) |
| 72 | + except Exception as e: |
| 73 | + print(f"{Fore.RED}An unexpected error occurred: {e}{Style.RESET_ALL}") |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + interactive_loop() |
0 commit comments