Skip to content

Instantly share code, notes, and snippets.

@michaelschade
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelschade/3039d9bdbec2d496f206 to your computer and use it in GitHub Desktop.
Save michaelschade/3039d9bdbec2d496f206 to your computer and use it in GitHub Desktop.
> tulip:navi go 🌱 master 🍂 9e7fbf5
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/michaelschade/gozsh/zshprompt"
)
func main() {
pb := &zshprompt.PromptBuilder{}
pb.PushColoredText(zshprompt.Magenta, "%m:")
// make go codepaths special ^.^
if strings.HasPrefix(os.Getenv("PWD"), os.Getenv("GOPATH")) {
pb.PushColoredText(zshprompt.White, "%1d")
pb.PushColoredText(zshprompt.Red, " go")
} else {
pb.PushColoredText(zshprompt.White, "%2d")
}
// git branch name
status := exec.Command("git", "rev-parse", "--abbrev-ref", "HEAD")
status.Dir = os.Getenv("PWD")
if output, err := status.Output(); err == nil {
branch := string(output[:len(output)-1])
pb.PushColoredText(zshprompt.Red, " 🌱 "+branch)
}
// git commit hash
commit := exec.Command("git", "rev-parse", "--short", "HEAD")
commit.Dir = os.Getenv("PWD")
if output, err := commit.Output(); err == nil {
sha := string(output[:len(output)-1])
pb.PushColoredText(zshprompt.Yellow, " 🍂 "+sha)
}
pb.PushColor(zshprompt.White)
fmt.Print(pb.BuildPrompt())
}
package zshprompt
import (
"fmt"
"strings"
)
type PromptBuilder struct {
texts []string
}
const (
Black = iota
Red
Green
Yellow
Blue
Magenta
Cyan
White
)
// Adds colored text to the list of zsh prompt text. Text can include the
// standard zsh prompt expansions:
//
// http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
func (pb *PromptBuilder) PushColoredText(color int, text string) {
pb.PushColor(color)
pb.PushText(text)
}
// Queues up a color change on the list of zsh prompt text.
func (pb *PromptBuilder) PushColor(color int) {
text := fmt.Sprintf("%%F{%d}", color)
pb.PushText(text)
}
// Adds arbitrary text to the list of zsh prompt text. Text can include the
// standard zsh prompt expansions:
//
// http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html
func (pb *PromptBuilder) PushText(text string) {
pb.texts = append(pb.texts, text)
}
// Joins the list of prompt texts together into a string ready for use.
func (pb *PromptBuilder) BuildPrompt() string {
return strings.Join(pb.texts, "")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment