|
| 1 | +"""Calendar. |
| 2 | +
|
| 3 | +Port of Vestaboard/vbml/src/calendar.ts |
| 4 | +""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import math |
| 9 | +from calendar import monthrange |
| 10 | +from datetime import date |
| 11 | + |
| 12 | +from .character_codes import CharacterCode |
| 13 | + |
| 14 | + |
| 15 | +def _char_code_for_digit(digit: str) -> int: |
| 16 | + """Char code for digit.""" |
| 17 | + return 36 if digit == "0" else int(digit) + 26 |
| 18 | + |
| 19 | + |
| 20 | +def _char_code_for_day(day: str) -> int: |
| 21 | + """Char code for day.""" |
| 22 | + return { |
| 23 | + "Sun": 19, |
| 24 | + "Mon": 13, |
| 25 | + "Tue": 20, |
| 26 | + "Wed": 23, |
| 27 | + "Thu": 20, |
| 28 | + "Fri": 6, |
| 29 | + "Sat": 19, |
| 30 | + }.get(day, 0) |
| 31 | + |
| 32 | + |
| 33 | +def make_calendar( |
| 34 | + month: int, |
| 35 | + year: int, |
| 36 | + *, |
| 37 | + default_day_color: CharacterCode | int | None = None, |
| 38 | + highlighted_days: dict[int | str, CharacterCode | int] | None = None, |
| 39 | + hide_day_of_week: bool = False, |
| 40 | + hide_dates: bool = False, |
| 41 | + hide_month_year: bool = False, |
| 42 | +) -> list[list[int]]: |
| 43 | + """Make calendar.""" |
| 44 | + num_days = monthrange(year, month)[1] |
| 45 | + |
| 46 | + first_day_of_month = date(year, month, 1).strftime("%a") # e.g. "Mon" |
| 47 | + days_of_week = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"] |
| 48 | + offset = days_of_week.index(first_day_of_month) |
| 49 | + |
| 50 | + cal_color = CharacterCode.YELLOW if default_day_color is None else default_day_color |
| 51 | + |
| 52 | + # Row day ranges (as strings, matching the TS template literals) |
| 53 | + first_row_days = [str(1), str(7 - offset)] |
| 54 | + second_row_days = [str(7 - offset + 1), str(7 - offset + 7)] |
| 55 | + third_row_days = [str(7 - offset + 8), str(7 - offset + 14)] |
| 56 | + fourth_row_days = [str(7 - offset + 15), str(7 - offset + 21)] |
| 57 | + fifth_start = 7 - offset + 22 |
| 58 | + fifth_end = min(7 - offset + num_days, num_days) |
| 59 | + fifth_row_days = ( |
| 60 | + [str(fifth_start), str(fifth_end)] if fifth_start <= num_days else None |
| 61 | + ) |
| 62 | + num_days_last_row = fifth_end - (7 - offset + 22) + 1 |
| 63 | + |
| 64 | + def dc(digit: str) -> int: |
| 65 | + """Digit → character code, or 0 when dates are hidden.""" |
| 66 | + return 0 if hide_dates else _char_code_for_digit(digit) |
| 67 | + |
| 68 | + if first_row_days[0] == first_row_days[1]: |
| 69 | + first_row = ( |
| 70 | + [0, 0, 0, dc(first_row_days[0]), 0] |
| 71 | + + [0] * offset |
| 72 | + + [cal_color] * (7 - offset) |
| 73 | + + [0] * (22 - 12) |
| 74 | + ) |
| 75 | + else: |
| 76 | + first_row = ( |
| 77 | + [ |
| 78 | + 0, |
| 79 | + dc(first_row_days[0]), |
| 80 | + 0 if hide_dates else 44, |
| 81 | + dc(first_row_days[1]), |
| 82 | + 0, |
| 83 | + ] |
| 84 | + + [0] * offset |
| 85 | + + [cal_color] * (7 - offset) |
| 86 | + + [0] * (22 - 12) |
| 87 | + ) |
| 88 | + |
| 89 | + def _two_digit_row(row_days: list[str]) -> list[int]: |
| 90 | + start, end = list(row_days[0]), list(row_days[1]) |
| 91 | + row: list[int] = [] |
| 92 | + row += [dc(start[0]), dc(start[1])] if len(start) > 1 else [0, dc(start[0])] |
| 93 | + row += [0 if hide_dates else 44] |
| 94 | + row += [dc(end[0]), dc(end[1])] if len(end) > 1 else [dc(end[0]), 0] |
| 95 | + row += [cal_color] * 7 |
| 96 | + row += [0] * (22 - 12) |
| 97 | + return row |
| 98 | + |
| 99 | + second_row = _two_digit_row(second_row_days) |
| 100 | + third_row = _two_digit_row(third_row_days) |
| 101 | + |
| 102 | + def _exact_two_digit_row(row_days: list[str]) -> list[int]: |
| 103 | + start, end = list(row_days[0]), list(row_days[1]) |
| 104 | + row = [ |
| 105 | + dc(start[0]), |
| 106 | + dc(start[1]), |
| 107 | + 0 if hide_dates else 44, |
| 108 | + dc(end[0]), |
| 109 | + dc(end[1]), |
| 110 | + ] |
| 111 | + row += [cal_color] * 7 |
| 112 | + row += [0] * (22 - 12) |
| 113 | + return row |
| 114 | + |
| 115 | + fourth_row = _exact_two_digit_row(fourth_row_days) |
| 116 | + |
| 117 | + if not fifth_row_days: |
| 118 | + fifth_row = [0] * 22 |
| 119 | + else: |
| 120 | + start, end = list(fifth_row_days[0]), list(fifth_row_days[1]) |
| 121 | + fifth_row = [ |
| 122 | + dc(start[0]), |
| 123 | + dc(start[1]), |
| 124 | + 0 if hide_dates or start == end else 44, |
| 125 | + 0 if start == end else dc(end[0]), |
| 126 | + 0 if start == end else dc(end[1]), |
| 127 | + ] |
| 128 | + fifth_row += [cal_color] * num_days_last_row |
| 129 | + fifth_row += [0] * (22 - (5 + num_days_last_row)) |
| 130 | + |
| 131 | + # Header: month/year + day-of-week labels |
| 132 | + if hide_month_year: |
| 133 | + month_year = [0, 0, 0, 0, 0] |
| 134 | + else: |
| 135 | + month_year = ( |
| 136 | + [_char_code_for_digit(c) for c in str(month)] |
| 137 | + + [59] # slash |
| 138 | + + [_char_code_for_digit(c) for c in str(year)[2:4]] |
| 139 | + ) |
| 140 | + |
| 141 | + header_space = 5 - len(month_year) |
| 142 | + header_row = ( |
| 143 | + month_year |
| 144 | + + [0] * header_space |
| 145 | + + ( |
| 146 | + [0] * 7 |
| 147 | + if hide_day_of_week |
| 148 | + else [_char_code_for_day(d) for d in days_of_week] |
| 149 | + ) |
| 150 | + + [0] * (22 - (7 + 5)) |
| 151 | + ) |
| 152 | + |
| 153 | + calendar = [header_row, first_row, second_row, third_row, fourth_row, fifth_row] |
| 154 | + |
| 155 | + # Overlay individual day colors |
| 156 | + for day_key, color in (highlighted_days or {}).items(): |
| 157 | + if (day := int(day_key)) > num_days: |
| 158 | + continue # ignore days that don't exist in month |
| 159 | + todays_row = math.floor((day + offset - 1) / 7) + 1 |
| 160 | + modulus = (day + offset - 1) % 7 |
| 161 | + # Account for spillover off the board |
| 162 | + todays_col = (12 if modulus == 0 else 13) if todays_row > 5 else modulus + 5 |
| 163 | + calendar[5 if todays_row > 5 else todays_row][todays_col] = color |
| 164 | + |
| 165 | + return calendar |
0 commit comments