Skip to content

Commit

Permalink
Add __init__ with self.highlight_day = highlight_day
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Feb 3, 2025
1 parent a6da663 commit 0aa9eb0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
21 changes: 3 additions & 18 deletions Doc/library/calendar.rst
Original file line number Diff line number Diff line change
Expand Up @@ -166,18 +166,13 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
the specified width, representing an empty day. The *weekday* parameter
is unused.

.. method:: formatweek(theweek, w=0, highlight_day=None)
.. method:: formatweek(theweek, w=0)

Return a single week in a string with no newline. If *w* is provided, it
specifies the width of the date columns, which are centered. Depends
on the first weekday as specified in the constructor or set by the
:meth:`setfirstweekday` method.

.. versionchanged:: 3.14
If *highlight_day* is given, this date is highlighted in color.
This can be :ref:`controlled using environment variables
<using-on-controlling-color>`.


.. method:: formatweekday(weekday, width)

Expand All @@ -193,19 +188,14 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
settings and are padded to the specified width.


.. method:: formatmonth(theyear, themonth, w=0, l=0, highlight_day=None)
.. method:: formatmonth(theyear, themonth, w=0, l=0)

Return a month's calendar in a multi-line string. If *w* is provided, it
specifies the width of the date columns, which are centered. If *l* is
given, it specifies the number of lines that each week will use. Depends
on the first weekday as specified in the constructor or set by the
:meth:`setfirstweekday` method.

.. versionchanged:: 3.14
If *highlight_day* is given, this date is highlighted in color.
This can be :ref:`controlled using environment variables
<using-on-controlling-color>`.


.. method:: formatmonthname(theyear, themonth, width=0, withyear=True)

Expand All @@ -220,7 +210,7 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
Print a month's calendar as returned by :meth:`formatmonth`.


.. method:: formatyear(theyear, w=2, l=1, c=6, m=3, highlight_day=None)
.. method:: formatyear(theyear, w=2, l=1, c=6, m=3)

Return a *m*-column calendar for an entire year as a multi-line string.
Optional parameters *w*, *l*, and *c* are for date column width, lines per
Expand All @@ -229,11 +219,6 @@ interpreted as prescribed by the ISO 8601 standard. Year 0 is 1 BC, year -1 is
:meth:`setfirstweekday` method. The earliest year for which a calendar
can be generated is platform-dependent.

.. versionchanged:: 3.14
If *highlight_day* is given, this date is highlighted in color.
This can be :ref:`controlled using environment variables
<using-on-controlling-color>`.


.. method:: pryear(theyear, w=2, l=1, c=6, m=3)

Expand Down
34 changes: 21 additions & 13 deletions Lib/calendar.py
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,10 @@ def formatmonthname(self, theyear, themonth, withyear=True):


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).
Expand All @@ -671,11 +675,14 @@ def formatweek(self, theweek, width, *, highlight_day=None):
for (d, wd) in theweek
)

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
if self.highlight_day and self.highlight_day.year == theyear:
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)
Expand All @@ -688,7 +695,7 @@ def formatmonth(self, theyear, themonth, w=0, l=0, *, highlight_day=None):
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.
"""
Expand All @@ -713,8 +720,12 @@ def formatyear(self, theyear, w=2, l=1, c=6, m=3, *, highlight_day=None):
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)
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

Expand All @@ -726,7 +737,9 @@ def formatyear(self, theyear, w=2, l=1, c=6, m=3, *, highlight_day=None):
if j >= len(cal):
weeks.append('')
else:
day = highlight_day.day if k == month_pos else None
day = (
self.highlight_day.day if k == month_pos else None
)
weeks.append(
self.formatweek(cal[j], w, highlight_day=day)
)
Expand Down Expand Up @@ -876,9 +889,9 @@ def main(args=None):
write(cal.formatyearpage(options.year, **optdict))
else:
if options.locale:
cal = _CLIDemoCalendar(locale=locale)
cal = _CLIDemoCalendar(highlight_day=today, locale=locale)
else:
cal = _CLIDemoCalendar()
cal = _CLIDemoCalendar(highlight_day=today)
cal.setfirstweekday(options.first_weekday)
optdict = dict(w=options.width, l=options.lines)
if options.month is None:
Expand All @@ -887,15 +900,10 @@ def main(args=None):
if options.month is not None:
_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:
Expand Down

0 comments on commit 0aa9eb0

Please sign in to comment.