diff --git a/docs/docs/segment-executiontime.md b/docs/docs/segment-executiontime.md index 09f11ac7..a00adb3a 100644 --- a/docs/docs/segment-executiontime.md +++ b/docs/docs/segment-executiontime.md @@ -40,12 +40,13 @@ The [installation guide][install] shows how to include this argument for PowerSh Style specifies the format in which the time will be displayed. The table below shows some example times in each option. | format | 0.001s | 2.1s | 3m2.1s | 4h3m2.1s | -|-----------|----------------|--------------|---------------|------------------| +| --------- | -------------- | ------------ | ------------- | ---------------- | | austin | `1ms` | `2.1s` | `3m 2.1s` | `4h 3m 2.1s` | | roundrock | `1ms` | `2s 100ms` | `3m 2s 100ms` | `4h 3m 2s 100ms` | | dallas | `0.001` | `2.1` | `3:2.1` | `4:3:2.1` | | 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` | [install]: /docs/installation diff --git a/src/segment_executiontime.go b/src/segment_executiontime.go index 8caba6d8..c3be37fe 100644 --- a/src/segment_executiontime.go +++ b/src/segment_executiontime.go @@ -32,6 +32,8 @@ const ( Houston DurationStyle = "houston" // Amarillo seconds Amarillo DurationStyle = "amarillo" + // Round will round the output of the format + Round DurationStyle = "round" second = 1000 minute = 60000 @@ -78,6 +80,8 @@ func (t *executiontime) formatDuration(ms int64, style DurationStyle) string { return t.formatDurationHouston(ms) case Amarillo: return t.formatDurationAmarillo(ms) + case Round: + return t.formatDurationRound(ms) default: return fmt.Sprintf("Style: %s is not available", style) } @@ -175,3 +179,19 @@ func (t *executiontime) formatDurationAmarillo(ms int64) string { return result } + +func (t *executiontime) formatDurationRound(ms int64) string { + if ms >= day { + return fmt.Sprintf("%dd", ms/day) + } + if ms >= hour { + return fmt.Sprintf("%dh", ms/hour%hoursPerDay) + } + if ms >= minute { + return fmt.Sprintf("%dm", ms/minute%secondsPerMinute) + } + if ms >= second { + return fmt.Sprintf("%ds", (ms%minute)/second) + } + return fmt.Sprintf("%dms", ms%second) +}