small pixel drawing of a pufferfish dotfiles

simplify prompt
Jes Olson j3s@c3f.net
Sat, 04 Mar 2023 15:13:37 -0800
commit

4a0ce0c5234b4c8e4ec3e5284bec3a42a41530c0

parent

d5cdd5a4a7c923f435210042609532cafff36b3b

1 files changed, 37 insertions(+), 33 deletions(-)

jump to
M bin/prompt.gobin/prompt.go

@@ -6,53 +6,34 @@ "os"

"os/exec" "path/filepath" "strings" - "time" ) -// getRepoRoot returns the full path to the root -// of the closest git dir -func gitTopLevel() string { - path, err := exec.Command("git", "rev-parse", "--show-toplevel").Output() +func main() { + wd, err := os.Getwd() if err != nil { - // assume the error means there's no git - // dir above us - return "/" + fmt.Print("$ ") + return } - return strings.TrimSpace(string(path)) -} - -func resolveEmoji(hostname string) string { - emoji := "💀" - if hostname == "zora" { - emoji = "🌸" - } else if hostname == "nostromo" { - emoji = "🛸" + cwd, err := filepath.EvalSymlinks(wd) + if err != nil { + fmt.Print("$ ") + return } - return emoji -} - -func main() { - wd, _ := os.Getwd() - cwd, _ := filepath.EvalSymlinks(wd) - host, _ := os.Hostname() - home := os.Getenv("HOME") - now := time.Now().Format("15:04:05") - emoji := resolveEmoji(host) - fmt.Printf("%s %s ", now, emoji) + emoji := resolveEmoji() var promptRoot string - gitTop := gitTopLevel() - if gitTop == home { + gitRoot := gitRoot() + if gitRoot == os.Getenv("HOME") { promptRoot = "~" } else { - promptRoot = filepath.Base(gitTop) + promptRoot = filepath.Base(promptRoot) } // 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(gitTop):] - fmt.Printf("%s", promptRoot) + suffix := cwd[len(gitRoot):] + fmt.Printf("%s %s", emoji, promptRoot) var parts []string parts = strings.Split(suffix, "/")

@@ -68,3 +49,26 @@ }

} } } + +func resolveEmoji() string { + hostname, _ := os.Hostname() + if hostname == "zora" { + return "🌸" + } + if hostname == "nostromo" { + 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)) +}