Search notes:
cal.py
cal.py uses the Python standard library
calendar to print the calendar with colors (sunday is red).
#!/usr/bin/env python3
#
# V.1
#
# Print a calendar and highlight Sundays in red.
# Compare with
# python -m calendar
# python -m calendar 2025
# python -m calendar 2025 8
#
import calendar
import sys
import datetime
try:
year = int(sys.argv[1])
month = int(sys.argv[2])
except (IndexError, ValueError):
today = datetime.datetime.today()
year = today.year
month = today.month
# Use the standard library's Sunday-first month grid directly.
# weeks is an array of arrays
# [
# [ 0, 0, 0, 0, 0, 0, 1],
# [ 2, 3, 4, 5, 6, 7, 8],
# [ 9, 10, 11, 12, 13, 14, 15],
# [16, 17, 18, 19, 20, 21, 22],
# [23, 24, 25, 26, 27, 28, 29],
# [30, 31, 0, 0, 0, 0, 0]
# ]
#
weeks = calendar.Calendar(firstweekday=6).monthdayscalendar(year, month)
# Print the month header in blue.
print(f'\033[94m {month} {year}\033[0m')
# Print Sundays in red to match the Sunday date column.
print(f'\033[91mSu\033[0m Mo Tu We Th Fr Sa')
for week in weeks:
formatted_week = []
for date in week:
if date == 0:
formatted_week.append(' ')
else:
formatted_week.append(str(date).rjust(2))
# Keep Sunday values bright red without introducing a dedicated constant.
formatted_week[0] = f'\033[91m{formatted_week[0]}\033[0m'
print('%s %s %s %s %s %s %s' % tuple(formatted_week))
print('')