commit 02e3bce471d5913b9008897f92d4a7668daf853e
parent ceb82f3a743f96960fbf349512a6db8a1b72d795
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date: Tue, 19 Mar 2024 20:23:20 -0700
Put printduration() into html.c
While it's not simple enough for html.c yet, I'll try to get it
there in the future. Regardless, I need it for something else in
the next commit.
Diffstat:
M | html.c | | | 44 | ++++++++++++++++++++++++++++++++++++++++++++ |
M | pages/main.c | | | 57 | +++++++++++++++++++++------------------------------------ |
2 files changed, 65 insertions(+), 36 deletions(-)
diff --git a/html.c b/html.c
@@ -76,3 +76,47 @@ htmldate(struct pagedata *pd, time_t time)
return KCGI_SYSTEM;
return htmldatetime(pd, buf, NULL);
}
+
+/*
+ * Not a pretty function, unfortunately kcgihtml(3) isn't built
+ * using arrays instead of variable arguments so I can't think of
+ * an elegant way of provding the same ability
+ */
+enum kcgi_err
+htmlduration(struct pagedata *pd, time_t duration, char *id, int showseconds)
+{
+ enum kcgi_err status;
+ time_t hr, mn, sc;
+ char datetime[13]; /* allows up to four digits for hours */
+
+ assert(duration >= 0);
+
+ mn = duration / 60;
+ sc = duration % 60;
+ hr = mn / 60;
+ mn = mn % 60;
+
+ if (snprintf(datetime, sizeof(datetime), "%lldh%lldm%llds",
+ hr, mn, sc) >= (int)sizeof(datetime)) {
+ err(1, "snprintf (%lldh %lldm %llds)", hr, mn, sc);
+ }
+
+ if (id)
+ status = khtml_attr(&pd->html, KELEM_TIME,
+ KATTR_ID, id, KATTR_DATETIME, datetime, KATTR__MAX);
+ else
+ status = khtml_attr(&pd->html, KELEM_TIME,
+ KATTR_DATETIME, datetime, KATTR__MAX);
+
+ if (status != KCGI_OK)
+ return status;
+
+ if (showseconds)
+ status = khtml_printf(&pd->html, "%.2lld:%.2lld:%.2lld", hr, mn, sc);
+ else
+ status = khtml_printf(&pd->html, "%.2lld:%.2lld", hr, mn);
+
+ if (status != KCGI_OK || (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
+ return status;
+ return KCGI_OK;
+}
diff --git a/pages/main.c b/pages/main.c
@@ -25,40 +25,6 @@
static char *datetime_fmt = "%FT%T+0000";
enum kcgi_err
-printduration(struct pagedata *pd, time_t duration, char *id)
-{
- enum kcgi_err status;
- time_t hr, mn, sc;
- char datetime[13]; /* allows up to four digits for hours */
-
- assert(duration >= 0);
-
- mn = duration / 60;
- sc = duration % 60;
- hr = mn / 60;
- mn = mn % 60;
-
- if (snprintf(datetime, sizeof(datetime), "%lldh%lldm%llds",
- hr, mn, sc) >= (int)sizeof(datetime)) {
- err(1, "snprintf (%lldh %lldm %llds)", hr, mn, sc);
- }
-
- if (id)
- status = khtml_attr(&pd->html, KELEM_TIME,
- KATTR_ID, id, KATTR_DATETIME, datetime, KATTR__MAX);
- else
- status = khtml_attr(&pd->html, KELEM_TIME,
- KATTR_DATETIME, datetime, KATTR__MAX);
-
- if (status != KCGI_OK || (status =
- khtml_printf(&pd->html, "%.2lld:%.2lld:%.2lld", hr, mn, sc)) != KCGI_OK ||
- (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
- return status;
-
- return KCGI_OK;
-}
-
-enum kcgi_err
printhours(struct pagedata *pd, struct timesheet *ts)
{
int final;
@@ -69,7 +35,7 @@ printhours(struct pagedata *pd, struct timesheet *ts)
final = (ts->set & EndTimeFlag ||
(ts->set & BreakStartTimeFlag && !(ts->set & BreakEndTimeFlag)));
- status = printduration(pd, getduration(ts), final ? NULL : "counter");
+ status = htmlduration(pd, getduration(ts), final ? NULL : "counter", 1);
if (status != KCGI_OK)
return status;
@@ -106,7 +72,7 @@ printtotal(struct pagedata *pd, struct timesheet *ts)
for (i = StartTime; i <= EndTime + 1; ++i)
if ((status = khtml_elem(&pd->html, KELEM_TD)) != KCGI_OK ||
- (i > EndTime && (status = printduration(pd, t, "total")) != KCGI_OK) ||
+ (i > EndTime && (status = htmlduration(pd, t, "total", 1)) != KCGI_OK) ||
(status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
return status;
return KCGI_OK;
@@ -297,6 +263,25 @@ gettf(char *tfs)
}
enum kcgi_err
+printheader(struct pagedata *pd, char **pages)
+{
+ enum kcgi_err status;
+
+ if ((status = khtml_elem(&pd->html, KELEM_HEADER)) != KCGI_OK ||
+ (status = khtml_elem(&pd->html, KELEM_UL)) != KCGI_OK)
+ return status;
+
+ do {
+ if ((status = khtml_elem(&pd->html, KELEM_LI)) != KCGI_OK ||
+ (status = htmllink(pd, *pages, *pages)) != KCGI_OK ||
+ (status = khtml_closeelem(&pd->html, 1)) != KCGI_OK)
+ return status;
+ } while ((++pages)[0]);
+
+ return khtml_closeelem(&pd->html, 2);
+}
+
+enum kcgi_err
exportform(struct pagedata *pd)
{
enum kcgi_err status;