···
6
6
"os/exec"
7
7
"path/filepath"
8
8
"strings"
9
9
-
"time"
10
9
)
11
10
12
12
-
// getRepoRoot returns the full path to the root
13
13
-
// of the closest git dir
14
14
-
func gitTopLevel() string {
15
15
-
path, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
11
11
+
func main() {
12
12
+
wd, err := os.Getwd()
16
13
if err != nil {
17
17
-
// assume the error means there's no git
18
18
-
// dir above us
19
19
-
return "/"
14
14
+
fmt.Print("$ ")
15
15
+
return
20
16
}
21
21
-
return strings.TrimSpace(string(path))
22
22
-
}
23
23
-
24
24
-
func resolveEmoji(hostname string) string {
25
25
-
emoji := "💀"
26
26
-
if hostname == "zora" {
27
27
-
emoji = "🌸"
28
28
-
} else if hostname == "nostromo" {
29
29
-
emoji = "🛸"
17
17
+
cwd, err := filepath.EvalSymlinks(wd)
18
18
+
if err != nil {
19
19
+
fmt.Print("$ ")
20
20
+
return
30
21
}
31
31
-
return emoji
32
32
-
}
33
33
-
34
34
-
func main() {
35
35
-
wd, _ := os.Getwd()
36
36
-
cwd, _ := filepath.EvalSymlinks(wd)
37
37
-
host, _ := os.Hostname()
38
38
-
home := os.Getenv("HOME")
39
39
-
now := time.Now().Format("15:04:05")
40
40
-
emoji := resolveEmoji(host)
41
41
-
fmt.Printf("%s %s ", now, emoji)
22
22
+
emoji := resolveEmoji()
42
23
43
24
var promptRoot string
44
44
-
gitTop := gitTopLevel()
45
45
-
if gitTop == home {
25
25
+
gitRoot := gitRoot()
26
26
+
if gitRoot == os.Getenv("HOME") {
46
27
promptRoot = "~"
47
28
} else {
48
48
-
promptRoot = filepath.Base(gitTop)
29
29
+
promptRoot = filepath.Base(promptRoot)
49
30
}
50
31
// subtract the git toplevel "/home/j3s"
51
32
// from the cwd "/home/j3s/code/nongitdir"
52
33
// to get the suffix "/code/nongitdir"
53
34
// os.cwd gets the symlink, git does not
54
54
-
suffix := cwd[len(gitTop):]
55
55
-
fmt.Printf("%s", promptRoot)
35
35
+
suffix := cwd[len(gitRoot):]
36
36
+
fmt.Printf("%s %s", emoji, promptRoot)
56
37
57
38
var parts []string
58
39
parts = strings.Split(suffix, "/")
···
68
49
}
69
50
}
70
51
}
52
52
+
53
53
+
func resolveEmoji() string {
54
54
+
hostname, _ := os.Hostname()
55
55
+
if hostname == "zora" {
56
56
+
return "🌸"
57
57
+
}
58
58
+
if hostname == "nostromo" {
59
59
+
return "🌹"
60
60
+
}
61
61
+
return "💀"
62
62
+
}
63
63
+
64
64
+
// getRepoRoot returns the full path to the root
65
65
+
// of the closest git dir
66
66
+
func gitRoot() string {
67
67
+
path, err := exec.Command("git", "rev-parse", "--show-toplevel").Output()
68
68
+
if err != nil {
69
69
+
// assume the error means there's no git
70
70
+
// dir above us, or git isn't installed.
71
71
+
return "/"
72
72
+
}
73
73
+
return strings.TrimSpace(string(path))
74
74
+
}