refactor: remove unused constants, move io functions to module
This commit is contained in:
+41
-42
@@ -1,56 +1,55 @@
|
||||
import sys
|
||||
from rich.console import Console
|
||||
from rich.prompt import Prompt, Confirm
|
||||
from rich.panel import Panel
|
||||
|
||||
from git_flow import COLOR_BLACK_BOLD, COLOR_BLUE, COLOR_GREEN, COLOR_RED, COLOR_RESET, COLOR_YELLOW
|
||||
|
||||
def confirm(prompt, default: bool = True):
|
||||
user_input = input(prompt + (" [Y/n]: " if default else " [y/N]: "))
|
||||
|
||||
return default if len(user_input) == 0 else user_input.startswith("y")
|
||||
console = Console(highlight=False)
|
||||
|
||||
|
||||
def choose(prompt: str, options: list[str]):
|
||||
print(prompt)
|
||||
def success(msg: str):
|
||||
console.print(f":white_check_mark: [green]{msg}[/green]")
|
||||
|
||||
for i, option in enumerate(options):
|
||||
print(f"\t{i+1}. {option}")
|
||||
|
||||
selection = None
|
||||
def warning(msg: str):
|
||||
console.print(f":warning: [yellow]{msg}[/yellow]")
|
||||
|
||||
while selection is None:
|
||||
user_input = input(f"Seleccione una opción [1-{len(options)}] o escribala: ")
|
||||
|
||||
if user_input.isdigit():
|
||||
user_input = int(user_input)
|
||||
def error(msg: str):
|
||||
console.print(f":x: [red]{msg}[/red]")
|
||||
|
||||
if 1 <= user_input and user_input <= len(options):
|
||||
selection = options[user_input - 1]
|
||||
|
||||
if not confirm(
|
||||
f"Seleccionó la opción {user_input} ({selection}), ¿es correcto?"
|
||||
):
|
||||
selection = None
|
||||
else:
|
||||
print_error(f"La opción {user_input} está fuera del rango permitido.")
|
||||
elif user_input in options:
|
||||
selection = user_input
|
||||
def info(msg: str):
|
||||
console.print(f":information: [blue]{msg}[/blue]")
|
||||
|
||||
|
||||
def panel(title: str, text: str):
|
||||
console.print(Panel(text, title=title, expand=False, title_align="left"))
|
||||
|
||||
|
||||
def prompt(
|
||||
prompt: str,
|
||||
default: str | None = None,
|
||||
persistent: bool = False,
|
||||
strip: bool = True,
|
||||
) -> str:
|
||||
onetime = not persistent
|
||||
|
||||
while onetime or persistent:
|
||||
result = Prompt.ask(prompt)
|
||||
result = result.strip() if strip else result
|
||||
|
||||
if result:
|
||||
return result
|
||||
elif default is not None:
|
||||
return default
|
||||
elif persistent:
|
||||
error("Debe ingresar un valor no vacío.")
|
||||
else:
|
||||
print_error(f"La opción '{user_input}' es inválida.")
|
||||
|
||||
return selection
|
||||
return result
|
||||
|
||||
|
||||
def print_error(message: str):
|
||||
print(f"{COLOR_RED}[err] {message}{COLOR_RESET}", file=sys.stderr)
|
||||
def confirm(question: str, default: bool = True):
|
||||
return Confirm.ask(question, default=default)
|
||||
|
||||
|
||||
def print_warning(message: str):
|
||||
print(f"{COLOR_YELLOW}[wrn] {message}{COLOR_RESET}")
|
||||
|
||||
def print_success(message: str):
|
||||
print(f"{COLOR_GREEN}[ok!] {message}{COLOR_RESET}")
|
||||
|
||||
def print_info(message: str):
|
||||
print(f"{COLOR_BLUE}[inf] {message}{COLOR_RESET}")
|
||||
|
||||
def print_debug(message: str):
|
||||
print(f"{COLOR_BLACK_BOLD}[dbg] {message}{COLOR_RESET}")
|
||||
def choice(prompt: str, options: list[str]):
|
||||
return Prompt.ask(prompt, choices=options)
|
||||
|
||||
Reference in New Issue
Block a user