@@ -349,27 +349,11 @@ def formatday(self, day, weekday, width):
349
349
s = '%2i' % day # right-align single-digit days
350
350
return s .center (width )
351
351
352
- def formatweek (self , theweek , width , * , highlight_day = None ):
352
+ def formatweek (self , theweek , width ):
353
353
"""
354
354
Returns a single week in a string (no newline).
355
355
"""
356
- if highlight_day :
357
- from _colorize import get_colors
358
-
359
- ansi = get_colors ()
360
- highlight = f"{ ansi .BLACK } { ansi .BACKGROUND_YELLOW } "
361
- reset = ansi .RESET
362
- else :
363
- highlight = reset = ""
364
-
365
- return ' ' .join (
366
- (
367
- f"{ highlight } { self .formatday (d , wd , width )} { reset } "
368
- if d == highlight_day
369
- else self .formatday (d , wd , width )
370
- )
371
- for (d , wd ) in theweek
372
- )
356
+ return ' ' .join (self .formatday (d , wd , width ) for (d , wd ) in theweek )
373
357
374
358
def formatweekday (self , day , width ):
375
359
"""
@@ -404,11 +388,10 @@ def prmonth(self, theyear, themonth, w=0, l=0):
404
388
"""
405
389
print (self .formatmonth (theyear , themonth , w , l ), end = '' )
406
390
407
- def formatmonth (self , theyear , themonth , w = 0 , l = 0 , * , highlight_day = None ):
391
+ def formatmonth (self , theyear , themonth , w = 0 , l = 0 ):
408
392
"""
409
393
Return a month's calendar string (multi-line).
410
394
"""
411
- highlight_day = highlight_day .day if highlight_day else None
412
395
w = max (2 , w )
413
396
l = max (1 , l )
414
397
s = self .formatmonthname (theyear , themonth , 7 * (w + 1 ) - 1 )
@@ -417,11 +400,11 @@ def formatmonth(self, theyear, themonth, w=0, l=0, *, highlight_day=None):
417
400
s += self .formatweekheader (w ).rstrip ()
418
401
s += '\n ' * l
419
402
for week in self .monthdays2calendar (theyear , themonth ):
420
- s += self .formatweek (week , w , highlight_day = highlight_day ).rstrip ()
403
+ s += self .formatweek (week , w ).rstrip ()
421
404
s += '\n ' * l
422
405
return s
423
406
424
- def formatyear (self , theyear , w = 2 , l = 1 , c = 6 , m = 3 , * , highlight_day = None ):
407
+ def formatyear (self , theyear , w = 2 , l = 1 , c = 6 , m = 3 ):
425
408
"""
426
409
Returns a year's calendar as a multi-line string.
427
410
"""
@@ -446,23 +429,15 @@ def formatyear(self, theyear, w=2, l=1, c=6, m=3, *, highlight_day=None):
446
429
a (formatstring (headers , colwidth , c ).rstrip ())
447
430
a ('\n ' * l )
448
431
449
- if highlight_day and highlight_day .month in months :
450
- month_pos = months .index (highlight_day .month )
451
- else :
452
- month_pos = None
453
-
454
432
# max number of weeks for this row
455
433
height = max (len (cal ) for cal in row )
456
434
for j in range (height ):
457
435
weeks = []
458
- for k , cal in enumerate ( row ) :
436
+ for cal in row :
459
437
if j >= len (cal ):
460
438
weeks .append ('' )
461
439
else :
462
- day = highlight_day .day if k == month_pos else None
463
- weeks .append (
464
- self .formatweek (cal [j ], w , highlight_day = day )
465
- )
440
+ weeks .append (self .formatweek (cal [j ], w ))
466
441
a (formatstring (weeks , colwidth , c ).rstrip ())
467
442
a ('\n ' * l )
468
443
return '' .join (v )
@@ -672,6 +647,111 @@ def formatmonthname(self, theyear, themonth, withyear=True):
672
647
with different_locale (self .locale ):
673
648
return super ().formatmonthname (theyear , themonth , withyear )
674
649
650
+
651
+ class _CLIDemoCalendar (LocaleTextCalendar ):
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 )
709
+ colwidth = (w + 1 ) * 7 - 1
710
+ v = []
711
+ a = v .append
712
+ a (repr (theyear ).center (colwidth * m + c * (m - 1 )).rstrip ())
713
+ a ('\n ' * l )
714
+ header = self .formatweekheader (w )
715
+ for (i , row ) in enumerate (self .yeardays2calendar (theyear , m )):
716
+ # months in this row
717
+ months = range (m * i + 1 , min (m * (i + 1 )+ 1 , 13 ))
718
+ a ('\n ' * l )
719
+ names = (self .formatmonthname (theyear , k , colwidth , False )
720
+ for k in months )
721
+ a (formatstring (names , colwidth , c ).rstrip ())
722
+ a ('\n ' * l )
723
+ headers = (header for k in months )
724
+ a (formatstring (headers , colwidth , c ).rstrip ())
725
+ a ('\n ' * l )
726
+
727
+ if (
728
+ self .highlight_day
729
+ and self .highlight_day .year == theyear
730
+ and self .highlight_day .month in months
731
+ ):
732
+ month_pos = months .index (self .highlight_day .month )
733
+ else :
734
+ month_pos = None
735
+
736
+ # max number of weeks for this row
737
+ height = max (len (cal ) for cal in row )
738
+ for j in range (height ):
739
+ weeks = []
740
+ for k , cal in enumerate (row ):
741
+ if j >= len (cal ):
742
+ weeks .append ('' )
743
+ else :
744
+ day = (
745
+ self .highlight_day .day if k == month_pos else None
746
+ )
747
+ weeks .append (
748
+ self .formatweek (cal [j ], w , highlight_day = day )
749
+ )
750
+ a (formatstring (weeks , colwidth , c ).rstrip ())
751
+ a ('\n ' * l )
752
+ return '' .join (v )
753
+
754
+
675
755
# Support for old module level interface
676
756
c = TextCalendar ()
677
757
@@ -813,26 +893,21 @@ def main(args=None):
813
893
write (cal .formatyearpage (options .year , ** optdict ))
814
894
else :
815
895
if options .locale :
816
- cal = LocaleTextCalendar ( locale = locale )
896
+ cal = _CLIDemoCalendar ( highlight_day = today , locale = locale )
817
897
else :
818
- cal = TextCalendar ( )
898
+ cal = _CLIDemoCalendar ( highlight_day = today )
819
899
cal .setfirstweekday (options .first_weekday )
820
900
optdict = dict (w = options .width , l = options .lines )
821
901
if options .month is None :
822
902
optdict ["c" ] = options .spacing
823
903
optdict ["m" ] = options .months
824
- if options . month is not None :
904
+ else :
825
905
_validate_month (options .month )
826
906
if options .year is None :
827
- optdict ["highlight_day" ] = today
828
907
result = cal .formatyear (today .year , ** optdict )
829
908
elif options .month is None :
830
- if options .year == today .year :
831
- optdict ["highlight_day" ] = today
832
909
result = cal .formatyear (options .year , ** optdict )
833
910
else :
834
- if options .year == today .year and options .month == today .month :
835
- optdict ["highlight_day" ] = today
836
911
result = cal .formatmonth (options .year , options .month , ** optdict )
837
912
write = sys .stdout .write
838
913
if options .encoding :
0 commit comments