module_05.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <time.h>
  4. #include <unistd.h>
  5. #include <signal.h>
  6. #include <stdlib.h>
  7. #include "pseudographic_font_5.h"
  8. // Флаг для корректного завершения
  9. static volatile sig_atomic_t keep_running = 1;
  10. // Переменная для отслеживания обработчика SIGINT
  11. static volatile int handler_set = 0;
  12. // Обработчик сигнала SIGINT (Ctrl+C)
  13. static void handle_sigint(int sig) {
  14. (void)sig;
  15. keep_running = 0;
  16. printf("\033[2J\033[H\033[?25h");
  17. fflush(stdout);
  18. _exit(0);
  19. }
  20. // Функция для вывода псевдографического времени
  21. static void print_pseudographic_time(int hours, int mins, int secs) {
  22. if (hours < 0 || hours > 23 || mins < 0 || mins > 59 || secs < 0 || secs > 59) {
  23. printf("\033[H\033[1mInvalid time: %02d:%02d:%02d\033[0m\n", hours, mins, secs);
  24. fflush(stdout);
  25. return;
  26. }
  27. char time_str[9];
  28. snprintf(time_str, 9, "%02d:%02d:%02d", hours, mins, secs);
  29. const char** chars[8];
  30. for (int i = 0; i < 8; i++) {
  31. chars[i] = get_pseudographic_char_5(time_str[i]);
  32. }
  33. printf("\033[H");
  34. for (int row = 0; row < 5; row++) {
  35. for (int i = 0; i < 8; i++) {
  36. if (i == 2 || i == 5) {
  37. printf("\033[1m\033[90m%s\033[0m", chars[i][row]);
  38. } else {
  39. printf("\033[1m\033[37m%s\033[0m", chars[i][row]);
  40. }
  41. }
  42. printf("\n");
  43. }
  44. fflush(stdout);
  45. }
  46. int module_05_run(const char *arg) {
  47. if (arg == NULL || strcmp(arg, "m5") != 0) {
  48. return 0;
  49. }
  50. if (!handler_set) {
  51. signal(SIGINT, handle_sigint);
  52. handler_set = 1;
  53. }
  54. printf("\033[?25l\n\n\n\n\n");
  55. fflush(stdout);
  56. while (keep_running) {
  57. time_t rawtime;
  58. struct tm *timeinfo;
  59. if (time(&rawtime) == (time_t)-1) {
  60. printf("\033[H\033[1mFailed to get time\033[0m\n");
  61. fflush(stdout);
  62. sleep(1);
  63. continue;
  64. }
  65. timeinfo = localtime(&rawtime);
  66. if (timeinfo == NULL) {
  67. printf("\033[H\033[1mFailed to parse time\033[0m\n");
  68. fflush(stdout);
  69. sleep(1);
  70. continue;
  71. }
  72. print_pseudographic_time(timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
  73. sleep(1);
  74. }
  75. return 0;
  76. }