osbuild: share terminal formats between files

This commit is contained in:
Simon de Vlieger 2022-07-14 10:56:24 +02:00
parent 5cb2da55f1
commit 873a071d43
3 changed files with 51 additions and 27 deletions

32
osbuild/util/term.py Normal file
View file

@ -0,0 +1,32 @@
"""Wrapper module for output formatting."""
import sys
from typing import Dict
class VT:
"""Video terminal output, disables formatting when stdout is not a tty."""
isatty: bool
escape_sequences: Dict[str, str] = {
"reset": "\033[0m",
"bold": "\033[1m",
"red": "\033[31m",
"green": "\033[32m",
}
def __init__(self) -> None:
self.isatty = sys.stdout.isatty()
def __getattr__(self, name: str) -> str:
if not self.isatty:
return ""
return self.escape_sequences[name]
fmt = VT()