Skip to content

Commit 9335e29

Browse files
committed
Add support for path arguments to filter output
1 parent 236e5d9 commit 9335e29

File tree

3 files changed

+95
-36
lines changed

3 files changed

+95
-36
lines changed

README.md

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -57,30 +57,24 @@ You can simply start duf without any command-line arguments:
5757

5858
duf
5959

60+
If you supply arguments, duf will only list specific devices & mount points:
61+
62+
duf /home /some/file
63+
6064
If you want to list everything (including pseudo, duplicate, inaccessible file systems):
6165

6266
duf --all
6367

64-
You can show only individual tables:
68+
You can show and hide specific tables:
6569

6670
duf --only local,network,fuse,special,loops,binds
67-
68-
You can also show only specific filesystems:
69-
70-
duf --only-fs tmpfs,vfat
71-
72-
You can hide individual tables:
73-
7471
duf --hide local,network,fuse,special,loops,binds
7572

76-
You can also hide specific filesystems:
73+
You can also show and hide specific filesystems:
7774

75+
duf --only-fs tmpfs,vfat
7876
duf --hide-fs tmpfs,vfat
7977

80-
List inode information instead of block usage:
81-
82-
duf --inodes
83-
8478
Sort the output:
8579

8680
duf --sort size
@@ -95,6 +89,10 @@ Show or hide specific columns:
9589
Valid keys are: `mountpoint`, `size`, `used`, `avail`, `usage`, `inodes`,
9690
`inodes_used`, `inodes_avail`, `inodes_usage`, `type`, `filesystem`.
9791

92+
List inode information instead of block usage:
93+
94+
duf --inodes
95+
9896
If duf doesn't detect your terminal's colors correctly, you can set a theme:
9997

10098
duf --theme light

filesystems.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,50 @@
11
package main
22

3+
import (
4+
"os"
5+
"path/filepath"
6+
"strings"
7+
)
8+
9+
func findMounts(mounts []Mount, path string) ([]Mount, error) {
10+
var err error
11+
path, err = filepath.Abs(path)
12+
if err != nil {
13+
return nil, err
14+
}
15+
16+
_, err = os.Stat(path)
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
var m []Mount
22+
for _, v := range mounts {
23+
if path == v.Device {
24+
return []Mount{v}, nil
25+
}
26+
27+
if strings.HasPrefix(path, v.Mountpoint) {
28+
var nm []Mount
29+
30+
// keep all entries that are as close or closer to the target
31+
for _, mv := range m {
32+
if len(mv.Mountpoint) >= len(v.Mountpoint) {
33+
nm = append(nm, mv)
34+
}
35+
}
36+
m = nm
37+
38+
// add entry only if we didn't already find something closer
39+
if len(nm) == 0 || len(v.Mountpoint) >= len(nm[0].Mountpoint) {
40+
m = append(m, v)
41+
}
42+
}
43+
}
44+
45+
return m, nil
46+
}
47+
348
func deviceType(m Mount) string {
449
if isNetworkFs(m) {
550
return networkDevice

main.go

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,24 @@ func main() {
129129
os.Exit(0)
130130
}
131131

132+
// read mount table
133+
m, warnings, err := mounts()
134+
if err != nil {
135+
fmt.Fprintln(os.Stderr, err)
136+
os.Exit(1)
137+
}
138+
139+
// print JSON
140+
if *jsonOutput {
141+
err := renderJSON(m)
142+
if err != nil {
143+
fmt.Fprintln(os.Stderr, err)
144+
}
145+
146+
return
147+
}
148+
132149
// validate theme
133-
var err error
134150
theme, err = loadTheme(*themeOpt)
135151
if err != nil {
136152
fmt.Fprintln(os.Stderr, err)
@@ -174,23 +190,21 @@ func main() {
174190
OnlyFilesystems: parseCommaSeparatedValues(*onlyFs),
175191
}
176192

177-
// detect terminal width
178-
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
179-
if isTerminal && *width == 0 {
180-
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
181-
if err == nil {
182-
*width = uint(w)
193+
// validate arguments
194+
if len(flag.Args()) > 0 {
195+
var mounts []Mount
196+
197+
for _, v := range flag.Args() {
198+
fm, err := findMounts(m, v)
199+
if err != nil {
200+
fmt.Println(err)
201+
os.Exit(1)
202+
}
203+
204+
mounts = append(mounts, fm...)
183205
}
184-
}
185-
if *width == 0 {
186-
*width = 80
187-
}
188206

189-
// read mount table
190-
m, warnings, err := mounts()
191-
if err != nil {
192-
fmt.Fprintln(os.Stderr, err)
193-
os.Exit(1)
207+
m = mounts
194208
}
195209

196210
// print out warnings
@@ -200,14 +214,16 @@ func main() {
200214
}
201215
}
202216

203-
// print JSON
204-
if *jsonOutput {
205-
err := renderJSON(m)
206-
if err != nil {
207-
fmt.Fprintln(os.Stderr, err)
217+
// detect terminal width
218+
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
219+
if isTerminal && *width == 0 {
220+
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
221+
if err == nil {
222+
*width = uint(w)
208223
}
209-
210-
return
224+
}
225+
if *width == 0 {
226+
*width = 80
211227
}
212228

213229
// print tables

0 commit comments

Comments
 (0)