Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import curses
- import subprocess
- import sys
- import os
- # ASCII art for homepage and files view
- ascii_art = r"""
- __ __ _
- \ \ / / | |
- \ \ /\ / /__| | ___ ___ _ __ ___ ___
- \ \/ \/ / _ \ |/ __/ _ \| '_ ` _ \ / _ \
- \ /\ / __/ | (_| (_) | | | | | | __/
- ___\/__\/ \___|_|\___\___/|_| |_| |_|\___| ┌──┐
- |__ __| |██|
- | | ___ /█████\ ┌────┘██└────┐
- | |/ _ \ ███████ |████████████|
- | | (_) | | | | | └────┐██┌────┘
- __|_|\___/_ └┐ _ ┌┘ _ _ |██|
- | ____| | | └─┬─┘ | ( ) |██|
- | |__ __| |_ __ ___ _| _ _ __ __| |/ ___ |██|
- __| / _` | '_ ` _ \| | | | '_ \ / _` | / __||██|
- | |___| (_| | | | | | | |_| | | | | (_| | \__ \|██|
- |______\__,_|_| |_| |_|\__,_|_| |_|\__,_| |___/|██| _
- | | | | └──┘| |
- | |__| | ___ _ __ ___ ___ _ __ __ _ __ _ ___| |
- | __ |/ _ \| '_ ` _ \ / _ \ '_ \ / _` |/ _` |/ _ \ |
- | | | | (_) | | | | | | __/ |_) | (_| | (_| | __/_|
- |_| |_|\___/|_| |_| |_|\___| .__/ \__,_|\__, |\___(_)
- | | __/ |
- |_| |___/
- """
- files_art = r"""
- homepage
- ─►files
- """
- def install_curses():
- if sys.platform.startswith('win'):
- subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'windows-curses'])
- elif sys.platform.startswith('darwin') or sys.platform.startswith('linux'):
- subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'curses'])
- # Create a file to mark that curses has been installed
- with open("curses_installed.txt", "w") as f:
- f.write("curses installed")
- def display_homepage(stdscr):
- # Clear screen
- stdscr.clear()
- # Display the ASCII art
- for idx, line in enumerate(ascii_art.splitlines()):
- stdscr.addstr(idx, 0, line)
- # Refresh the screen to update the display
- stdscr.refresh()
- def display_files_page(stdscr, folder_name):
- # Clear screen
- stdscr.clear()
- # Display the ASCII art with arrow pointing to folder_name
- for idx, line in enumerate(ascii_art.splitlines()):
- if idx == len(ascii_art.splitlines()) - 1:
- stdscr.addstr(idx, 0, "─►" + line.strip()[0] + " " + folder_name)
- else:
- stdscr.addstr(idx, 0, line)
- # Display the files inside the folder
- os.chdir(folder_name)
- files = os.listdir()
- for i, item in enumerate(files):
- stdscr.addstr(len(ascii_art.splitlines()) + 1 + i, 0, f"{item}")
- # Refresh the screen to update the display
- stdscr.refresh()
- return files
- def open_file(file_name):
- try:
- subprocess.Popen(["xdg-open", file_name]) # Linux/Unix
- except OSError:
- try:
- subprocess.Popen(["open", file_name]) # macOS
- except OSError:
- subprocess.Popen(["start", file_name], shell=True) # Windows
- def main(stdscr):
- curses.curs_set(0) # Hide the cursor
- current_path = os.getcwd()
- while True:
- display_homepage(stdscr)
- # Initialize an empty string to capture user input
- user_input = ""
- while True:
- max_y, max_x = stdscr.getmaxyx()
- stdscr.move(max_y - 1, 0)
- stdscr.clrtoeol() # Clear input line
- stdscr.refresh()
- key = stdscr.getkey()
- if key == '\n': # Enter key pressed
- break
- user_input += key
- # Check the user input
- if user_input.strip() == "files":
- try:
- display_files_page(stdscr, ".")
- except Exception as e:
- stdscr.addstr(0, 0, str(e))
- stdscr.refresh()
- stdscr.getch()
- stdscr.clear()
- stdscr.refresh()
- elif user_input.strip() in os.listdir():
- item_path = os.path.join(current_path, user_input.strip())
- if os.path.isfile(item_path):
- open_file(item_path)
- elif os.path.isdir(item_path):
- try:
- display_files_page(stdscr, user_input.strip())
- except Exception as e:
- stdscr.addstr(0, 0, str(e))
- stdscr.refresh()
- stdscr.getch()
- stdscr.clear()
- stdscr.refresh()
- if __name__ == '__main__':
- # Check if curses is already installed by looking for the marker file
- if not os.path.isfile("curses_installed.txt"):
- try:
- curses.wrapper(main)
- except ImportError:
- install_curses()
- curses.wrapper(main)
- else:
- curses.wrapper(main)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement