Last active
August 29, 2015 14:21
-
-
Save michaelschade/3039d9bdbec2d496f206 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> tulip:navi go π± master π 9e7fbf5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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