diff --git a/appview/pages/funcmap.go b/appview/pages/funcmap.go index f15de800..1dccb182 100644 --- a/appview/pages/funcmap.go +++ b/appview/pages/funcmap.go @@ -105,15 +105,9 @@ func (p *Pages) funcMap() template.FuncMap { s = append(s, values...) return s }, - "timeFmt": humanize.Time, - "longTimeFmt": func(t time.Time) string { - return t.Format("Jan 2, 2006, 3:04 PM MST") - }, - "iso8601Fmt": func(t time.Time) string { - return t.Format("2006-01-02T15:04:05-07:00") - }, - "commaFmt": humanize.Comma, - "shortTimeFmt": func(t time.Time) string { + "commaFmt": humanize.Comma, + "relTimeFmt": humanize.Time, + "shortRelTimeFmt": func(t time.Time) string { return humanize.CustomRelTime(t, time.Now(), "", "", []humanize.RelTimeMagnitude{ {time.Second, "now", time.Second}, {2 * time.Second, "1s %s", 1}, @@ -132,31 +126,24 @@ func (p *Pages) funcMap() template.FuncMap { {math.MaxInt64, "a long while %s", 1}, }) }, - "durationFmt": func(duration time.Duration) string { + "longTimeFmt": func(t time.Time) string { + return t.Format("Jan 2, 2006, 3:04 PM MST") + }, + "iso8601DateTimeFmt": func(t time.Time) string { + return t.Format("2006-01-02T15:04:05-07:00") + }, + "iso8601DurationFmt": func(duration time.Duration) string { days := int64(duration.Hours() / 24) hours := int64(math.Mod(duration.Hours(), 24)) minutes := int64(math.Mod(duration.Minutes(), 60)) seconds := int64(math.Mod(duration.Seconds(), 60)) - - chunks := []struct { - name string - amount int64 - }{ - {"d", days}, - {"hr", hours}, - {"min", minutes}, - {"s", seconds}, - } - - parts := []string{} - - for _, chunk := range chunks { - if chunk.amount != 0 { - parts = append(parts, fmt.Sprintf("%d%s", chunk.amount, chunk.name)) - } - } - - return strings.Join(parts, " ") + return fmt.Sprintf("P%dD%dH%dM%dS", days, hours, minutes, seconds) + }, + "durationFmt": func(duration time.Duration) string { + return durationFmt(duration, [4]string{"d", "hr", "min", "s"}) + }, + "longDurationFmt": func(duration time.Duration) string { + return durationFmt(duration, [4]string{"days", "hours", "minutes", "seconds"}) }, "byteFmt": humanize.Bytes, "length": func(slice any) int { @@ -291,3 +278,30 @@ func icon(name string, classes []string) (template.HTML, error) { modifiedSVG := svgStr[:svgTagEnd] + classTag + svgStr[svgTagEnd:] return template.HTML(modifiedSVG), nil } + +func durationFmt(duration time.Duration, names [4]string) string { + days := int64(duration.Hours() / 24) + hours := int64(math.Mod(duration.Hours(), 24)) + minutes := int64(math.Mod(duration.Minutes(), 60)) + seconds := int64(math.Mod(duration.Seconds(), 60)) + + chunks := []struct { + name string + amount int64 + }{ + {names[0], days}, + {names[1], hours}, + {names[2], minutes}, + {names[3], seconds}, + } + + parts := []string{} + + for _, chunk := range chunks { + if chunk.amount != 0 { + parts = append(parts, fmt.Sprintf("%d%s", chunk.amount, chunk.name)) + } + } + + return strings.Join(parts, " ") +} diff --git a/appview/pages/templates/repo/fragments/time.html b/appview/pages/templates/repo/fragments/time.html index 7934f817..1e2521a3 100644 --- a/appview/pages/templates/repo/fragments/time.html +++ b/appview/pages/templates/repo/fragments/time.html @@ -1,15 +1,19 @@ {{ define "repo/fragments/timeWrapper" }} - + {{ end }} {{ define "repo/fragments/time" }} -{{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (. | timeFmt)) }} +{{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (. | relTimeFmt)) }} {{ end }} {{ define "repo/fragments/shortTime" }} -{{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (. | shortTimeFmt)) }} +{{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (. | shortRelTimeFmt)) }} {{ end }} {{ define "repo/fragments/shortTimeAgo" }} -{{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (print (. | shortTimeFmt) " ago")) }} +{{ template "repo/fragments/timeWrapper" (dict "Time" . "Content" (print (. | shortRelTimeFmt) " ago")) }} +{{ end }} + +{{ define "repo/fragments/duration" }} + {{ end }} \ No newline at end of file diff --git a/appview/pages/templates/repo/pipelines/fragments/tooltip.html b/appview/pages/templates/repo/pipelines/fragments/tooltip.html index 24f398ff..1b2c9a94 100644 --- a/appview/pages/templates/repo/pipelines/fragments/tooltip.html +++ b/appview/pages/templates/repo/pipelines/fragments/tooltip.html @@ -10,21 +10,17 @@ {{ $lastStatus := $all.Latest }} {{ $kind := $lastStatus.Status.String }} - {{ $t := .TimeTaken }} - {{ $time := "" }} - {{ if $t }} - {{ $time = durationFmt $t }} - {{ else }} - {{ $time = printf "%s ago" (shortTimeFmt $pipeline.Created) }} - {{ end }} -
{{ template "repo/pipelines/fragments/workflowSymbol" $all }} {{ $name }}
diff --git a/appview/pages/templates/repo/pipelines/workflow.html b/appview/pages/templates/repo/pipelines/workflow.html index 94d519d8..994cf7c4 100644 --- a/appview/pages/templates/repo/pipelines/workflow.html +++ b/appview/pages/templates/repo/pipelines/workflow.html @@ -32,22 +32,17 @@ {{ $lastStatus := $all.Latest }} {{ $kind := $lastStatus.Status.String }} - {{ $t := .TimeTaken }} - {{ $time := "" }} - - {{ if $t }} - {{ $time = durationFmt $t }} - {{ else }} - {{ $time = printf "%s ago" (shortTimeFmt $lastStatus.Created) }} - {{ end }} -
{{ template "repo/pipelines/fragments/workflowSymbol" $all }} {{ $name }}
diff --git a/appview/pages/templates/repo/pulls/pull.html b/appview/pages/templates/repo/pulls/pull.html index 2d5ce34c..51906ce3 100644 --- a/appview/pages/templates/repo/pulls/pull.html +++ b/appview/pages/templates/repo/pulls/pull.html @@ -277,22 +277,17 @@ {{ $lastStatus := $all.Latest }} {{ $kind := $lastStatus.Status.String }} - {{ $t := .TimeTaken }} - {{ $time := "" }} - - {{ if $t }} - {{ $time = durationFmt $t }} - {{ else }} - {{ $time = printf "%s ago" (shortTimeFmt $lastStatus.Created) }} - {{ end }} -
{{ template "repo/pipelines/fragments/workflowSymbol" $all }} {{ $name }}