summaryrefslogtreecommitdiff
path: root/pkg/prompt
diff options
context:
space:
mode:
Diffstat (limited to 'pkg/prompt')
-rw-r--r--pkg/prompt/login.go2
-rw-r--r--pkg/prompt/main.go12
-rw-r--r--pkg/prompt/pwd.go112
3 files changed, 124 insertions, 2 deletions
diff --git a/pkg/prompt/login.go b/pkg/prompt/login.go
index 6e7d8f7..010dd37 100644
--- a/pkg/prompt/login.go
+++ b/pkg/prompt/login.go
@@ -47,7 +47,7 @@ func getLoginField() string {
return result + withColor(
getenvOrDefault("PRETTYPROMPT_HOSTCOLOR", "0;33"),
hostname,
- ) + " "
+ )
}
func getUserName() string {
diff --git a/pkg/prompt/main.go b/pkg/prompt/main.go
index 8ff56c9..dac8495 100644
--- a/pkg/prompt/main.go
+++ b/pkg/prompt/main.go
@@ -34,6 +34,9 @@ func Exec() int {
fields := []string{
getLoginField(),
}
+ cwd := CurrentDirectory()
+ fields = appendUnlessEmpty(fields, getDirectoryField(cwd))
+
line := strings.Join(fields, " ")
lineWidth := getPrintableLength(line)
@@ -51,7 +54,7 @@ func Exec() int {
for idx := range dashes {
dashes[idx] = '-'
}
- line += string(dashes)
+ line += withColor("1", string(dashes))
}
os.Stdout.Write([]byte(line + "\n"))
@@ -66,6 +69,13 @@ func getenvOrDefault(key, defaultValue string) (value string) {
return
}
+func appendUnlessEmpty(list []string, val string) []string {
+ if val == "" {
+ return list
+ }
+ return append(list, val)
+}
+
func handleError(err error) {
if err != nil {
os.Stderr.Write([]byte("\x1B[1;31mPrompt error: " + err.Error() + "\x1B[0m\n"))
diff --git a/pkg/prompt/pwd.go b/pkg/prompt/pwd.go
new file mode 100644
index 0000000..77642c6
--- /dev/null
+++ b/pkg/prompt/pwd.go
@@ -0,0 +1,112 @@
+/*******************************************************************************
+*
+* Copyright 2017 Stefan Majewsky <majewsky@gmx.net>
+*
+* This program is free software: you can redistribute it and/or modify it under
+* the terms of the GNU General Public License as published by the Free Software
+* Foundation, either version 3 of the License, or (at your option) any later
+* version.
+*
+* This program is distributed in the hope that it will be useful, but WITHOUT ANY
+* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
+* A PARTICULAR PURPOSE. See the GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License along with
+* this program. If not, see <http://www.gnu.org/licenses/>.
+*
+*******************************************************************************/
+
+package prompt
+
+import (
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+//Directory contains all data about a directory that the prompt needs.
+type Directory struct {
+ Path string
+ DisplayPath string
+ InBuildTree bool
+ InRepoTree bool
+ // RepoRootPath string
+ // LastAccessiblePath string
+}
+
+//CurrentDirectory prepares a Directory struct for the current working
+//directory.
+func CurrentDirectory() Directory {
+ cwd, err := os.Getwd()
+ if err != nil {
+ cwd = filepath.Clean(os.Getenv("PWD"))
+ }
+ return NewDirectory(cwd)
+}
+
+//NewDirectory prepares a Directory struct for the given path.
+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)
+ }
+ }
+
+ //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()
+ return
+}
+
+//This part can benefit from a "return" in the middle, so it's in a separate function.
+func (dir *Directory) stripHomeDirFromDisplay() {
+ if !strings.HasPrefix(dir.DisplayPath, "/") {
+ return
+ }
+
+ homePath := os.Getenv("HOME")
+ if homePath == "" {
+ return
+ }
+
+ rel, _ := filepath.Rel(homePath, dir.DisplayPath)
+ if rel == "." {
+ //do not display an empty DisplayPath if tags are displayed
+ if dir.InBuildTree || dir.InRepoTree {
+ return
+ }
+ rel = ""
+ }
+ if !strings.HasPrefix(rel, "..") {
+ dir.DisplayPath = rel
+ }
+}
+
+func getDirectoryField(dir Directory) string {
+ if dir.DisplayPath == "" {
+ return ""
+ }
+
+ txt := withColor("1;36", dir.DisplayPath)
+ if dir.InRepoTree {
+ txt = withType("repo", txt)
+ }
+ if dir.InBuildTree {
+ txt = withType("build", txt)
+ }
+ return txt
+}