From b492903f089bebdd0c051f3d3cbe1786ff718da9 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Mon, 2 Sep 2024 12:34:46 +0200 Subject: [PATCH] feat(template): add functional stat, dir and base resolves #5508 --- src/template/files.go | 13 +++++++++++++ src/template/func_map.go | 8 +++++++- src/template/link.go | 2 +- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/template/files.go b/src/template/files.go index 862ef604..72c53cb5 100644 --- a/src/template/files.go +++ b/src/template/files.go @@ -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 +} diff --git a/src/template/func_map.go b/src/template/func_map.go index 0a13d03e..e1eed05d 100644 --- a/src/template/func_map.go +++ b/src/template/func_map.go @@ -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) } diff --git a/src/template/link.go b/src/template/link.go index 3b180e54..93b38e5a 100644 --- a/src/template/link.go +++ b/src/template/link.go @@ -23,7 +23,7 @@ func url(text, url string) (string, error) { return fmt.Sprintf("%s%s", 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