2021-04-13 09:47:52 +02:00
|
|
|
import sys
|
2023-02-08 01:47:26 +01:00
|
|
|
from typing import NoReturn, Optional
|
2021-01-15 02:26:06 +01:00
|
|
|
|
2022-12-11 08:43:29 +01:00
|
|
|
from colorama import Fore, init, Style
|
|
|
|
|
2021-01-15 02:26:06 +01:00
|
|
|
init(autoreset=True)
|
|
|
|
|
|
|
|
newline = True
|
|
|
|
|
2021-04-21 18:01:00 +02:00
|
|
|
Status = Optional[str]
|
2021-04-13 09:47:52 +02:00
|
|
|
|
2022-05-05 16:08:16 +02:00
|
|
|
|
2021-01-15 02:26:06 +01:00
|
|
|
def write(*args, status=None, **kwargs):
|
|
|
|
global newline
|
|
|
|
|
|
|
|
if not newline:
|
|
|
|
print("")
|
|
|
|
newline = True
|
|
|
|
|
|
|
|
print(status_to_ansi(status) + str(args[0]), *args[1:], **kwargs)
|
|
|
|
|
2022-05-05 16:08:16 +02:00
|
|
|
|
2023-02-08 01:47:26 +01:00
|
|
|
def error(*args, **kwargs) -> NoReturn:
|
2021-04-13 09:47:52 +02:00
|
|
|
write(*args, **kwargs, status="error")
|
|
|
|
sys.exit(2)
|
|
|
|
|
2022-05-05 16:08:16 +02:00
|
|
|
|
|
|
|
# The line_num is expected to be zero-indexed
|
|
|
|
def parsing_error_preamble(path, line_num, line):
|
|
|
|
write("")
|
|
|
|
write(f"error reading {path}, line {line_num + 1}:", status="error")
|
|
|
|
write(f"\t{line}")
|
|
|
|
|
|
|
|
|
2021-04-13 09:47:52 +02:00
|
|
|
def status_to_ansi(status: Status):
|
2021-01-15 02:26:06 +01:00
|
|
|
if status == "ok":
|
|
|
|
return Fore.GREEN
|
|
|
|
elif status == "warn":
|
|
|
|
return Fore.YELLOW + Style.BRIGHT
|
|
|
|
elif status == "error":
|
|
|
|
return Fore.RED + Style.BRIGHT
|
|
|
|
elif status == "skip":
|
|
|
|
return Style.DIM
|
|
|
|
else:
|
|
|
|
return ""
|