2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-03-10 21:41:01 +00:00
|
|
|
#include <LibCore/DateTime.h>
|
2022-01-17 20:06:59 +00:00
|
|
|
#include <LibCore/System.h>
|
2021-11-27 14:00:20 +00:00
|
|
|
#include <LibMain/Main.h>
|
2019-12-02 14:22:55 +00:00
|
|
|
#include <stdio.h>
|
2020-03-08 11:05:14 +00:00
|
|
|
#include <string.h>
|
2019-12-02 14:22:55 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
int const line_width = 70;
|
|
|
|
int const line_count = 8;
|
|
|
|
int const column_width = 22;
|
2019-12-04 13:51:43 +00:00
|
|
|
|
|
|
|
char print_buffer[line_width * line_count];
|
|
|
|
char temp_buffer[line_width * 8];
|
|
|
|
|
|
|
|
int target_day;
|
|
|
|
|
|
|
|
int current_year;
|
|
|
|
int current_month;
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static void append_to_print(char* buffer, int row, int column, char* text)
|
2019-12-04 13:51:43 +00:00
|
|
|
{
|
|
|
|
int starting_point = (line_width * row) + (column * column_width);
|
|
|
|
for (int i = 0; text[i] != '\0'; i++) {
|
|
|
|
buffer[starting_point + i] = text[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static void insert_month_to_print(int column, int month, int year)
|
2019-12-04 13:51:43 +00:00
|
|
|
{
|
|
|
|
int printing_column = column;
|
|
|
|
int printing_row = 0;
|
|
|
|
|
|
|
|
// FIXME: Both the month name and month header text should be provided by a locale
|
|
|
|
sprintf(temp_buffer, " %02u - %04u ", month, year);
|
|
|
|
append_to_print(print_buffer, printing_row, printing_column, temp_buffer);
|
|
|
|
printing_row++;
|
|
|
|
|
|
|
|
sprintf(temp_buffer, "Su Mo Tu We Th Fr Sa");
|
|
|
|
append_to_print(print_buffer, printing_row, printing_column, temp_buffer);
|
|
|
|
printing_row++;
|
|
|
|
int day_to_print = 1;
|
2020-03-10 21:41:01 +00: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();
|
2019-12-04 13:51:43 +00:00
|
|
|
int last_written_chars = 0;
|
|
|
|
for (int i = 1; day_to_print <= days_in_the_month; ++i) {
|
|
|
|
if (i - 1 < first_day_of_week_for_month) {
|
|
|
|
last_written_chars += sprintf(temp_buffer + last_written_chars, " ");
|
|
|
|
} else {
|
|
|
|
if (year == current_year && month == current_month && target_day == day_to_print) {
|
|
|
|
// 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.
|
|
|
|
last_written_chars += sprintf(temp_buffer + last_written_chars, "%2d*", day_to_print);
|
|
|
|
} else {
|
|
|
|
last_written_chars += sprintf(temp_buffer + last_written_chars, "%2d ", day_to_print);
|
|
|
|
}
|
|
|
|
day_to_print++;
|
|
|
|
}
|
|
|
|
|
|
|
|
append_to_print(print_buffer, printing_row, printing_column, temp_buffer);
|
|
|
|
|
|
|
|
if (i % 7 == 0) {
|
|
|
|
printing_row++;
|
|
|
|
memset(temp_buffer, ' ', line_width * 8);
|
|
|
|
temp_buffer[line_width * 8 - 1] = '\0';
|
|
|
|
last_written_chars = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-10 21:48:37 +00:00
|
|
|
static void clean_buffers()
|
2019-12-04 13:51:43 +00:00
|
|
|
{
|
|
|
|
for (int i = 1; i < line_width * line_count; ++i) {
|
|
|
|
print_buffer[i - 1] = i % line_width == 0 ? '\n' : ' ';
|
|
|
|
}
|
|
|
|
print_buffer[line_width * line_count - 1] = '\0';
|
|
|
|
|
|
|
|
for (int i = 0; i < line_width; ++i) {
|
|
|
|
temp_buffer[i] = ' ';
|
|
|
|
}
|
|
|
|
temp_buffer[line_width - 1] = '\0';
|
|
|
|
}
|
|
|
|
|
2021-11-27 14:00:20 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-12-02 14:22:55 +00:00
|
|
|
{
|
2022-01-20 17:27:56 +00:00
|
|
|
TRY(Core::System::pledge("stdio rpath"));
|
|
|
|
TRY(Core::System::unveil("/etc/timezone", "r"));
|
2022-01-17 20:06:59 +00:00
|
|
|
TRY(Core::System::unveil(nullptr, nullptr));
|
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
int day = 0;
|
|
|
|
int month = 0;
|
|
|
|
int year = 0;
|
2019-12-04 13:51:43 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2020-12-05 15:22:58 +00:00
|
|
|
args_parser.set_general_help("Display a nice overview of a month or year, defaulting to the current month.");
|
2020-01-27 17:25:36 +00:00
|
|
|
// FIXME: This should ensure two values get parsed as month + year
|
2020-02-02 11:34:39 +00:00
|
|
|
args_parser.add_positional_argument(day, "Day of year", "day", Core::ArgsParser::Required::No);
|
|
|
|
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 14:00:20 +00:00
|
|
|
args_parser.parse(arguments);
|
2019-12-02 14:22:55 +00:00
|
|
|
|
|
|
|
time_t now = time(nullptr);
|
|
|
|
auto* tm = localtime(&now);
|
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
// Hack: workaround two values parsing as day + month.
|
|
|
|
if (day && month && !year) {
|
|
|
|
year = month;
|
|
|
|
month = day;
|
|
|
|
day = 0;
|
2019-12-02 14:22:55 +00:00
|
|
|
}
|
2019-12-04 13:51:43 +00:00
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
bool year_mode = !day && !month && year;
|
|
|
|
|
|
|
|
if (!year)
|
|
|
|
year = tm->tm_year + 1900;
|
|
|
|
if (!month)
|
|
|
|
month = tm->tm_mon + 1;
|
|
|
|
if (!day)
|
|
|
|
day = tm->tm_mday;
|
|
|
|
|
|
|
|
current_year = year;
|
|
|
|
current_month = month;
|
|
|
|
|
2019-12-04 13:51:43 +00:00
|
|
|
clean_buffers();
|
|
|
|
|
|
|
|
if (year_mode) {
|
2021-05-31 14:43:25 +00:00
|
|
|
out(" Year {:04} ", year);
|
|
|
|
outln();
|
|
|
|
outln();
|
2019-12-04 13:51:43 +00:00
|
|
|
|
|
|
|
for (int i = 1; i < 12; ++i) {
|
2020-01-27 17:25:36 +00:00
|
|
|
insert_month_to_print(0, i++, year);
|
|
|
|
insert_month_to_print(1, i++, year);
|
|
|
|
insert_month_to_print(2, i, year);
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("{}", print_buffer);
|
2019-12-04 13:51:43 +00:00
|
|
|
clean_buffers();
|
|
|
|
}
|
|
|
|
} else {
|
2020-01-27 17:25:36 +00:00
|
|
|
insert_month_to_print(0, month, year);
|
2021-05-31 14:43:25 +00:00
|
|
|
outln("{}", print_buffer);
|
2019-12-04 13:51:43 +00:00
|
|
|
clean_buffers();
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2019-12-02 14:22:55 +00:00
|
|
|
}
|