commit 83b7d018915e55e37a98cfc0d3fd8888c827d861
parent 7c46a4947b705412738793ef1cbf4e01d390f9d2
Author: Andrew Laack <andrew@laack.co>
Date: Wed, 9 Sep 2026 21:18:05 -0500
run figlet to handle minutes
Diffstat:
| A | .gitignore | | | 1 | + |
| M | README | | | 4 | ++++ |
| M | main.c | | | 81 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- |
3 files changed, 84 insertions(+), 2 deletions(-)
diff --git a/.gitignore b/.gitignore
@@ -0,0 +1 @@
+tt
diff --git a/README b/README
@@ -2,6 +2,10 @@ tt - terminal timer
===================
tt is a simple terminal timer program, written in C.
+depends
+=======
+figlet
+
usage
=====
run:
diff --git a/main.c b/main.c
@@ -1,22 +1,99 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
+#include <sys/select.h>
+#include <termios.h>
+#include <limits.h>
#define USAGE_STRING "usage: tt {num_minutes}\n"
#define INVALID_INPUT -1
+void clear_screen() {
+ printf("\033[2J\033[1;1H");
+ fflush(stdout);
+}
+
int main(int argc, char** argv) {
+
+ fd_set readfds;
+ int nfds = 1;
+ int paused = 0;
+
+ FD_ZERO(&readfds);
+ FD_SET(0, &readfds);
+
if (argc <= 1 || argc > 2) {
printf(USAGE_STRING);
return INVALID_INPUT;
}
char *end;
- long value = strtol(argv[1], &end, 10);
- if (end == argv[1] || *end != '\0' || errno == ERANGE) {
+ errno = 0;
+
+ long value = strtol(argv[1], &end, 10);
+ if (end == argv[1] || *end != '\0' || errno == ERANGE || value <= 0) {
printf("Invalid number: %s\n", argv[1]);
return INVALID_INPUT;
}
+ if (value > LONG_MAX / 60) {
+ printf("Number too large: %s\n", argv[1]);
+ return INVALID_INPUT;
+ }
+
+ value *= 60;
+
+ struct termios oldt, newt;
+ tcgetattr(0, &oldt);
+ newt = oldt;
+ newt.c_lflag &= ~(ICANON | ECHO);
+ tcsetattr(0, TCSANOW, &newt);
+
+ clear_screen();
+
+ while (value > 0) {
+ long minutes = value / 60;
+ long seconds = value % 60;
+
+ char timeStr[16];
+ snprintf(timeStr, sizeof(timeStr), "%02ld:%02ld", minutes, seconds);
+
+ char commandToRun[100];
+ snprintf(commandToRun, sizeof(commandToRun), "figlet %s", timeStr);
+ system(commandToRun);
+
+ while (1) {
+ struct timeval timeout;
+ timeout.tv_sec = paused ? 0 : 1;
+ timeout.tv_usec = 0;
+
+ FD_ZERO(&readfds);
+ FD_SET(0, &readfds);
+
+ select(nfds, &readfds, NULL, NULL, &timeout);
+
+ if (FD_ISSET(0, &readfds)) {
+ char c;
+ read(0, &c, 1);
+
+ if (c == ' ') {
+ paused = !paused;
+ }
+ }
+
+ if (!paused) {
+ value -= 1;
+ break;
+ }
+ }
+
+ if (value > 0) {
+ clear_screen();
+ }
+ }
+
+ tcsetattr(0, TCSANOW, &oldt);
+
return 0;
}