osbuild: share terminal formats between files
This commit is contained in:
parent
5cb2da55f1
commit
873a071d43
3 changed files with 51 additions and 27 deletions
32
osbuild/util/term.py
Normal file
32
osbuild/util/term.py
Normal 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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue