From b57c7184182d597266fad8ff706b1698575552da Mon Sep 17 00:00:00 2001 From: Karol Baraniecki Date: Thu, 23 Feb 2023 16:53:40 +0100 Subject: [PATCH] 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`. --- Base/usr/share/man/man1/cal.md | 3 ++- Userland/Utilities/cal.cpp | 16 ++++++---------- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/Base/usr/share/man/man1/cal.md b/Base/usr/share/man/man1/cal.md index c591f12ffa..175fa05cb0 100644 --- a/Base/usr/share/man/man1/cal.md +++ b/Base/usr/share/man/man1/cal.md @@ -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. diff --git a/Userland/Utilities/cal.cpp b/Userland/Utilities/cal.cpp index cc0a3d6176..ea33848d9a 100644 --- a/Userland/Utilities/cal.cpp +++ b/Userland/Utilities/cal.cpp @@ -94,14 +94,12 @@ ErrorOr 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 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;