Advertisement
EdmundC

home

Jul 18th, 2024 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.17 KB | None | 0 0
  1. import curses
  2. import subprocess
  3. import sys
  4. import os
  5.  
  6. # ASCII art for homepage and files view
  7. ascii_art = r"""
  8. __          __  _                                        
  9. \ \       / / | |                                      
  10.  \ \ /\ / /__| | ___ ___  _ __ ___   ___              
  11.   \ \/  \/ / _ \ |/ __/ _ \| '_ ` _ \ / _ \            
  12.    \ /\ /  __/ | (_| (_) | | | | | |  __/              
  13.  ___\/__\/ \___|_|\___\___/|_| |_| |_|\___|    ┌──┐      
  14. |__   __|                                      |██|      
  15.    | | ___           /█████\             ┌────┘██└────┐
  16.    | |/ _ \         ███████              |████████████|
  17.    | | (_) |         | | | |              └────┐██┌────┘
  18.  __|_|\___/_         └┐ _ ┌┘           _ _     |██|      
  19. |  ____|  | |         └─┬─┘           | ( )    |██|      
  20. | |__   __| |_ __ ___  _|  _ _ __   __| |/ ___ |██|      
  21.  __| / _` | '_ ` _ \| | | | '_ \ / _` | / __||██|      
  22. | |___| (_| | | | | | | |_| | | | | (_| | \__ \|██|      
  23. |______\__,_|_| |_| |_|\__,_|_| |_|\__,_| |___/|██| _    
  24. | |  | |                                       └──┘| |  
  25. | |__| | ___  _ __ ___   ___ _ __   __ _  __ _  ___| |  
  26. |  __  |/ _ \| '_ ` _ \ / _ \ '_ \ / _` |/ _` |/ _ \ |  
  27. | |  | | (_) | | | | | |  __/ |_) | (_| | (_| |  __/_|  
  28. |_|  |_|\___/|_| |_| |_|\___| .__/ \__,_|\__, |\___(_)  
  29.                             | |           __/ |          
  30.                             |_|          |___/          
  31. """
  32.  
  33. files_art = r"""
  34. homepage
  35. ─►files  
  36. """
  37.  
  38. def install_curses():
  39.     if sys.platform.startswith('win'):
  40.         subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'windows-curses'])
  41.     elif sys.platform.startswith('darwin') or sys.platform.startswith('linux'):
  42.         subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'curses'])
  43.     # Create a file to mark that curses has been installed
  44.     with open("curses_installed.txt", "w") as f:
  45.         f.write("curses installed")
  46.  
  47. def display_homepage(stdscr):
  48.     # Clear screen
  49.     stdscr.clear()
  50.  
  51.     # Display the ASCII art
  52.     for idx, line in enumerate(ascii_art.splitlines()):
  53.         stdscr.addstr(idx, 0, line)
  54.  
  55.     # Refresh the screen to update the display
  56.     stdscr.refresh()
  57.  
  58. def display_files_page(stdscr, folder_name):
  59.     # Clear screen
  60.     stdscr.clear()
  61.  
  62.     # Display the ASCII art with arrow pointing to folder_name
  63.     for idx, line in enumerate(ascii_art.splitlines()):
  64.         if idx == len(ascii_art.splitlines()) - 1:
  65.             stdscr.addstr(idx, 0, "─►" + line.strip()[0] + " " + folder_name)
  66.         else:
  67.             stdscr.addstr(idx, 0, line)
  68.  
  69.     # Display the files inside the folder
  70.     os.chdir(folder_name)
  71.     files = os.listdir()
  72.     for i, item in enumerate(files):
  73.         stdscr.addstr(len(ascii_art.splitlines()) + 1 + i, 0, f"{item}")
  74.  
  75.     # Refresh the screen to update the display
  76.     stdscr.refresh()
  77.  
  78.     return files
  79.  
  80. def open_file(file_name):
  81.     try:
  82.         subprocess.Popen(["xdg-open", file_name])  # Linux/Unix
  83.     except OSError:
  84.         try:
  85.             subprocess.Popen(["open", file_name])  # macOS
  86.         except OSError:
  87.             subprocess.Popen(["start", file_name], shell=True)  # Windows
  88.  
  89. def main(stdscr):
  90.     curses.curs_set(0)  # Hide the cursor
  91.     current_path = os.getcwd()
  92.     while True:
  93.         display_homepage(stdscr)
  94.  
  95.         # Initialize an empty string to capture user input
  96.         user_input = ""
  97.         while True:
  98.             max_y, max_x = stdscr.getmaxyx()
  99.             stdscr.move(max_y - 1, 0)
  100.             stdscr.clrtoeol()  # Clear input line
  101.             stdscr.refresh()
  102.  
  103.             key = stdscr.getkey()
  104.             if key == '\n':  # Enter key pressed
  105.                 break
  106.             user_input += key
  107.  
  108.         # Check the user input
  109.         if user_input.strip() == "files":
  110.             try:
  111.                 display_files_page(stdscr, ".")
  112.             except Exception as e:
  113.                 stdscr.addstr(0, 0, str(e))
  114.                 stdscr.refresh()
  115.                 stdscr.getch()
  116.                 stdscr.clear()
  117.                 stdscr.refresh()
  118.         elif user_input.strip() in os.listdir():
  119.             item_path = os.path.join(current_path, user_input.strip())
  120.             if os.path.isfile(item_path):
  121.                 open_file(item_path)
  122.             elif os.path.isdir(item_path):
  123.                 try:
  124.                     display_files_page(stdscr, user_input.strip())
  125.                 except Exception as e:
  126.                     stdscr.addstr(0, 0, str(e))
  127.                     stdscr.refresh()
  128.                     stdscr.getch()
  129.                     stdscr.clear()
  130.                     stdscr.refresh()
  131.  
  132. if __name__ == '__main__':
  133.     # Check if curses is already installed by looking for the marker file
  134.     if not os.path.isfile("curses_installed.txt"):
  135.         try:
  136.             curses.wrapper(main)
  137.         except ImportError:
  138.             install_curses()
  139.             curses.wrapper(main)
  140.     else:
  141.         curses.wrapper(main)
  142.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement