Skip to content

Commit 9e95c43

Browse files
committed
Add more colour to calendar
1 parent e007631 commit 9e95c43

File tree

3 files changed

+63
-15
lines changed

3 files changed

+63
-15
lines changed

Lib/_colorize.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,14 @@ class Argparse(ThemeSection):
188188
message: str = ANSIColors.MAGENTA
189189

190190

191+
@dataclass(frozen=True, kw_only=True)
192+
class Calendar(ThemeSection):
193+
header: str = ANSIColors.BOLD
194+
highlight: str = ANSIColors.BLACK + ANSIColors.BACKGROUND_YELLOW
195+
weekday: str = ANSIColors.CYAN
196+
reset: str = ANSIColors.RESET
197+
198+
191199
@dataclass(frozen=True, kw_only=True)
192200
class Difflib(ThemeSection):
193201
"""A 'git diff'-like theme for `difflib.unified_diff`."""
@@ -404,6 +412,7 @@ class Theme:
404412
below.
405413
"""
406414
argparse: Argparse = field(default_factory=Argparse)
415+
calendar: Calendar = field(default_factory=Calendar)
407416
difflib: Difflib = field(default_factory=Difflib)
408417
fancycompleter: FancyCompleter = field(default_factory=FancyCompleter)
409418
http_server: HttpServer = field(default_factory=HttpServer)
@@ -417,6 +426,7 @@ def copy_with(
417426
self,
418427
*,
419428
argparse: Argparse | None = None,
429+
calendar: Calendar | None = None,
420430
difflib: Difflib | None = None,
421431
fancycompleter: FancyCompleter | None = None,
422432
http_server: HttpServer | None = None,
@@ -433,6 +443,7 @@ def copy_with(
433443
"""
434444
return type(self)(
435445
argparse=argparse or self.argparse,
446+
calendar=calendar or self.calendar,
436447
difflib=difflib or self.difflib,
437448
fancycompleter=fancycompleter or self.fancycompleter,
438449
http_server=http_server or self.http_server,
@@ -453,6 +464,7 @@ def no_colors(cls) -> Self:
453464
"""
454465
return cls(
455466
argparse=Argparse.no_colors(),
467+
calendar=Calendar.no_colors(),
456468
difflib=Difflib.no_colors(),
457469
fancycompleter=FancyCompleter.no_colors(),
458470
http_server=HttpServer.no_colors(),

Lib/calendar.py

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -686,28 +686,61 @@ def __init__(self, highlight_day=None, *args, **kwargs):
686686
super().__init__(*args, **kwargs)
687687
self.highlight_day = highlight_day
688688

689-
def formatweek(self, theweek, width, *, highlight_day=None):
689+
def _get_theme(self):
690+
from _colorize import get_theme
691+
692+
return get_theme(tty_file=sys.stdout)
693+
694+
def formatday(self, day, weekday, width, *, highlight_day=None):
690695
"""
691-
Returns a single week in a string (no newline).
696+
Returns a formatted day.
692697
"""
693-
if highlight_day:
694-
from _colorize import get_colors
695-
696-
ansi = get_colors()
697-
highlight = f"{ansi.BLACK}{ansi.BACKGROUND_YELLOW}"
698-
reset = ansi.RESET
698+
if day == 0:
699+
s = ''
699700
else:
700-
highlight = reset = ""
701+
s = f'{day:2}'
702+
s = s.center(width)
703+
if day == highlight_day:
704+
theme = self._get_theme().calendar
705+
s = f"{theme.highlight}{s}{theme.reset}"
706+
return s
701707

708+
def formatweek(self, theweek, width, *, highlight_day=None):
709+
"""
710+
Returns a single week in a string (no newline).
711+
"""
702712
return ' '.join(
703-
(
704-
f"{highlight}{self.formatday(d, wd, width)}{reset}"
705-
if d == highlight_day
706-
else self.formatday(d, wd, width)
707-
)
713+
self.formatday(d, wd, width, highlight_day=highlight_day)
708714
for (d, wd) in theweek
709715
)
710716

717+
def formatweekheader(self, width):
718+
"""
719+
Return a header for a week.
720+
"""
721+
header = super().formatweekheader(width)
722+
theme = self._get_theme().calendar
723+
return f"{theme.weekday}{header}{theme.reset}"
724+
725+
def formatmonthname(self, theyear, themonth, width, withyear=True):
726+
"""
727+
Return a formatted month name.
728+
"""
729+
name = super().formatmonthname(theyear, themonth, width, withyear)
730+
theme = self._get_theme().calendar
731+
if (
732+
self.highlight_day
733+
and self.highlight_day.year == theyear
734+
and self.highlight_day.month == themonth
735+
):
736+
color = theme.highlight
737+
name_only = name.strip()
738+
colored_name = f"{color}{name_only}{theme.reset}"
739+
return name.replace(name_only, colored_name, 1)
740+
else:
741+
color = theme.header
742+
return f"{color}{name}{theme.reset}"
743+
711744
def formatmonth(self, theyear, themonth, w=0, l=0):
712745
"""
713746
Return a month's calendar string (multi-line).
@@ -742,7 +775,9 @@ def formatyear(self, theyear, w=2, l=1, c=6, m=3):
742775
colwidth = (w + 1) * 7 - 1
743776
v = []
744777
a = v.append
745-
a(repr(theyear).center(colwidth*m+c*(m-1)).rstrip())
778+
theme = self._get_theme().calendar
779+
year = repr(theyear).center(colwidth*m+c*(m-1)).rstrip()
780+
a(f"{theme.header}{year}{theme.reset}")
746781
a('\n'*l)
747782
header = self.formatweekheader(w)
748783
for (i, row) in enumerate(self.yeardays2calendar(theyear, m)):

Lib/test/test_calendar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,6 +1063,7 @@ def test_several_leapyears_in_range(self):
10631063
def conv(s):
10641064
return s.replace('\n', os.linesep).encode()
10651065

1066+
@support.force_not_colorized_test_class
10661067
class CommandLineTestCase(unittest.TestCase):
10671068
def setUp(self):
10681069
self.runners = [self.run_cli_ok, self.run_cmd_ok]

0 commit comments

Comments
 (0)