summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2017-08-19 18:50:20 +0200
committerStefan Majewsky <majewsky@gmx.net>2017-08-19 18:50:20 +0200
commit9f482b7644238feadec16337f8b0201c8833fd34 (patch)
treeb83a304a592d76663a4b137ebb3d97a581e1092c
parented989ce48005e96e273904971507764779143a83 (diff)
downloadgofu-9f482b7644238feadec16337f8b0201c8833fd34.tar.gz
prettyprompt: highlight path elements inside Git repo (if any)
-rw-r--r--pkg/prompt/pwd.go42
1 files changed, 33 insertions, 9 deletions
diff --git a/pkg/prompt/pwd.go b/pkg/prompt/pwd.go
index 25c30bb..1623a5f 100644
--- a/pkg/prompt/pwd.go
+++ b/pkg/prompt/pwd.go
@@ -26,11 +26,11 @@ import (
//Directory contains all data about a directory that the prompt needs.
type Directory struct {
- Path string
- DisplayPath string
- InBuildTree bool
- InRepoTree bool
- // RepoRootPath string
+ Path string
+ DisplayPath string
+ InBuildTree bool
+ InRepoTree bool
+ RepoRootPath string
NearestAccessiblePath string
}
@@ -52,7 +52,8 @@ func NewDirectory(path string) (dir Directory) {
//check if path actually exists
dir.NearestAccessiblePath = findNearestAccessiblePath(dir.Path)
if dir.NearestAccessiblePath == dir.Path {
- dir.NearestAccessiblePath = "" //marks that everything is okay existence-wise
+ //marks that everything is okay existence-wise
+ dir.NearestAccessiblePath = ""
//display tag if below /x/build
if buildPath := os.Getenv("BUILD_ROOT"); buildPath != "" {
@@ -73,7 +74,11 @@ func NewDirectory(path string) (dir Directory) {
}
}
+ //strip $HOME prefix if applicable and desirable
dir.stripHomeDirFromDisplay()
+
+ //check if we are inside a Git repository
+ dir.RepoRootPath = findRepoRootPath(dir.Path)
}
return
@@ -111,16 +116,35 @@ func findNearestAccessiblePath(path string) string {
return findNearestAccessiblePath(filepath.Dir(path))
}
+//Returns empty string if `path` is not inside a Git repo.
+func findRepoRootPath(path string) string {
+ _, err := os.Stat(filepath.Join(path, ".git"))
+ if err == nil {
+ return path
+ }
+ if path == "/" {
+ return ""
+ }
+ return findRepoRootPath(filepath.Dir(path))
+}
+
func getDirectoryField(dir Directory) string {
if dir.DisplayPath == "" {
return ""
}
- //highlight inaccessible path elements
- var txt string
+ txt := withColor("1;36", dir.DisplayPath)
if dir.NearestAccessiblePath == "" {
- txt = withColor("1;36", dir.DisplayPath)
+ //cwd accessible -> highlight path elements inside the repo (if any)
+ if dir.RepoRootPath != "" && dir.RepoRootPath != dir.Path {
+ rel, _ := filepath.Rel(dir.RepoRootPath, dir.Path)
+ if strings.HasSuffix(dir.DisplayPath, rel) {
+ base := strings.TrimSuffix(dir.DisplayPath, rel)
+ txt = withColor("0;36", base) + withColor("1;36", rel)
+ }
+ }
} else {
+ //cwd inaccessible -> highlight inaccessible path elements
rel, _ := filepath.Rel(dir.NearestAccessiblePath, dir.Path)
txt = withColor("1;36", dir.NearestAccessiblePath+"/") + withColor("1;31", rel)
}