diff --git a/src/git_flow/__init__.py b/src/git_flow/__init__.py index 049ad89..8adffc8 100644 --- a/src/git_flow/__init__.py +++ b/src/git_flow/__init__.py @@ -49,15 +49,3 @@ SUPPORTED_REMOTE_APIS = [ ] REPOSITORY_TOKEN_FILENAME = ".repository-token" - -COLOR_BLACK = "\033[30m" -COLOR_RED = "\033[31m" -COLOR_GREEN = "\033[32m" -COLOR_YELLOW = "\033[33m" -COLOR_BLUE = "\033[34m" -COLOR_5 = "\033[35m" -COLOR_6 = "\033[36m" -COLOR_7 = "\033[37m" -COLOR_BLACK_BOLD = "\033[1;30m" -COLOR_WHITE_BOLD = "\033[1;37m" -COLOR_RESET = "\033[0m" diff --git a/src/git_flow/command/base.py b/src/git_flow/command/base.py index f11a5b5..27fa331 100644 --- a/src/git_flow/command/base.py +++ b/src/git_flow/command/base.py @@ -7,70 +7,18 @@ from git_flow.git import FLOWCONFIG_FILE, Git from git_flow.remote.base import RemoteAPI from git_flow.remote.bitbucket import BitbucketRemoteAPI from git_flow.remote.github import GithubRemoteAPI +from git_flow.io import * from os.path import isfile -from rich.console import Console -from rich.prompt import Prompt, Confirm -from rich.panel import Panel TYPE_SUCCESS = 0 TYPE_WARNING = 1 TYPE_ERROR = 2 TYPE_INFO = 3 -console = Console(highlight=False) + flowconfig = Git.get_config(FLOWCONFIG_FILE) if isfile(FLOWCONFIG_FILE) else {} -def success(msg: str): - console.print(f":white_check_mark: [green]{msg}[/green]") - - -def warning(msg: str): - console.print(f":warning: [yellow]{msg}[/yellow]") - - -def error(msg: str): - console.print(f":x: [red]{msg}[/red]") - - -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: - return result - - -def confirm(question: str, default: bool = True): - return Confirm.ask(question, default=default) - - -def choice(prompt: str, options: list[str]): - return Prompt.ask(prompt, choices=options) - - def ensure_initialized(): initialized = flowconfig["flow.initialized"] if flowconfig else None diff --git a/src/git_flow/git.py b/src/git_flow/git.py index 55e6fce..2fe4fd4 100644 --- a/src/git_flow/git.py +++ b/src/git_flow/git.py @@ -1,9 +1,7 @@ import subprocess -from git_flow import GitFlowError from git_flow.io import * - FLOWCONFIG_FILE = ".flowconfig" CHANGELOG_FILE = "CHANGELOG.md" @@ -88,7 +86,9 @@ class Git: except subprocess.CalledProcessError: return None - def __init__(self, subcommand: str, *args: str, **kwargs: str | int | bool | list[str] | None) -> None: + def __init__( + self, subcommand: str, *args: str, **kwargs: str | int | bool | list[str] | None + ) -> None: self.command = ["git", subcommand] for option, value in kwargs.items(): @@ -133,7 +133,7 @@ class Git: def exec(self, **kwargs): self._run(**kwargs) - def _get(self, print: str|None = None, check: bool = True): + def _get(self, print: str | None = None, check: bool = True): process = subprocess.run(self.command, capture_output=True, text=True) if print is not None: @@ -144,9 +144,10 @@ class Git: return process - - def _run(self, print: str|None = None, check: bool = True): - process = subprocess.run(self.command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + def _run(self, print: str | None = None, check: bool = True): + process = subprocess.run( + self.command, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL + ) if print is not None: self._print_process("", "", title=print) @@ -168,12 +169,12 @@ class Git: command.append(arg) - title = title or "Ejecutando" - print(COLOR_YELLOW + "| " + title + COLOR_RESET) - print(COLOR_YELLOW + "| $ " + " ".join(command) + COLOR_RESET) + text = f"[yellow]$ {" ".join(command)}[/yellow]" for line in stdout.splitlines(): - print(COLOR_YELLOW + "| " + COLOR_RESET + "[out] " + line) + text += "\n\\[out] " + line for line in stderr.splitlines(): - print(COLOR_YELLOW + "| " + COLOR_RED + "[err] " + line + COLOR_RESET) + text += "\n[red]\\[err][/red] " + line + + panel(title or "Ejecutando", text) diff --git a/src/git_flow/io.py b/src/git_flow/io.py index fe5db23..a168e3c 100644 --- a/src/git_flow/io.py +++ b/src/git_flow/io.py @@ -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)