diff --git a/examples/calendar_layout.py b/examples/calendar_layout.py deleted file mode 100644 index 5b27b60c..00000000 --- a/examples/calendar_layout.py +++ /dev/null @@ -1,56 +0,0 @@ -""" -Builds calendar layout using Columns and Tables. -Usage: -python calendar_layout.py [YEAR] -Example: -python calendar_layout.py 2021 -""" -import argparse - -import calendar -from datetime import datetime - -from rich.table import Table -from rich.columns import Columns -from rich import print - - -def display_calendar(year): - - today = datetime.today() - year = int(year) - cal = calendar.Calendar() - - tables = [] - for month in range(1, 13): - - table = Table(title=f"{calendar.month_name[month]} {year}", style="green") - - for week_day in cal.iterweekdays(): - table.add_column("{:.3}".format(calendar.day_name[week_day])) - - month_days = cal.monthdayscalendar(year, month) - for weekdays in month_days: - days = [] - for day in weekdays: - today_tuple = today.day, today.month, today.year - - if day and (day, month, year) == today_tuple: - _day = f"[bold underline red]{day}" - else: - _day = "" if day == 0 else str(day) - days.append(_day) - table.add_row(*days) - - tables.append(table) - - columns = Columns(tables, equal=True, expand=True) - print(columns) - - -if __name__ == "__main__": - parser = argparse.ArgumentParser(description="Rich calendar") - parser.add_argument("year", metavar="year", type=int) - args = parser.parse_args() - - display_calendar(args.year) diff --git a/examples/print_calendar.py b/examples/print_calendar.py new file mode 100644 index 00000000..d9586aaa --- /dev/null +++ b/examples/print_calendar.py @@ -0,0 +1,70 @@ +""" +Builds calendar layout using Columns and Tables. +Usage: +python print_calendar.py [YEAR] +Example: +python print_calendar.py 2021 +""" +import argparse +import calendar +from datetime import datetime + +from rich.align import Align +from rich import box +from rich.columns import Columns +from rich.console import Console +from rich.table import Table +from rich.text import Text + + +def print_calendar(year): + """Print a calendar for a given year.""" + + today = datetime.today() + year = int(year) + cal = calendar.Calendar() + today_tuple = today.day, today.month, today.year + + tables = [] + + for month in range(1, 13): + table = Table( + title=f"{calendar.month_name[month]} {year}", + style="green", + box=box.SIMPLE_HEAVY, + padding=0, + ) + + for week_day in cal.iterweekdays(): + table.add_column( + "{:.3}".format(calendar.day_name[week_day]), justify="right" + ) + + month_days = cal.monthdayscalendar(year, month) + for weekdays in month_days: + days = [] + for index, day in enumerate(weekdays): + day_label = Text(str(day or ""), style="magenta") + if index in (5, 6): + day_label.stylize("blue") + if day and (day, month, year) == today_tuple: + day_label.stylize("white on dark_red") + days.append(day_label) + table.add_row(*days) + + tables.append(Align.center(table)) + + console = Console() + columns = Columns(tables, padding=1, expand=True) + console.rule(str(year)) + console.print() + console.print(columns) + console.rule(str(year)) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Rich calendar") + parser.add_argument("year", metavar="year", type=int) + args = parser.parse_args() + + print_calendar(args.year)