bin/prompt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
)
func main() {
wd, err := os.Getwd()
if err != nil {
fmt.Print("$ ")
return
}
cwd, err := filepath.EvalSymlinks(wd)
if err != nil {
fmt.Print("$ ")
return
}
emoji := resolveEmoji()
var promptRoot string
gitRoot := gitRoot()
if gitRoot == os.Getenv("HOME") {
promptRoot = "~"
} else {
promptRoot = filepath.Base(gitRoot)
}
// subtract the git toplevel "/home/j3s"
// from the cwd "/home/j3s/code/nongitdir"
// to get the suffix "/code/nongitdir"
// os.cwd gets the symlink, git does not
suffix := cwd[len(gitRoot):]
fmt.Printf("%s %s", emoji, promptRoot)
var parts []string
parts = strings.Split(suffix, "/")
for i, part := range parts {
if i == len(parts)-1 {
fmt.Printf("%s", part)
} else {
if len(part) != 0 {
fmt.Printf("%c/", part[0])
} else {
fmt.Printf("/")
}
}
}
}
func resolveEmoji() string {
hostname, _ := os.Hostname()
if hostname == "zora" {
return "πΈ"
}
if hostname == "rose" {
return "πΉ"
}
if hostname == "F67JYTFQT4" {
return "γΈβΏ(γ)βΏγ"
}
return "π"
}
// getRepoRoot returns the full path to the root
// of the closest git dir
func gitRoot() string {
path, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
if err != nil {
// assume the error means there's no git
// dir above us, or git isn't installed.
return "/"
}
return strings.TrimSpace(string(path))
}