cal: Don't accept passing a [day]

Every other cal implementation just highlights the current day instead
of letting you specify a custom one to highlight. It doesn't seem to be
that useful, and is currently broken - no day gets highlighted at all,
because the `target_day` global is never written to.

Moreover, this complicates parsing the arguments. This commit also fixes
parsing a case where just a year is provided to `cal` - for example `cal
2023`.
This commit is contained in:
Karol Baraniecki 2023-02-23 16:53:40 +01:00 committed by Andrew Kaster
parent f5cb46e316
commit b57c718418
2 changed files with 8 additions and 11 deletions

View file

@ -5,12 +5,13 @@ cal - Display a calendar
## Synopsis
```**sh
$ cal [[[day] month] year]
$ cal [[month] year]
```
## Description
This program displays a simple calendar. If no arguments are specified, the current month is displayed with the current day highlighted.
An overview of a whole year is displayed when a `year` is passed without a `month`.
Days, months and years are specified with numbers. Week starts at Sunday.

View file

@ -94,14 +94,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::unveil("/etc/timezone", "r"));
TRY(Core::System::unveil(nullptr, nullptr));
int day = 0;
int month = 0;
int year = 0;
Core::ArgsParser args_parser;
args_parser.set_general_help("Display a nice overview of a month or year, defaulting to the current month.");
// FIXME: This should ensure two values get parsed as month + year
args_parser.add_positional_argument(day, "Day of year", "day", Core::ArgsParser::Required::No);
// FIXME: This should ensure one value gets parsed as just a year
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.parse(arguments);
@ -109,22 +107,20 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
time_t now = time(nullptr);
auto* tm = localtime(&now);
// Hack: workaround two values parsing as day + month.
if (day && month && !year) {
// Hack: workaround one value parsing as a month
if (month && !year) {
year = month;
month = day;
day = 0;
month = 0;
}
bool year_mode = !day && !month && year;
bool year_mode = !month && year;
if (!year)
year = tm->tm_year + 1900;
if (!month)
month = tm->tm_mon + 1;
if (!day)
day = tm->tm_mday;
// FIXME: Those are really _target_ year and month values - not current ones
current_year = year;
current_month = month;