prompt/prompt.go (view raw)
1package main
2
3import (
4 "fmt"
5 "os"
6 "strings"
7
8 git "github.com/libgit2/git2go/v33"
9)
10
11// Truncates the current working directory:
12// /home/icy/foo/bar -> ~/f/bar
13func trimPath(cwd, home string) string {
14
15 var path string
16 if strings.HasPrefix(cwd, home) {
17 path = "~" + strings.TrimPrefix(cwd, home)
18 } else {
19 // If path doesn't contain $HOME, return the
20 // entire path as is.
21 path = cwd
22 return path
23 }
24 return path
25}
26
27func main() {
28 if len(os.Args) != 3 {
29 os.Exit(1)
30 }
31
32 gitDir := getGitDir(os.Args[2])
33 switch os.Args[1] {
34 case "-p":
35 // current working directory
36 home := os.Getenv("HOME")
37 cwd := os.Args[2]
38 fmt.Print(trimPath(cwd, home))
39 case "-gb":
40 branchCh := make(chan string)
41 if len(gitDir) > 0 {
42 repo, _ := git.OpenRepository(gitDir)
43 go getGitBranch(repo)
44 fmt.Print(<-branchCh)
45 }
46 fmt.Print("")
47 case "-gs":
48 statusCh := make(chan string) // git info
49 if len(gitDir) > 0 {
50 repo, _ := git.OpenRepository(gitDir)
51 go getGitStatus(repo, "•", "×")
52 fmt.Print(<-statusCh)
53 }
54 fmt.Print("")
55 default:
56 os.Exit(1)
57 }
58}