This repository has been archived by the owner on Jan 24, 2023. It is now read-only.
/
table-params.go
89 lines (69 loc) · 1.5 KB
/
table-params.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
package table
import (
"sort"
"github.com/DK-92/harviewer/structs"
"github.com/lxn/walk"
)
type ParamTable struct {
header *structs.Param
}
type ParamModel struct {
walk.TableModelBase
walk.SorterBase
sortColumn int
sortOrder walk.SortOrder
items []*ParamTable
}
func NewParamModel(params *[]structs.Param) *ParamModel {
m := new(ParamModel)
m.FillRows(params)
return m
}
func (m *ParamModel) RowCount() int {
return len(m.items)
}
func (m *ParamModel) Value(row, col int) interface{} {
item := m.items[row]
switch col {
case 0:
return item.header.Name
case 1:
return item.header.Value
case 2:
return item.header.Comment
default:
return ""
}
panic("unexpected col")
}
func (m *ParamModel) Sort(col int, order walk.SortOrder) error {
m.sortColumn, m.sortOrder = col, order
sort.SliceStable(m.items, func(i, j int) bool {
a, b := m.items[i], m.items[j]
c := func(ls bool) bool {
if m.sortOrder == walk.SortAscending {
return ls
}
return !ls
}
switch m.sortColumn {
case 0:
return c(a.header.Name < b.header.Name)
case 1:
return c(a.header.Value < b.header.Value)
case 2:
return c(a.header.Comment < b.header.Comment)
}
panic("unreachable")
})
return m.SorterBase.Sort(col, order)
}
func (m *ParamModel) FillRows(params *[]structs.Param) {
m.items = make([]*ParamTable, len((*params)))
for i := range m.items {
m.items[i] = &ParamTable{}
m.items[i].header = &(*params)[i]
}
m.PublishRowsReset()
m.Sort(m.sortColumn, m.sortOrder)
}