oboeru

a collection of simple, scriptable flashcard programs
git clone anongit@rnpnr.xyz:oboeru.git
Log | Files | Refs | Feed | README | LICENSE

Commit: efd4e68e07774eaafb85ad29ac22a917f2dc2368
Parent: 88044806c8b458ad97f025a434ad9a4e2526f312
Author: Randy Palamar
Date:   Sun, 22 May 2022 08:28:37 -0600

oboeru: add a signal handler to print remaining reviews

Diffstat:
Mconfig.def.h | 5++++-
Moboeru.c | 19++++++++++++++++---
2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/config.def.h b/config.def.h @@ -17,7 +17,7 @@ /* should be < 1. this leads to exponential decay */ #define SHRINK_RATE (0.66) -/* card id formatting. needs to use ld */ +/* card id formatting. needs a %ld */ #define CARDID "%05ld" /* string used as a field delimiter */ @@ -26,3 +26,6 @@ /* format for times in the output deck file */ const char *timefmt = "%Y年%m月%d日%H時%M分"; + +/* remaining reviews string. needs a %ld */ +const char *remfmt = "残り:%ld枚\n"; diff --git a/oboeru.c b/oboeru.c @@ -1,6 +1,7 @@ /* See LICENSE for license details. */ #include <fcntl.h> #include <limits.h> +#include <signal.h> #include <stdint.h> #include <stdio.h> #include <stdlib.h> @@ -37,7 +38,7 @@ static const char *scanfmt = "%ld" DELIM "%[^"DELIM"]" DELIM "%[^"DELIM"]" DELIM static const char *logfmt = CARDID DELIM "%s" DELIM "%s" DELIM "%d" DELIM "%s\n"; static Node *head; -static size_t n_reviews; +static size_t n_reviews, n_reviewed; /* option parsing variables */ char *argv0; @@ -51,6 +52,12 @@ usage(void) } static void +sighandler(const int signo) +{ + fprintf(stderr, remfmt, n_reviews - n_reviewed); +} + +static void freenodes(Node *node) { if (node->next) @@ -231,7 +238,7 @@ review_loop(Card *r[], const char *decks[], const char *fifo) { .str = "fail", .status = CARD_FAIL } }; - for (i = 0; i < n_reviews; i++) { + for (i = 0; i < n_reviews; i++, n_reviewed++) { fprintf(stdout, "%s\t"CARDID"\n", decks[r[i]->deck], r[i]->id); /* force a flush before blocking in open() */ fflush(stdout); @@ -310,6 +317,7 @@ main(int argc, char *argv[]) Card **reviews; size_t i, n_decks = 0; const char *fifo = NULL, **decks = NULL; + struct sigaction sa; struct stat sb; ARGBEGIN { @@ -335,9 +343,14 @@ main(int argc, char *argv[]) usage(); } + memset(&sa, 0, sizeof(sa)); + sa.sa_flags = SA_RESTART; + sa.sa_handler = sighandler; + sigaction(SIGUSR1, &sa, NULL); + tail = head = xmalloc(sizeof(Node)); - /* remaining argv elements are deck jsons */ + /* remaining argv elements are deck files */ for (i = 0; argc && *argv; argv++, i++, argc--) { decks = xreallocarray(decks, ++n_decks, sizeof(char *)); decks[i] = *argv;