cal.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. #include <LibCore/ArgsParser.h>
  27. #include <LibCore/DateTime.h>
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <time.h>
  31. const int line_width = 70;
  32. const int line_count = 8;
  33. const int column_width = 22;
  34. char print_buffer[line_width * line_count];
  35. char temp_buffer[line_width * 8];
  36. int target_year;
  37. int target_month;
  38. int target_day;
  39. int current_year;
  40. int current_month;
  41. static void append_to_print(char* buffer, int row, int column, char* text)
  42. {
  43. int starting_point = (line_width * row) + (column * column_width);
  44. for (int i = 0; text[i] != '\0'; i++) {
  45. buffer[starting_point + i] = text[i];
  46. }
  47. }
  48. static void insert_month_to_print(int column, int month, int year)
  49. {
  50. int printing_column = column;
  51. int printing_row = 0;
  52. // FIXME: Both the month name and month header text should be provided by a locale
  53. sprintf(temp_buffer, " %02u - %04u ", month, year);
  54. append_to_print(print_buffer, printing_row, printing_column, temp_buffer);
  55. printing_row++;
  56. sprintf(temp_buffer, "Su Mo Tu We Th Fr Sa");
  57. append_to_print(print_buffer, printing_row, printing_column, temp_buffer);
  58. printing_row++;
  59. int day_to_print = 1;
  60. auto date_time = Core::DateTime::create(year, month, 1);
  61. int first_day_of_week_for_month = date_time.weekday();
  62. int days_in_the_month = date_time.days_in_month();
  63. int last_written_chars = 0;
  64. for (int i = 1; day_to_print <= days_in_the_month; ++i) {
  65. if (i - 1 < first_day_of_week_for_month) {
  66. last_written_chars += sprintf(temp_buffer + last_written_chars, " ");
  67. } else {
  68. if (year == current_year && month == current_month && target_day == day_to_print) {
  69. // FIXME: To replicate Unix cal it would be better to use "\x1b[30;47m%2d\x1b[0m " in here instead of *.
  70. // However, doing that messes up the layout.
  71. last_written_chars += sprintf(temp_buffer + last_written_chars, "%2d*", day_to_print);
  72. } else {
  73. last_written_chars += sprintf(temp_buffer + last_written_chars, "%2d ", day_to_print);
  74. }
  75. day_to_print++;
  76. }
  77. append_to_print(print_buffer, printing_row, printing_column, temp_buffer);
  78. if (i % 7 == 0) {
  79. printing_row++;
  80. memset(temp_buffer, ' ', line_width * 8);
  81. temp_buffer[line_width * 8 - 1] = '\0';
  82. last_written_chars = 0;
  83. }
  84. }
  85. }
  86. static void clean_buffers()
  87. {
  88. for (int i = 1; i < line_width * line_count; ++i) {
  89. print_buffer[i - 1] = i % line_width == 0 ? '\n' : ' ';
  90. }
  91. print_buffer[line_width * line_count - 1] = '\0';
  92. for (int i = 0; i < line_width; ++i) {
  93. temp_buffer[i] = ' ';
  94. }
  95. temp_buffer[line_width - 1] = '\0';
  96. }
  97. int main(int argc, char** argv)
  98. {
  99. int day = 0;
  100. int month = 0;
  101. int year = 0;
  102. Core::ArgsParser args_parser;
  103. // FIXME: This should ensure two values get parsed as month + year
  104. args_parser.add_positional_argument(day, "Day of year", "day", Core::ArgsParser::Required::No);
  105. args_parser.add_positional_argument(month, "Month", "month", Core::ArgsParser::Required::No);
  106. args_parser.add_positional_argument(year, "Year", "year", Core::ArgsParser::Required::No);
  107. args_parser.parse(argc, argv);
  108. time_t now = time(nullptr);
  109. auto* tm = localtime(&now);
  110. // Hack: workaround two values parsing as day + month.
  111. if (day && month && !year) {
  112. year = month;
  113. month = day;
  114. day = 0;
  115. }
  116. bool year_mode = !day && !month && year;
  117. if (!year)
  118. year = tm->tm_year + 1900;
  119. if (!month)
  120. month = tm->tm_mon + 1;
  121. if (!day)
  122. day = tm->tm_mday;
  123. current_year = year;
  124. current_month = month;
  125. clean_buffers();
  126. if (year_mode) {
  127. printf(" ");
  128. printf("Year %4d", year);
  129. printf(" \n\n");
  130. for (int i = 1; i < 12; ++i) {
  131. insert_month_to_print(0, i++, year);
  132. insert_month_to_print(1, i++, year);
  133. insert_month_to_print(2, i, year);
  134. printf(print_buffer);
  135. printf("\n");
  136. clean_buffers();
  137. }
  138. } else {
  139. insert_month_to_print(0, month, year);
  140. printf(print_buffer);
  141. printf("\n\n");
  142. clean_buffers();
  143. }
  144. return 0;
  145. }