forked from wtfutil/wtf
/
display.go
110 lines (92 loc) · 2.63 KB
/
display.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
105
106
107
108
109
110
package git
import (
"fmt"
"strings"
"unicode/utf8"
)
func (widget *Widget) display() {
widget.Redraw(widget.content)
}
func (widget *Widget) content() (string, string, bool) {
repoData := widget.currentData()
if repoData == nil {
return widget.CommonSettings().Title, " Git repo data is unavailable ", false
}
widgetTitle := ""
if widget.settings.lastFolderTitle {
pathParts := strings.Split(repoData.Repository, "/")
widgetTitle += pathParts[len(pathParts)-1]
} else {
widgetTitle = repoData.Repository
}
if widget.settings.branchInTitle {
widgetTitle += fmt.Sprintf(" <%s>", repoData.Branch)
}
title := ""
if widget.settings.showModuleName {
title = fmt.Sprintf(
"%s - %s[white]",
widget.CommonSettings().Title,
widgetTitle,
)
} else {
title = fmt.Sprintf(
"%s[white]",
widgetTitle,
)
}
_, _, width, _ := widget.View.GetRect()
str := widget.settings.PaginationMarker(len(widget.GitRepos), widget.Idx, width) + "\n"
for _, v := range widget.settings.sections {
if v == "branch" {
str += fmt.Sprintf(" [%s]Branch[white]\n", widget.settings.Colors.Subheading)
str += fmt.Sprintf(" %s", repoData.Branch)
} else if v == "files" && (widget.settings.showFilesIfEmpty || len(repoData.ChangedFiles) > 1) {
str += widget.formatChanges(repoData.ChangedFiles)
} else if v == "commits" {
str += widget.formatCommits(repoData.Commits)
}
str += "\n"
}
return title, str, false
}
func (widget *Widget) formatChanges(data []string) string {
str := fmt.Sprintf(" [%s]Changed Files[white]\n", widget.settings.Colors.Subheading)
if len(data) == 1 {
str += " [grey]none[white]\n"
} else {
for _, line := range data {
str += widget.formatChange(line)
}
}
return str
}
func (widget *Widget) formatChange(line string) string {
if line == "" {
return ""
}
line = strings.TrimSpace(line)
firstChar, _ := utf8.DecodeRuneInString(line)
// Revisit this and kill the ugly duplication
switch firstChar {
case 'A':
line = strings.Replace(line, "A", "[green]A[white]", 1)
case 'D':
line = strings.Replace(line, "D", "[red]D[white]", 1)
case 'M':
line = strings.Replace(line, "M", "[yellow]M[white]", 1)
case 'R':
line = strings.Replace(line, "R", "[purple]R[white]", 1)
}
return fmt.Sprintf(" %s\n", strings.ReplaceAll(line, "\"", ""))
}
func (widget *Widget) formatCommits(data []string) string {
str := fmt.Sprintf(" [%s]Recent Commits[white]\n", widget.settings.Colors.Subheading)
for _, line := range data {
str += widget.formatCommit(line)
}
return str
}
func (widget *Widget) formatCommit(line string) string {
return fmt.Sprintf(" %s\n", strings.ReplaceAll(line, "\"", ""))
}