From 5f045431cfaa7fdfec8665c91a5a494998c660a9 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Fri, 21 May 2021 08:14:02 +0200 Subject: [PATCH] fix(executiontime): round values logically --- docs/docs/segment-executiontime.md | 2 +- src/segment_executiontime.go | 17 +++++++++++++---- src/segment_executiontime_test.go | 25 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/docs/docs/segment-executiontime.md b/docs/docs/segment-executiontime.md index a00adb3a..f17bccfb 100644 --- a/docs/docs/segment-executiontime.md +++ b/docs/docs/segment-executiontime.md @@ -47,6 +47,6 @@ Style specifies the format in which the time will be displayed. The table below | galveston | `00:00:00` | `00:00:02` | `00:03:02` | `04:03:02` | | houston | `00:00:00.001` | `00:00:02.1` | `00:03:02.1` | `04:03:02.1` | | amarillo | `0.001s` | `2.1s` | `182.1s` | `14,582.1s` | -| round | `1ms` | `2s` | `3m` | `4h` | +| round | `1ms` | `2s` | `3m 2s` | `4h 3m` | [install]: /docs/installation diff --git a/src/segment_executiontime.go b/src/segment_executiontime.go index c3be37fe..6a32dbaa 100644 --- a/src/segment_executiontime.go +++ b/src/segment_executiontime.go @@ -181,17 +181,26 @@ func (t *executiontime) formatDurationAmarillo(ms int64) string { } func (t *executiontime) formatDurationRound(ms int64) string { + toRoundString := func(one, two int64, oneText, twoText string) string { + if two == 0 { + return fmt.Sprintf("%d%s", one, oneText) + } + return fmt.Sprintf("%d%s %d%s", one, oneText, two, twoText) + } + hours := ms / hour % hoursPerDay if ms >= day { - return fmt.Sprintf("%dd", ms/day) + return toRoundString(ms/day, hours, "d", "h") } + minutes := ms / minute % secondsPerMinute if ms >= hour { - return fmt.Sprintf("%dh", ms/hour%hoursPerDay) + return toRoundString(hours, minutes, "h", "m") } + seconds := (ms % minute) / second if ms >= minute { - return fmt.Sprintf("%dm", ms/minute%secondsPerMinute) + return toRoundString(minutes, seconds, "m", "s") } if ms >= second { - return fmt.Sprintf("%ds", (ms%minute)/second) + return fmt.Sprintf("%ds", seconds) } return fmt.Sprintf("%dms", ms%second) } diff --git a/src/segment_executiontime_test.go b/src/segment_executiontime_test.go index 4aace155..ab069390 100644 --- a/src/segment_executiontime_test.go +++ b/src/segment_executiontime_test.go @@ -228,3 +228,28 @@ func TestExecutionTimeFormatAmarillo(t *testing.T) { assert.Equal(t, tc.Expected, output) } } + +func TestExecutionTimeFormatDurationRound(t *testing.T) { + cases := []struct { + Input string + Expected string + }{ + {Input: "0.001s", Expected: "1ms"}, + {Input: "0.1s", Expected: "100ms"}, + {Input: "1s", Expected: "1s"}, + {Input: "2.1s", Expected: "2s"}, + {Input: "1m", Expected: "1m"}, + {Input: "3m2.1s", Expected: "3m 2s"}, + {Input: "1h", Expected: "1h"}, + {Input: "4h3m2.1s", Expected: "4h 3m"}, + {Input: "124h3m2.1s", Expected: "5d 4h"}, + {Input: "124h3m2.0s", Expected: "5d 4h"}, + } + + for _, tc := range cases { + duration, _ := time.ParseDuration(tc.Input) + executionTime := &executiontime{} + output := executionTime.formatDurationRound(duration.Milliseconds()) + assert.Equal(t, tc.Expected, output) + } +}