-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
104 lines (95 loc) · 3.04 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"fmt"
"os"
"os/exec"
"regexp"
"strings"
"flag"
)
// TODO: implement random color func
func main() {
vers := flag.Bool("v", false, "get the version")
status := flag.Bool("status", false, "formatting git status if any")
branch := flag.Bool("b", false, "get active branch")
modified := flag.Bool("m", false, "get number of tracked modified files")
deleted := flag.Bool("d", false, "get number of tracked deleted files")
untracked := flag.Bool("u", false, "get number of untracked files")
color := flag.Bool("c", false, "color the status output")
flag.Parse()
if *vers == true {
fmt.Println("Git Prettier version v0.0.3")
os.Exit(1)
}
if *status {
nMod, nDel, isUntr := gitStatus()
branch := gitBranch()
if branch == "" { os.Exit(1) }
var fmtStats string
if *color {
fmtStats = fmtResultsColor(branch, nMod, nDel, isUntr)
} else {
fmtStats = fmtResults(branch, nMod, nDel, isUntr)
}
fmt.Println(fmtStats)
}
if *branch { fmt.Println(gitBranch()) }
if *modified {
if nMod, _, _ := gitStatus(); nMod > 0 { fmt.Printf("+%v\n", nMod) }
}
if *deleted {
if _, nDel, _ := gitStatus(); nDel > 0 { fmt.Printf("-%v\n", nDel) }
}
if *untracked {
if _, _, isUntr := gitStatus(); isUntr { fmt.Println("U…") }
}
}
func gitBranch() (string){
cmd := exec.Command("git", "branch")
output, err := cmd.Output()
if err != nil {}
rBranch := regexp.MustCompile(`\*\s.*`)
branch := rBranch.FindAllString(string(output[:]), 1000)
branch = strings.Split(strings.Join(branch, ""), "* ")
parsBranch := strings.Join(branch, "")
return strings.TrimSuffix(parsBranch, "\n")
}
func gitStatus() (int, int, bool){
cmd := exec.Command("git", "status")
output, err := cmd.Output()
if err != nil {}
rMod := regexp.MustCompile(`modified:`)
nMod := len(rMod.FindAllString(string(output[:]), 1000))
rNew := regexp.MustCompile(`new file:`)
nNew := len(rNew.FindAllString(string(output[:]), 1000))
nMod = nMod + nNew
rDel := regexp.MustCompile(`deleted:`)
nDel := len(rDel.FindAllString(string(output[:]), 1000))
rUntr := regexp.MustCompile(`Untracked files:`)
return nMod, nDel, rUntr.MatchString(string(output[:]))
}
func fmtResults(branch string, nMod int, nDel int, isUntr bool) (string) {
stats := "["
stats += branch
if nMod > 0 || nDel > 0 { stats += " |" }
if nMod > 0 { stats = fmt.Sprint(stats, " +", nMod) }
if nDel > 0 { stats = fmt.Sprint(stats, " -", nDel) }
if isUntr { stats = fmt.Sprint(stats, " U…") }
stats += "]"
return stats
}
func fmtResultsColor(branch string, nMod int, nDel int, isUntr bool) (string) {
RED := "\033[91m"
BLUE := "\033[34m"
GREEN := "\033[32m"
PURPLE := "\033[95m"
DEFAULT := "\033[0m"
stats := "["
stats += PURPLE + branch + DEFAULT
if nMod > 0 || nDel > 0 { stats += " |" }
if nMod > 0 { stats = fmt.Sprint(stats, GREEN, " +", nMod, DEFAULT) }
if nDel > 0 { stats = fmt.Sprint(stats, RED, " -", nDel, DEFAULT) }
if isUntr { stats = fmt.Sprint(stats, BLUE, " U…", DEFAULT) }
stats += "]"
return stats
}