Skip to content

Commit

Permalink
增强了 console
Browse files Browse the repository at this point in the history
  • Loading branch information
carefree0910 committed Dec 25, 2023
1 parent 35ca6ef commit 2c1dbe9
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions cftool/console.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
from typing import Any
from typing import Dict
from typing import List
from typing import Optional

from rich.prompt import Prompt
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(
question: str,
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)

0 comments on commit 2c1dbe9

Please sign in to comment.