From 94e14bbe659174e11bed9bb36740e3ef5c9b05ca Mon Sep 17 00:00:00 2001 From: Karol Baraniecki Date: Wed, 5 Apr 2023 17:05:41 +0200 Subject: [PATCH] cal: Get default week start day from Calendar Making it configurable in system settings :^) The --start-day option can still overwrite this global default. This change makes it no longer possible to use unveil: as we have to load the Calendar config file, which might be in a dynamic location. It's also neccessary to add `cpath` to the pledge, as opening a nonexistent config file with Core::ConfigFile::open_for_app creates it. --- Base/usr/share/man/man1/cal.md | 4 ++++ Userland/Utilities/cal.cpp | 18 +++++++++++++----- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/Base/usr/share/man/man1/cal.md b/Base/usr/share/man/man1/cal.md index 4d99d9b30b..0be41ad8cc 100644 --- a/Base/usr/share/man/man1/cal.md +++ b/Base/usr/share/man/man1/cal.md @@ -15,6 +15,9 @@ An overview of a whole year is displayed when a `year` is passed without a `mont The current day is always highlighted. +Months and years are specified with numbers. Weeks start at what's configured in the Calendar system settings, +unless the `--starting-day` option is passed. + Days, months and years are specified with numbers. Week starts at Sunday. ## Options @@ -45,6 +48,7 @@ Su Mo Tu We Th Fr Sa 24 25 26 27 28 29 30 31 +# Display an entire year $ cal 2023 Year 2023 diff --git a/Userland/Utilities/cal.cpp b/Userland/Utilities/cal.cpp index 562b17dd16..5b27e75318 100644 --- a/Userland/Utilities/cal.cpp +++ b/Userland/Utilities/cal.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -47,6 +48,13 @@ static ErrorOr weekday_index(StringView weekday_name) return Error::from_string_view(TRY(String::formatted("Unknown weekday name: '{}'", weekday_name))); } +static ErrorOr default_weekday_start() +{ + auto calendar_config = TRY(Core::ConfigFile::open_for_app("Calendar"sv)); + String default_first_day_of_week = TRY(String::from_utf8(calendar_config->read_entry("View"sv, "FirstDayOfWeek"sv, "Sunday"sv))); + return TRY(weekday_index(default_first_day_of_week)); +} + static ErrorOr month_name(int month) { int month_index = month - 1; @@ -137,9 +145,7 @@ static void print_months_side_by_side(Vector const& left_month, Vector serenity_main(Main::Arguments arguments) { - TRY(Core::System::pledge("stdio rpath")); - TRY(Core::System::unveil("/etc/timezone", "r")); - TRY(Core::System::unveil(nullptr, nullptr)); + TRY(Core::System::pledge("stdio rpath cpath")); int month = 0; int year = 0; @@ -167,8 +173,10 @@ ErrorOr serenity_main(Main::Arguments arguments) bool year_mode = !month && year; - int week_start_day = 0; - if (!week_start_day_name.is_empty()) + int week_start_day; + if (week_start_day_name.is_empty()) + week_start_day = TRY(default_weekday_start()); + else week_start_day = TRY(weekday_index(week_start_day_name)); if (!year)