ref: e5051cf2345b46f0c3f30572214ee8d2d6e8e99a
parent: 662804331795230f76a836ce2db230317149d709
author: sirjofri <sirjofri@sirjofri.de>
date: Thu Jan 29 04:30:32 EST 2026
new watch, allows supplying report .hours files
--- a/report
+++ b/report
@@ -8,6 +8,9 @@
echo '.SH
Report
.R'
-
-hours/hours.awk <$clockfile
+if (~ $#* 0)
+ hours/hours.awk <$clockfile
+if not
+ for (f in $*)
+ hours/hours.awk <$f
} | tbl | troff -ms
--- a/watch/mkfile
+++ /dev/null
@@ -1,11 +1,0 @@
-</$objtype/mkfile
-
-BIN=$home/bin/$objtype/hours
-TARG=watch
-OFILES=watch.$O
-
-</sys/src/cmd/mkone
-
-$BIN/$TARG: $O.out
- mkdir -p `{basename -d $target}- cp $prereq $BIN/$TARG
--- a/watch/watch.c
+++ /dev/null
@@ -1,270 +1,0 @@
-#include <u.h>
-#include <libc.h>
-#include <bio.h>
-#include <draw.h>
-#include <event.h>
-#include <keyboard.h>
-
-void
-usage(void)
-{- fprint(2, "usage: %s\n", argv0);
- exits("usage");-}
-
-char *libhours = nil;
-char *watchfile = nil;
-
-char *message = nil;
-char *runtime = nil;
-
-int lineheight = 0;
-
-Image *playimg = nil;
-Image *stopimg = nil;
-
-Rectangle buttonbox;
-int timerrunning = 0;
-
-Point playps[] = {- { 20, 20 },- { 20, 80 },- { 80, 50 },-};
-
-Point stopps[] = {- { 20, 20 },- { 20, 80 },- { 80, 80 },- { 80, 20 },-};
-
-void
-initimages(void)
-{- playimg = allocimage(display, Rect(0, 0, 100, 100), screen->chan, 0, DWhite);
- stopimg = allocimage(display, Rect(0, 0, 100, 100), screen->chan, 0, DWhite);
-
- border(playimg, playimg->r, 1, display->black, ZP);
- border(stopimg, stopimg->r, 1, display->black, ZP);
-
- fillpoly(playimg, playps, 3, 0, display->black, ZP);
- fillpoly(stopimg, stopps, 4, 0, display->black, ZP);
-}
-
-void
-setmessage(char *s)
-{- if (message)
- free(message);
- message = s;
-}
-
-void
-setruntime(long tm)
-{- int hours, minutes, seconds;
-
- if (runtime)
- free(runtime);
-
- hours = (int)(tm/3600);
- minutes = (int)((tm/60)%60);
- seconds = (int)(tm % 60);
-
- runtime = smprint("%dh %dm %ds", hours, minutes, seconds);-}
-
-void
-redraw(void)
-{- Point p;
- int x;
- Image *todraw;
- draw(screen, screen->r, display->white, nil, ZP);
-
- x = screen->r.min.x + 5;
- p.y = screen->r.min.y + 5;
- p.x = x;
-
- p = string(screen, p, display->black, ZP, font, runtime);
- p.x = x;
- p.y += lineheight;
- p = string(screen, p, display->black, ZP, font, message);
-
- todraw = timerrunning ? stopimg : playimg;
-
- p.x = x;
- p.y += lineheight;
- buttonbox = rectaddpt(todraw->r, p);
- draw(screen, buttonbox, todraw, nil, ZP);
-}
-
-long
-getstarttime(char **msg)
-{- Biobuf *bin;
- char *s;
- long t;
-
- bin = Bopen(watchfile, OREAD);
- if (!bin)
- return -1;
-
- s = Brdstr(bin, '\n', 1);
- Bterm(bin);
-
- *msg = strchr(s, '\t');
- (*msg)++;
- *msg = strdup(*msg);
- t = atol(s);
- free(s);
- return t;
-}
-
-void
-update(void)
-{- char *msg;
- long current, start;
-
- msg = nil;
- start = getstarttime(&msg);
-
- if (start < 0) {- timerrunning = 0;
- setmessage(strdup("no timer"));- setruntime(0);
- redraw();
- return;
- }
-
- current = time(nil);
- current -= start;
-
- timerrunning = 1;
- setmessage(msg);
- setruntime(current);
-
- redraw();
-}
-
-void
-stoptimer(void)
-{- int fd;
- char *msg;
- long start, current;
-
- fd = open(".hours", OWRITE);- if (fd < 0)
- fd = open(libhours, OWRITE);
-
- if (fd < 0)
- fd = create(libhours, OWRITE, 0666);
-
- if (fd < 0)
- sysfatal("error accessing hours file: %r");-
- msg = nil;
- start = getstarttime(&msg);
- if (start < 0)
- return;
-
- current = time(nil);
-
- seek(fd, 0, 2);
- fprint(fd, "%ld\t%ld\t%s\n", start, current, msg);
- close(fd);
-
- remove(watchfile);
-}
-
-void
-starttimer(Mouse m)
-{- int fd;
- long t;
- char buf[1024];
-
- if (!access(watchfile, AEXIST))
- return;
-
- fd = create(watchfile, OWRITE, 0666);
- if (fd < 0)
- sysfatal("create: %r");-
- buf[0] = 0;
- if (eenter("desc", buf, sizeof buf, &m) < 0)- return;
-
- t = time(nil);
-
- fprint(fd, "%ld\t%s\n", t, buf);
- close(fd);
-}
-
-void
-mouseinput(Mouse m)
-{- if (m.buttons != 1)
- return;
-
- if (ptinrect(m.xy, buttonbox)) {- if (timerrunning)
- stoptimer();
- else
- starttimer(m);
- update();
- return;
- }
-}
-
-void
-eresized(int new)
-{- if (new && getwindow(display, Refnone) < 0)
- sysfatal("%r");-
- redraw();
-}
-
-void
-main(int argc, char **argv)
-{- int e, timer;
- Event ev;
-
- ARGBEGIN{- case 'h':
- usage();
- }ARGEND;
-
- if (initdraw(nil, nil, "hours/watch") < 0)
- sysfatal("initdraw: %r");- initimages();
-
- einit(Emouse|Ekeyboard);
- timer = etimer(0, 1000);
-
- watchfile = smprint("/usr/%s/lib/clocked_hour", getenv("user"));- libhours = smprint("/usr/%s/lib/hours", getenv("user"));- assert(watchfile && libhours);
-
- lineheight = stringsize(font, "M").y;
-
- for (;;) {- e = event(&ev);
- switch (e) {- case Emouse:
- mouseinput(ev.mouse);
- break;
- case Ekeyboard:
- if (ev.kbdc == 'q' || ev.kbdc == Kdel)
- exits(nil);
- break;
- }
- if (e == timer)
- update();
- }
-}
--
⑨