all repos — dotfiles @ 61fed1b49b25bdaee135fa1488ea5435edd10b72

my *nix dotfiles

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	// items := strings.Split(path, "/")
26	// truncItems := []string{}
27	// for i, item := range items {
28	// 	if i == (len(items) - 1) {
29	// 		truncItems = append(truncItems, item)
30	// 		break
31	// 	}
32	// 	truncItems = append(truncItems, item[:1])
33	// }
34	// return filepath.Join(truncItems...)
35}
36
37func main() {
38
39	help := `usage: prompt OPTIONS PATH
40	-p				path to truncate
41	-gb				git branch
42	-gs				git status`
43	if len(os.Args) != 3 {
44		fmt.Println(help)
45		os.Exit(1)
46	}
47	gitDir := getGitDir(os.Args[2])
48	switch os.Args[1] {
49	case "-p":
50		// current working directory
51		home := os.Getenv("HOME")
52		cwd := os.Args[2]
53		fmt.Print(trimPath(cwd, home))
54	case "-gb":
55		branchCh := make(chan string)
56		if len(gitDir) > 0 {
57			repo, _ := git.OpenRepository(gitDir)
58			go getGitBranch(repo, branchCh)
59			fmt.Print(<-branchCh)
60		}
61		fmt.Print("")
62	case "-gs":
63		statusCh := make(chan string) // git info
64		if len(gitDir) > 0 {
65			repo, _ := git.OpenRepository(gitDir)
66			go getGitStatus(repo, statusCh)
67			fmt.Print(<-statusCh)
68		}
69		fmt.Print("")
70	default:
71		fmt.Println(help)
72		os.Exit(1)
73	}
74}