mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-10 13:04:04 -08:00
feat(executiontime): add template properties
This commit is contained in:
parent
2040771190
commit
f5aeed466e
|
@ -48,3 +48,8 @@ Style specifies the format in which the time will be displayed. The table below
|
|||
| 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 2s` | `4h 3m` |
|
||||
|
||||
## Template Properties
|
||||
|
||||
- `.Ms`: `number` - the execution time in milliseconds
|
||||
- `.FormattedMs`: `string` - the formatted value based on the `style` above.
|
||||
|
|
|
@ -9,9 +9,11 @@ import (
|
|||
)
|
||||
|
||||
type executiontime struct {
|
||||
props *properties
|
||||
env environmentInfo
|
||||
output string
|
||||
props *properties
|
||||
env environmentInfo
|
||||
|
||||
FormattedMs string
|
||||
Ms int64
|
||||
}
|
||||
|
||||
// DurationStyle how to display the time
|
||||
|
@ -52,13 +54,13 @@ func (t *executiontime) enabled() bool {
|
|||
return false
|
||||
}
|
||||
style := DurationStyle(t.props.getString(Style, string(Austin)))
|
||||
t.output = t.formatDuration(int64(executionTimeMs), style)
|
||||
|
||||
return t.output != ""
|
||||
t.Ms = int64(executionTimeMs)
|
||||
t.FormattedMs = t.formatDuration(style)
|
||||
return t.FormattedMs != ""
|
||||
}
|
||||
|
||||
func (t *executiontime) string() string {
|
||||
return t.output
|
||||
return t.FormattedMs
|
||||
}
|
||||
|
||||
func (t *executiontime) init(props *properties, env environmentInfo) {
|
||||
|
@ -66,103 +68,103 @@ func (t *executiontime) init(props *properties, env environmentInfo) {
|
|||
t.env = env
|
||||
}
|
||||
|
||||
func (t *executiontime) formatDuration(ms int64, style DurationStyle) string {
|
||||
func (t *executiontime) formatDuration(style DurationStyle) string {
|
||||
switch style {
|
||||
case Austin:
|
||||
return t.formatDurationAustin(ms)
|
||||
return t.formatDurationAustin()
|
||||
case Roundrock:
|
||||
return t.formatDurationRoundrock(ms)
|
||||
return t.formatDurationRoundrock()
|
||||
case Dallas:
|
||||
return t.formatDurationDallas(ms)
|
||||
return t.formatDurationDallas()
|
||||
case Galveston:
|
||||
return t.formatDurationGalveston(ms)
|
||||
return t.formatDurationGalveston()
|
||||
case Houston:
|
||||
return t.formatDurationHouston(ms)
|
||||
return t.formatDurationHouston()
|
||||
case Amarillo:
|
||||
return t.formatDurationAmarillo(ms)
|
||||
return t.formatDurationAmarillo()
|
||||
case Round:
|
||||
return t.formatDurationRound(ms)
|
||||
return t.formatDurationRound()
|
||||
default:
|
||||
return fmt.Sprintf("Style: %s is not available", style)
|
||||
}
|
||||
}
|
||||
|
||||
func (t *executiontime) formatDurationAustin(ms int64) string {
|
||||
if ms < second {
|
||||
return fmt.Sprintf("%dms", ms%second)
|
||||
func (t *executiontime) formatDurationAustin() string {
|
||||
if t.Ms < second {
|
||||
return fmt.Sprintf("%dms", t.Ms%second)
|
||||
}
|
||||
|
||||
seconds := float64(ms%minute) / second
|
||||
seconds := float64(t.Ms%minute) / second
|
||||
result := strconv.FormatFloat(seconds, 'f', -1, 64) + "s"
|
||||
|
||||
if ms >= minute {
|
||||
result = fmt.Sprintf("%dm %s", ms/minute%secondsPerMinute, result)
|
||||
if t.Ms >= minute {
|
||||
result = fmt.Sprintf("%dm %s", t.Ms/minute%secondsPerMinute, result)
|
||||
}
|
||||
if ms >= hour {
|
||||
result = fmt.Sprintf("%dh %s", ms/hour%hoursPerDay, result)
|
||||
if t.Ms >= hour {
|
||||
result = fmt.Sprintf("%dh %s", t.Ms/hour%hoursPerDay, result)
|
||||
}
|
||||
if ms >= day {
|
||||
result = fmt.Sprintf("%dd %s", ms/day, result)
|
||||
if t.Ms >= day {
|
||||
result = fmt.Sprintf("%dd %s", t.Ms/day, result)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (t *executiontime) formatDurationRoundrock(ms int64) string {
|
||||
result := fmt.Sprintf("%dms", ms%second)
|
||||
if ms >= second {
|
||||
result = fmt.Sprintf("%ds %s", ms/second%secondsPerMinute, result)
|
||||
func (t *executiontime) formatDurationRoundrock() string {
|
||||
result := fmt.Sprintf("%dms", t.Ms%second)
|
||||
if t.Ms >= second {
|
||||
result = fmt.Sprintf("%ds %s", t.Ms/second%secondsPerMinute, result)
|
||||
}
|
||||
if ms >= minute {
|
||||
result = fmt.Sprintf("%dm %s", ms/minute%minutesPerHour, result)
|
||||
if t.Ms >= minute {
|
||||
result = fmt.Sprintf("%dm %s", t.Ms/minute%minutesPerHour, result)
|
||||
}
|
||||
if ms >= hour {
|
||||
result = fmt.Sprintf("%dh %s", ms/hour%hoursPerDay, result)
|
||||
if t.Ms >= hour {
|
||||
result = fmt.Sprintf("%dh %s", t.Ms/hour%hoursPerDay, result)
|
||||
}
|
||||
if ms >= day {
|
||||
result = fmt.Sprintf("%dd %s", ms/day, result)
|
||||
if t.Ms >= day {
|
||||
result = fmt.Sprintf("%dd %s", t.Ms/day, result)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (t *executiontime) formatDurationDallas(ms int64) string {
|
||||
seconds := float64(ms%minute) / second
|
||||
func (t *executiontime) formatDurationDallas() string {
|
||||
seconds := float64(t.Ms%minute) / second
|
||||
result := strconv.FormatFloat(seconds, 'f', -1, 64)
|
||||
|
||||
if ms >= minute {
|
||||
result = fmt.Sprintf("%d:%s", ms/minute%minutesPerHour, result)
|
||||
if t.Ms >= minute {
|
||||
result = fmt.Sprintf("%d:%s", t.Ms/minute%minutesPerHour, result)
|
||||
}
|
||||
if ms >= hour {
|
||||
result = fmt.Sprintf("%d:%s", ms/hour%hoursPerDay, result)
|
||||
if t.Ms >= hour {
|
||||
result = fmt.Sprintf("%d:%s", t.Ms/hour%hoursPerDay, result)
|
||||
}
|
||||
if ms >= day {
|
||||
result = fmt.Sprintf("%d:%s", ms/day, result)
|
||||
if t.Ms >= day {
|
||||
result = fmt.Sprintf("%d:%s", t.Ms/day, result)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func (t *executiontime) formatDurationGalveston(ms int64) string {
|
||||
result := fmt.Sprintf("%02d:%02d:%02d", ms/hour, ms/minute%minutesPerHour, ms%minute/second)
|
||||
func (t *executiontime) formatDurationGalveston() string {
|
||||
result := fmt.Sprintf("%02d:%02d:%02d", t.Ms/hour, t.Ms/minute%minutesPerHour, t.Ms%minute/second)
|
||||
return result
|
||||
}
|
||||
|
||||
func (t *executiontime) formatDurationHouston(ms int64) string {
|
||||
func (t *executiontime) formatDurationHouston() string {
|
||||
milliseconds := ".0"
|
||||
if ms%second > 0 {
|
||||
if t.Ms%second > 0 {
|
||||
// format milliseconds as a string with truncated trailing zeros
|
||||
milliseconds = strconv.FormatFloat(float64(ms%second)/second, 'f', -1, 64)
|
||||
milliseconds = strconv.FormatFloat(float64(t.Ms%second)/second, 'f', -1, 64)
|
||||
// at this point milliseconds looks like "0.5". remove the leading "0"
|
||||
milliseconds = milliseconds[1:]
|
||||
}
|
||||
|
||||
result := fmt.Sprintf("%02d:%02d:%02d%s", ms/hour, ms/minute%minutesPerHour, ms%minute/second, milliseconds)
|
||||
result := fmt.Sprintf("%02d:%02d:%02d%s", t.Ms/hour, t.Ms/minute%minutesPerHour, t.Ms%minute/second, milliseconds)
|
||||
return result
|
||||
}
|
||||
|
||||
func (t *executiontime) formatDurationAmarillo(ms int64) string {
|
||||
func (t *executiontime) formatDurationAmarillo() string {
|
||||
// wholeNumber represents the value to the left of the decimal point (seconds)
|
||||
wholeNumber := ms / second
|
||||
wholeNumber := t.Ms / second
|
||||
// decimalNumber represents the value to the right of the decimal point (milliseconds)
|
||||
decimalNumber := float64(ms%second) / second
|
||||
decimalNumber := float64(t.Ms%second) / second
|
||||
|
||||
// format wholeNumber as a string with thousands separators
|
||||
printer := message.NewPrinter(lang.English)
|
||||
|
@ -180,27 +182,27 @@ func (t *executiontime) formatDurationAmarillo(ms int64) string {
|
|||
return result
|
||||
}
|
||||
|
||||
func (t *executiontime) formatDurationRound(ms int64) string {
|
||||
func (t *executiontime) formatDurationRound() 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 toRoundString(ms/day, hours, "d", "h")
|
||||
hours := t.Ms / hour % hoursPerDay
|
||||
if t.Ms >= day {
|
||||
return toRoundString(t.Ms/day, hours, "d", "h")
|
||||
}
|
||||
minutes := ms / minute % secondsPerMinute
|
||||
if ms >= hour {
|
||||
minutes := t.Ms / minute % secondsPerMinute
|
||||
if t.Ms >= hour {
|
||||
return toRoundString(hours, minutes, "h", "m")
|
||||
}
|
||||
seconds := (ms % minute) / second
|
||||
if ms >= minute {
|
||||
seconds := (t.Ms % minute) / second
|
||||
if t.Ms >= minute {
|
||||
return toRoundString(minutes, seconds, "m", "s")
|
||||
}
|
||||
if ms >= second {
|
||||
if t.Ms >= second {
|
||||
return fmt.Sprintf("%ds", seconds)
|
||||
}
|
||||
return fmt.Sprintf("%dms", ms%second)
|
||||
return fmt.Sprintf("%dms", t.Ms%second)
|
||||
}
|
||||
|
|
|
@ -64,7 +64,7 @@ func TestExecutionTimeWriterDuration(t *testing.T) {
|
|||
env: env,
|
||||
}
|
||||
executionTime.enabled()
|
||||
assert.Equal(t, expected, executionTime.output)
|
||||
assert.Equal(t, expected, executionTime.FormattedMs)
|
||||
}
|
||||
|
||||
func TestExecutionTimeWriterDuration2(t *testing.T) {
|
||||
|
@ -76,7 +76,7 @@ func TestExecutionTimeWriterDuration2(t *testing.T) {
|
|||
env: env,
|
||||
}
|
||||
executionTime.enabled()
|
||||
assert.Equal(t, expected, executionTime.output)
|
||||
assert.Equal(t, expected, executionTime.FormattedMs)
|
||||
}
|
||||
|
||||
func TestExecutionTimeFormatDurationAustin(t *testing.T) {
|
||||
|
@ -99,7 +99,8 @@ func TestExecutionTimeFormatDurationAustin(t *testing.T) {
|
|||
for _, tc := range cases {
|
||||
duration, _ := time.ParseDuration(tc.Input)
|
||||
executionTime := &executiontime{}
|
||||
output := executionTime.formatDurationAustin(duration.Milliseconds())
|
||||
executionTime.Ms = duration.Milliseconds()
|
||||
output := executionTime.formatDurationAustin()
|
||||
assert.Equal(t, tc.Expected, output)
|
||||
}
|
||||
}
|
||||
|
@ -124,7 +125,8 @@ func TestExecutionTimeFormatDurationRoundrock(t *testing.T) {
|
|||
for _, tc := range cases {
|
||||
duration, _ := time.ParseDuration(tc.Input)
|
||||
executionTime := &executiontime{}
|
||||
output := executionTime.formatDurationRoundrock(duration.Milliseconds())
|
||||
executionTime.Ms = duration.Milliseconds()
|
||||
output := executionTime.formatDurationRoundrock()
|
||||
assert.Equal(t, tc.Expected, output)
|
||||
}
|
||||
}
|
||||
|
@ -149,7 +151,8 @@ func TestExecutionTimeFormatDallas(t *testing.T) {
|
|||
for _, tc := range cases {
|
||||
duration, _ := time.ParseDuration(tc.Input)
|
||||
executionTime := &executiontime{}
|
||||
output := executionTime.formatDurationDallas(duration.Milliseconds())
|
||||
executionTime.Ms = duration.Milliseconds()
|
||||
output := executionTime.formatDurationDallas()
|
||||
assert.Equal(t, tc.Expected, output)
|
||||
}
|
||||
}
|
||||
|
@ -174,7 +177,8 @@ func TestExecutionTimeFormatGalveston(t *testing.T) {
|
|||
for _, tc := range cases {
|
||||
duration, _ := time.ParseDuration(tc.Input)
|
||||
executionTime := &executiontime{}
|
||||
output := executionTime.formatDurationGalveston(duration.Milliseconds())
|
||||
executionTime.Ms = duration.Milliseconds()
|
||||
output := executionTime.formatDurationGalveston()
|
||||
assert.Equal(t, tc.Expected, output)
|
||||
}
|
||||
}
|
||||
|
@ -199,7 +203,8 @@ func TestExecutionTimeFormatHouston(t *testing.T) {
|
|||
for _, tc := range cases {
|
||||
duration, _ := time.ParseDuration(tc.Input)
|
||||
executionTime := &executiontime{}
|
||||
output := executionTime.formatDurationHouston(duration.Milliseconds())
|
||||
executionTime.Ms = duration.Milliseconds()
|
||||
output := executionTime.formatDurationHouston()
|
||||
assert.Equal(t, tc.Expected, output)
|
||||
}
|
||||
}
|
||||
|
@ -224,7 +229,8 @@ func TestExecutionTimeFormatAmarillo(t *testing.T) {
|
|||
for _, tc := range cases {
|
||||
duration, _ := time.ParseDuration(tc.Input)
|
||||
executionTime := &executiontime{}
|
||||
output := executionTime.formatDurationAmarillo(duration.Milliseconds())
|
||||
executionTime.Ms = duration.Milliseconds()
|
||||
output := executionTime.formatDurationAmarillo()
|
||||
assert.Equal(t, tc.Expected, output)
|
||||
}
|
||||
}
|
||||
|
@ -249,7 +255,8 @@ func TestExecutionTimeFormatDurationRound(t *testing.T) {
|
|||
for _, tc := range cases {
|
||||
duration, _ := time.ParseDuration(tc.Input)
|
||||
executionTime := &executiontime{}
|
||||
output := executionTime.formatDurationRound(duration.Milliseconds())
|
||||
executionTime.Ms = duration.Milliseconds()
|
||||
output := executionTime.formatDurationRound()
|
||||
assert.Equal(t, tc.Expected, output)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue