diff --git a/Base/usr/share/man/man1/cal.md b/Base/usr/share/man/man1/cal.md index 0be41ad8cc..bdfde3d356 100644 --- a/Base/usr/share/man/man1/cal.md +++ b/Base/usr/share/man/man1/cal.md @@ -5,7 +5,7 @@ cal - Display a calendar ## Synopsis ```**sh -$ cal [--starting-day weekday] [[month] year] +$ cal [--starting-day weekday] [--three-month-view] [[month] year] ``` ## Description @@ -23,6 +23,7 @@ Days, months and years are specified with numbers. Week starts at Sunday. ## Options * `-s`, `--starting-day`: Specify which day should start the week. Accepts either short or long weekday names or indexes (0 being Sunday). +* `-3`, `--three-month-view`: Display the previous, current, and next months side-by-side. ## Examples @@ -48,6 +49,16 @@ Su Mo Tu We Th Fr Sa 24 25 26 27 28 29 30 31 +# Display three months side-by-side +$ cal -3 3 2023 + February - 2023 March - 2023 April - 2023 +Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa + 1 2 3 4 1 2 3 4 1 + 5 6 7 8 9 10 11 5 6 7 8 9 10 11 2 3 4 5 6 7 8 +12 13 14 15 16 17 18 12 13 14 15 16 17 18 9 10 11 12 13 14 15 +19 20 21 22 23 24 25 19 20 21 22 23 24 25 16 17 18 19 20 21 22 +26 27 28 26 27 28 29 30 31 23 24 25 26 27 28 29 + # Display an entire year $ cal 2023 Year 2023 diff --git a/Userland/Utilities/cal.cpp b/Userland/Utilities/cal.cpp index 5b27e75318..99d4cbcc9b 100644 --- a/Userland/Utilities/cal.cpp +++ b/Userland/Utilities/cal.cpp @@ -143,6 +143,24 @@ static void print_months_side_by_side(Vector const& left_month, Vector 12) { + year += 1; + month = 1; + } +} + +static void go_to_previous_month(int& month, int& year) +{ + month -= 1; + if (month < 1) { + year -= 1; + month = 12; + } +} + ErrorOr serenity_main(Main::Arguments arguments) { TRY(Core::System::pledge("stdio rpath cpath")); @@ -150,6 +168,7 @@ ErrorOr serenity_main(Main::Arguments arguments) int month = 0; int year = 0; StringView week_start_day_name {}; + bool three_month_mode = false; Core::ArgsParser args_parser; args_parser.set_general_help("Display a nice overview of a month or year, defaulting to the current month."); @@ -157,6 +176,7 @@ ErrorOr serenity_main(Main::Arguments arguments) args_parser.add_positional_argument(month, "Month", "month", Core::ArgsParser::Required::No); args_parser.add_positional_argument(year, "Year", "year", Core::ArgsParser::Required::No); args_parser.add_option(week_start_day_name, "Day that starts the week", "starting-day", 's', "day"); + args_parser.add_option(three_month_mode, "Show the previous and next month beside the current one", "three-month-view", '3'); args_parser.parse(arguments); time_t now = time(nullptr); @@ -195,6 +215,17 @@ ErrorOr serenity_main(Main::Arguments arguments) Vector lines_right = TRY(month_lines_to_print(Header::Month, week_start_day, month_index, year)); print_months_side_by_side(lines_left, lines_center, lines_right); } + } else if (three_month_mode) { + int month_on_left = month, year_on_left = year; + go_to_previous_month(month_on_left, year_on_left); + + int month_on_right = month, year_on_right = year; + go_to_next_month(month_on_right, year_on_right); + + Vector lines_previous_month = TRY(month_lines_to_print(Header::MonthAndYear, week_start_day, month_on_left, year_on_left)); + Vector lines_current_month = TRY(month_lines_to_print(Header::MonthAndYear, week_start_day, month, year)); + Vector lines_next_month = TRY(month_lines_to_print(Header::MonthAndYear, week_start_day, month_on_right, year_on_right)); + print_months_side_by_side(lines_previous_month, lines_current_month, lines_next_month); } else { Vector lines = TRY(month_lines_to_print(Header::MonthAndYear, week_start_day, month, year)); for (String const& line : lines) {