aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2021-03-05 15:41:22 +0100
committerStefan Majewsky <majewsky@gmx.net>2021-03-05 15:41:22 +0100
commit9a8a7b7d5a87b1ac588c00afe73a167a30c23865 (patch)
treef6e75261e3142af91e117c61e1f56c62b0b58d7c
parent122068efd4a348921b736b4456d4e1c999dbfb79 (diff)
downloadgofu-9a8a7b7d5a87b1ac588c00afe73a167a30c23865.tar.gz
prettyprompt: show hostname and shortened pwd in terminal title
-rw-r--r--internal/prompt/login.go3
-rw-r--r--internal/prompt/main.go7
-rw-r--r--internal/prompt/pwd.go19
-rw-r--r--internal/prompt/term.go43
4 files changed, 67 insertions, 5 deletions
diff --git a/internal/prompt/login.go b/internal/prompt/login.go
index 92cc270..187ae79 100644
--- a/internal/prompt/login.go
+++ b/internal/prompt/login.go
@@ -25,7 +25,7 @@ import "os"
//#include <unistd.h>
import "C"
-func getLoginField() string {
+func getLoginField(tt *TerminalTitle) string {
var result string
//show user name
@@ -45,6 +45,7 @@ func getLoginField() string {
handleError(err)
hostname = "<unknown>"
}
+ tt.HostName = hostname
return result + withColor(
getenvOrDefault("PRETTYPROMPT_HOSTCOLOR", "0;33"),
hostname,
diff --git a/internal/prompt/main.go b/internal/prompt/main.go
index dcef513..be5bb59 100644
--- a/internal/prompt/main.go
+++ b/internal/prompt/main.go
@@ -31,11 +31,12 @@ import (
//Exec executes the prettyprompt applet and returns an exit code (0 for
//success, >0 for error).
func Exec(args []string) int {
+ var tt TerminalTitle
fields := []string{
- getLoginField(),
+ getLoginField(&tt),
}
cwd := CurrentDirectory()
- fields = appendUnlessEmpty(fields, getDirectoryField(cwd))
+ fields = appendUnlessEmpty(fields, getDirectoryField(cwd, &tt))
fields = appendUnlessEmpty(fields, getDeletedMessageField(cwd))
fields = appendUnlessEmpty(fields, getRepoStatusField(cwd.Repo))
fields = appendUnlessEmpty(fields, getTerminalField())
@@ -78,6 +79,8 @@ func Exec(args []string) int {
}
os.Stdout.Write([]byte(shellIdent + "$ "))
+ tt.PrintTo(os.Stdout)
+
return 0
}
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
}
diff --git a/internal/prompt/term.go b/internal/prompt/term.go
new file mode 100644
index 0000000..dcf614e
--- /dev/null
+++ b/internal/prompt/term.go
@@ -0,0 +1,43 @@
+/*******************************************************************************
+*
+* Copyright 2021 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 (
+ "fmt"
+ "io"
+)
+
+//TerminalTitle contains all pieces that go into the terminal title.
+type TerminalTitle struct {
+ HostName string
+ Path string
+}
+
+//PrintTo produces the VT command that sets the terminal title.
+func (t TerminalTitle) PrintTo(stdout io.Writer) {
+ //warn about missing pieces
+ if t.HostName == "" {
+ t.HostName = "N/A"
+ }
+ if t.Path == "" {
+ t.Path = "N/A"
+ }
+
+ fmt.Fprintf(stdout, "\x1B]0;\u23A3%s\u23A6 %s\x07", t.HostName, t.Path)
+}