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.
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

View file

@ -12,6 +12,7 @@
#include <AK/StringUtils.h>
#include <AK/StringView.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/ConfigFile.h>
#include <LibCore/DateTime.h>
#include <LibCore/System.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)));
}
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)
{
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)
{
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<int> 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)