| 649 | |
| 650 | |
| 651 | class _CLIDemoCalendar(TextCalendar): |
| 652 | def __init__(self, highlight_day=None, *args, **kwargs): |
| 653 | super().__init__(*args, **kwargs) |
| 654 | self.highlight_day = highlight_day |
| 655 | |
| 656 | def formatweek(self, theweek, width, *, highlight_day=None): |
| 657 | """ |
| 658 | Returns a single week in a string (no newline). |
| 659 | """ |
| 660 | if highlight_day: |
| 661 | from _colorize import get_colors |
| 662 | |
| 663 | ansi = get_colors() |
| 664 | highlight = f"{ansi.BLACK}{ansi.BACKGROUND_YELLOW}" |
| 665 | reset = ansi.RESET |
| 666 | else: |
| 667 | highlight = reset = "" |
| 668 | |
| 669 | return ' '.join( |
| 670 | ( |
| 671 | f"{highlight}{self.formatday(d, wd, width)}{reset}" |
| 672 | if d == highlight_day |
| 673 | else self.formatday(d, wd, width) |
| 674 | ) |
| 675 | for (d, wd) in theweek |
| 676 | ) |
| 677 | |
| 678 | def formatmonth(self, theyear, themonth, w=0, l=0): |
| 679 | """ |
| 680 | Return a month's calendar string (multi-line). |
| 681 | """ |
| 682 | if ( |
| 683 | self.highlight_day |
| 684 | and self.highlight_day.year == theyear |
| 685 | and self.highlight_day.month == themonth |
| 686 | ): |
| 687 | highlight_day = self.highlight_day.day |
| 688 | else: |
| 689 | highlight_day = None |
| 690 | w = max(2, w) |
| 691 | l = max(1, l) |
| 692 | s = self.formatmonthname(theyear, themonth, 7 * (w + 1) - 1) |
| 693 | s = s.rstrip() |
| 694 | s += '\n' * l |
| 695 | s += self.formatweekheader(w).rstrip() |
| 696 | s += '\n' * l |
| 697 | for week in self.monthdays2calendar(theyear, themonth): |
| 698 | s += self.formatweek(week, w, highlight_day=highlight_day).rstrip() |
| 699 | s += '\n' * l |
| 700 | return s |
| 701 | |
| 702 | def formatyear(self, theyear, w=2, l=1, c=6, m=3): |
| 703 | """ |
| 704 | Returns a year's calendar as a multi-line string. |
| 705 | """ |
| 706 | w = max(2, w) |
| 707 | l = max(1, l) |
| 708 | c = max(2, c) |