diff --git a/pkgs/clan-cli/clan_cli/tty.py b/pkgs/clan-cli/clan_cli/tty.py new file mode 100644 index 0000000..5d930ec --- /dev/null +++ b/pkgs/clan-cli/clan_cli/tty.py @@ -0,0 +1,25 @@ +import sys +from typing import IO, Any, Callable + + +def is_interactive() -> bool: + """Returns true if the current process is interactive""" + return sys.stdin.isatty() and sys.stdout.isatty() + + +def color_text(code: int, file: IO[Any] = sys.stdout) -> Callable[[str], None]: + """ + Print with color if stderr is a tty + """ + + def wrapper(text: str) -> None: + if file.isatty(): + print(f"\x1b[{code}m{text}\x1b[0m", file=file) + else: + print(text, file=file) + + return wrapper + + +warn = color_text(91, file=sys.stderr) +info = color_text(92, file=sys.stderr)