1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 19:55:06 +00:00

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.
This commit is contained in:
Karol Baraniecki 2023-04-05 17:05:41 +02:00 committed by Andrew Kaster
parent 114da3a275
commit 94e14bbe65
2 changed files with 17 additions and 5 deletions

View file

@ -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. 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. Days, months and years are specified with numbers. Week starts at Sunday.
## Options ## Options
@ -45,6 +48,7 @@ Su Mo Tu We Th Fr Sa
24 25 26 27 28 29 30 24 25 26 27 28 29 30
31 31
# Display an entire year
$ cal 2023 $ cal 2023
Year 2023 Year 2023

View file

@ -12,6 +12,7 @@
#include <AK/StringUtils.h> #include <AK/StringUtils.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <LibCore/ArgsParser.h> #include <LibCore/ArgsParser.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/DateTime.h> #include <LibCore/DateTime.h>
#include <LibCore/System.h> #include <LibCore/System.h>
#include <LibMain/Main.h> #include <LibMain/Main.h>
@ -47,6 +48,13 @@ static ErrorOr<int> weekday_index(StringView weekday_name)
return Error::from_string_view(TRY(String::formatted("Unknown weekday name: '{}'", weekday_name))); return Error::from_string_view(TRY(String::formatted("Unknown weekday name: '{}'", weekday_name)));
} }
static ErrorOr<int> 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<StringView> month_name(int month) static ErrorOr<StringView> month_name(int month)
{ {
int month_index = month - 1; int month_index = month - 1;
@ -137,9 +145,7 @@ static void print_months_side_by_side(Vector<String> const& left_month, Vector<S
ErrorOr<int> serenity_main(Main::Arguments arguments) ErrorOr<int> serenity_main(Main::Arguments arguments)
{ {
TRY(Core::System::pledge("stdio rpath")); TRY(Core::System::pledge("stdio rpath cpath"));
TRY(Core::System::unveil("/etc/timezone", "r"));
TRY(Core::System::unveil(nullptr, nullptr));
int month = 0; int month = 0;
int year = 0; int year = 0;
@ -167,8 +173,10 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bool year_mode = !month && year; bool year_mode = !month && year;
int week_start_day = 0; int week_start_day;
if (!week_start_day_name.is_empty()) 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)); week_start_day = TRY(weekday_index(week_start_day_name));
if (!year) if (!year)