-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit.go
More file actions
135 lines (111 loc) · 3.66 KB
/
Copy pathgit.go
File metadata and controls
135 lines (111 loc) · 3.66 KB
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package widgets
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/yuys13/agystatusline/types"
)
// GitBranchWidget displays the current Git branch name.
type GitBranchWidget struct{}
func (g *GitBranchWidget) GetDefaultColor() string { return "brightMagenta" }
func (g *GitBranchWidget) GetDisplayName() string { return "Git Branch" }
func (g *GitBranchWidget) GetBodyColor(item types.WidgetItem, ctx types.RenderContext) string {
if ctx.Data.VCS != nil && ctx.Data.VCS.Dirty != nil && *ctx.Data.VCS.Dirty {
return "brightRed"
}
return "brightBlue"
}
func (g *GitBranchWidget) Render(item types.WidgetItem, ctx types.RenderContext, settings types.Settings) (string, string, error) {
symbol := "⎇ "
if item.CustomSymbol != "" {
symbol = item.CustomSymbol
}
// Try to get branch from VCS telemetry first
var branch string
var dirty bool
if ctx.Data.VCS != nil {
branch = ctx.Data.VCS.Branch
if ctx.Data.VCS.Dirty != nil {
dirty = *ctx.Data.VCS.Dirty
}
}
if branch == "" && !ctx.IsPreview {
// Fallback to git command
isGit, err := runGitCommand("rev-parse --is-inside-work-tree", ctx, ctx.GitCacheTTLSeconds)
if err == nil && isGit == "true" {
branch, _ = runGitCommand("symbolic-ref --short HEAD", ctx, ctx.GitCacheTTLSeconds)
status, _ := runGitCommand("status --porcelain", ctx, ctx.GitCacheTTLSeconds)
dirty = strings.TrimSpace(status) != ""
}
}
if ctx.IsPreview && branch == "" {
branch = "main"
}
if branch == "" {
if item.Hide != nil && *item.Hide {
return "", "", nil
}
return "", symbol + "no git", nil
}
preserveColors := item.PreserveColors != nil && *item.PreserveColors
if preserveColors {
// statusline.sh style coloring:
// branch name is brightBlue (or brightRed if dirty with a brightYellow '*' appended)
var bodyStr string
if dirty {
bodyStr = "\x1b[91m" + branch + "\x1b[39m\x1b[93m*\x1b[39m"
} else {
bodyStr = "\x1b[94m" + branch + "\x1b[39m"
}
return "", bodyStr, nil
}
bodyStr := symbol + branch
if dirty {
bodyStr += "*"
}
if item.RawValue != nil && *item.RawValue {
return "", branch, nil
}
return "", bodyStr, nil
}
// GitChangesWidget displays the counts of Git insertions and deletions.
type GitChangesWidget struct{}
func (g *GitChangesWidget) GetDefaultColor() string { return "yellow" }
func (g *GitChangesWidget) GetDisplayName() string { return "Git Changes" }
func (g *GitChangesWidget) GetBodyColor(item types.WidgetItem, ctx types.RenderContext) string {
return "yellow"
}
func (g *GitChangesWidget) Render(item types.WidgetItem, ctx types.RenderContext, settings types.Settings) (string, string, error) {
if ctx.IsPreview {
return "", "(+42,-10)", nil
}
// Check if inside git tree
isGit, err := runGitCommand("rev-parse --is-inside-work-tree", ctx, ctx.GitCacheTTLSeconds)
if err != nil || isGit != "true" {
if item.Hide != nil && *item.Hide {
return "", "", nil
}
return "", "(no git)", nil
}
unstagedStat, _ := runGitCommand("diff --shortstat", ctx, ctx.GitCacheTTLSeconds)
stagedStat, _ := runGitCommand("diff --cached --shortstat", ctx, ctx.GitCacheTTLSeconds)
uIns, uDel := parseShortStat(unstagedStat)
sIns, sDel := parseShortStat(stagedStat)
insertions := uIns + sIns
deletions := uDel + sDel
return "", fmt.Sprintf("(+%d,-%d)", insertions, deletions), nil
}
func parseShortStat(stat string) (int, int) {
insertMatch := regexp.MustCompile(`(\d+)\s+insertions?`).FindStringSubmatch(stat)
deleteMatch := regexp.MustCompile(`(\d+)\s+deletions?`).FindStringSubmatch(stat)
ins := 0
del := 0
if len(insertMatch) > 1 {
ins, _ = strconv.Atoi(insertMatch[1])
}
if len(deleteMatch) > 1 {
del, _ = strconv.Atoi(deleteMatch[1])
}
return ins, del
}