cal: Add the -3 option

This allows to view just the previous, current, and next month
simultaneously. The idea for this is shamelessly stolen from FreeBSD :^)
This commit is contained in:
Karol Baraniecki 2023-04-05 17:11:22 +02:00 committed by Andrew Kaster
parent 94e14bbe65
commit dab82e1531
2 changed files with 43 additions and 1 deletions

View file

@ -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

View file

@ -143,6 +143,24 @@ static void print_months_side_by_side(Vector<String> const& left_month, Vector<S
}
}
static void go_to_next_month(int& month, int& year)
{
month += 1;
if (month > 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<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath cpath"));
@ -150,6 +168,7 @@ ErrorOr<int> 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<int> 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<int> serenity_main(Main::Arguments arguments)
Vector<String> 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<String> lines_previous_month = TRY(month_lines_to_print(Header::MonthAndYear, week_start_day, month_on_left, year_on_left));
Vector<String> lines_current_month = TRY(month_lines_to_print(Header::MonthAndYear, week_start_day, month, year));
Vector<String> 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<String> lines = TRY(month_lines_to_print(Header::MonthAndYear, week_start_day, month, year));
for (String const& line : lines) {