Serenity Operating System
at hosted 169 lines 5.8 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include <LibCore/ArgsParser.h> 28#include <LibCore/DateTime.h> 29#include <stdio.h> 30#include <string.h> 31#include <time.h> 32 33const int line_width = 70; 34const int line_count = 8; 35const int column_width = 22; 36 37char print_buffer[line_width * line_count]; 38char temp_buffer[line_width * 8]; 39 40int target_year; 41int target_month; 42int target_day; 43 44int current_year; 45int current_month; 46 47void append_to_print(char* buffer, int row, int column, char* text) 48{ 49 int starting_point = (line_width * row) + (column * column_width); 50 for (int i = 0; text[i] != '\0'; i++) { 51 buffer[starting_point + i] = text[i]; 52 } 53} 54 55void insert_month_to_print(int column, int month, int year) 56{ 57 int printing_column = column; 58 int printing_row = 0; 59 60 // FIXME: Both the month name and month header text should be provided by a locale 61 sprintf(temp_buffer, " %02u - %04u ", month, year); 62 append_to_print(print_buffer, printing_row, printing_column, temp_buffer); 63 printing_row++; 64 65 sprintf(temp_buffer, "Su Mo Tu We Th Fr Sa"); 66 append_to_print(print_buffer, printing_row, printing_column, temp_buffer); 67 printing_row++; 68 int day_to_print = 1; 69 auto date_time = Core::DateTime::create(year, month, 1); 70 int first_day_of_week_for_month = date_time.weekday(); 71 int days_in_the_month = date_time.days_in_month(); 72 int last_written_chars = 0; 73 for (int i = 1; day_to_print <= days_in_the_month; ++i) { 74 if (i - 1 < first_day_of_week_for_month) { 75 last_written_chars += sprintf(temp_buffer + last_written_chars, " "); 76 } else { 77 if (year == current_year && month == current_month && target_day == day_to_print) { 78 // FIXME: To replicate Unix cal it would be better to use "\x1b[30;47m%2d\x1b[0m " in here instead of *. 79 // However, doing that messes up the layout. 80 last_written_chars += sprintf(temp_buffer + last_written_chars, "%2d*", day_to_print); 81 } else { 82 last_written_chars += sprintf(temp_buffer + last_written_chars, "%2d ", day_to_print); 83 } 84 day_to_print++; 85 } 86 87 append_to_print(print_buffer, printing_row, printing_column, temp_buffer); 88 89 if (i % 7 == 0) { 90 printing_row++; 91 memset(temp_buffer, ' ', line_width * 8); 92 temp_buffer[line_width * 8 - 1] = '\0'; 93 last_written_chars = 0; 94 } 95 } 96} 97 98void clean_buffers() 99{ 100 for (int i = 1; i < line_width * line_count; ++i) { 101 print_buffer[i - 1] = i % line_width == 0 ? '\n' : ' '; 102 } 103 print_buffer[line_width * line_count - 1] = '\0'; 104 105 for (int i = 0; i < line_width; ++i) { 106 temp_buffer[i] = ' '; 107 } 108 temp_buffer[line_width - 1] = '\0'; 109} 110 111int main(int argc, char** argv) 112{ 113 int day = 0; 114 int month = 0; 115 int year = 0; 116 117 Core::ArgsParser args_parser; 118 // FIXME: This should ensure two values get parsed as month + year 119 args_parser.add_positional_argument(day, "Day of year", "day", Core::ArgsParser::Required::No); 120 args_parser.add_positional_argument(month, "Month", "month", Core::ArgsParser::Required::No); 121 args_parser.add_positional_argument(year, "Year", "year", Core::ArgsParser::Required::No); 122 args_parser.parse(argc, argv); 123 124 time_t now = time(nullptr); 125 auto* tm = localtime(&now); 126 127 // Hack: workaround two values parsing as day + month. 128 if (day && month && !year) { 129 year = month; 130 month = day; 131 day = 0; 132 } 133 134 bool year_mode = !day && !month && year; 135 136 if (!year) 137 year = tm->tm_year + 1900; 138 if (!month) 139 month = tm->tm_mon + 1; 140 if (!day) 141 day = tm->tm_mday; 142 143 current_year = year; 144 current_month = month; 145 146 clean_buffers(); 147 148 if (year_mode) { 149 printf(" "); 150 printf("Year %4d", year); 151 printf(" \n\n"); 152 153 for (int i = 1; i < 12; ++i) { 154 insert_month_to_print(0, i++, year); 155 insert_month_to_print(1, i++, year); 156 insert_month_to_print(2, i, year); 157 printf(print_buffer); 158 printf("\n"); 159 clean_buffers(); 160 } 161 } else { 162 insert_month_to_print(0, month, year); 163 printf(print_buffer); 164 printf("\n\n"); 165 clean_buffers(); 166 } 167 168 return 0; 169}