-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtable_container.go
More file actions
156 lines (146 loc) · 4.5 KB
/
Copy pathtable_container.go
File metadata and controls
156 lines (146 loc) · 4.5 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package snapshot_browser
import (
"fmt"
"math/big"
"sort"
"strings"
"zfs-file-history/internal/data"
"zfs-file-history/internal/data/diff_state"
"zfs-file-history/internal/ui/table"
"zfs-file-history/internal/ui/theme"
"github.com/dustin/go-humanize"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func createSnapshotBrowserTable(application *tview.Application) *table.RowSelectionTable[data.SnapshotBrowserEntry] {
tableContainer := table.NewTableContainer[data.SnapshotBrowserEntry](
application,
createSnapshotBrowserTableCells,
createSnapshotBrowserTableSortFunction,
)
return tableContainer
}
func createSnapshotBrowserTableCells(row int, columns []*table.Column, entry *data.SnapshotBrowserEntry) (cells []*tview.TableCell) {
result := []*tview.TableCell{}
statusColor := determineStatusColor(entry)
for _, column := range columns {
cellText := "N/A"
cellAlign := tview.AlignLeft
cellColor := determineBaseTextColor(entry)
switch column {
case columnDate:
cellText = entry.Snapshot.Properties.CreationDate.Format(theme.Style.Format.DateTime)
case columnName:
cellText = entry.Snapshot.Name
case columnDiff:
cellAlign = tview.AlignCenter
switch entry.DiffState {
case diff_state.Equal:
cellText = "="
cellColor = theme.Colors.SnapshotBrowser.Table.State.Equal
case diff_state.Deleted:
cellText = "+"
cellColor = theme.Colors.SnapshotBrowser.Table.State.SnapshotOnly
case diff_state.Added:
cellText = "-"
cellColor = theme.Colors.SnapshotBrowser.Table.State.LocalOnly
case diff_state.Modified:
cellText = "≠"
cellColor = theme.Colors.SnapshotBrowser.Table.State.Modified
case diff_state.Unknown:
cellText = "N/A"
cellColor = theme.Colors.SnapshotBrowser.Table.State.Unknown
}
case columnUsed:
cellText = humanize.IBytes(entry.Snapshot.Properties.Used)
case columnRefer:
cellText = humanize.IBytes(entry.Snapshot.Properties.Referenced)
case columnRatio:
ratio := entry.Snapshot.Properties.CompressionRatio
cellText = fmt.Sprintf("%.2fx", ratio)
case columnClones:
cellText = fmt.Sprintf("%d", entry.Snapshot.Properties.Clones)
}
cell := tview.NewTableCell(cellText).
SetTextColor(cellColor).
SetAlign(cellAlign)
cell.SetSelectedStyle(
tcell.StyleDefault.
Foreground(theme.Colors.Layout.Table.SelectedForeground).
Background(statusColor),
)
result = append(result, cell)
}
return result
}
func determineStatusColor(entry *data.SnapshotBrowserEntry) tcell.Color {
switch entry.DiffState {
case diff_state.Equal:
return theme.Colors.SnapshotBrowser.Table.State.Equal
case diff_state.Deleted:
return theme.Colors.SnapshotBrowser.Table.State.SnapshotOnly
case diff_state.Added:
return theme.Colors.SnapshotBrowser.Table.State.LocalOnly
case diff_state.Modified:
return theme.Colors.SnapshotBrowser.Table.State.Modified
case diff_state.Unknown:
fallthrough
default:
return theme.Colors.SnapshotBrowser.Table.State.Unknown
}
}
func determineBaseTextColor(entry *data.SnapshotBrowserEntry) tcell.Color {
switch entry.DiffState {
case diff_state.Deleted:
fallthrough
case diff_state.Added:
fallthrough
case diff_state.Modified:
return tcell.ColorWhite
default:
return tcell.ColorGray
}
}
func createSnapshotBrowserTableSortFunction(entries []*data.SnapshotBrowserEntry, columnToSortBy *table.Column, inverted bool) []*data.SnapshotBrowserEntry {
sort.SliceStable(entries, func(i, j int) bool {
a := entries[i]
b := entries[j]
result := 0
switch columnToSortBy {
case columnName:
result = strings.Compare(strings.ToLower(a.Snapshot.Name), strings.ToLower(b.Snapshot.Name))
case columnDate:
result = a.Snapshot.Properties.CreationDate.Compare(*b.Snapshot.Properties.CreationDate)
case columnDiff:
result = int(b.DiffState - a.DiffState)
case columnUsed:
result = int(b.Snapshot.Properties.Used - a.Snapshot.Properties.Used)
case columnRefer:
result = int(b.Snapshot.Properties.Referenced - a.Snapshot.Properties.Referenced)
case columnRatio:
ratioA := a.Snapshot.Properties.CompressionRatio
ratioB := b.Snapshot.Properties.CompressionRatio
result = big.NewFloat(ratioA).Cmp(big.NewFloat(ratioB))
case columnClones:
clonesA := a.Snapshot.Properties.Clones
clonesB := b.Snapshot.Properties.Clones
switch {
case clonesA < clonesB:
result = -1
case clonesA > clonesB:
result = 1
default:
result = 0
}
}
if inverted {
result *= -1
}
if result <= 0 {
return true
} else {
return false
}
})
return entries
}