Skip to content

Commit 4eb03b3

Browse files
authored
Bug fix and added flags (#2)
1 parent c704005 commit 4eb03b3

File tree

4 files changed

+82
-15
lines changed

4 files changed

+82
-15
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,14 @@
11
# Changelog
22

3+
## [0.0.3]
4+
5+
### Added
6+
- Added new flags
7+
- Added new version of binary
8+
9+
### Fixed
10+
- Fixed bug in regex, removed `^`
11+
312
## [0.0.2]
413

514
### Fixed

README.md

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,42 @@ gp -v
1414
```
1515

1616
## Usage
17-
`gp` only serves two flags `-v` and `-status`. The latter yield the git status on a pre-defined format and will only print values if in a git folder.
17+
`gp` serves a couple of flags you may want to use. See the list underneath.
1818

19+
| flag | type | default | description |
20+
| :--- | :--- | :------ | :---------- |
21+
| -v | bool | false | get the version |
22+
| -status | bool | false | formatting git status if any |
23+
| -c | bool | false | color the status output |
24+
| -b | bool | false | get active branch |
25+
| -m | bool | false | get number of tracked modified files |
26+
| -d | bool | false | get number of tracked deleted files |
27+
| -u | bool | false | get number of untracked files |
28+
29+
### Examples
30+
#### `-status`
31+
This flag yield the git status on a pre-defined format and will only print values if in a git folder.
1932
```sh
2033
gp -status
2134
>[master | +1 -2 U…]
2235
```
36+
You can also use the color flag `-c` in combination with `-status`.
37+
38+
#### `-b` `-m` `-d` `-u`
39+
```sh
40+
gp -b
41+
>master
42+
gp -m
43+
>+1
44+
````
45+
You can use flags in combination, but they will each print on a separate line.
46+
```sh
47+
gp -b -m -d -u
48+
>master
49+
>+1
50+
>-1
51+
>U…
52+
```
2353

2454
### PS1
2555
> :information_source: This is an example of how you can set `PS1`. Feel free to do whatever you want.

bin/gp

80 Bytes
Binary file not shown.

main.go

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,50 @@ import (
1010
)
1111

1212
// TODO: implement random color func
13-
// TODO: Add flag -b branch -m modidied -d deleted -u untraced (-bmdu)
1413

1514
func main() {
16-
vers := flag.Bool("v", false, "get the version")
15+
vers := flag.Bool("v", false, "get the version")
16+
status := flag.Bool("status", false, "formatting git status if any")
17+
branch := flag.Bool("b", false, "get active branch")
18+
modified := flag.Bool("m", false, "get number of tracked modified files")
19+
deleted := flag.Bool("d", false, "get number of tracked deleted files")
20+
untracked := flag.Bool("u", false, "get number of untracked files")
21+
color := flag.Bool("c", false, "color the status output")
1722

18-
status := flag.Bool("status", false, "formatting git status if any")
1923
flag.Parse()
2024
if *vers == true {
21-
fmt.Println("Git Prettier version v0.0.2")
25+
fmt.Println("Git Prettier version v0.0.3")
2226
os.Exit(1)
2327
}
2428
if *status {
25-
nMod, nDel, nUntr := gitStatus()
29+
nMod, nDel, isUntr := gitStatus()
2630
branch := gitBranch()
27-
2831
if branch == "" { os.Exit(1) }
29-
30-
fmtStats := fmtResults(branch, nMod, nDel, nUntr)
32+
var fmtStats string
33+
if *color {
34+
fmtStats = fmtResultsColor(branch, nMod, nDel, isUntr)
35+
} else {
36+
fmtStats = fmtResults(branch, nMod, nDel, isUntr)
37+
}
3138
fmt.Println(fmtStats)
3239
}
40+
if *branch { fmt.Println(gitBranch()) }
41+
if *modified {
42+
if nMod, _, _ := gitStatus(); nMod > 0 { fmt.Printf("+%v\n", nMod) }
43+
}
44+
if *deleted {
45+
if _, nDel, _ := gitStatus(); nDel > 0 { fmt.Printf("-%v\n", nDel) }
46+
}
47+
if *untracked {
48+
if _, _, isUntr := gitStatus(); isUntr { fmt.Println("U…") }
49+
}
3350
}
3451

3552
func gitBranch() (string){
3653
cmd := exec.Command("git", "branch")
3754
output, err := cmd.Output()
3855
if err != nil {}
39-
rBranch := regexp.MustCompile(`^\*\s.*`)
56+
rBranch := regexp.MustCompile(`\*\s.*`)
4057
branch := rBranch.FindAllString(string(output[:]), 1000)
4158
branch = strings.Split(strings.Join(branch, ""), "* ")
4259
parsBranch := strings.Join(branch, "")
@@ -47,18 +64,29 @@ func gitStatus() (int, int, bool){
4764
cmd := exec.Command("git", "status")
4865
output, err := cmd.Output()
4966
if err != nil {}
50-
rMod := regexp.MustCompile(`modified:`)
51-
nMod := len(rMod.FindAllString(string(output[:]), 1000))
67+
rMod := regexp.MustCompile(`modified:`)
68+
nMod := len(rMod.FindAllString(string(output[:]), 1000))
69+
rNew := regexp.MustCompile(`new file:`)
70+
nNew := len(rNew.FindAllString(string(output[:]), 1000))
71+
nMod = nMod + nNew
5272
rDel := regexp.MustCompile(`deleted:`)
5373
nDel := len(rDel.FindAllString(string(output[:]), 1000))
54-
rNew := regexp.MustCompile(`new file:`)
55-
nNew := len(rNew.FindAllString(string(output[:]), 1000))
5674
rUntr := regexp.MustCompile(`Untracked files:`)
57-
nMod = nMod + nNew
5875
return nMod, nDel, rUntr.MatchString(string(output[:]))
5976
}
6077

6178
func fmtResults(branch string, nMod int, nDel int, isUntr bool) (string) {
79+
stats := "["
80+
stats += branch
81+
if nMod > 0 || nDel > 0 { stats += " |" }
82+
if nMod > 0 { stats = fmt.Sprint(stats, " +", nMod) }
83+
if nDel > 0 { stats = fmt.Sprint(stats, " -", nDel) }
84+
if isUntr { stats = fmt.Sprint(stats, " U…") }
85+
stats += "]"
86+
return stats
87+
}
88+
89+
func fmtResultsColor(branch string, nMod int, nDel int, isUntr bool) (string) {
6290
RED := "\033[91m"
6391
BLUE := "\033[34m"
6492
GREEN := "\033[32m"

0 commit comments

Comments
 (0)