mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49: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` |
|
| 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` |
|
| amarillo | `0.001s` | `2.1s` | `182.1s` | `14,582.1s` |
|
||||||
| round | `1ms` | `2s` | `3m 2s` | `4h 3m` |
|
| 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.
|
||||||
|
|
|
@ -11,7 +11,9 @@ import (
|
||||||
type executiontime struct {
|
type executiontime struct {
|
||||||
props *properties
|
props *properties
|
||||||
env environmentInfo
|
env environmentInfo
|
||||||
output string
|
|
||||||
|
FormattedMs string
|
||||||
|
Ms int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// DurationStyle how to display the time
|
// DurationStyle how to display the time
|
||||||
|
@ -52,13 +54,13 @@ func (t *executiontime) enabled() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
style := DurationStyle(t.props.getString(Style, string(Austin)))
|
style := DurationStyle(t.props.getString(Style, string(Austin)))
|
||||||
t.output = t.formatDuration(int64(executionTimeMs), style)
|
t.Ms = int64(executionTimeMs)
|
||||||
|
t.FormattedMs = t.formatDuration(style)
|
||||||
return t.output != ""
|
return t.FormattedMs != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *executiontime) string() string {
|
func (t *executiontime) string() string {
|
||||||
return t.output
|
return t.FormattedMs
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *executiontime) init(props *properties, env environmentInfo) {
|
func (t *executiontime) init(props *properties, env environmentInfo) {
|
||||||
|
@ -66,103 +68,103 @@ func (t *executiontime) init(props *properties, env environmentInfo) {
|
||||||
t.env = env
|
t.env = env
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *executiontime) formatDuration(ms int64, style DurationStyle) string {
|
func (t *executiontime) formatDuration(style DurationStyle) string {
|
||||||
switch style {
|
switch style {
|
||||||
case Austin:
|
case Austin:
|
||||||
return t.formatDurationAustin(ms)
|
return t.formatDurationAustin()
|
||||||
case Roundrock:
|
case Roundrock:
|
||||||
return t.formatDurationRoundrock(ms)
|
return t.formatDurationRoundrock()
|
||||||
case Dallas:
|
case Dallas:
|
||||||
return t.formatDurationDallas(ms)
|
return t.formatDurationDallas()
|
||||||
case Galveston:
|
case Galveston:
|
||||||
return t.formatDurationGalveston(ms)
|
return t.formatDurationGalveston()
|
||||||
case Houston:
|
case Houston:
|
||||||
return t.formatDurationHouston(ms)
|
return t.formatDurationHouston()
|
||||||
case Amarillo:
|
case Amarillo:
|
||||||
return t.formatDurationAmarillo(ms)
|
return t.formatDurationAmarillo()
|
||||||
case Round:
|
case Round:
|
||||||
return t.formatDurationRound(ms)
|
return t.formatDurationRound()
|
||||||
default:
|
default:
|
||||||
return fmt.Sprintf("Style: %s is not available", style)
|
return fmt.Sprintf("Style: %s is not available", style)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *executiontime) formatDurationAustin(ms int64) string {
|
func (t *executiontime) formatDurationAustin() string {
|
||||||
if ms < second {
|
if t.Ms < second {
|
||||||
return fmt.Sprintf("%dms", 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"
|
result := strconv.FormatFloat(seconds, 'f', -1, 64) + "s"
|
||||||
|
|
||||||
if ms >= minute {
|
if t.Ms >= minute {
|
||||||
result = fmt.Sprintf("%dm %s", ms/minute%secondsPerMinute, result)
|
result = fmt.Sprintf("%dm %s", t.Ms/minute%secondsPerMinute, result)
|
||||||
}
|
}
|
||||||
if ms >= hour {
|
if t.Ms >= hour {
|
||||||
result = fmt.Sprintf("%dh %s", ms/hour%hoursPerDay, result)
|
result = fmt.Sprintf("%dh %s", t.Ms/hour%hoursPerDay, result)
|
||||||
}
|
}
|
||||||
if ms >= day {
|
if t.Ms >= day {
|
||||||
result = fmt.Sprintf("%dd %s", ms/day, result)
|
result = fmt.Sprintf("%dd %s", t.Ms/day, result)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *executiontime) formatDurationRoundrock(ms int64) string {
|
func (t *executiontime) formatDurationRoundrock() string {
|
||||||
result := fmt.Sprintf("%dms", ms%second)
|
result := fmt.Sprintf("%dms", t.Ms%second)
|
||||||
if ms >= second {
|
if t.Ms >= second {
|
||||||
result = fmt.Sprintf("%ds %s", ms/second%secondsPerMinute, result)
|
result = fmt.Sprintf("%ds %s", t.Ms/second%secondsPerMinute, result)
|
||||||
}
|
}
|
||||||
if ms >= minute {
|
if t.Ms >= minute {
|
||||||
result = fmt.Sprintf("%dm %s", ms/minute%minutesPerHour, result)
|
result = fmt.Sprintf("%dm %s", t.Ms/minute%minutesPerHour, result)
|
||||||
}
|
}
|
||||||
if ms >= hour {
|
if t.Ms >= hour {
|
||||||
result = fmt.Sprintf("%dh %s", ms/hour%hoursPerDay, result)
|
result = fmt.Sprintf("%dh %s", t.Ms/hour%hoursPerDay, result)
|
||||||
}
|
}
|
||||||
if ms >= day {
|
if t.Ms >= day {
|
||||||
result = fmt.Sprintf("%dd %s", ms/day, result)
|
result = fmt.Sprintf("%dd %s", t.Ms/day, result)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *executiontime) formatDurationDallas(ms int64) string {
|
func (t *executiontime) formatDurationDallas() string {
|
||||||
seconds := float64(ms%minute) / second
|
seconds := float64(t.Ms%minute) / second
|
||||||
result := strconv.FormatFloat(seconds, 'f', -1, 64)
|
result := strconv.FormatFloat(seconds, 'f', -1, 64)
|
||||||
|
|
||||||
if ms >= minute {
|
if t.Ms >= minute {
|
||||||
result = fmt.Sprintf("%d:%s", ms/minute%minutesPerHour, result)
|
result = fmt.Sprintf("%d:%s", t.Ms/minute%minutesPerHour, result)
|
||||||
}
|
}
|
||||||
if ms >= hour {
|
if t.Ms >= hour {
|
||||||
result = fmt.Sprintf("%d:%s", ms/hour%hoursPerDay, result)
|
result = fmt.Sprintf("%d:%s", t.Ms/hour%hoursPerDay, result)
|
||||||
}
|
}
|
||||||
if ms >= day {
|
if t.Ms >= day {
|
||||||
result = fmt.Sprintf("%d:%s", ms/day, result)
|
result = fmt.Sprintf("%d:%s", t.Ms/day, result)
|
||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *executiontime) formatDurationGalveston(ms int64) string {
|
func (t *executiontime) formatDurationGalveston() string {
|
||||||
result := fmt.Sprintf("%02d:%02d:%02d", ms/hour, ms/minute%minutesPerHour, ms%minute/second)
|
result := fmt.Sprintf("%02d:%02d:%02d", t.Ms/hour, t.Ms/minute%minutesPerHour, t.Ms%minute/second)
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *executiontime) formatDurationHouston(ms int64) string {
|
func (t *executiontime) formatDurationHouston() string {
|
||||||
milliseconds := ".0"
|
milliseconds := ".0"
|
||||||
if ms%second > 0 {
|
if t.Ms%second > 0 {
|
||||||
// format milliseconds as a string with truncated trailing zeros
|
// 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"
|
// at this point milliseconds looks like "0.5". remove the leading "0"
|
||||||
milliseconds = milliseconds[1:]
|
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
|
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 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 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
|
// format wholeNumber as a string with thousands separators
|
||||||
printer := message.NewPrinter(lang.English)
|
printer := message.NewPrinter(lang.English)
|
||||||
|
@ -180,27 +182,27 @@ func (t *executiontime) formatDurationAmarillo(ms int64) string {
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *executiontime) formatDurationRound(ms int64) string {
|
func (t *executiontime) formatDurationRound() string {
|
||||||
toRoundString := func(one, two int64, oneText, twoText string) string {
|
toRoundString := func(one, two int64, oneText, twoText string) string {
|
||||||
if two == 0 {
|
if two == 0 {
|
||||||
return fmt.Sprintf("%d%s", one, oneText)
|
return fmt.Sprintf("%d%s", one, oneText)
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%d%s %d%s", one, oneText, two, twoText)
|
return fmt.Sprintf("%d%s %d%s", one, oneText, two, twoText)
|
||||||
}
|
}
|
||||||
hours := ms / hour % hoursPerDay
|
hours := t.Ms / hour % hoursPerDay
|
||||||
if ms >= day {
|
if t.Ms >= day {
|
||||||
return toRoundString(ms/day, hours, "d", "h")
|
return toRoundString(t.Ms/day, hours, "d", "h")
|
||||||
}
|
}
|
||||||
minutes := ms / minute % secondsPerMinute
|
minutes := t.Ms / minute % secondsPerMinute
|
||||||
if ms >= hour {
|
if t.Ms >= hour {
|
||||||
return toRoundString(hours, minutes, "h", "m")
|
return toRoundString(hours, minutes, "h", "m")
|
||||||
}
|
}
|
||||||
seconds := (ms % minute) / second
|
seconds := (t.Ms % minute) / second
|
||||||
if ms >= minute {
|
if t.Ms >= minute {
|
||||||
return toRoundString(minutes, seconds, "m", "s")
|
return toRoundString(minutes, seconds, "m", "s")
|
||||||
}
|
}
|
||||||
if ms >= second {
|
if t.Ms >= second {
|
||||||
return fmt.Sprintf("%ds", seconds)
|
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,
|
env: env,
|
||||||
}
|
}
|
||||||
executionTime.enabled()
|
executionTime.enabled()
|
||||||
assert.Equal(t, expected, executionTime.output)
|
assert.Equal(t, expected, executionTime.FormattedMs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExecutionTimeWriterDuration2(t *testing.T) {
|
func TestExecutionTimeWriterDuration2(t *testing.T) {
|
||||||
|
@ -76,7 +76,7 @@ func TestExecutionTimeWriterDuration2(t *testing.T) {
|
||||||
env: env,
|
env: env,
|
||||||
}
|
}
|
||||||
executionTime.enabled()
|
executionTime.enabled()
|
||||||
assert.Equal(t, expected, executionTime.output)
|
assert.Equal(t, expected, executionTime.FormattedMs)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestExecutionTimeFormatDurationAustin(t *testing.T) {
|
func TestExecutionTimeFormatDurationAustin(t *testing.T) {
|
||||||
|
@ -99,7 +99,8 @@ func TestExecutionTimeFormatDurationAustin(t *testing.T) {
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
duration, _ := time.ParseDuration(tc.Input)
|
duration, _ := time.ParseDuration(tc.Input)
|
||||||
executionTime := &executiontime{}
|
executionTime := &executiontime{}
|
||||||
output := executionTime.formatDurationAustin(duration.Milliseconds())
|
executionTime.Ms = duration.Milliseconds()
|
||||||
|
output := executionTime.formatDurationAustin()
|
||||||
assert.Equal(t, tc.Expected, output)
|
assert.Equal(t, tc.Expected, output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,7 +125,8 @@ func TestExecutionTimeFormatDurationRoundrock(t *testing.T) {
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
duration, _ := time.ParseDuration(tc.Input)
|
duration, _ := time.ParseDuration(tc.Input)
|
||||||
executionTime := &executiontime{}
|
executionTime := &executiontime{}
|
||||||
output := executionTime.formatDurationRoundrock(duration.Milliseconds())
|
executionTime.Ms = duration.Milliseconds()
|
||||||
|
output := executionTime.formatDurationRoundrock()
|
||||||
assert.Equal(t, tc.Expected, output)
|
assert.Equal(t, tc.Expected, output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -149,7 +151,8 @@ func TestExecutionTimeFormatDallas(t *testing.T) {
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
duration, _ := time.ParseDuration(tc.Input)
|
duration, _ := time.ParseDuration(tc.Input)
|
||||||
executionTime := &executiontime{}
|
executionTime := &executiontime{}
|
||||||
output := executionTime.formatDurationDallas(duration.Milliseconds())
|
executionTime.Ms = duration.Milliseconds()
|
||||||
|
output := executionTime.formatDurationDallas()
|
||||||
assert.Equal(t, tc.Expected, output)
|
assert.Equal(t, tc.Expected, output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -174,7 +177,8 @@ func TestExecutionTimeFormatGalveston(t *testing.T) {
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
duration, _ := time.ParseDuration(tc.Input)
|
duration, _ := time.ParseDuration(tc.Input)
|
||||||
executionTime := &executiontime{}
|
executionTime := &executiontime{}
|
||||||
output := executionTime.formatDurationGalveston(duration.Milliseconds())
|
executionTime.Ms = duration.Milliseconds()
|
||||||
|
output := executionTime.formatDurationGalveston()
|
||||||
assert.Equal(t, tc.Expected, output)
|
assert.Equal(t, tc.Expected, output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -199,7 +203,8 @@ func TestExecutionTimeFormatHouston(t *testing.T) {
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
duration, _ := time.ParseDuration(tc.Input)
|
duration, _ := time.ParseDuration(tc.Input)
|
||||||
executionTime := &executiontime{}
|
executionTime := &executiontime{}
|
||||||
output := executionTime.formatDurationHouston(duration.Milliseconds())
|
executionTime.Ms = duration.Milliseconds()
|
||||||
|
output := executionTime.formatDurationHouston()
|
||||||
assert.Equal(t, tc.Expected, output)
|
assert.Equal(t, tc.Expected, output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -224,7 +229,8 @@ func TestExecutionTimeFormatAmarillo(t *testing.T) {
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
duration, _ := time.ParseDuration(tc.Input)
|
duration, _ := time.ParseDuration(tc.Input)
|
||||||
executionTime := &executiontime{}
|
executionTime := &executiontime{}
|
||||||
output := executionTime.formatDurationAmarillo(duration.Milliseconds())
|
executionTime.Ms = duration.Milliseconds()
|
||||||
|
output := executionTime.formatDurationAmarillo()
|
||||||
assert.Equal(t, tc.Expected, output)
|
assert.Equal(t, tc.Expected, output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -249,7 +255,8 @@ func TestExecutionTimeFormatDurationRound(t *testing.T) {
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
duration, _ := time.ParseDuration(tc.Input)
|
duration, _ := time.ParseDuration(tc.Input)
|
||||||
executionTime := &executiontime{}
|
executionTime := &executiontime{}
|
||||||
output := executionTime.formatDurationRound(duration.Milliseconds())
|
executionTime.Ms = duration.Milliseconds()
|
||||||
|
output := executionTime.formatDurationRound()
|
||||||
assert.Equal(t, tc.Expected, output)
|
assert.Equal(t, tc.Expected, output)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue