mirror of
https://github.com/python/cpython.git
synced 2025-08-04 08:59:19 +00:00
gh-128317: Move CLI calendar highlighting to private class (#129625)
Co-authored-by: Petr Viktorin <encukou@gmail.com>
This commit is contained in:
parent
a56ead089c
commit
1bccd6c34f
3 changed files with 120 additions and 58 deletions
155
Lib/calendar.py
155
Lib/calendar.py
|
@ -349,27 +349,11 @@ class TextCalendar(Calendar):
|
|||
s = '%2i' % day # right-align single-digit days
|
||||
return s.center(width)
|
||||
|
||||
def formatweek(self, theweek, width, *, highlight_day=None):
|
||||
def formatweek(self, theweek, width):
|
||||
"""
|
||||
Returns a single week in a string (no newline).
|
||||
"""
|
||||
if highlight_day:
|
||||
from _colorize import get_colors
|
||||
|
||||
ansi = get_colors()
|
||||
highlight = f"{ansi.BLACK}{ansi.BACKGROUND_YELLOW}"
|
||||
reset = ansi.RESET
|
||||
else:
|
||||
highlight = reset = ""
|
||||
|
||||
return ' '.join(
|
||||
(
|
||||
f"{highlight}{self.formatday(d, wd, width)}{reset}"
|
||||
if d == highlight_day
|
||||
else self.formatday(d, wd, width)
|
||||
)
|
||||
for (d, wd) in theweek
|
||||
)
|
||||
return ' '.join(self.formatday(d, wd, width) for (d, wd) in theweek)
|
||||
|
||||
def formatweekday(self, day, width):
|
||||
"""
|
||||
|
@ -404,11 +388,10 @@ class TextCalendar(Calendar):
|
|||
"""
|
||||
print(self.formatmonth(theyear, themonth, w, l), end='')
|
||||
|
||||
def formatmonth(self, theyear, themonth, w=0, l=0, *, highlight_day=None):
|
||||
def formatmonth(self, theyear, themonth, w=0, l=0):
|
||||
"""
|
||||
Return a month's calendar string (multi-line).
|
||||
"""
|
||||
highlight_day = highlight_day.day if highlight_day else None
|
||||
w = max(2, w)
|
||||
l = max(1, l)
|
||||
s = self.formatmonthname(theyear, themonth, 7 * (w + 1) - 1)
|
||||
|
@ -417,11 +400,11 @@ class TextCalendar(Calendar):
|
|||
s += self.formatweekheader(w).rstrip()
|
||||
s += '\n' * l
|
||||
for week in self.monthdays2calendar(theyear, themonth):
|
||||
s += self.formatweek(week, w, highlight_day=highlight_day).rstrip()
|
||||
s += self.formatweek(week, w).rstrip()
|
||||
s += '\n' * l
|
||||
return s
|
||||
|
||||
def formatyear(self, theyear, w=2, l=1, c=6, m=3, *, highlight_day=None):
|
||||
def formatyear(self, theyear, w=2, l=1, c=6, m=3):
|
||||
"""
|
||||
Returns a year's calendar as a multi-line string.
|
||||
"""
|
||||
|
@ -446,23 +429,15 @@ class TextCalendar(Calendar):
|
|||
a(formatstring(headers, colwidth, c).rstrip())
|
||||
a('\n'*l)
|
||||
|
||||
if highlight_day and highlight_day.month in months:
|
||||
month_pos = months.index(highlight_day.month)
|
||||
else:
|
||||
month_pos = None
|
||||
|
||||
# max number of weeks for this row
|
||||
height = max(len(cal) for cal in row)
|
||||
for j in range(height):
|
||||
weeks = []
|
||||
for k, cal in enumerate(row):
|
||||
for cal in row:
|
||||
if j >= len(cal):
|
||||
weeks.append('')
|
||||
else:
|
||||
day = highlight_day.day if k == month_pos else None
|
||||
weeks.append(
|
||||
self.formatweek(cal[j], w, highlight_day=day)
|
||||
)
|
||||
weeks.append(self.formatweek(cal[j], w))
|
||||
a(formatstring(weeks, colwidth, c).rstrip())
|
||||
a('\n' * l)
|
||||
return ''.join(v)
|
||||
|
@ -672,6 +647,111 @@ class LocaleHTMLCalendar(HTMLCalendar):
|
|||
with different_locale(self.locale):
|
||||
return super().formatmonthname(theyear, themonth, withyear)
|
||||
|
||||
|
||||
class _CLIDemoCalendar(LocaleTextCalendar):
|
||||
def __init__(self, highlight_day=None, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.highlight_day = highlight_day
|
||||
|
||||
def formatweek(self, theweek, width, *, highlight_day=None):
|
||||
"""
|
||||
Returns a single week in a string (no newline).
|
||||
"""
|
||||
if highlight_day:
|
||||
from _colorize import get_colors
|
||||
|
||||
ansi = get_colors()
|
||||
highlight = f"{ansi.BLACK}{ansi.BACKGROUND_YELLOW}"
|
||||
reset = ansi.RESET
|
||||
else:
|
||||
highlight = reset = ""
|
||||
|
||||
return ' '.join(
|
||||
(
|
||||
f"{highlight}{self.formatday(d, wd, width)}{reset}"
|
||||
if d == highlight_day
|
||||
else self.formatday(d, wd, width)
|
||||
)
|
||||
for (d, wd) in theweek
|
||||
)
|
||||
|
||||
def formatmonth(self, theyear, themonth, w=0, l=0):
|
||||
"""
|
||||
Return a month's calendar string (multi-line).
|
||||
"""
|
||||
if (
|
||||
self.highlight_day
|
||||
and self.highlight_day.year == theyear
|
||||
and self.highlight_day.month == themonth
|
||||
):
|
||||
highlight_day = self.highlight_day.day
|
||||
else:
|
||||
highlight_day = None
|
||||
w = max(2, w)
|
||||
l = max(1, l)
|
||||
s = self.formatmonthname(theyear, themonth, 7 * (w + 1) - 1)
|
||||
s = s.rstrip()
|
||||
s += '\n' * l
|
||||
s += self.formatweekheader(w).rstrip()
|
||||
s += '\n' * l
|
||||
for week in self.monthdays2calendar(theyear, themonth):
|
||||
s += self.formatweek(week, w, highlight_day=highlight_day).rstrip()
|
||||
s += '\n' * l
|
||||
return s
|
||||
|
||||
def formatyear(self, theyear, w=2, l=1, c=6, m=3):
|
||||
"""
|
||||
Returns a year's calendar as a multi-line string.
|
||||
"""
|
||||
w = max(2, w)
|
||||
l = max(1, l)
|
||||
c = max(2, c)
|
||||
colwidth = (w + 1) * 7 - 1
|
||||
v = []
|
||||
a = v.append
|
||||
a(repr(theyear).center(colwidth*m+c*(m-1)).rstrip())
|
||||
a('\n'*l)
|
||||
header = self.formatweekheader(w)
|
||||
for (i, row) in enumerate(self.yeardays2calendar(theyear, m)):
|
||||
# months in this row
|
||||
months = range(m*i+1, min(m*(i+1)+1, 13))
|
||||
a('\n'*l)
|
||||
names = (self.formatmonthname(theyear, k, colwidth, False)
|
||||
for k in months)
|
||||
a(formatstring(names, colwidth, c).rstrip())
|
||||
a('\n'*l)
|
||||
headers = (header for k in months)
|
||||
a(formatstring(headers, colwidth, c).rstrip())
|
||||
a('\n'*l)
|
||||
|
||||
if (
|
||||
self.highlight_day
|
||||
and self.highlight_day.year == theyear
|
||||
and self.highlight_day.month in months
|
||||
):
|
||||
month_pos = months.index(self.highlight_day.month)
|
||||
else:
|
||||
month_pos = None
|
||||
|
||||
# max number of weeks for this row
|
||||
height = max(len(cal) for cal in row)
|
||||
for j in range(height):
|
||||
weeks = []
|
||||
for k, cal in enumerate(row):
|
||||
if j >= len(cal):
|
||||
weeks.append('')
|
||||
else:
|
||||
day = (
|
||||
self.highlight_day.day if k == month_pos else None
|
||||
)
|
||||
weeks.append(
|
||||
self.formatweek(cal[j], w, highlight_day=day)
|
||||
)
|
||||
a(formatstring(weeks, colwidth, c).rstrip())
|
||||
a('\n' * l)
|
||||
return ''.join(v)
|
||||
|
||||
|
||||
# Support for old module level interface
|
||||
c = TextCalendar()
|
||||
|
||||
|
@ -813,26 +893,21 @@ def main(args=None):
|
|||
write(cal.formatyearpage(options.year, **optdict))
|
||||
else:
|
||||
if options.locale:
|
||||
cal = LocaleTextCalendar(locale=locale)
|
||||
cal = _CLIDemoCalendar(highlight_day=today, locale=locale)
|
||||
else:
|
||||
cal = TextCalendar()
|
||||
cal = _CLIDemoCalendar(highlight_day=today)
|
||||
cal.setfirstweekday(options.first_weekday)
|
||||
optdict = dict(w=options.width, l=options.lines)
|
||||
if options.month is None:
|
||||
optdict["c"] = options.spacing
|
||||
optdict["m"] = options.months
|
||||
if options.month is not None:
|
||||
else:
|
||||
_validate_month(options.month)
|
||||
if options.year is None:
|
||||
optdict["highlight_day"] = today
|
||||
result = cal.formatyear(today.year, **optdict)
|
||||
elif options.month is None:
|
||||
if options.year == today.year:
|
||||
optdict["highlight_day"] = today
|
||||
result = cal.formatyear(options.year, **optdict)
|
||||
else:
|
||||
if options.year == today.year and options.month == today.month:
|
||||
optdict["highlight_day"] = today
|
||||
result = cal.formatmonth(options.year, options.month, **optdict)
|
||||
write = sys.stdout.write
|
||||
if options.encoding:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue