Created
September 1, 2025 11:00
-
-
Save CypherpunkSamurai/984bfa1e7706434e991442ca7ecc6980 to your computer and use it in GitHub Desktop.
shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
A basic shell with TUI using only Python standard libraries. | |
Works cross-platform (Windows, Linux, macOS). | |
""" | |
import os | |
import sys | |
import subprocess | |
# ANSI escape codes for colors and formatting | |
class ANSI: | |
# Formatting | |
RESET = '\033[0m' | |
BOLD = '\033[1m' | |
# Colors | |
GREEN = '\033[32m' | |
BLUE = '\033[34m' | |
RED = '\033[31m' | |
# Cursor and screen | |
CLEAR_SCREEN = '\033[2J' | |
CURSOR_HOME = '\033[H' | |
def enable_windows_ansi(): | |
"""Enable ANSI escape sequences on Windows""" | |
if sys.platform == "win32": | |
try: | |
import ctypes | |
kernel32 = ctypes.windll.kernel32 | |
kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7) | |
except: | |
pass | |
def get_prompt(): | |
"""Generate a simple cross-platform prompt""" | |
try: | |
cwd = os.getcwd() | |
# Shorten home directory path | |
home = os.path.expanduser("~") | |
if cwd.startswith(home): | |
cwd = "~" + cwd[len(home):] | |
except: | |
cwd = "." | |
return f"{ANSI.GREEN}> {ANSI.RESET}" | |
def execute_command(command): | |
"""Execute a command and return its output""" | |
command = command.strip() | |
if not command: | |
return "" | |
# Built-in commands | |
if command == "exit": | |
return None # Special signal to exit | |
elif command == "clear": | |
# Return special clear screen sequence | |
return ANSI.CLEAR_SCREEN + ANSI.CURSOR_HOME | |
elif command.startswith("cd "): | |
try: | |
path = command[3:].strip() | |
os.chdir(os.path.expanduser(path)) | |
return "" | |
except Exception as e: | |
return f"cd: {e}" | |
# External commands | |
try: | |
result = subprocess.run( | |
command, | |
shell=True, | |
capture_output=True, | |
text=True, | |
timeout=10 | |
) | |
output = result.stdout | |
if result.stderr: | |
output += result.stderr | |
return output | |
except subprocess.TimeoutExpired: | |
return "Command timed out" | |
except Exception as e: | |
return f"Error: {e}" | |
def main(): | |
"""Main shell loop""" | |
# Enable ANSI support on Windows | |
enable_windows_ansi() | |
# Clear screen | |
sys.stdout.write(ANSI.CLEAR_SCREEN + ANSI.CURSOR_HOME) | |
sys.stdout.flush() | |
print("Simple Shell - Type 'exit' to quit") | |
while True: | |
try: | |
# Show prompt | |
prompt = get_prompt() | |
command = input(prompt) | |
# Execute command | |
result = execute_command(command) | |
# Handle special cases | |
if result is None: # Exit command | |
break | |
elif result == ANSI.CLEAR_SCREEN + ANSI.CURSOR_HOME: # Clear command | |
sys.stdout.write(result) | |
sys.stdout.flush() | |
elif result: | |
print(result, end='') | |
except KeyboardInterrupt: | |
print() # New line after Ctrl+C | |
continue | |
except EOFError: | |
print() # New line on Ctrl+D | |
break | |
except Exception as e: | |
print(f"Shell error: {e}") | |
print(f"{ANSI.RESET}Goodbye!") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment