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 ( import (
"os" "os"
"os/exec"
"path/filepath" "path/filepath"
"github.com/jandedobbeleer/oh-my-posh/src/log"
) )
func glob(pattern string) (bool, error) { func glob(pattern string) (bool, error) {
@ -17,3 +20,13 @@ func readFile(path string) string {
content, _ := os.ReadFile(path) content, _ := os.ReadFile(path)
return string(content) 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 package template
import ( import (
"path/filepath"
"text/template" "text/template"
"github.com/Masterminds/sprig/v3" "github.com/Masterminds/sprig/v3"
@ -10,7 +11,7 @@ func funcMap() template.FuncMap {
funcMap := map[string]any{ funcMap := map[string]any{
"secondsRound": secondsRound, "secondsRound": secondsRound,
"url": url, "url": url,
"path": path, "path": filePath,
"glob": glob, "glob": glob,
"matchP": matchP, "matchP": matchP,
"replaceP": replaceP, "replaceP": replaceP,
@ -20,11 +21,16 @@ func funcMap() template.FuncMap {
"hresult": hresult, "hresult": hresult,
"trunc": trunc, "trunc": trunc,
"readFile": readFile, "readFile": readFile,
"stat": stat,
"dir": filepath.Dir,
"base": filepath.Base,
} }
for key, fun := range sprig.TxtFuncMap() { for key, fun := range sprig.TxtFuncMap() {
if _, ok := funcMap[key]; !ok { if _, ok := funcMap[key]; !ok {
funcMap[key] = fun funcMap[key] = fun
} }
} }
return template.FuncMap(funcMap) 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 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} unsupported := []string{elvish, xonsh}
if slices.Contains(unsupported, shell) { if slices.Contains(unsupported, shell) {
return text, nil return text, nil