From 1ee91c1abe965f2a0b65e777481006baf23cf062 Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Wed, 5 Feb 2025 12:16:42 -0800 Subject: [PATCH] Add tt{l,d,s} notice to web UI --- web/html/html.go | 31 +++++++++++++++++++++++++++++-- web/html/item.html | 7 +++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/web/html/html.go b/web/html/html.go index a39835b..a3088af 100644 --- a/web/html/html.go +++ b/web/html/html.go @@ -3,6 +3,7 @@ package html import ( "embed" "encoding/json" + "fmt" "html/template" "io" "log" @@ -15,11 +16,35 @@ func rawHtml(str string) template.HTML { return template.HTML(str) } -func tsToDate(t int) string { - tm := time.Unix(int64(t), 0).UTC() +func dateFormat(tm time.Time) string { return tm.Format(time.DateTime) } +func tsToDate(t int) time.Time { + return time.Unix(int64(t), 0).UTC() +} + +func until(tm time.Time) string { + dur := time.Until(tm).Round(time.Second) + + var format string + if dur < 0 { + format = "%s ago" + dur = dur.Abs() + } else { + format = "in %s" + } + + var length string + if dur.Hours() > 24 { + length = fmt.Sprintf("%dd", int(dur.Hours()/24)) + } else { + length = dur.String() + } + + return fmt.Sprintf(format, length) +} + func massDeactivateVals(items []core.Item) string { var shorts []string for _, item := range items { @@ -37,7 +62,9 @@ func massDeactivateVals(items []core.Item) string { var funcs = template.FuncMap{ "raw": rawHtml, + "dateFormat": dateFormat, "tsToDate": tsToDate, + "until": until, "massDeacVars": massDeactivateVals, } diff --git a/web/html/item.html b/web/html/item.html index 3d28238..7a7301e 100644 --- a/web/html/item.html +++ b/web/html/item.html @@ -56,14 +56,17 @@ {{ if or .Author .Time }} {{ .Author }} -{{ .Time | tsToDate }} +{{ .Time | tsToDate | dateFormat }}
{{ end -}} {{- /* source/id/created footer line */ -}} {{ .Source }}/{{ .Id }} -{{ .Created | tsToDate }} +{{ .Created | tsToDate | dateFormat }} +{{ if .Ttl }}[L]{{ end }} +{{ if .Ttd }}[D]{{ end }} +{{ if .Tts }}[S]{{ end }} {{ end -}}