pmbootstrap: add color support (MR 2090)

Can be disabled by setting the $NO_COLOR environment variable
This commit is contained in:
BO41 2021-08-12 17:03:39 +02:00
parent e1aef47271
commit 87dd1d0961
No known key found for this signature in database
GPG key ID: 5AE7F5513E0885CB
3 changed files with 52 additions and 6 deletions

View file

@ -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()