Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
highlight inaccessible directory path in red
Browse files Browse the repository at this point in the history
  • Loading branch information
talal committed Jan 23, 2019
1 parent 7279f6c commit d37cd8c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## v1.2.0 - 2019-01-23
### Added
- Highlight inaccessible directory path in red.

## v1.1.2 - 2019-01-10
### Fixed
- Home path not being converted to `~` in some cases.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ PROMPT='%(?.%F{magenta}.%F{red})${prompt_symbol}%f '
| Option | Description | Usage |
| --- | --- | --- |
| `MIMIR_KUBE` | Disable Kubernetes context and namespace info. | `export MIMIR_KUBE='false'` |
| `MIMIR_OS_CLOUD` | Disable OpenStack cloud info. | `export MIMIR_OS_CLOUD='false'` |
| `MIMIR_KUBE` | Disable Kubernetes context and namespace info. | `export MIMIR_KUBE='0'` |
| `MIMIR_OS_CLOUD` | Disable OpenStack cloud info. | `export MIMIR_OS_CLOUD='0'` |
## Credits
Expand Down
4 changes: 2 additions & 2 deletions pkg/prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
func Info() string {
var line []string
line = appendUnlessEmpty(line, getDir())
if os.Getenv("MIMIR_KUBE") != "false" {
if os.Getenv("MIMIR_KUBE") != "0" {
line = appendUnlessEmpty(line, getKube())
}
if os.Getenv("MIMIR_OS_CLOUD") != "false" {
if os.Getenv("MIMIR_OS_CLOUD") != "0" {
line = appendUnlessEmpty(line, getOSCloud())
}

Expand Down
65 changes: 50 additions & 15 deletions pkg/prompt/pwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,28 @@ import (
// directory is a git repo).
func getDir() string {
cwd, err := os.Getwd()
handleError(err)
if err != nil {
cwd = os.Getenv("PWD")
}
cwd = filepath.Clean(cwd)

pathToDisplay := filepath.Clean(cwd)
if pathToDisplay != "/" {
homePath := os.Getenv("HOME")
pathToDisplay = strings.Replace(pathToDisplay, homePath, "~", 1)

if pathList := strings.Split(pathToDisplay, "/"); len(pathList) > 6 {
for i, v := range pathList[:len(pathList)-2] {
// pathList[0] will be an empty string due to leading '/'
if len(v) > 0 {
pathList[i] = v[:1]
}
}
pathToDisplay = strings.Join(pathList, "/")
}
if cwd == "/" {
return color.Sprintf(color.Blue, cwd)
}

nearestAccessiblePath := findNearestAccessiblePath(cwd)

if nearestAccessiblePath != cwd {
inAccessiblePath := strings.TrimPrefix(cwd, nearestAccessiblePath)
nearestAccessiblePath = shortenLongPath(stripHomeDir(nearestAccessiblePath), 1)

return color.Sprintf(color.Blue, nearestAccessiblePath) +
color.Sprintf(color.Red, inAccessiblePath)
}

pathToDisplay := stripHomeDir(cwd)
pathToDisplay = shortenLongPath(pathToDisplay, 2)

gitDir, err := findGitRepo(cwd)
handleError(err)

Expand All @@ -42,6 +46,37 @@ func getDir() string {
return color.Sprintf(color.Blue, pathToDisplay)
}

func findNearestAccessiblePath(path string) string {
_, err := os.Stat(path)
if err == nil {
return path
}

return findNearestAccessiblePath(filepath.Dir(path))
}

func shortenLongPath(path string, length int) string {
pList := strings.Split(path, "/")
if len(pList) < 7 {
return path
}

shortenedPList := pList[:len(pList)-length]
for i, v := range shortenedPList {
// shortenedPList[0] will be an empty string due to leading '/'
if len(v) > 0 {
shortenedPList[i] = v[:1]
}
}

shortenedPList = append(shortenedPList, pList[len(pList)-length:]...)
return strings.Join(shortenedPList, "/")
}

func stripHomeDir(path string) string {
return strings.Replace(path, os.Getenv("HOME"), "~", 1)
}

func findGitRepo(path string) (string, error) {
gitEntry := filepath.Join(path, ".git")
fi, err := os.Stat(gitEntry)
Expand Down

0 comments on commit d37cd8c

Please sign in to comment.