-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdataset_info.go
More file actions
218 lines (179 loc) · 6.33 KB
/
Copy pathdataset_info.go
File metadata and controls
218 lines (179 loc) · 6.33 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
package dataset_info
import (
"fmt"
"strings"
"zfs-file-history/internal/logging"
"zfs-file-history/internal/ui/shortcut_helper"
"zfs-file-history/internal/ui/theme"
uiutil "zfs-file-history/internal/ui/util"
"zfs-file-history/internal/util"
"zfs-file-history/internal/zfs"
"github.com/dustin/go-humanize"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type DatasetInfoComponent struct {
application *tview.Application
dataset *zfs.Dataset
layout *tview.TextView
}
func NewDatasetInfo(application *tview.Application) *DatasetInfoComponent {
datasetInfo := &DatasetInfoComponent{
application: application,
}
datasetInfo.createLayout()
return datasetInfo
}
func (datasetInfo *DatasetInfoComponent) SetPath(path string) {
if path == "" {
datasetInfo.SetDataset(nil)
return
}
dataset, err := zfs.FindHostDataset(path)
if err == nil {
datasetInfo.SetDataset(dataset)
} else {
logging.Error("Could not find dataset for path %s: %s", path, err.Error())
datasetInfo.SetDataset(nil)
}
}
func (datasetInfo *DatasetInfoComponent) SetDataset(dataset *zfs.Dataset) {
if datasetInfo.dataset == dataset || datasetInfo.dataset != nil && dataset != nil && datasetInfo.dataset.GetName() == dataset.GetName() {
return
}
datasetInfo.dataset = dataset
datasetInfo.updateUi()
}
type DatasetInfoTableEntry struct {
Name string
Value string
}
func (datasetInfo *DatasetInfoComponent) createLayout() {
layout := tview.NewTextView().
SetDynamicColors(true).
SetWrap(false).
SetScrollable(true)
layout.SetBorder(true)
uiutil.SetupWindow(layout, "Dataset")
datasetInfo.layout = layout
datasetInfo.updateUi()
}
func (datasetInfo *DatasetInfoComponent) updateUi() {
dataset := datasetInfo.dataset
titleText := "Dataset"
if dataset == nil {
datasetInfo.layout.Clear()
uiutil.SetupWindow(datasetInfo.layout, titleText)
return
}
titleText = fmt.Sprintf("%s: %s", titleText, dataset.Path)
uiutil.SetupWindow(datasetInfo.layout, titleText)
properties := []*DatasetInfoTableEntry{
{Name: "Type", Value: dataset.GetType()},
{Name: "Name", Value: dataset.GetName()},
{Name: "Creation", Value: dataset.GetCreationString().Format(theme.Style.Format.DateTime)},
{Name: "Mountpoint", Value: dataset.GetMountPoint()},
{Name: "Mounted", Value: dataset.GetMounted()},
{Name: "Readonly", Value: dataset.GetReadonly()}, // "on" or "off"
{Name: "Volsize", Value: humanize.IBytes(dataset.GetVolSize())},
{Name: "Avail", Value: humanize.IBytes(dataset.GetAvailable())},
{Name: "Used", Value: humanize.IBytes(dataset.GetUsed())},
{Name: "Compression", Value: fmt.Sprintf("%s (%s)", dataset.GetCompression(), dataset.GetCompressRatio())}, // Combine for compact view
{Name: "Snapdir", Value: dataset.GetSnapdir()}, // "visible" or "hidden"
{Name: "Case", Value: dataset.GetCaseSensitivity()}, // "sensitive" / "insensitive"
}
// If encryption is utilized on the host pool
if dataset.IsEncrypted() {
properties = append(properties, &DatasetInfoTableEntry{
Name: "Encryption", Value: fmt.Sprintf("%s [%s]", dataset.GetEncryption(), dataset.GetKeyStatus()),
})
}
if !util.IsBlank(dataset.GetOrigin()) {
properties = append(properties, &DatasetInfoTableEntry{
Name: "Origin", Value: dataset.GetOrigin(),
})
}
if dataset.GetSnapshotLimit() > 0 {
properties = append(properties, &DatasetInfoTableEntry{
Name: "Snapshot Limit", Value: fmt.Sprintf("%d/%d", dataset.GetSnapshotCount(), dataset.GetSnapshotLimit()),
})
}
datasetInfo.layout.Clear()
// Calculate alignment padding dynamically based on longest key name
maxKeyLen := 0
for _, entry := range properties {
if len(entry.Name) > maxKeyLen {
maxKeyLen = len(entry.Name)
}
}
keyColorTag := colorTag(theme.Colors.Layout.Table.Header)
var out strings.Builder
for _, entry := range properties {
valueColor := resolveValueColor(entry.Name, entry.Value)
valueColorTag := colorTag(valueColor)
// Format key with trailing colon, maintaining clean alignment padding
labelText := fmt.Sprintf("%s:", entry.Name)
out.WriteString(fmt.Sprintf("%s%*s[-] %s%s[-]\n",
keyColorTag,
maxKeyLen+1, // +1 maps to the colon addition
tview.Escape(labelText),
valueColorTag,
tview.Escape(entry.Value),
))
}
datasetInfo.layout.SetText(out.String())
}
func (datasetInfo *DatasetInfoComponent) HasFocus() bool {
return datasetInfo.layout.HasFocus()
}
func (datasetInfo *DatasetInfoComponent) Focus() {
datasetInfo.application.SetFocus(datasetInfo.layout)
}
func (datasetInfo *DatasetInfoComponent) GetLayout() tview.Primitive {
return datasetInfo.layout
}
func (datasetInfo *DatasetInfoComponent) CreateSnapshot(name string) error {
if datasetInfo.dataset == nil {
return fmt.Errorf("no dataset for current file selection")
}
return datasetInfo.dataset.CreateSnapshot(name)
}
func (datasetInfo *DatasetInfoComponent) GetShortcutMap() []shortcut_helper.ShortcutEntry {
return []shortcut_helper.ShortcutEntry{}
}
// Helper formatting utilities matching ConfigInfo Component patterns
func colorTag(color tcell.Color) string {
r, g, b := color.RGB()
return fmt.Sprintf("[#%02x%02x%02x]", uint8(r), uint8(g), uint8(b))
}
func resolveValueColor(name, value string) tcell.Color {
if value == "" || value == "-" || strings.EqualFold(value, "none") {
return tcell.ColorGray
}
lowerName := strings.ToLower(name)
lowerValue := strings.ToLower(value)
// Warning / Restrictive States
if lowerName == "readonly" && lowerValue == "on" {
return tcell.ColorOrange // Visual cue that writes/restores are blocked
}
if strings.Contains(lowerValue, "unavailable") {
return tcell.ColorRed // Key is missing/locked
}
// Paths / Mountpoints
if strings.HasPrefix(value, "/") || lowerName == "mountpoint" || lowerName == "origin" {
return tcell.ColorLightBlue
}
// Booleans / Positive Flags
if lowerValue == "yes" || lowerValue == "true" || lowerValue == "on" || lowerValue == "visible" {
return tcell.ColorGreen
}
if lowerValue == "no" || lowerValue == "false" || lowerValue == "off" || lowerValue == "hidden" {
return tcell.ColorRed
}
// File Sizes & Ratios
if lowerName == "volsize" || lowerName == "avail" || lowerName == "used" || strings.Contains(lowerValue, "x") {
return tcell.ColorYellow
}
// Fallback Default String Color
return tcell.ColorWhite
}