diff --git a/pmb/config/__init__.py b/pmb/config/__init__.py index 5ef10f80..86f05b21 100644 --- a/pmb/config/__init__.py +++ b/pmb/config/__init__.py @@ -123,6 +123,21 @@ is_interactive = sys.stdout.isatty() and \ sys.stdin.isatty() +# ANSI escape codes to highlight stdout +styles = { + "BLUE": '\033[94m', + "BOLD": '\033[1m', + "GREEN": '\033[92m', + "RED": '\033[91m', + "YELLOW": '\033[93m', + "END": '\033[0m' +} + +if "NO_COLOR" in os.environ: + for style in styles.keys(): + styles[style] = "" + + # List of available locales taken from musl-locales package; see # https://pkgs.alpinelinux.org/contents?name=musl-locales locales = [ diff --git a/pmb/helpers/cli.py b/pmb/helpers/cli.py index 2917fbfb..32ac70c4 100644 --- a/pmb/helpers/cli.py +++ b/pmb/helpers/cli.py @@ -50,13 +50,17 @@ def ask(args, question="Continue?", choices=["y", "n"], default="n", :param validation_regex: if set, keep asking until regex matches :param complete: set to a list to enable tab completion """ + styles = pmb.config.styles + while True: date = datetime.datetime.now().strftime("%H:%M:%S") - question_full = "[" + date + "] " + question + line = question if choices: - question_full += " (" + str.join("/", choices) + ")" + line += f" ({str.join('/', choices)})" if default: - question_full += " [" + str(default) + "]" + line += f" [{default}]" + line_color = f"[{date}] {styles['BOLD']}{line}{styles['END']}" + line = f"[{date}] {line}" if complete: readline.parse_and_bind('tab: complete') @@ -67,7 +71,7 @@ def ask(args, question="Continue?", choices=["y", "n"], default="n", readline.set_completer( ReadlineTabCompleter(complete).completer_func) - ret = input(question_full + ": ") + ret = input(f"{line_color}: ") # Stop completing (question is answered) if complete: @@ -79,7 +83,7 @@ def ask(args, question="Continue?", choices=["y", "n"], default="n", if ret == "": ret = str(default) - args.logfd.write(question_full + " " + ret + "\n") + args.logfd.write(f"{line}: {ret}\n") args.logfd.flush() # Validate with regex diff --git a/pmb/helpers/logging.py b/pmb/helpers/logging.py index 094bf824..820d399f 100644 --- a/pmb/helpers/logging.py +++ b/pmb/helpers/logging.py @@ -3,6 +3,7 @@ import logging import os import sys +import pmb.config class log_handler(logging.StreamHandler): @@ -20,7 +21,33 @@ class log_handler(logging.StreamHandler): not self._args.quiet and record.levelno >= logging.INFO): stream = self.stream - stream.write(msg) + + styles = pmb.config.styles + + msg_col = ( + msg.replace( + "NOTE:", + f"{styles['BLUE']}NOTE:{styles['END']}", + 1, + ) + .replace( + "WARNING:", + f"{styles['YELLOW']}WARNING:{styles['END']}", + 1, + ) + .replace( + "ERROR:", + f"{styles['RED']}ERROR:{styles['END']}", + 1, + ) + .replace( + "DONE!", + f"{styles['GREEN']}DONE!{styles['END']}", + 1, + ) + ) + + stream.write(msg_col) stream.write(self.terminator) self.flush()