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