From nobody Fri May 10 15:25:26 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VbXkv10pgz5JfPr; Fri, 10 May 2024 15:25:27 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VbXkt5rw2z4pvy; Fri, 10 May 2024 15:25:26 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1715354726; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=cAS8kbTDNMIfUwp5frt0HljXRXX4cr6NzlXIZbjaEBg=; b=n7E+Ae//SImXlgSyf/hVEBkhTHroj4U59A2otohiX2od2XRLFe40nbQhMSkBLkEwlBwlDx 8xVv9Wn3UIWXEUTPzzu4LXRzPUAF7q1KP091Gwv/SJRZBL+oYaQ8DxZG2yGc3RD7cIgKtS kV/axSRCAv4fjlc2/Tl039xhLSHjhcN8aIm2dl7bDIK7Pc458FPhRCK2kOaSjPzY0nyP38 9MOMw5SDV/eKcKVF7wlK1egXICGwxAu2gfNklsfYH/Ez57JeABEl4npAfJ4goaDLQ4px9K u7tUXORNPz4t8tkcEeoGWMB2PyDPDznHFgS+YmmelF5rdf6ec2Th0TPCSp+tUA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1715354726; a=rsa-sha256; cv=none; b=oWezpaZ22e6I8402sM2KXaeqirh/1QAeEHy9ixhQvJfXhBTiUET1UZIcMJZqpSMnXZWYqp XYSJPr73K2cfbhk29jiTVgSbJ8V6KCkWh3o3jjJXgzncYUExISZOGOLw3/Zrvz+KtKX4op Fre3yI8Nk/XVhVFPWdbx0fBQtgyKqoEuSR5+6MvOPVe9W6NcDbOCumaH5TKEYWLBN6/XUw Ak6SDdaZZMPOzfRQ3zKjkRv9o3xK4hAFDcC0HUGkz6jCcfxiCgMLUe3tfeyyIGwEhj9/My eV+QMgLHHyVxlylHRybsL5j+bICnmgTCmN9G4VGctbRkzKDpaFxte84HyO6xIQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1715354726; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=cAS8kbTDNMIfUwp5frt0HljXRXX4cr6NzlXIZbjaEBg=; b=iMDl+xh6ElenNsHNWcTwMaofwS6buPgWpFlanCyIKXgPlEhDkmoY+rDPjIbGy284933CEi VoB7mYmNId1rt3OafYaelcuBzp1MWcVJbCP9dLQQvDm9McLK+bKhOgQZd6W7qMRCo9tJZV JY5A4CELSh/dzaDG6DuEl6UGybra7xv/G85HVkBC99OwOZoaCnqogjFIzO+Pa1aatf440t YShz+HGj3E49vQDg0lNSHhTWRIFbcrVqfyTCmxprR6JWK70iBJ4lYRJkaGhCziOIyfYORY 0wlsEWdL3Zn6O2YMaellGuc6EdWOZRmq9Zs02cADvsEIkb/9CKPkAyBDuzKXlQ== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VbXkt5SDKzjGW; Fri, 10 May 2024 15:25:26 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 44AFPQkX087839; Fri, 10 May 2024 15:25:26 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 44AFPQTF087836; Fri, 10 May 2024 15:25:26 GMT (envelope-from git) Date: Fri, 10 May 2024 15:25:26 GMT Message-Id: <202405101525.44AFPQTF087836@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: 71ed1adda83d - main - grdc: add countdown timer mode List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 71ed1adda83dfaa5d1a2482341b47bd7919498e3 Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=71ed1adda83dfaa5d1a2482341b47bd7919498e3 commit 71ed1adda83dfaa5d1a2482341b47bd7919498e3 Author: Gavin Atkinson AuthorDate: 2024-05-10 15:22:38 +0000 Commit: Warner Losh CommitDate: 2024-05-10 15:22:38 +0000 grdc: add countdown timer mode PR: 35113 Differential Revision: https://reviews.freebsd.org/D43463 --- usr.bin/grdc/grdc.6 | 19 +++++++++++- usr.bin/grdc/grdc.c | 83 ++++++++++++++++++++++++++++++++++------------------- 2 files changed, 71 insertions(+), 31 deletions(-) diff --git a/usr.bin/grdc/grdc.6 b/usr.bin/grdc/grdc.6 index 3c9660055e2b..7420c8ecbe8b 100644 --- a/usr.bin/grdc/grdc.6 +++ b/usr.bin/grdc/grdc.6 @@ -1,4 +1,4 @@ -.Dd September 25, 2001 +.Dd January 15, 2023 .Dt GRDC 6 .Os .Sh NAME @@ -8,6 +8,9 @@ .Nm .Op Fl st .Op Ar n +.Nm +.Fl c +.Ar n .Sh DESCRIPTION .Nm runs a digital clock made of reverse-video blanks on a curses @@ -17,6 +20,11 @@ With an optional numeric argument it stops after .Ar n seconds (default never). +The clock can act as a countdown timer with the +.Fl c +flag, +.Ar n +specifies the number of seconds to time for. The optional .Fl s flag makes digits scroll as they change. @@ -37,8 +45,17 @@ for more information. If this variable is not set, the time zone is determined based on .Pa /etc/localtime . .El +.Sh NOTES +In countdown timer mode, the specifying of +.Fl n +> 360000 seconds (100 hours) will lead to the counter displaying +incorrect remaining time, however it will time correctly, and +display correctly when the remaining time becomes less than +100 hours. .Sh AUTHORS .An -nosplit .An Amos Shapir , modified for curses by .An John Lupien . +Countdown timer mode by +.An Gavin Atkinson . diff --git a/usr.bin/grdc/grdc.c b/usr.bin/grdc/grdc.c index e2a694ebc461..07d12f5a7fb0 100644 --- a/usr.bin/grdc/grdc.c +++ b/usr.bin/grdc/grdc.c @@ -1,12 +1,15 @@ /* * Grand digital clock for curses compatible terminals * Usage: grdc [-st] [n] -- run for n seconds (default infinity) - * Flags: -s: scroll + * grdc -c n -- countdown n seconds + * Flags: -c: Countdown timer mode + * -s: scroll * -t: output time in 12-hour format * * * modified 10-18-89 for curses (jrl) * 10-18-89 added signal handling + * 02-18-02 added countdown timer mode * * modified 03-25-03 for 12 hour option * - Samy Al Bahra @@ -26,6 +29,7 @@ static struct timespec now; static struct tm *tm; +static struct timespec end; static short disp[11] = { 075557, 011111, 071747, 071717, 055711, @@ -59,18 +63,19 @@ main(int argc, char *argv[]) int i, j, s, k; int n; int ch; - int scrol; - int t12; + bool scrol = false, t12 = false, timer = false; + int hour, minute, second; - t12 = scrol = 0; - - while ((ch = getopt(argc, argv, "ts")) != -1) + while ((ch = getopt(argc, argv, "cst")) != -1) switch (ch) { + case 'c': + timer = true; + break; case 's': - scrol = 1; + scrol = true; break; case 't': - t12 = 1; + t12 = true; break; case '?': default: @@ -80,7 +85,7 @@ main(int argc, char *argv[]) argc -= optind; argv += optind; - if (argc > 1) { + if ((argc > 1) || (argc == 0 && timer)) { usage(); /* NOTREACHED */ } @@ -95,6 +100,9 @@ main(int argc, char *argv[]) } else n = 0; + if (timer && n == 0) + return(0); + initscr(); signal(SIGINT,sighndl); @@ -107,7 +115,7 @@ main(int argc, char *argv[]) hascolor = has_colors(); - if(hascolor) { + if (hascolor) { start_color(); init_pair(1, COLOR_BLACK, COLOR_RED); init_pair(2, COLOR_RED, COLOR_BLACK); @@ -118,7 +126,7 @@ main(int argc, char *argv[]) clear(); refresh(); - if(hascolor) { + if (hascolor) { attrset(COLOR_PAIR(3)); mvaddch(YBASE - 2, XBASE - 3, ACS_ULCORNER); @@ -139,28 +147,42 @@ main(int argc, char *argv[]) } clock_gettime(CLOCK_REALTIME_FAST, &now); prev_sec = now.tv_sec; + if (timer) { + end = now; + end.tv_sec += n; + } do { mask = 0; - tm = localtime(&now.tv_sec); - set(tm->tm_sec%10, 0); - set(tm->tm_sec/10, 4); - set(tm->tm_min%10, 10); - set(tm->tm_min/10, 14); - - if (t12) { - if (tm->tm_hour < 12) { - if (tm->tm_hour == 0) - tm->tm_hour = 12; - mvaddstr(YBASE + 5, XBASE + 52, "AM"); - } else { - if (tm->tm_hour > 12) - tm->tm_hour -= 12; - mvaddstr(YBASE + 5, XBASE + 52, "PM"); + if (!timer) { + tm = localtime(&now.tv_sec); + if (t12) { + if (tm->tm_hour < 12) { + if (tm->tm_hour == 0) + tm->tm_hour = 12; + mvaddstr(YBASE + 5, XBASE + 52, "AM"); + } else { + if (tm->tm_hour > 12) + tm->tm_hour -= 12; + mvaddstr(YBASE + 5, XBASE + 52, "PM"); + } } + hour = tm->tm_hour; + minute = tm->tm_min; + second = tm->tm_sec; + } else { + n = end.tv_sec - now.tv_sec; + if (n <= 0) + break; + hour = (n / 3600) % 100; + minute = (n / 60) % 60; + second = n % 60; } - - set(tm->tm_hour%10, 20); - set(tm->tm_hour/10, 24); + set(second % 10, 0); + set(second / 10, 4); + set(minute % 10, 10); + set(minute / 10, 14); + set(hour % 10, 20); + set(hour / 10, 24); set(10, 7); set(10, 17); for(k=0; k<6; k++) { @@ -266,6 +288,7 @@ static void usage(void) { - (void)fprintf(stderr, "usage: grdc [-st] [n]\n"); + (void)fprintf(stderr, "usage: grdc [-st] [n]\n" + " grdc -c n\n"); exit(1); }