From 2c1dbe9034ae287071145f5ccbbc1daaf599d9e7 Mon Sep 17 00:00:00 2001 From: carefree0910 Date: Mon, 25 Dec 2023 22:35:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E4=BA=86=20`console`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- cftool/console.py | 43 +++++++++++++++++++++++++++++-------------- 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/cftool/console.py b/cftool/console.py index db9fa83..97201e5 100644 --- a/cftool/console.py +++ b/cftool/console.py @@ -1,5 +1,4 @@ from typing import Any -from typing import Dict from typing import List from typing import Optional @@ -7,24 +6,38 @@ from rich.status import Status from rich.console import Console +from .misc import shallow_copy_dict + _console = Console() -def deprecate(msg: str) -> None: - _console.print(f"[yellow]DeprecationWarning: {msg}[/yellow]") +def log(msg: str, *args: Any, **kwargs: Any) -> None: + _console.log(msg, *args, **kwargs) + + +def debug(msg: str, *args: Any, prefix: str = "", **kwargs: Any) -> None: + log(f"[grey42]{prefix}{msg}[/grey42]", *args, **kwargs) + + +def warn(msg: str, *args: Any, prefix: str = "Warning: ", **kwargs: Any) -> None: + log(f"[yellow]{prefix}{msg}[/yellow]", *args, **kwargs) + + +def deprecated(msg: str, *args: Any, **kwargs: Any) -> None: + warn(msg, *args, prefix="DeprecationWarning: ", **kwargs) -def log(msg: str) -> None: - _console.log(msg) +def error(msg: str, *args: Any, prefix: str = "Error: ", **kwargs: Any) -> None: + log(f"[red]{prefix}{msg}[/red]", *args, **kwargs) -def print(msg: str) -> None: - _console.print(msg) +def print(msg: str, *args: Any, **kwargs: Any) -> None: + _console.print(msg, *args, **kwargs) -def rule(title: str) -> None: - _console.rule(title) +def rule(title: str, **kwargs: Any) -> None: + _console.rule(title, **kwargs) def ask( @@ -32,12 +45,14 @@ def ask( choices: Optional[List[str]] = None, *, default: Optional[str] = None, + **kwargs: Any, ) -> str: - kw: Dict[str, Any] = dict(choices=choices) + kwargs = shallow_copy_dict(kwargs) + kwargs["choices"] = choices if default is not None: - kw["default"] = default - return Prompt.ask(question, **kw) # type: ignore + kwargs["default"] = default + return Prompt.ask(question, **kwargs) -def status(msg: str) -> Status: - return _console.status(msg) +def status(msg: str, **kwargs: Any) -> Status: + return _console.status(msg, **kwargs)