diff -ruN a/ttym.c b/ttym.c --- a/ttym.c 2026-05-20 12:02:00.123998202 +0200 +++ b/ttym.c 2026-05-20 12:24:33.731972801 +0200 @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include #include @@ -23,6 +25,7 @@ typedef struct { int quiet; /* -q: suppress completion bell */ int silent; /* -s: no bell, no extras */ + char comment[512]; /* passed as 3rd arg to hooks */ } Config; static struct termios oldtios; @@ -226,6 +229,43 @@ fflush(stdout); } +/* ===== hooks ===== */ + +static int +build_hook_path(char *buf, size_t bufsz, const char *name) +{ + const char *home = getenv("HOME"); + const char *xdg = getenv("XDG_CONFIG_HOME"); + if (xdg && *xdg) snprintf(buf, bufsz, "%s/ttym/hooks/%s", xdg, name); + else if (home && *home) snprintf(buf, bufsz, "%s/.config/ttym/hooks/%s", home, name); + else return -1; + return 0; +} + +static void +run_hook(const char *name, const char *mode, long duration_s, const char *comment) +{ + char path[512]; + if (build_hook_path(path, sizeof(path), name) < 0) return; + if (access(path, X_OK) != 0) return; + char dur[32]; + snprintf(dur, sizeof(dur), "%ld", duration_s); + pid_t pid = fork(); + if (pid == 0) { + int devnull = open("/dev/null", O_RDWR); + if (devnull >= 0) { + dup2(devnull, 0); dup2(devnull, 1); dup2(devnull, 2); + if (devnull > 2) close(devnull); + } + char *argv[] = { path, (char *)mode, dur, + (char *)(comment ? comment : ""), NULL }; + execvp(argv[0], argv); + _exit(127); + } else if (pid > 0) { + waitpid(pid, NULL, 0); + } +} + /* ===== usage ===== * Patches MUST update this in lockstep with new flags so -h always * reflects the build. @@ -241,12 +281,14 @@ "\n" "DURATION: 25m | 90s | 2h | 1h30m | 01:30:00 | 25:00\n" "FLAGS:\n" +" -c TEXT comment, passed as 3rd arg to hooks\n" " -q no sound\n" " -s silent\n" " -- end of options\n" " -h help\n" "\n" -"controls: [space] pause/resume [q | Ctrl+C] quit\n"); +"controls: [space] pause/resume [q | Ctrl+C] quit\n" +"hooks: ~/.config/ttym/hooks/{on_start,on_done,on_quit} (executable)\n"); } /* ===== run loop ===== */ @@ -280,6 +322,9 @@ if (out_tty) fputs("\033[?25l\033[?7l", stdout); + run_hook("on_start", stopwatch ? "stopwatch" : "countdown", + stopwatch ? 0 : total, cfg->comment); + long start_ms = now_ms(); long pause_accum = 0; long pause_start = 0; @@ -356,6 +401,7 @@ printf("\r\033[32mSTOP %s%*s\033[0m\n", el, 30, ""); else printf("STOP %s\n", el); + run_hook("on_quit", "stopwatch", elapsed_s, cfg->comment); } else if (user_quit) { char el[24]; fmt_clock(elapsed_s, el, sizeof(el)); @@ -363,6 +409,7 @@ printf("\r\033[33mQUIT at %s%*s\033[0m\n", el, 30, ""); else printf("QUIT at %s\n", el); + run_hook("on_quit", "countdown", elapsed_s, cfg->comment); } else { char tb[24]; fmt_clock(total, tb, sizeof(tb)); @@ -370,6 +417,7 @@ printf("\r\033[32mDONE %s 100%%%*s\033[0m\n", tb, 20, ""); else printf("DONE %s 100%%\n", tb); + run_hook("on_done", "countdown", total, cfg->comment); if (!cfg->silent && !cfg->quiet && out_tty) { fputc('\a', stdout); fflush(stdout); @@ -404,10 +452,11 @@ new_argv[new_argc] = NULL; int opt; - while ((opt = getopt(new_argc, new_argv, "qsh")) != -1) { + while ((opt = getopt(new_argc, new_argv, "qsc:h")) != -1) { switch (opt) { case 'q': cfg.quiet = 1; break; case 's': cfg.silent = 1; break; + case 'c': snprintf(cfg.comment, sizeof(cfg.comment), "%s", optarg); break; case 'h': usage(stdout); free(new_argv); return 0; default: usage(stderr); free(new_argv); return 1; }