cal: Only show year next to each month in one-month-mode

Additionally rename a loop variable in serenity_main() because
after introducing the `Header` enum IMO it's less readable with `i`.
This commit is contained in:
Karol Baraniecki 2023-04-04 17:42:52 +02:00 committed by Andrew Kaster
parent a3e6b18efc
commit af2a606f25
2 changed files with 26 additions and 11 deletions

View file

@ -45,7 +45,7 @@ $ cal 2023
Year 2023
January - 2023 February - 2023 March - 2023
January February March
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 5 6 7 1 2 3 4 1 2 3 4
8 9 10 11 12 13 14 5 6 7 8 9 10 11 5 6 7 8 9 10 11
@ -54,7 +54,7 @@ Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
29 30 31 26 27 28 26 27 28 29 30 31
April - 2023 May - 2023 June - 2023
April May June
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 6 1 2 3
2 3 4 5 6 7 8 7 8 9 10 11 12 13 4 5 6 7 8 9 10
@ -64,7 +64,7 @@ Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
30
July - 2023 August - 2023 September - 2023
July August September
Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
1 1 2 3 4 5 1 2
2 3 4 5 6 7 8 6 7 8 9 10 11 12 3 4 5 6 7 8 9
@ -74,7 +74,7 @@ Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa
30 31
October - 2023 November - 2023 December - 2023
October November December
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 5 6 7 1 2 3 4 1 2
8 9 10 11 12 13 14 5 6 7 8 9 10 11 3 4 5 6 7 8 9

View file

@ -35,12 +35,27 @@ static ErrorOr<StringView> month_name(int month)
return AK::long_month_names.at(month_index);
}
static ErrorOr<Vector<String>> month_lines_to_print(int month, int year)
enum class Header {
MonthAndYear,
Month,
};
static ErrorOr<Vector<String>> month_lines_to_print(Header header_mode, int month, int year)
{
Vector<String> lines;
// FIXME: Both the month name and month header text should be provided by a locale
TRY(lines.try_append(TRY(String::formatted("{: ^{}s}", TRY(String::formatted("{} - {}", TRY(month_name(month)), year)), month_width))));
String header;
switch (header_mode) {
case Header::Month:
header = TRY(String::from_utf8(TRY(month_name(month))));
break;
case Header::MonthAndYear:
header = TRY(String::formatted("{} - {}", TRY(month_name(month)), year));
break;
}
TRY(lines.try_append(TRY(String::formatted("{: ^{}s}", header, month_width))));
TRY(lines.try_append(TRY(String::from_utf8("Su Mo Tu We Th Fr Sa"sv))));
auto date_time = Core::DateTime::create(year, month, 1);
@ -121,16 +136,16 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
if (year_mode) {
outln("{: ^{}}", TRY(String::formatted("Year {}", year)), year_width);
for (int i = 1; i < 12; ++i) {
for (int month_index = 1; month_index < 12; ++month_index) {
outln();
outln();
Vector<String> lines_left = TRY(month_lines_to_print(i++, year));
Vector<String> lines_center = TRY(month_lines_to_print(i++, year));
Vector<String> lines_right = TRY(month_lines_to_print(i, year));
Vector<String> lines_left = TRY(month_lines_to_print(Header::Month, month_index++, year));
Vector<String> lines_center = TRY(month_lines_to_print(Header::Month, month_index++, year));
Vector<String> lines_right = TRY(month_lines_to_print(Header::Month, month_index, year));
print_months_side_by_side(lines_left, lines_center, lines_right);
}
} else {
Vector<String> lines = TRY(month_lines_to_print(month, year));
Vector<String> lines = TRY(month_lines_to_print(Header::MonthAndYear, month, year));
for (String const& line : lines) {
outln("{}", line);
}