aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2017-08-19 18:38:30 +0200
committerStefan Majewsky <majewsky@gmx.net>2017-08-19 18:38:30 +0200
commited989ce48005e96e273904971507764779143a83 (patch)
treed3d0b65888506be6a2f7d4e01936e973c90552cc
parent5c3787bd979611712f7dc7ce534355c64bdab430 (diff)
downloadgofu-ed989ce48005e96e273904971507764779143a83.tar.gz
prettyprompt: add accessibility check to cwd display
-rw-r--r--pkg/prompt/main.go1
-rw-r--r--pkg/prompt/pwd.go65
2 files changed, 49 insertions, 17 deletions
diff --git a/pkg/prompt/main.go b/pkg/prompt/main.go
index dac8495..4146b73 100644
--- a/pkg/prompt/main.go
+++ b/pkg/prompt/main.go
@@ -36,6 +36,7 @@ func Exec() int {
}
cwd := CurrentDirectory()
fields = appendUnlessEmpty(fields, getDirectoryField(cwd))
+ fields = appendUnlessEmpty(fields, getDeletedMessageField(cwd))
line := strings.Join(fields, " ")
lineWidth := getPrintableLength(line)
diff --git a/pkg/prompt/pwd.go b/pkg/prompt/pwd.go
index 77642c6..25c30bb 100644
--- a/pkg/prompt/pwd.go
+++ b/pkg/prompt/pwd.go
@@ -31,7 +31,7 @@ type Directory struct {
InBuildTree bool
InRepoTree bool
// RepoRootPath string
- // LastAccessiblePath string
+ NearestAccessiblePath string
}
//CurrentDirectory prepares a Directory struct for the current working
@@ -49,26 +49,33 @@ func NewDirectory(path string) (dir Directory) {
dir.Path = path
dir.DisplayPath = path
- //display tag if below /x/build
- if buildPath := os.Getenv("BUILD_ROOT"); buildPath != "" {
- rel, _ := filepath.Rel(buildPath, dir.DisplayPath)
- if !strings.HasPrefix(rel, "..") && rel != "." {
- dir.InBuildTree = true
- dir.DisplayPath = filepath.Join("/", rel)
+ //check if path actually exists
+ dir.NearestAccessiblePath = findNearestAccessiblePath(dir.Path)
+ if dir.NearestAccessiblePath == dir.Path {
+ dir.NearestAccessiblePath = "" //marks that everything is okay existence-wise
+
+ //display tag if below /x/build
+ if buildPath := os.Getenv("BUILD_ROOT"); buildPath != "" {
+ rel, _ := filepath.Rel(buildPath, dir.DisplayPath)
+ if !strings.HasPrefix(rel, "..") && rel != "." {
+ dir.InBuildTree = true
+ dir.DisplayPath = filepath.Join("/", rel)
+ }
}
- }
- //display tag if below /x/src
- if gopath := os.Getenv("GOPATH"); gopath != "" {
- repoPath := filepath.Join(gopath, "src")
- rel, _ := filepath.Rel(repoPath, dir.DisplayPath)
- if !strings.HasPrefix(rel, "..") && rel != "." {
- dir.InRepoTree = true
- dir.DisplayPath = rel
+ //display tag if below /x/src
+ if gopath := os.Getenv("GOPATH"); gopath != "" {
+ repoPath := filepath.Join(gopath, "src")
+ rel, _ := filepath.Rel(repoPath, dir.DisplayPath)
+ if !strings.HasPrefix(rel, "..") && rel != "." {
+ dir.InRepoTree = true
+ dir.DisplayPath = rel
+ }
}
+
+ dir.stripHomeDirFromDisplay()
}
- dir.stripHomeDirFromDisplay()
return
}
@@ -96,12 +103,29 @@ func (dir *Directory) stripHomeDirFromDisplay() {
}
}
+func findNearestAccessiblePath(path string) string {
+ _, err := os.Stat(path)
+ if err == nil {
+ return path
+ }
+ return findNearestAccessiblePath(filepath.Dir(path))
+}
+
func getDirectoryField(dir Directory) string {
if dir.DisplayPath == "" {
return ""
}
- txt := withColor("1;36", dir.DisplayPath)
+ //highlight inaccessible path elements
+ var txt string
+ if dir.NearestAccessiblePath == "" {
+ txt = withColor("1;36", dir.DisplayPath)
+ } else {
+ rel, _ := filepath.Rel(dir.NearestAccessiblePath, dir.Path)
+ txt = withColor("1;36", dir.NearestAccessiblePath+"/") + withColor("1;31", rel)
+ }
+
+ //apply tags
if dir.InRepoTree {
txt = withType("repo", txt)
}
@@ -110,3 +134,10 @@ func getDirectoryField(dir Directory) string {
}
return txt
}
+
+func getDeletedMessageField(dir Directory) string {
+ if dir.NearestAccessiblePath == "" {
+ return ""
+ }
+ return withColor("1;41", "cannot stat cwd")
+}