forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment-hg.go
72 lines (61 loc) · 1.5 KB
/
segment-hg.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
package main
import (
"fmt"
pwl "github.com/justjanne/powerline-go/powerline"
"os/exec"
"strings"
)
func getHgStatus() (bool, bool, bool) {
hasModifiedFiles := false
hasUntrackedFiles := false
hasMissingFiles := false
out, err := exec.Command("hg", "status").Output()
if err == nil {
output := strings.Split(string(out), "\n")
for _, line := range output {
if line != "" {
if line[0] == '?' {
hasUntrackedFiles = true
} else if line[0] == '!' {
hasMissingFiles = true
} else {
hasModifiedFiles = true
}
}
}
}
return hasModifiedFiles, hasUntrackedFiles, hasMissingFiles
}
func segmentHg(p *powerline) []pwl.Segment {
out, _ := exec.Command("hg", "branch").Output()
output := strings.SplitN(string(out), "\n", 2)
if len(output) > 0 && output[0] != "" {
branch := output[0]
hasModifiedFiles, hasUntrackedFiles, hasMissingFiles := getHgStatus()
var foreground, background uint8
var content string
if hasModifiedFiles || hasUntrackedFiles || hasMissingFiles {
foreground = p.theme.RepoDirtyFg
background = p.theme.RepoDirtyBg
extra := ""
if hasUntrackedFiles {
extra += "+"
}
if hasMissingFiles {
extra += "!"
}
content = fmt.Sprintf("%s %s", branch, extra)
} else {
foreground = p.theme.RepoCleanFg
background = p.theme.RepoCleanBg
content = fmt.Sprintf(branch)
}
return []pwl.Segment{{
Name: "hg",
Content: content,
Foreground: foreground,
Background: background,
}}
}
return []pwl.Segment{}
}