feat(template): add functional stat, dir and base

resolves #5508
This commit is contained in:
Jan De Dobbeleer 2024-09-02 12:34:46 +02:00 committed by Jan De Dobbeleer
parent 90f3789057
commit b492903f08
3 changed files with 21 additions and 2 deletions

View file

@ -2,7 +2,10 @@ package template
import (
"os"
"os/exec"
"path/filepath"
"github.com/jandedobbeleer/oh-my-posh/src/log"
)
func glob(pattern string) (bool, error) {
@ -17,3 +20,13 @@ func readFile(path string) string {
content, _ := os.ReadFile(path)
return string(content)
}
func stat(path string) string {
fullPath, err := exec.LookPath(path)
if err != nil {
log.Error(err)
return ""
}
return fullPath
}

View file

@ -1,6 +1,7 @@
package template
import (
"path/filepath"
"text/template"
"github.com/Masterminds/sprig/v3"
@ -10,7 +11,7 @@ func funcMap() template.FuncMap {
funcMap := map[string]any{
"secondsRound": secondsRound,
"url": url,
"path": path,
"path": filePath,
"glob": glob,
"matchP": matchP,
"replaceP": replaceP,
@ -20,11 +21,16 @@ func funcMap() template.FuncMap {
"hresult": hresult,
"trunc": trunc,
"readFile": readFile,
"stat": stat,
"dir": filepath.Dir,
"base": filepath.Base,
}
for key, fun := range sprig.TxtFuncMap() {
if _, ok := funcMap[key]; !ok {
funcMap[key] = fun
}
}
return template.FuncMap(funcMap)
}

View file

@ -23,7 +23,7 @@ func url(text, url string) (string, error) {
return fmt.Sprintf("<LINK>%s<TEXT>%s</TEXT></LINK>", url, text), nil
}
func path(text, path string) (string, error) {
func filePath(text, path string) (string, error) {
unsupported := []string{elvish, xonsh}
if slices.Contains(unsupported, shell) {
return text, nil