Skip to content

Commit f73b053

Browse files
committed
Show or hide specific columns with --output
1 parent 02d3bac commit f73b053

File tree

6 files changed

+281
-187
lines changed

6 files changed

+281
-187
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ Sort the output:
6060
Valid keys are: `mountpoint`, `size`, `used`, `avail`, `usage`, `inodes`,
6161
`inodes_used`, `inodes_avail`, `inodes_usage`, `type`, `filesystem`.
6262

63+
Show or hide specific columns:
64+
65+
duf --output mountpoint,size,usage
66+
67+
Valid keys are: `mountpoint`, `size`, `used`, `avail`, `usage`, `inodes`,
68+
`inodes_used`, `inodes_avail`, `inodes_usage`, `type`, `filesystem`.
69+
6370
If you prefer your output as JSON:
6471

6572
duf --json

main.go

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,15 @@ var (
2222
hideLoops = flag.Bool("hide-loops", true, "hide loop devices")
2323
hideBinds = flag.Bool("hide-binds", true, "hide bind mounts")
2424

25-
sortBy = flag.String("sort", "mountpoint", "sort output by: mountpoint, size, used, avail, usage, type, filesystem")
25+
output = flag.String("output", "", "output fields: "+strings.Join(columnIDs(), ", "))
26+
sortBy = flag.String("sort", "mountpoint", "sort output by: "+strings.Join(columnIDs(), ", "))
2627
width = flag.Uint("width", 0, "max output width")
2728

2829
inodes = flag.Bool("inodes", false, "list inode information instead of block usage")
2930
jsonOutput = flag.Bool("json", false, "output all devices in JSON format")
3031
)
3132

32-
func renderTables(m []Mount, sortCol int) {
33+
func renderTables(m []Mount, sortCol int) error {
3334
var local, network, fuse, special []Mount
3435

3536
// sort/filter devices
@@ -71,19 +72,33 @@ func renderTables(m []Mount, sortCol int) {
7172
local = append(local, v)
7273
}
7374

75+
columns, err := parseColumns(*output)
76+
if err != nil {
77+
return err
78+
}
79+
if len(columns) == 0 {
80+
// no columns supplied, use defaults
81+
if *inodes {
82+
columns = []int{1, 6, 7, 8, 9, 10, 11}
83+
} else {
84+
columns = []int{1, 2, 3, 4, 5, 10, 11}
85+
}
86+
}
87+
7488
// print tables
7589
if !*hideLocal || *all {
76-
printTable("local", local, sortCol, *inodes)
90+
printTable("local", local, sortCol, columns)
7791
}
7892
if !*hideNetwork || *all {
79-
printTable("network", network, sortCol, *inodes)
93+
printTable("network", network, sortCol, columns)
8094
}
8195
if !*hideFuse || *all {
82-
printTable("FUSE", fuse, sortCol, *inodes)
96+
printTable("FUSE", fuse, sortCol, columns)
8397
}
8498
if !*hideSpecial || *all {
85-
printTable("special", special, sortCol, *inodes)
99+
printTable("special", special, sortCol, columns)
86100
}
101+
return nil
87102
}
88103

89104
func renderJSON(m []Mount) error {
@@ -96,12 +111,33 @@ func renderJSON(m []Mount) error {
96111
return nil
97112
}
98113

114+
func parseColumns(cols string) ([]int, error) {
115+
var i []int
116+
117+
s := strings.Split(cols, ",")
118+
for _, v := range s {
119+
v = strings.TrimSpace(v)
120+
if len(v) == 0 {
121+
continue
122+
}
123+
124+
col, err := stringToColumn(v)
125+
if err != nil {
126+
return nil, err
127+
}
128+
129+
i = append(i, col)
130+
}
131+
132+
return i, nil
133+
}
134+
99135
func main() {
100136
flag.Parse()
101137

102-
sortCol, err := stringToColumn(*sortBy)
138+
sortCol, err := stringToSortIndex(*sortBy)
103139
if err != nil {
104-
fmt.Fprintln(os.Stderr, "unknown sort key, valid values: mountpoint, size, used, avail, usage, type, filesystem")
140+
fmt.Fprintln(os.Stderr, err)
105141
os.Exit(1)
106142
}
107143

@@ -137,5 +173,8 @@ func main() {
137173
return
138174
}
139175

140-
renderTables(m, sortCol)
176+
err = renderTables(m, sortCol)
177+
if err != nil {
178+
fmt.Fprintln(os.Stderr, err)
179+
}
141180
}

mounts.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,19 @@ import (
99
)
1010

1111
type Mount struct {
12-
Device string `json:"device"`
13-
DeviceType string `json:"device_type"`
14-
Mountpoint string `json:"mount_point"`
15-
Fstype string `json:"fs_type"`
16-
Type string `json:"type"`
17-
Opts string `json:"opts"`
18-
Total uint64 `json:"total"`
19-
Free uint64 `json:"free"`
20-
Used uint64 `json:"used"`
21-
InodesTotal uint64 `json:"inodes_total"`
22-
InodesFree uint64 `json:"inodes_free"`
23-
InodesUsed uint64 `json:"inodes_used"`
24-
Stat unix.Statfs_t `json:"-"`
12+
Device string `json:"device"`
13+
DeviceType string `json:"device_type"`
14+
Mountpoint string `json:"mount_point"`
15+
Fstype string `json:"fs_type"`
16+
Type string `json:"type"`
17+
Opts string `json:"opts"`
18+
Total uint64 `json:"total"`
19+
Free uint64 `json:"free"`
20+
Used uint64 `json:"used"`
21+
Inodes uint64 `json:"inodes"`
22+
InodesFree uint64 `json:"inodes_free"`
23+
InodesUsed uint64 `json:"inodes_used"`
24+
Stat unix.Statfs_t `json:"-"`
2525
}
2626

2727
func readLines(filename string) ([]string, error) {

mounts_darwin.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,18 @@ func mounts() ([]Mount, []string, error) {
6767
}
6868

6969
d := Mount{
70-
Device: device,
71-
Mountpoint: mountPoint,
72-
Fstype: fsType,
73-
Type: fsType,
74-
Opts: opts,
75-
Stat: stat,
76-
Total: (uint64(stat.Blocks) * uint64(stat.Bsize)),
77-
Free: (uint64(stat.Bavail) * uint64(stat.Bsize)),
78-
Used: (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(stat.Bsize),
79-
InodesTotal: stat.Files,
80-
InodesFree: stat.Ffree,
81-
InodesUsed: stat.Files - stat.Ffree,
70+
Device: device,
71+
Mountpoint: mountPoint,
72+
Fstype: fsType,
73+
Type: fsType,
74+
Opts: opts,
75+
Stat: stat,
76+
Total: (uint64(stat.Blocks) * uint64(stat.Bsize)),
77+
Free: (uint64(stat.Bavail) * uint64(stat.Bsize)),
78+
Used: (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(stat.Bsize),
79+
Inodes: stat.Files,
80+
InodesFree: stat.Ffree,
81+
InodesUsed: stat.Files - stat.Ffree,
8282
}
8383
d.DeviceType = deviceType(d)
8484

mounts_linux.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,18 @@ func mounts() ([]Mount, []string, error) {
5454
}
5555

5656
d := Mount{
57-
Device: device,
58-
Mountpoint: mountPoint,
59-
Fstype: fstype,
60-
Type: fsTypeMap[int64(stat.Type)],
61-
Opts: mountOpts,
62-
Stat: stat,
63-
Total: (uint64(stat.Blocks) * uint64(stat.Bsize)),
64-
Free: (uint64(stat.Bavail) * uint64(stat.Bsize)),
65-
Used: (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(stat.Bsize),
66-
InodesTotal: stat.Files,
67-
InodesFree: stat.Ffree,
68-
InodesUsed: stat.Files - stat.Ffree,
57+
Device: device,
58+
Mountpoint: mountPoint,
59+
Fstype: fstype,
60+
Type: fsTypeMap[int64(stat.Type)],
61+
Opts: mountOpts,
62+
Stat: stat,
63+
Total: (uint64(stat.Blocks) * uint64(stat.Bsize)),
64+
Free: (uint64(stat.Bavail) * uint64(stat.Bsize)),
65+
Used: (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(stat.Bsize),
66+
Inodes: stat.Files,
67+
InodesFree: stat.Ffree,
68+
InodesUsed: stat.Files - stat.Ffree,
6969
}
7070
d.DeviceType = deviceType(d)
7171

0 commit comments

Comments
 (0)