forked from muesli/duf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
297 lines (257 loc) · 8.15 KB
/
table.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
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
package main
import (
"fmt"
"os"
"strings"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/jedib0t/go-pretty/v6/text"
"github.com/muesli/termenv"
)
type TableOptions struct {
Columns []int
SortBy int
Style table.Style
}
type Column struct {
ID string
Name string
SortIndex int
Width int
}
var (
// "Mounted on", "Size", "Used", "Avail", "Use%", "Inodes", "Used", "Avail", "Use%", "Type", "Filesystem"
// mountpoint, size, used, avail, usage, inodes, inodes_used, inodes_avail, inodes_usage, type, filesystem
columns = []Column{
{ID: "mountpoint", Name: "Mounted on", SortIndex: 1},
{ID: "size", Name: "Size", SortIndex: 12, Width: 7},
{ID: "used", Name: "Used", SortIndex: 13, Width: 7},
{ID: "avail", Name: "Avail", SortIndex: 14, Width: 7},
{ID: "usage", Name: "Use%", SortIndex: 15, Width: 6},
{ID: "inodes", Name: "Inodes", SortIndex: 16, Width: 7},
{ID: "inodes_used", Name: "Used", SortIndex: 17, Width: 7},
{ID: "inodes_avail", Name: "Avail", SortIndex: 18, Width: 7},
{ID: "inodes_usage", Name: "Use%", SortIndex: 19, Width: 6},
{ID: "type", Name: "Type", SortIndex: 10},
{ID: "filesystem", Name: "Filesystem", SortIndex: 11},
}
)
// printTable prints an individual table of mounts.
func printTable(title string, m []Mount, opts TableOptions) {
tab := table.NewWriter()
tab.SetAllowedRowLength(int(*width))
tab.SetOutputMirror(os.Stdout)
tab.Style().Options.SeparateColumns = true
tab.SetStyle(opts.Style)
if barWidth() > 0 {
columns[4].Width = barWidth() + 7
columns[8].Width = barWidth() + 7
}
twidth := tableWidth(opts.Columns, tab.Style().Options.SeparateColumns)
tab.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, Hidden: !inColumns(opts.Columns, 1), WidthMax: int(float64(twidth) * 0.4)},
{Number: 2, Hidden: !inColumns(opts.Columns, 2), Transformer: sizeTransformer, Align: text.AlignRight, AlignHeader: text.AlignRight},
{Number: 3, Hidden: !inColumns(opts.Columns, 3), Transformer: sizeTransformer, Align: text.AlignRight, AlignHeader: text.AlignRight},
{Number: 4, Hidden: !inColumns(opts.Columns, 4), Transformer: spaceTransformer, Align: text.AlignRight, AlignHeader: text.AlignRight},
{Number: 5, Hidden: !inColumns(opts.Columns, 5), Transformer: barTransformer, AlignHeader: text.AlignCenter},
{Number: 6, Hidden: !inColumns(opts.Columns, 6), Align: text.AlignRight, AlignHeader: text.AlignRight},
{Number: 7, Hidden: !inColumns(opts.Columns, 7), Align: text.AlignRight, AlignHeader: text.AlignRight},
{Number: 8, Hidden: !inColumns(opts.Columns, 8), Align: text.AlignRight, AlignHeader: text.AlignRight},
{Number: 9, Hidden: !inColumns(opts.Columns, 9), Transformer: barTransformer, AlignHeader: text.AlignCenter},
{Number: 10, Hidden: !inColumns(opts.Columns, 10), WidthMax: int(float64(twidth) * 0.2)},
{Number: 11, Hidden: !inColumns(opts.Columns, 11), WidthMax: int(float64(twidth) * 0.4)},
{Number: 12, Hidden: true}, // sortBy helper for size
{Number: 13, Hidden: true}, // sortBy helper for used
{Number: 14, Hidden: true}, // sortBy helper for avail
{Number: 15, Hidden: true}, // sortBy helper for usage
{Number: 16, Hidden: true}, // sortBy helper for inodes size
{Number: 17, Hidden: true}, // sortBy helper for inodes used
{Number: 18, Hidden: true}, // sortBy helper for inodes avail
{Number: 19, Hidden: true}, // sortBy helper for inodes usage
})
headers := table.Row{}
for _, v := range columns {
headers = append(headers, v.Name)
}
tab.AppendHeader(headers)
for _, v := range m {
// spew.Dump(v)
var usage, inodeUsage float64
if v.Total > 0 {
usage = float64(v.Used) / float64(v.Total)
if usage > 1.0 {
usage = 1.0
}
}
if v.Inodes > 0 {
inodeUsage = float64(v.InodesUsed) / float64(v.Inodes)
if inodeUsage > 1.0 {
inodeUsage = 1.0
}
}
tab.AppendRow([]interface{}{
termenv.String(v.Mountpoint).Foreground(theme.colorBlue), // mounted on
v.Total, // size
v.Used, // used
v.Free, // avail
usage, // use%
v.Inodes, // inodes
v.InodesUsed, // inodes used
v.InodesFree, // inodes avail
inodeUsage, // inodes use%
termenv.String(v.Fstype).Foreground(theme.colorGray), // type
termenv.String(v.Device).Foreground(theme.colorGray), // filesystem
v.Total, // size sorting helper
v.Used, // used sorting helper
v.Free, // avail sorting helper
usage, // use% sorting helper
v.Inodes, // inodes sorting helper
v.InodesUsed, // inodes used sorting helper
v.InodesFree, // inodes avail sorting helper
inodeUsage, // inodes use% sorting helper
})
}
if tab.Length() == 0 {
return
}
suffix := "device"
if tab.Length() > 1 {
suffix = "devices"
}
tab.SetTitle("%d %s %s", tab.Length(), title, suffix)
//tab.AppendFooter(table.Row{fmt.Sprintf("%d %s", tab.Length(), title)})
sortMode := table.Asc
if opts.SortBy >= 12 {
sortMode = table.AscNumeric
}
tab.SortBy([]table.SortBy{{Number: opts.SortBy, Mode: sortMode}})
tab.Render()
}
// sizeTransformer makes a size human-readable.
func sizeTransformer(val interface{}) string {
return sizeToString(val.(uint64))
}
// spaceTransformer makes a size human-readable and applies a color coding.
func spaceTransformer(val interface{}) string {
free := val.(uint64)
var s = termenv.String(sizeToString(free))
switch {
case free < 1<<30:
s = s.Foreground(theme.colorRed)
case free < 10*1<<30:
s = s.Foreground(theme.colorYellow)
default:
s = s.Foreground(theme.colorGreen)
}
return s.String()
}
// barTransformer transforms a percentage into a progress-bar.
func barTransformer(val interface{}) string {
usage := val.(float64)
s := termenv.String()
if usage > 0 {
if barWidth() > 0 {
bw := barWidth() - 2
s = termenv.String(fmt.Sprintf("[%s%s] %5.1f%%",
strings.Repeat("#", int(usage*float64(bw))),
strings.Repeat(".", bw-int(usage*float64(bw))),
usage*100,
))
} else {
s = termenv.String(fmt.Sprintf("%5.1f%%", usage*100))
}
}
// apply color to progress-bar
switch {
case usage >= 0.9:
s = s.Foreground(theme.colorRed)
case usage >= 0.5:
s = s.Foreground(theme.colorYellow)
default:
s = s.Foreground(theme.colorGreen)
}
return s.String()
}
// inColumns return true if the column with index i is in the slice of visible
// columns cols.
func inColumns(cols []int, i int) bool {
for _, v := range cols {
if v == i {
return true
}
}
return false
}
// barWidth returns the width of progress-bars for the given render width.
func barWidth() int {
switch {
case *width < 100:
return 0
case *width < 120:
return 12
default:
return 22
}
}
// tableWidth returns the required minimum table width for the given columns.
func tableWidth(cols []int, separators bool) int {
var sw int
if separators {
sw = 1
}
twidth := int(*width)
for i := 0; i < len(columns); i++ {
if inColumns(cols, i+1) {
twidth -= 2 + sw + columns[i].Width
}
}
return twidth
}
// sizeToString prettifies sizes.
func sizeToString(size uint64) (str string) {
b := float64(size)
switch {
case size >= 1<<60:
str = fmt.Sprintf("%.1fE", b/(1<<60))
case size >= 1<<50:
str = fmt.Sprintf("%.1fP", b/(1<<50))
case size >= 1<<40:
str = fmt.Sprintf("%.1fT", b/(1<<40))
case size >= 1<<30:
str = fmt.Sprintf("%.1fG", b/(1<<30))
case size >= 1<<20:
str = fmt.Sprintf("%.1fM", b/(1<<20))
case size >= 1<<10:
str = fmt.Sprintf("%.1fK", b/(1<<10))
default:
str = fmt.Sprintf("%dB", size)
}
return
}
// stringToColumn converts a column name to its index.
func stringToColumn(s string) (int, error) {
s = strings.ToLower(s)
for i, v := range columns {
if v.ID == s {
return i + 1, nil
}
}
return 0, fmt.Errorf("unknown column: %s (valid: %s)", s, strings.Join(columnIDs(), ", "))
}
// stringToSortIndex converts a column name to its sort index.
func stringToSortIndex(s string) (int, error) {
s = strings.ToLower(s)
for _, v := range columns {
if v.ID == s {
return v.SortIndex, nil
}
}
return 0, fmt.Errorf("unknown column: %s (valid: %s)", s, strings.Join(columnIDs(), ", "))
}
// columnsIDs returns a slice of all column IDs.
func columnIDs() []string {
s := make([]string, len(columns))
for i, v := range columns {
s[i] = v.ID
}
return s
}