forked from tangled.org/core
Monorepo for Tangled — https://tangled.org

appview/pages: use duration time component

Builds on the last commit to introduce a variant of the time component but for durations. I added a verbose/long duration fmt function based on the existing short duration fmt one, and also renamed the other time fmt functions for clarity.

authored by uncenter.dev and committed by Tangled dce4b41d aea12462

Changed files
+67 -63
appview
pages
templates
repo
fragments
pipelines
pulls
+44 -30
appview/pages/funcmap.go
··· 105 105 s = append(s, values...) 106 106 return s 107 107 }, 108 - "timeFmt": humanize.Time, 109 - "longTimeFmt": func(t time.Time) string { 110 - return t.Format("Jan 2, 2006, 3:04 PM MST") 111 - }, 112 - "iso8601Fmt": func(t time.Time) string { 113 - return t.Format("2006-01-02T15:04:05-07:00") 114 - }, 115 - "commaFmt": humanize.Comma, 116 - "shortTimeFmt": func(t time.Time) string { 108 + "commaFmt": humanize.Comma, 109 + "relTimeFmt": humanize.Time, 110 + "shortRelTimeFmt": func(t time.Time) string { 117 111 return humanize.CustomRelTime(t, time.Now(), "", "", []humanize.RelTimeMagnitude{ 118 112 {time.Second, "now", time.Second}, 119 113 {2 * time.Second, "1s %s", 1}, ··· 132 126 {math.MaxInt64, "a long while %s", 1}, 133 127 }) 134 128 }, 135 - "durationFmt": func(duration time.Duration) string { 129 + "longTimeFmt": func(t time.Time) string { 130 + return t.Format("Jan 2, 2006, 3:04 PM MST") 131 + }, 132 + "iso8601DateTimeFmt": func(t time.Time) string { 133 + return t.Format("2006-01-02T15:04:05-07:00") 134 + }, 135 + "iso8601DurationFmt": func(duration time.Duration) string { 136 136 days := int64(duration.Hours() / 24) 137 137 hours := int64(math.Mod(duration.Hours(), 24)) 138 138 minutes := int64(math.Mod(duration.Minutes(), 60)) 139 139 seconds := int64(math.Mod(duration.Seconds(), 60)) 140 - 141 - chunks := []struct { 142 - name string 143 - amount int64 144 - }{ 145 - {"d", days}, 146 - {"hr", hours}, 147 - {"min", minutes}, 148 - {"s", seconds}, 149 - } 150 - 151 - parts := []string{} 152 - 153 - for _, chunk := range chunks { 154 - if chunk.amount != 0 { 155 - parts = append(parts, fmt.Sprintf("%d%s", chunk.amount, chunk.name)) 156 - } 157 - } 158 - 159 - return strings.Join(parts, " ") 140 + return fmt.Sprintf("P%dD%dH%dM%dS", days, hours, minutes, seconds) 141 + }, 142 + "durationFmt": func(duration time.Duration) string { 143 + return durationFmt(duration, [4]string{"d", "hr", "min", "s"}) 144 + }, 145 + "longDurationFmt": func(duration time.Duration) string { 146 + return durationFmt(duration, [4]string{"days", "hours", "minutes", "seconds"}) 160 147 }, 161 148 "byteFmt": humanize.Bytes, 162 149 "length": func(slice any) int { ··· 291 278 modifiedSVG := svgStr[:svgTagEnd] + classTag + svgStr[svgTagEnd:] 292 279 return template.HTML(modifiedSVG), nil 293 280 } 281 + 282 + func durationFmt(duration time.Duration, names [4]string) string { 283 + days := int64(duration.Hours() / 24) 284 + hours := int64(math.Mod(duration.Hours(), 24)) 285 + minutes := int64(math.Mod(duration.Minutes(), 60)) 286 + seconds := int64(math.Mod(duration.Seconds(), 60)) 287 + 288 + chunks := []struct { 289 + name string 290 + amount int64 291 + }{ 292 + {names[0], days}, 293 + {names[1], hours}, 294 + {names[2], minutes}, 295 + {names[3], seconds}, 296 + } 297 + 298 + parts := []string{} 299 + 300 + for _, chunk := range chunks { 301 + if chunk.amount != 0 { 302 + parts = append(parts, fmt.Sprintf("%d%s", chunk.amount, chunk.name)) 303 + } 304 + } 305 + 306 + return strings.Join(parts, " ") 307 + }
+8 -4
appview/pages/templates/repo/fragments/time.html
··· 1 1 {{ define "repo/fragments/timeWrapper" }} 2 - <time datetime="{{ .Time | iso8601Fmt }}" title="{{ .Time | longTimeFmt }}">{{ .Content }}</time> 2 + <time datetime="{{ .Time | iso8601DateTimeFmt }}" title="{{ .Time | longTimeFmt }}">{{ .Content }}</time> 3 3 {{ end }} 4 4 5 5 {{ define "repo/fragments/time" }} 6 - {{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (. | timeFmt)) }} 6 + {{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (. | relTimeFmt)) }} 7 7 {{ end }} 8 8 9 9 {{ define "repo/fragments/shortTime" }} 10 - {{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (. | shortTimeFmt)) }} 10 + {{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (. | shortRelTimeFmt)) }} 11 11 {{ end }} 12 12 13 13 {{ define "repo/fragments/shortTimeAgo" }} 14 - {{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (print (. | shortTimeFmt) " ago")) }} 14 + {{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (print (. | shortRelTimeFmt) " ago")) }} 15 + {{ end }} 16 + 17 + {{ define "repo/fragments/duration" }} 18 + <time datetime="{{ . | iso8601DurationFmt }}" title="{{ . | longDurationFmt }}">{{ . | durationFmt }}</time> 15 19 {{ end }}
+5 -9
appview/pages/templates/repo/pipelines/fragments/tooltip.html
··· 10 10 {{ $lastStatus := $all.Latest }} 11 11 {{ $kind := $lastStatus.Status.String }} 12 12 13 - {{ $t := .TimeTaken }} 14 - {{ $time := "" }} 15 - {{ if $t }} 16 - {{ $time = durationFmt $t }} 17 - {{ else }} 18 - {{ $time = printf "%s ago" (shortTimeFmt $pipeline.Created) }} 19 - {{ end }} 20 - 21 13 <div id="left" class="flex items-center gap-2 flex-shrink-0"> 22 14 {{ template "repo/pipelines/fragments/workflowSymbol" $all }} 23 15 {{ $name }} 24 16 </div> 25 17 <div id="right" class="flex items-center gap-2 flex-shrink-0"> 26 18 <span class="font-bold">{{ $kind }}</span> 27 - <time>{{ $time }}</time> 19 + {{ if .TimeTaken }} 20 + {{ template "repo/fragments/duration" .TimeTaken }} 21 + {{ else }} 22 + {{ template "repo/fragments/shortTimeAgo" $pipeline.Created }} 23 + {{ end }} 28 24 </div> 29 25 </div> 30 26 </a>
+5 -10
appview/pages/templates/repo/pipelines/workflow.html
··· 32 32 {{ $lastStatus := $all.Latest }} 33 33 {{ $kind := $lastStatus.Status.String }} 34 34 35 - {{ $t := .TimeTaken }} 36 - {{ $time := "" }} 37 - 38 - {{ if $t }} 39 - {{ $time = durationFmt $t }} 40 - {{ else }} 41 - {{ $time = printf "%s ago" (shortTimeFmt $lastStatus.Created) }} 42 - {{ end }} 43 - 44 35 <div id="left" class="flex items-center gap-2 flex-shrink-0"> 45 36 {{ template "repo/pipelines/fragments/workflowSymbol" $all }} 46 37 {{ $name }} 47 38 </div> 48 39 <div id="right" class="flex items-center gap-2 flex-shrink-0"> 49 40 <span class="font-bold">{{ $kind }}</span> 50 - <time>{{ $time }}</time> 41 + {{ if .TimeTaken }} 42 + {{ template "repo/fragments/duration" .TimeTaken }} 43 + {{ else }} 44 + {{ template "repo/fragments/shortTimeAgo" $lastStatus.Created }} 45 + {{ end }} 51 46 </div> 52 47 </div> 53 48 </a>
+5 -10
appview/pages/templates/repo/pulls/pull.html
··· 277 277 {{ $lastStatus := $all.Latest }} 278 278 {{ $kind := $lastStatus.Status.String }} 279 279 280 - {{ $t := .TimeTaken }} 281 - {{ $time := "" }} 282 - 283 - {{ if $t }} 284 - {{ $time = durationFmt $t }} 285 - {{ else }} 286 - {{ $time = printf "%s ago" (shortTimeFmt $lastStatus.Created) }} 287 - {{ end }} 288 - 289 280 <div id="left" class="flex items-center gap-2 flex-shrink-0"> 290 281 {{ template "repo/pipelines/fragments/workflowSymbol" $all }} 291 282 {{ $name }} 292 283 </div> 293 284 <div id="right" class="flex items-center gap-2 flex-shrink-0"> 294 285 <span class="font-bold">{{ $kind }}</span> 295 - <time>{{ $time }}</time> 286 + {{ if .TimeTaken }} 287 + {{ template "repo/fragments/duration" .TimeTaken }} 288 + {{ else }} 289 + {{ template "repo/fragments/shortTimeAgo" $lastStatus.Created }} 290 + {{ end }} 296 291 </div> 297 292 </div> 298 293 </a>