-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ComponentFileTable.go
244 lines (202 loc) · 5.09 KB
/
ComponentFileTable.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package Cli
import (
"sort"
"strconv"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
pb "github.com/halleck45/ast-metrics/src/NodeType"
)
type ComponentFileTable struct {
isInteractive bool
files []*pb.File
sortColumnIndex int
table table.Model
search ComponentSearch
}
// index of cols
var colsRisks = map[string]int{
"Name": 0,
"Risk": 1,
"Commits": 2,
"Authors": 3,
"LOC": 4,
"Cyclomatic": 5,
}
func NewComponentFileTable(isInteractive bool, files []*pb.File) *ComponentFileTable {
v := &ComponentFileTable{
isInteractive: isInteractive,
files: files,
sortColumnIndex: 0,
}
v.Init()
return v
}
func (v *ComponentFileTable) Render() string {
var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
// Help and handers
text := StyleHelp(`
Use arrows to navigate and esc to quit.
Press following keys to sort by column:
(n) by name (l) by LOC (r) by risk
(c) by cyclomatic complexity (g) by commits
Press (ctrl+f) to search.
`).Render() + "\n"
// search box
text = text + v.search.Render()
// The results
if len(v.table.Rows()) == 0 {
if v.search.HasSearch() {
text += "No file found with the search: " + v.search.Expression + ".\n" + StyleHelp(`
Press Ctrl+C or Esc to quit.
`).Render()
} else {
text += "No file found.\n" + StyleHelp(`
Press Ctrl+C or Esc to quit.
`).Render()
}
} else {
text = text + baseStyle.Render(v.table.View())
}
return text
}
func (v *ComponentFileTable) Init() {
// search
if &v.search == nil {
v.search = ComponentSearch{Expression: ""}
}
columns := []table.Column{
{Title: "File", Width: 60},
{Title: "Risk", Width: 9},
{Title: "Commits", Width: 9},
{Title: "Authors", Width: 9},
{Title: "LOC", Width: 9},
{Title: "Cyclomatic", Width: 9},
}
rows := []table.Row{}
for _, file := range v.files {
// Search
if v.search.HasSearch() {
// search by file name
if !v.search.Match(file.Path) {
continue
}
}
nbCommits := 0
nbCommiters := 0
if file.Commits != nil {
nbCommits = int(file.Commits.Count)
nbCommiters = int(file.Commits.CountCommiters)
}
cyclo := 0
if file.Stmts != nil && file.Stmts.Analyze != nil && file.Stmts.Analyze.Complexity != nil {
cyclo = int(*file.Stmts.Analyze.Complexity.Cyclomatic)
}
risk := float32(0.0)
if file.Stmts != nil && file.Stmts.Analyze != nil && file.Stmts.Analyze.Risk != nil {
risk = float32(file.Stmts.Analyze.Risk.Score)
}
// truncate filename, but to the left
filename := file.Path
if len(filename) > 60 {
filename = "..." + filename[len(filename)-57:]
// remove the extension
}
rows = append(rows, table.Row{
filename,
strconv.FormatFloat(float64(risk), 'f', 2, 32),
strconv.Itoa(nbCommits),
strconv.Itoa(nbCommiters),
strconv.Itoa(int(file.LinesOfCode.GetLinesOfCode())),
strconv.Itoa(int(cyclo)),
})
}
// sort by name by default
sort.Slice(rows, func(i, j int) bool {
return rows[i][0] < rows[j][0]
})
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(15),
)
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Bold(false)
t.SetStyles(s)
v.table = t
}
func (v *ComponentFileTable) Sort(sortColumnIndex int) {
// Sort rows by second column (Loc)
rows := v.table.Rows()
sort.Slice(rows, func(i, j int) bool {
if sortColumnIndex == 0 {
return rows[i][sortColumnIndex] < rows[j][sortColumnIndex]
}
// for floats
a, _ := strconv.ParseFloat(rows[i][sortColumnIndex], 32)
b, _ := strconv.ParseFloat(rows[j][sortColumnIndex], 32)
if a != b {
return a > b
}
// for integers
aInt, _ := strconv.Atoi(rows[i][sortColumnIndex])
bInt, _ := strconv.Atoi(rows[j][sortColumnIndex])
return aInt > bInt
})
v.sortColumnIndex = sortColumnIndex
v.table.SetRows(rows)
}
func (v *ComponentFileTable) SortByName() {
v.Sort(colsRisks["Name"])
}
func (v *ComponentFileTable) SortByLoc() {
v.Sort(colsRisks["LOC"])
}
func (v *ComponentFileTable) SortByCommits() {
v.Sort(colsRisks["Commits"])
}
func (v *ComponentFileTable) SortByCyclomaticComplexity() {
v.Sort(colsRisks["Cyclomatic"])
}
func (v *ComponentFileTable) SortByRisk() {
v.Sort(colsRisks["Risk"])
}
func (v *ComponentFileTable) Update(msg tea.Msg) {
v.search.Update(msg)
if v.search.IsEnabled() {
// Stop here, and make search do its job
// Apply search
v.Init()
v.table, _ = v.table.Update(msg)
return
}
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "g":
v.SortByCommits()
case "l":
v.SortByLoc()
case "c":
v.SortByCyclomaticComplexity()
case "n":
v.SortByName()
case "r":
v.SortByRisk()
case "ctrl+f":
v.search.Toggle("")
}
}
v.table, _ = v.table.Update(msg)
}