refactor: replace global command class to typer functions
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
from argparse import Namespace, ArgumentParser
|
||||
from abc import ABC, abstractmethod
|
||||
from os.path import isfile
|
||||
import rich
|
||||
from rich.prompt import Prompt, Confirm
|
||||
|
||||
from git_flow import (
|
||||
BRANCH_TYPES,
|
||||
@@ -44,16 +46,16 @@ class Command(ABC):
|
||||
)
|
||||
|
||||
def success(self, msg: str):
|
||||
self._print(TYPE_SUCCESS, msg)
|
||||
rich.print(f":white_check_mark: [green]{msg}[/green]")
|
||||
|
||||
def warning(self, msg: str):
|
||||
self._print(TYPE_WARNING, msg)
|
||||
rich.print(f":warning: [yellow]{msg}[/yellow]")
|
||||
|
||||
def error(self, msg: str):
|
||||
self._print(TYPE_ERROR, msg)
|
||||
rich.print(f":x: [red]{msg}[/red]")
|
||||
|
||||
def info(self, msg: str):
|
||||
self._print(TYPE_INFO, msg)
|
||||
rich.print(f":information: [blue]{msg}[/blue]")
|
||||
|
||||
def prompt(
|
||||
self,
|
||||
@@ -65,11 +67,7 @@ class Command(ABC):
|
||||
onetime = not persistent
|
||||
|
||||
while onetime or persistent:
|
||||
if default:
|
||||
prompt += f" [{default}]"
|
||||
|
||||
prompt += ": "
|
||||
result = input(prompt)
|
||||
result = Prompt.ask(prompt)
|
||||
result = result.strip() if strip else result
|
||||
|
||||
if result:
|
||||
@@ -82,40 +80,10 @@ class Command(ABC):
|
||||
return result
|
||||
|
||||
def confirm(self, question: str, default: bool = True):
|
||||
suffix = " [Y/n]: " if default else " [y/N]: "
|
||||
answer = input(question + suffix)
|
||||
|
||||
return default if len(answer) == 0 else answer.startswith("y")
|
||||
return Confirm.ask(question, default=default)
|
||||
|
||||
def choice(self, prompt: str, options: list[str]):
|
||||
print(prompt)
|
||||
|
||||
for i, option in enumerate(options):
|
||||
print(f"\t{i+1}. {option}")
|
||||
|
||||
selection = None
|
||||
|
||||
while selection is None:
|
||||
answer = input(f"Seleccione una opción [1-{len(options)}] o escribala: ")
|
||||
|
||||
if answer.isdigit():
|
||||
answer = int(answer)
|
||||
|
||||
if 1 <= answer and answer <= len(options):
|
||||
selection = options[answer - 1]
|
||||
|
||||
if not self.confirm(
|
||||
f"Seleccionó la opción {answer} ({selection}), ¿es correcto?"
|
||||
):
|
||||
selection = None
|
||||
else:
|
||||
self.error(f"La opción {answer} está fuera del rango permitido.")
|
||||
elif answer in options:
|
||||
selection = answer
|
||||
else:
|
||||
self.error(f"La opción '{answer}' es inválida.")
|
||||
|
||||
return selection
|
||||
return Prompt.ask(prompt, choices = options)
|
||||
|
||||
def setup_parser(self, parser: ArgumentParser) -> ArgumentParser:
|
||||
return parser
|
||||
|
||||
Reference in New Issue
Block a user