Coverage for rust2rpm/log.py: 85%
18 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-10-27 15:21 +0100
« prev ^ index » next coverage.py v7.6.4, created at 2024-10-27 15:21 +0100
1"""
2Simple logging implementation that wraps long messages to 80 characters,
3indents multi-line messages, and uses colors for errors (red), warnings
4(yellow), and success (green), and prints output to STDERR only.
5"""
7import shutil
8import sys
9import textwrap
11from termcolor import colored
14def _eprint(message: str):
15 print(message, file=sys.stderr)
18def _wrap(message: str, prefix: str, width: int | None = None):
19 if not width: 19 ↛ 22line 19 didn't jump to line 22 because the condition on line 19 was always true
20 width = shutil.get_terminal_size().columns
22 return textwrap.wrap(message, 80, initial_indent=f"{prefix} ", subsequent_indent=" " * (len(prefix) + 1))
25def success(message: str):
26 _eprint(colored("\n".join(_wrap(message, "•")), "green"))
29def info(message: str):
30 _eprint("\n".join(_wrap(message, "•")))
33def warn(message: str):
34 _eprint(colored("\n".join(_wrap(message, "WARNING:")), "yellow"))
37def error(message: str):
38 _eprint(colored("\n".join(_wrap(message, "ERROR:")), "red", attrs=["dark"]))