2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2023-03-12 09:45:24 +01:00
|
|
|
#include <AK/String.h>
|
|
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
|
#include <AK/StringView.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-03-10 16:41:01 -05:00
|
|
|
#include <LibCore/DateTime.h>
|
2022-01-17 21:06:59 +01:00
|
|
|
#include <LibCore/System.h>
|
2021-11-27 15:00:20 +01:00
|
|
|
#include <LibMain/Main.h>
|
2019-12-02 15:22:55 +01:00
|
|
|
#include <time.h>
|
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
int const line_width = 70;
|
|
|
|
|
int const line_count = 8;
|
|
|
|
|
int const column_width = 22;
|
2019-12-04 14:51:43 +01:00
|
|
|
|
|
|
|
|
int current_year;
|
|
|
|
|
int current_month;
|
2023-03-12 09:40:18 +01:00
|
|
|
int current_day;
|
2019-12-04 14:51:43 +01:00
|
|
|
|
2023-03-12 09:45:24 +01:00
|
|
|
static ErrorOr<Vector<String>> month_lines_to_print(int month, int year)
|
2019-12-04 14:51:43 +01:00
|
|
|
{
|
2023-03-12 09:45:24 +01:00
|
|
|
Vector<String> lines;
|
2019-12-04 14:51:43 +01:00
|
|
|
|
|
|
|
|
// FIXME: Both the month name and month header text should be provided by a locale
|
2023-03-12 09:45:24 +01:00
|
|
|
TRY(lines.try_append(TRY(String::formatted(" {:02} - {:02} ", month, year))));
|
|
|
|
|
TRY(lines.try_append(TRY(String::from_utf8("Su Mo Tu We Th Fr Sa"sv))));
|
2019-12-04 14:51:43 +01:00
|
|
|
|
|
|
|
|
int day_to_print = 1;
|
2023-03-12 09:45:24 +01:00
|
|
|
|
2020-03-10 16:41:01 -05:00
|
|
|
auto date_time = Core::DateTime::create(year, month, 1);
|
|
|
|
|
int first_day_of_week_for_month = date_time.weekday();
|
|
|
|
|
int days_in_the_month = date_time.days_in_month();
|
2023-03-12 09:45:24 +01:00
|
|
|
|
|
|
|
|
StringBuilder row;
|
2019-12-04 14:51:43 +01:00
|
|
|
for (int i = 1; day_to_print <= days_in_the_month; ++i) {
|
|
|
|
|
if (i - 1 < first_day_of_week_for_month) {
|
2023-03-12 09:45:24 +01:00
|
|
|
row.append(" "sv);
|
2019-12-04 14:51:43 +01:00
|
|
|
} else {
|
2023-03-12 09:40:18 +01:00
|
|
|
if (year == current_year && month == current_month && day_to_print == current_day) {
|
2019-12-04 14:51:43 +01:00
|
|
|
// FIXME: To replicate Unix cal it would be better to use "\x1b[30;47m%2d\x1b[0m " in here instead of *.
|
|
|
|
|
// However, doing that messes up the layout.
|
2023-03-12 09:45:24 +01:00
|
|
|
row.appendff("{:02}*", day_to_print);
|
2019-12-04 14:51:43 +01:00
|
|
|
} else {
|
2023-03-12 09:45:24 +01:00
|
|
|
row.appendff("{:02} ", day_to_print);
|
2019-12-04 14:51:43 +01:00
|
|
|
}
|
|
|
|
|
day_to_print++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (i % 7 == 0) {
|
2023-03-12 09:45:24 +01:00
|
|
|
TRY(lines.try_append(TRY(row.to_string())));
|
|
|
|
|
row.clear();
|
2019-12-04 14:51:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-12 09:45:24 +01:00
|
|
|
|
|
|
|
|
TRY(lines.try_append(TRY(row.to_string())));
|
|
|
|
|
|
|
|
|
|
return lines;
|
2019-12-04 14:51:43 +01:00
|
|
|
}
|
|
|
|
|
|
2023-03-12 09:45:24 +01:00
|
|
|
static void print_months_side_by_side(Vector<String> const& left_month, Vector<String> const& center_month, Vector<String> const& right_month)
|
2019-12-04 14:51:43 +01:00
|
|
|
{
|
2023-03-12 09:45:24 +01:00
|
|
|
for (size_t i = 0; i < left_month.size() || i < center_month.size() || i < right_month.size(); i++) {
|
|
|
|
|
StringView left = i < left_month.size() ? left_month[i] : ""sv;
|
|
|
|
|
StringView center = i < center_month.size() ? center_month[i] : ""sv;
|
|
|
|
|
StringView right = i < right_month.size() ? right_month[i] : ""sv;
|
2019-12-04 14:51:43 +01:00
|
|
|
|
2023-03-12 09:45:24 +01:00
|
|
|
outln("{: <21} {: <21} {: <21}", left, center, right);
|
2019-12-04 14:51:43 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-27 15:00:20 +01:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-12-02 15:22:55 +01:00
|
|
|
{
|
2022-01-20 12:27:56 -05:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
|
|
|
|
TRY(Core::System::unveil("/etc/timezone", "r"));
|
2022-01-17 21:06:59 +01:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
|
|
|
|
|
2020-01-27 20:25:36 +03:00
|
|
|
int month = 0;
|
|
|
|
|
int year = 0;
|
2019-12-04 14:51:43 +01:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 16:22:58 +01:00
|
|
|
args_parser.set_general_help("Display a nice overview of a month or year, defaulting to the current month.");
|
2023-02-23 16:53:40 +01:00
|
|
|
// FIXME: This should ensure one value gets parsed as just a year
|
2020-02-02 12:34:39 +01:00
|
|
|
args_parser.add_positional_argument(month, "Month", "month", Core::ArgsParser::Required::No);
|
|
|
|
|
args_parser.add_positional_argument(year, "Year", "year", Core::ArgsParser::Required::No);
|
2021-11-27 15:00:20 +01:00
|
|
|
args_parser.parse(arguments);
|
2019-12-02 15:22:55 +01:00
|
|
|
|
|
|
|
|
time_t now = time(nullptr);
|
|
|
|
|
auto* tm = localtime(&now);
|
2023-03-12 09:40:18 +01:00
|
|
|
current_year = tm->tm_year + 1900;
|
|
|
|
|
current_month = tm->tm_mon + 1;
|
|
|
|
|
current_day = tm->tm_mday;
|
2019-12-02 15:22:55 +01:00
|
|
|
|
2023-02-23 16:53:40 +01:00
|
|
|
// Hack: workaround one value parsing as a month
|
|
|
|
|
if (month && !year) {
|
2020-01-27 20:25:36 +03:00
|
|
|
year = month;
|
2023-02-23 16:53:40 +01:00
|
|
|
month = 0;
|
2019-12-02 15:22:55 +01:00
|
|
|
}
|
2019-12-04 14:51:43 +01:00
|
|
|
|
2023-02-23 16:53:40 +01:00
|
|
|
bool year_mode = !month && year;
|
2020-01-27 20:25:36 +03:00
|
|
|
|
|
|
|
|
if (!year)
|
2023-03-12 09:40:18 +01:00
|
|
|
year = current_year;
|
2020-01-27 20:25:36 +03:00
|
|
|
if (!month)
|
2023-03-12 09:40:18 +01:00
|
|
|
month = current_month;
|
2020-01-27 20:25:36 +03:00
|
|
|
|
2019-12-04 14:51:43 +01:00
|
|
|
if (year_mode) {
|
2023-03-12 09:45:24 +01:00
|
|
|
outln(" Year {:04} ", year);
|
2019-12-04 14:51:43 +01:00
|
|
|
|
|
|
|
|
for (int i = 1; i < 12; ++i) {
|
2023-03-12 09:45:24 +01:00
|
|
|
outln();
|
|
|
|
|
outln();
|
|
|
|
|
Vector<String> lines_left = TRY(month_lines_to_print(i++, year));
|
|
|
|
|
Vector<String> lines_center = TRY(month_lines_to_print(i++, year));
|
|
|
|
|
Vector<String> lines_right = TRY(month_lines_to_print(i, year));
|
|
|
|
|
print_months_side_by_side(lines_left, lines_center, lines_right);
|
2019-12-04 14:51:43 +01:00
|
|
|
}
|
|
|
|
|
} else {
|
2023-03-12 09:45:24 +01:00
|
|
|
Vector<String> lines = TRY(month_lines_to_print(month, year));
|
|
|
|
|
for (String const& line : lines) {
|
|
|
|
|
outln("{}", line);
|
|
|
|
|
}
|
2019-12-04 14:51:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2019-12-02 15:22:55 +01:00
|
|
|
}
|