Skip to content

Commit

Permalink
Fix: bright ANSI color; Update: logger styling
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanHeng committed Apr 20, 2024
1 parent bc0c294 commit 2ae7c22
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

VERSION = '0.39.0'
VERSION = '0.39.1'
DESCRIPTION = 'Machine Learning project startup utilities'
LONG_DESCRIPTION = 'My commonly used utilities for machine learning projects'

Expand All @@ -13,7 +13,7 @@
description=DESCRIPTION,
long_description=LONG_DESCRIPTION,
url='https://github.com/StefanHeng/stef-util',
download_url='https://github.com/StefanHeng/stef-util/archive/refs/tags/v0.39.0.tar.gz',
download_url='https://github.com/StefanHeng/stef-util/archive/refs/tags/v0.39.1.tar.gz',
packages=find_packages(),
include_package_data=True,
install_requires=[
Expand Down
30 changes: 21 additions & 9 deletions stefutil/prettier.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class PrettyStyler:
w='white',
)
# also add the bright versions, mapping to names used by `click.style()`
short_c2c.update({f'B{c}': f'bright_{c}' for c in short_c2c.keys()})
short_c2c.update({f'B{c}': f'bright_{c_}' for c, c_ in short_c2c.items()})
short_c2c.update( # now set default colors for each logging type
log='green',
warn='yellow',
Expand Down Expand Up @@ -554,7 +554,8 @@ class MyFormatter(logging.Formatter):
if ANSI_BACKEND == 'click':
# styling for each level and for time prefix
time = dict(fg='g')
sep = dict(fg='b')
sep = dict(fg='Bb') # bright blue
ref = dict(fg='Bm') # bright magenta

debug = dict(fg=None, dim=True, bold=True)
info = dict(fg=None, bold=True)
Expand Down Expand Up @@ -590,7 +591,9 @@ class MyFormatter(logging.Formatter):
KW_FUNC_NM = '%(funcName)s'
KW_NAME = '%(name)s'

def __init__(self, with_color=True, style_time: Dict[str, Any] = None, style_sep: Dict[str, Any] = None):
def __init__(
self, with_color=True, style_time: Dict[str, Any] = None, style_sep: Dict[str, Any] = None, style_ref: Dict[str, Any] = None
):
# time set to green by default, punc separator set to green by default
super().__init__()
self.with_color = with_color
Expand All @@ -600,6 +603,8 @@ def __init__(self, with_color=True, style_time: Dict[str, Any] = None, style_sep
self.time_style_args.update(style_time or dict())
self.sep_style_args = MyFormatter.sep.copy()
self.sep_style_args.update(style_sep or dict())
self.ref_style_args = MyFormatter.ref.copy()
self.ref_style_args.update(style_ref or dict())

color_time = s.s(MyFormatter.KW_TIME, **self.time_style_args) + s.s('|', **self.sep_style_args)
else:
Expand Down Expand Up @@ -628,10 +633,10 @@ def args2fmt(args_):
def fmt_meta(self, meta_abv, meta_style: Union[str, Dict[str, Any]] = None):
if self.with_color:
if ANSI_BACKEND == 'click':
return '[' + s.s(MyFormatter.KW_NAME, fg='m') + ']' \
+ s.s('::', **self.sep_style_args) + s.s(MyFormatter.KW_FUNC_NM, fg='m') \
+ s.s('::', **self.sep_style_args) + s.s(MyFormatter.KW_FNM, fg='m') \
+ s.s(':', **self.sep_style_args) + s.s(MyFormatter.KW_LINENO, fg='m') \
return '[' + s.s(MyFormatter.KW_NAME, **self.ref_style_args) + ']' \
+ s.s('::', **self.sep_style_args) + s.s(MyFormatter.KW_FUNC_NM, **self.ref_style_args) \
+ s.s('::', **self.sep_style_args) + s.s(MyFormatter.KW_FNM, **self.ref_style_args) \
+ s.s(':', **self.sep_style_args) + s.s(MyFormatter.KW_LINENO, **self.ref_style_args) \
+ s.s(':', **self.sep_style_args) + s.s(meta_abv, **meta_style)
else:
assert ANSI_BACKEND == 'colorama'
Expand Down Expand Up @@ -949,7 +954,7 @@ def check_nested_log_dict():
def check_logger():
logger = get_logger('blah')
logger.info('should appear once')
# check_logger()
check_logger()

def check_now():
sic(now(fmt='full'))
Expand Down Expand Up @@ -991,7 +996,7 @@ def check_float_pad():
print(s.pa(d, pad_float=False))

sic(s.pa(d, pad_float=False))
check_float_pad()
# check_float_pad()

def check_ordinal():
sic([ordinal(n) for n in range(1, 32)])
Expand Down Expand Up @@ -1123,3 +1128,10 @@ def check_now_tz():
sic(now(time_zone='US/Eastern'))
sic(now(time_zone='Europe/London'))
# check_now_tz()

def check_intense_color():
print(s.s('hello', fg='m'))
print(s.s('hello', fg='m', bold=True))
print(s.s('hello', fg='Bm'))
print(s.s('hello', fg='Bm', bold=True))
check_intense_color()

0 comments on commit 2ae7c22

Please sign in to comment.