-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtable_container.go
More file actions
329 lines (298 loc) · 7.99 KB
/
Copy pathtable_container.go
File metadata and controls
329 lines (298 loc) · 7.99 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package file_browser
import (
"fmt"
"sort"
"strconv"
"strings"
"zfs-file-history/internal/configuration"
"zfs-file-history/internal/data"
"zfs-file-history/internal/data/diff_state"
"zfs-file-history/internal/ui/table"
"zfs-file-history/internal/ui/theme"
"zfs-file-history/internal/util"
"github.com/dustin/go-humanize"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func createFileBrowserTable(application *tview.Application) *table.RowSelectionTable[data.FileBrowserEntry] {
tableContainer := table.NewTableContainer[data.FileBrowserEntry](
application,
fileBrowserEntryTableCellsFunction,
fileBrowserEntrySortFunction,
)
return tableContainer
}
func fileBrowserEntryTableCellsFunction(row int, columns []*table.Column, entry *data.FileBrowserEntry) (cells []*tview.TableCell) {
statusCellText := determineStatusIndicator(entry)
statusCellColor := determineStatusColor(entry)
typeCellText := determineTypeCellText(entry)
typeCellColor := determineTypeCellColor(entry)
for _, column := range columns {
var cellColor = tcell.ColorWhite
var cellText string
var cellAlignment = tview.AlignLeft
var cellExpansion = 0
switch column {
case columnName:
cellText = entry.Name
if entry.GetStat().IsDir() {
cellText = fmt.Sprintf("/%s", cellText)
}
cellColor = statusCellColor
case columnType:
cellText = typeCellText
cellColor = typeCellColor
cellAlignment = tview.AlignCenter
case columnDiff:
cellText = statusCellText
cellColor = statusCellColor
cellAlignment = tview.AlignCenter
case columnPermissions:
cellText = determinePermissionsText(entry)
cellColor = tcell.ColorGray
case columnUID:
cellText = determineUIDText(entry)
cellColor = tcell.ColorGray
case columnGID:
cellText = determineGIDText(entry)
cellColor = tcell.ColorGray
case columnDateTime:
cellText = entry.GetStat().ModTime().Format(theme.Style.Format.DateTime)
switch entry.DiffState {
case diff_state.Added, diff_state.Deleted:
cellColor = statusCellColor
case diff_state.Modified:
if entry.RealFile.Stat.ModTime() != entry.SnapshotFiles[0].Stat.ModTime() {
cellColor = statusCellColor
} else {
cellColor = tcell.ColorWhite
}
default:
cellColor = tcell.ColorGray
}
case columnSize:
cellText = humanize.IBytes(uint64(entry.GetStat().Size()))
if strings.HasSuffix(cellText, " B") {
withoutSuffix := strings.TrimSuffix(cellText, " B")
cellText = fmt.Sprintf("%s B", withoutSuffix)
}
if len(cellText) < 10 {
cellText = fmt.Sprintf("%s%s", strings.Repeat(" ", 10-len(cellText)), cellText)
}
switch entry.DiffState {
case diff_state.Added, diff_state.Deleted:
cellColor = statusCellColor
case diff_state.Modified:
if entry.RealFile.Stat.Size() != entry.SnapshotFiles[0].Stat.Size() {
cellColor = statusCellColor
} else {
cellColor = tcell.ColorWhite
}
default:
cellColor = tcell.ColorGray
}
cellAlignment = tview.AlignRight
default:
panic("Unknown column")
}
cell := tview.NewTableCell(cellText).
SetTextColor(cellColor).
SetAlign(cellAlignment).
SetExpansion(cellExpansion)
// Keep row statusCellText visible while selected by using statusCellColor as selected background.
cell.SetSelectedStyle(
tcell.StyleDefault.
Foreground(theme.Colors.Layout.Table.SelectedForeground).
Background(statusCellColor),
)
cells = append(cells, cell)
}
return cells
}
func determineTypeCellColor(entry *data.FileBrowserEntry) tcell.Color {
switch entry.Type {
case data.Directory:
return theme.Colors.Layout.Table.Header
case data.Link:
return tcell.ColorYellow
case data.File:
fallthrough
default:
return tcell.ColorGray
}
}
func determineTypeCellText(entry *data.FileBrowserEntry) string {
switch entry.Type {
case data.File:
return "F"
case data.Directory:
return "D"
case data.Link:
return "L"
default:
return "?"
}
}
func determineStatusIndicator(entry *data.FileBrowserEntry) string {
switch entry.DiffState {
case diff_state.Equal:
return "="
case diff_state.Deleted:
return "-"
case diff_state.Added:
return "+"
case diff_state.Modified:
return "≠"
case diff_state.Unknown:
fallthrough
default:
return "N/A"
}
}
func determineStatusColor(entry *data.FileBrowserEntry) tcell.Color {
switch entry.DiffState {
case diff_state.Equal:
return theme.Colors.FileBrowser.Table.State.Equal
case diff_state.Deleted:
return theme.Colors.FileBrowser.Table.State.Deleted
case diff_state.Added:
return theme.Colors.FileBrowser.Table.State.Added
case diff_state.Modified:
return theme.Colors.FileBrowser.Table.State.Modified
case diff_state.Unknown:
fallthrough
default:
return theme.Colors.FileBrowser.Table.State.Unknown
}
}
func determinePermissionsText(entry *data.FileBrowserEntry) string {
permissionsMode := configuration.CurrentConfig.FileBrowser.Permissions
if permissionsMode == configuration.FileBrowserPermissionsFormatSymbolic {
return util.UnixPermSymbolic(entry.GetStat().Mode())
}
return fmt.Sprintf("%04o", util.UnixPermissions(entry.GetStat().Mode()))
}
func determineUIDText(entry *data.FileBrowserEntry) string {
uid, _, ok := util.UnixOwnerIDs(entry.GetStat())
if !ok {
return "N/A"
}
return formatIdentity(uid, util.LookupUserName)
}
func determineGIDText(entry *data.FileBrowserEntry) string {
_, gid, ok := util.UnixOwnerIDs(entry.GetStat())
if !ok {
return "N/A"
}
return formatIdentity(gid, util.LookupGroupName)
}
func formatIdentity(id uint32, lookupName func(uint32) (string, error)) string {
idStr := strconv.FormatUint(uint64(id), 10)
displayMode := configuration.CurrentConfig.FileBrowser.Owner
name, err := lookupName(id)
if err != nil {
if displayMode == configuration.FileBrowserOwnerFormatName {
return idStr
}
return idStr
}
switch displayMode {
case configuration.FileBrowserOwnerFormatName:
return name
case configuration.FileBrowserOwnerFormatBoth:
return fmt.Sprintf("%s(%s)", idStr, name)
case configuration.FileBrowserOwnerFormatID:
fallthrough
default:
return idStr
}
}
func fileBrowserEntrySortFunction(entries []*data.FileBrowserEntry, columnToSortBy *table.Column, inverted bool) []*data.FileBrowserEntry {
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.Name), strings.ToLower(b.Name))
case columnDateTime:
result = a.GetStat().ModTime().Compare(b.GetStat().ModTime())
case columnType:
result = int(b.Type - a.Type)
case columnSize:
result = int(a.GetStat().Size() - b.GetStat().Size())
case columnDiff:
result = int(b.DiffState - a.DiffState)
case columnPermissions:
permA := util.UnixPermissions(a.GetStat().Mode())
permB := util.UnixPermissions(b.GetStat().Mode())
switch {
case permA < permB:
result = -1
case permA > permB:
result = 1
default:
result = 0
}
case columnUID:
uidA, _, okA := util.UnixOwnerIDs(a.GetStat())
uidB, _, okB := util.UnixOwnerIDs(b.GetStat())
result = compareUint32WithMissing(uidA, okA, uidB, okB)
case columnGID:
_, gidA, okA := util.UnixOwnerIDs(a.GetStat())
_, gidB, okB := util.UnixOwnerIDs(b.GetStat())
result = compareUint32WithMissing(gidA, okA, gidB, okB)
}
if inverted {
result *= -1
}
if result != 0 {
if result <= 0 {
return true
} else {
return false
}
}
result = int(b.Type - a.Type)
if result != 0 {
if result <= 0 {
return true
} else {
return false
}
}
result = strings.Compare(strings.ToLower(a.Name), strings.ToLower(b.Name))
if result != 0 {
if result <= 0 {
return true
} else {
return false
}
}
if result <= 0 {
return true
} else {
return false
}
})
return entries
}
func compareUint32WithMissing(a uint32, okA bool, b uint32, okB bool) int {
if !okA && !okB {
return 0
}
if !okA {
return -1
}
if !okB {
return 1
}
switch {
case a < b:
return -1
case a > b:
return 1
default:
return 0
}
}