Make the colorized build output readable on a light background

This fixes the readability of colorized output of compile.sh when
running on a terminal with a light background.  It uses the COLORFGBG
environment variable similarly to how the ip(8) command does.

Signed-off-by: Darsey Litzenberger <dlitz@dlitz.net>
This commit is contained in:
Darsey Litzenberger
2024-02-08 21:33:19 -07:00
committed by Igor
parent 110798c904
commit ce33bddb58
11 changed files with 118 additions and 19 deletions

View File

@@ -20,6 +20,8 @@ from pathlib import Path
import sys
from common.term_colors import background_dark_or_light
REGEX_WHITESPACE_LINEBREAK_COMMA_SEMICOLON = r"[\s,;\n]+"
ARMBIAN_BOARD_CONFIG_REGEX_GENERIC = r"^(?!\s)(?:[export |declare \-g]?)+([A-Z0-9_]+)=(?:'|\")(.*)(?:'|\")"
@@ -80,14 +82,24 @@ def setup_logging():
if is_debug():
level = "DEBUG"
format = "%(message)s"
styles = {
'trace': {'color': 'white', 'bold': False},
'debug': {'color': 'white', 'bold': False},
'info': {'color': 'green', 'bold': True},
'warning': {'color': 'yellow', 'bold': True},
'error': {'color': 'red'},
'critical': {'bold': True, 'color': 'red'}
}
if background_dark_or_light() == 'light':
styles = {
'trace': {'color': 'black', 'bright': True},
'debug': {'color': 'black', 'bright': True},
'info': {'color': 'green', 'bold': True, 'faint': True},
'warning': {'color': 'yellow', 'bold': True, 'faint': True},
'error': {'color': 'red'},
'critical': {'bold': True, 'color': 'red'}
}
else:
styles = {
'trace': {'color': 'white', 'bold': False},
'debug': {'color': 'white', 'bold': False},
'info': {'color': 'green', 'bold': True},
'warning': {'color': 'yellow', 'bold': True},
'error': {'color': 'red'},
'critical': {'bold': True, 'color': 'red'}
}
coloredlogs.install(level=level, stream=sys.stderr, isatty=True, fmt=format, level_styles=styles)
except ImportError:
level = logging.INFO

View File

@@ -20,6 +20,7 @@ from unidecode import unidecode
from unidiff import PatchSet
from common.patching_config import PatchingConfig
from common.term_colors import background_dark_or_light
MAGIC_MBOX_MARKER_STANDARD = "Mon Sep 17 00:00:00 2001"
MAGIC_MBOX_MARKER_B4 = "git@z Thu Jan 1 00:00:00 1970"
@@ -703,11 +704,12 @@ class PatchInPatchFile:
color = "yellow"
else:
color = "red"
bold = 'bold dim' if background_dark_or_light() == 'light' else 'bold'
# @TODO: once our ansi-haste supports it, use [link url=file://blaaa]
if self.parent.multiple_patches_in_file:
return f"[bold][{color}]{self.markdown_name(skip_markdown=True)}[/bold](:{self.counter})"
return f"[{bold}][{color}]{self.markdown_name(skip_markdown=True)}[/{bold}](:{self.counter})"
else:
return f"[bold {color}]{self.markdown_name(skip_markdown=True)}"
return f"[{bold} {color}]{self.markdown_name(skip_markdown=True)}"
def rich_patch_output(self):
ret = self.patch_output
@@ -716,10 +718,11 @@ class PatchInPatchFile:
'yellow': ['with fuzz', 'offset ', ' hunks ignored', ' hunk ignored'],
'red': ['hunk FAILED', 'hunks FAILED']
}
bold = 'bold dim' if background_dark_or_light() == 'light' else 'bold'
# use Rich's syntax highlighting to highlight with color
for color in color_tags:
for tag in color_tags[color]:
ret = ret.replace(tag, f"[bold {color}]{tag}[/bold {color}]")
ret = ret.replace(tag, f"[{bold} {color}]{tag}[/{bold} {color}]")
return ret
def apply_patch_date_to_files(self, working_dir, options):

View File

@@ -0,0 +1,31 @@
#!/usr/bin/env python3
#
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (c) 2024 Darsey Litzenberger, dlitz@dlitz.net
#
# This file is a part of the Armbian Build Framework
# https://github.com/armbian/build/
#
import os
def background_dark_or_light():
"""
Returns:
'dark' if the terminal background is dark,
'light' if the terminal background is light, or
'' if the terminal background color is unknown.
"""
colorfgbg = os.environ.get("COLORFGBG", "")
try:
_, bg = colorfgbg.split(';')
bg = int(bg)
except ValueError:
return ""
if 0 <= bg <= 6 or bg == 8:
return "dark"
elif bg == 7 or 9 <= bg <= 15:
return "light"
return ""