mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-01-13 20:27:28 -08:00
feat(template): add hresult function
This commit is contained in:
parent
3a580ff122
commit
8aac0992cd
|
@ -17,6 +17,7 @@ func funcMap() template.FuncMap {
|
|||
"gt": gt,
|
||||
"lt": lt,
|
||||
"reason": GetReasonFromStatus,
|
||||
"hresult": hresult,
|
||||
}
|
||||
for key, fun := range sprig.TxtFuncMap() {
|
||||
if _, ok := funcMap[key]; !ok {
|
||||
|
|
7
src/template/numbers.go
Normal file
7
src/template/numbers.go
Normal file
|
@ -0,0 +1,7 @@
|
|||
package template
|
||||
|
||||
import "fmt"
|
||||
|
||||
func hresult(number int) string {
|
||||
return fmt.Sprintf("0x%04X", uint32(number))
|
||||
}
|
43
src/template/numbers_test.go
Normal file
43
src/template/numbers_test.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package template
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/mock"
|
||||
"github.com/jandedobbeleer/oh-my-posh/src/platform"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
mock2 "github.com/stretchr/testify/mock"
|
||||
)
|
||||
|
||||
func TestHResult(t *testing.T) {
|
||||
cases := []struct {
|
||||
Case string
|
||||
Expected string
|
||||
Template string
|
||||
ShouldError bool
|
||||
}{
|
||||
{Case: "Windows exit code", Expected: "0x8A150014", Template: `{{ hresult -1978335212 }}`},
|
||||
{Case: "Not a number", Template: `{{ hresult "no number" }}`, ShouldError: true},
|
||||
}
|
||||
|
||||
env := &mock.MockedEnvironment{}
|
||||
env.On("TemplateCache").Return(&platform.TemplateCache{
|
||||
Env: make(map[string]string),
|
||||
})
|
||||
env.On("Error", mock2.Anything)
|
||||
env.On("Debug", mock2.Anything)
|
||||
for _, tc := range cases {
|
||||
tmpl := &Text{
|
||||
Template: tc.Template,
|
||||
Context: nil,
|
||||
Env: env,
|
||||
}
|
||||
text, err := tmpl.Render()
|
||||
if tc.ShouldError {
|
||||
assert.Error(t, err)
|
||||
continue
|
||||
}
|
||||
assert.Equal(t, tc.Expected, text, tc.Case)
|
||||
}
|
||||
}
|
|
@ -160,14 +160,15 @@ use them.
|
|||
|
||||
<!-- markdownlint-disable MD013 -->
|
||||
|
||||
| Template | Description |
|
||||
| -------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------ |
|
||||
| `{{ url .UpstreamIcon .UpstreamURL }}` | Create a hyperlink to a website to open your default browser (needs terminal [support][terminal-list-hyperlinks]). |
|
||||
| `{{ path .Path .Location }}` | Create a link to a folder to open your file explorer (needs terminal [support][terminal-list-hyperlinks]). |
|
||||
| `{{ secondsRound 3600 }}` | Round seconds to a time indication. In this case the output is `1h`. |
|
||||
| `{{ if glob "*.go" }}OK{{ else }}NOK{{ end }}` | Exposes [filepath.Glob][glob] as a boolean template function. |
|
||||
| `{{ if matchP "*.Repo" .Path }}Repo{{ else }}No Repo{{ end }}` | Exposes [regexp.MatchString][regexpms] as a boolean template function. |
|
||||
| `{{ replaceP "c.t" "cut code cat" "dog" }}` | Exposes [regexp.ReplaceAllString][regexpra] as a string template function. |
|
||||
| Template | Description |
|
||||
| -------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- |
|
||||
| `{{ url .UpstreamIcon .UpstreamURL }}` | Create a hyperlink to a website to open your default browser (needs terminal [support][terminal-list-hyperlinks]). |
|
||||
| `{{ path .Path .Location }}` | Create a link to a folder to open your file explorer (needs terminal [support][terminal-list-hyperlinks]). |
|
||||
| `{{ secondsRound 3600 }}` | Round seconds to a time indication. In this case the output is `1h`. |
|
||||
| `{{ if glob "*.go" }}OK{{ else }}NOK{{ end }}` | Exposes [filepath.Glob][glob] as a boolean template function. |
|
||||
| `{{ if matchP "*.Repo" .Path }}Repo{{ else }}No Repo{{ end }}` | Exposes [regexp.MatchString][regexpms] as a boolean template function. |
|
||||
| `{{ replaceP "c.t" "cut code cat" "dog" }}` | Exposes [regexp.ReplaceAllString][regexpra] as a string template function. |
|
||||
| <code>{{ .Code | hresult }}</code> | Transform a status code to its HRESULT value for easy troubleshooting. For example `-1978335212` becomes `0x8A150014`. |
|
||||
|
||||
<!-- markdownlint-enable MD013 -->
|
||||
|
||||
|
|
Loading…
Reference in a new issue