SSH-key-Manager/utils.py
2025-03-08 00:43:33 -06:00

39 lines
1 KiB
Python

# ssh_manager/utils.py
from enum import Enum
import sys
from functools import lru_cache
class Colors(Enum):
GREEN = "\033[0;32m"
RED = "\033[0;31m"
YELLOW = "\033[1;33m"
CYAN = "\033[0;36m"
BOLD = "\033[1m"
RESET = "\033[0m"
def __str__(self):
return self.value
@lru_cache(maxsize=32)
def _format_message(prefix: str, color: Colors, message: str) -> str:
return f"{color}{Colors.BOLD}[{prefix}] {Colors.RESET}{message}"
def print_error(message: str) -> None:
print(_format_message("", Colors.RED, message))
def print_warning(message: str) -> None:
print(_format_message("", Colors.YELLOW, message))
def print_info(message: str) -> None:
print(_format_message("", Colors.GREEN, message))
def safe_input(prompt: str = "") -> str:
"""
A wrapper around input() that exits the entire script on Ctrl+C.
"""
try:
return input(prompt)
except KeyboardInterrupt:
print_info("\nExiting on Ctrl+C.")
sys.exit(130) # Conventionally 130 indicates "terminated by Ctrl+C"