From 9a8a7b7d5a87b1ac588c00afe73a167a30c23865 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 5 Mar 2021 15:41:22 +0100 Subject: prettyprompt: show hostname and shortened pwd in terminal title --- internal/prompt/pwd.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'internal/prompt/pwd.go') diff --git a/internal/prompt/pwd.go b/internal/prompt/pwd.go index 1b93987..947606c 100644 --- a/internal/prompt/pwd.go +++ b/internal/prompt/pwd.go @@ -27,7 +27,8 @@ import ( //Directory contains all data about a directory that the prompt needs. type Directory struct { Path string - DisplayPath string + DisplayPath string //formatted for display in prompt line + TitlePath string //formatted for display in TerminalTitle InBuildTree bool InRepoTree bool Repo *gitRepo @@ -74,6 +75,10 @@ func NewDirectory(path string) (dir Directory) { } } + //terminal title has the same path truncations as above, but differs from + //this point + dir.TitlePath = dir.DisplayPath + //strip $HOME prefix if applicable and desirable dir.stripHomeDirFromDisplay() @@ -81,6 +86,12 @@ func NewDirectory(path string) (dir Directory) { var err error dir.Repo, err = findRepo(dir.Path) handleError(err) + + //inside a repository, terminal title only shows the repo directory and + //below (e.g. "gofu/internal/prompt" for the current directory) + if dir.Repo != nil { + dir.TitlePath, _ = filepath.Rel(filepath.Dir(dir.Repo.RootPath), dir.Path) + } } return @@ -107,6 +118,7 @@ func (dir *Directory) stripHomeDirFromDisplay() { } if !strings.HasPrefix(rel, "..") { dir.DisplayPath = rel + dir.TitlePath = "~/" + rel } } @@ -118,7 +130,7 @@ func findNearestAccessiblePath(path string) string { return findNearestAccessiblePath(filepath.Dir(path)) } -func getDirectoryField(dir Directory) string { +func getDirectoryField(dir Directory, tt *TerminalTitle) string { if dir.DisplayPath == "" { return "" } @@ -139,12 +151,15 @@ func getDirectoryField(dir Directory) string { txt = withColor("1;36", dir.NearestAccessiblePath+"/") + withColor("1;31", rel) } + tt.Path = dir.TitlePath + //apply tags if dir.InRepoTree { txt = withType("repo", txt) } if dir.InBuildTree { txt = withType("build", txt) + tt.Path = "build:" + tt.Path } return txt } -- cgit v1.3.1