small pixel drawing of a pufferfish dotfiles

bin/prompt.go

package main

import (
	"fmt"
	"os"
    "os/exec"
	"strings"
)

func main() {
	cwd, _ := os.Getwd()
	host, _ := os.Hostname()
	home := os.Getenv("HOME")
	var parts []string
	if strings.HasPrefix(cwd, home) {
		cwd = "~" + cwd[len(home):]
	}

    out, err := exec.Command("git", "branch", "--show-current").Output()
    if err != nil {
        out = []byte("^(;,;)^\n")
    }
    branchname := strings.TrimSuffix(string(out), "\n")

	fmt.Printf("\033[38;5;162m[%s]\033[0m ", host)

	parts = strings.Split(cwd, "/")
	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("/")
			}
		}
	}

    fmt.Printf(" %s", branchname)
}