banovski

Graphic Timer

Sep 8th, 2025 (edited)
355
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.19 KB | Source Code | 0 0
  1. /* The utility is a graphic timer. It should be launched in a terminal
  2.  * emulator. It determines the size of the terminal window and then
  3.  * fills all of it with the character "W", in the number of minutes
  4.  * indicated in the definition of the macro SESSION_DURATION_MINS. */
  5.  
  6. #include <stdio.h>
  7. #include <sys/ioctl.h>
  8. #include <unistd.h>
  9. #include <stdlib.h>
  10.  
  11. #define SESSION_DURATION_MINS 20
  12. #define MICROSECS_IN_A_MINUTE 60000000
  13.  
  14. int main() {
  15.     struct winsize w;
  16.  
  17.     // Get the number of columns and the number of lines of the terminal window
  18.     if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
  19.         perror("ioctl failed!");
  20.         return 1;
  21.     }
  22.  
  23.     int char_places_number = (int)(w.ws_row * w.ws_col);
  24.     if (char_places_number <= 0) {
  25.         fprintf(stderr, "Invalid terminal size.\n");
  26.         return 1;
  27.     }
  28.  
  29.     useconds_t timeout_microsecs = (useconds_t)(SESSION_DURATION_MINS * MICROSECS_IN_A_MINUTE / char_places_number);
  30.  
  31.     // Clear the screen
  32.     printf("\033[H\033[J");
  33.  
  34.     for (int i = 0; i < char_places_number; i++) {
  35.         (void) putchar(87);
  36.         (void) fflush(stdout);
  37.         (void) usleep(timeout_microsecs);
  38.     }
  39.     return 0;
  40. }
  41.  
Advertisement
Add Comment
Please, Sign In to add comment