Skip to content

Commit

Permalink
Add support for path arguments to filter output
Browse files Browse the repository at this point in the history
  • Loading branch information
muesli committed Nov 1, 2020
1 parent 236e5d9 commit 9335e29
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 36 deletions.
24 changes: 11 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,30 +57,24 @@ You can simply start duf without any command-line arguments:

duf

If you supply arguments, duf will only list specific devices & mount points:

duf /home /some/file

If you want to list everything (including pseudo, duplicate, inaccessible file systems):

duf --all

You can show only individual tables:
You can show and hide specific tables:

duf --only local,network,fuse,special,loops,binds

You can also show only specific filesystems:

duf --only-fs tmpfs,vfat

You can hide individual tables:

duf --hide local,network,fuse,special,loops,binds

You can also hide specific filesystems:
You can also show and hide specific filesystems:

duf --only-fs tmpfs,vfat
duf --hide-fs tmpfs,vfat

List inode information instead of block usage:

duf --inodes

Sort the output:

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

List inode information instead of block usage:

duf --inodes

If duf doesn't detect your terminal's colors correctly, you can set a theme:

duf --theme light
Expand Down
45 changes: 45 additions & 0 deletions filesystems.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,50 @@
package main

import (
"os"
"path/filepath"
"strings"
)

func findMounts(mounts []Mount, path string) ([]Mount, error) {
var err error
path, err = filepath.Abs(path)
if err != nil {
return nil, err
}

_, err = os.Stat(path)
if err != nil {
return nil, err
}

var m []Mount
for _, v := range mounts {
if path == v.Device {
return []Mount{v}, nil
}

if strings.HasPrefix(path, v.Mountpoint) {
var nm []Mount

// keep all entries that are as close or closer to the target
for _, mv := range m {
if len(mv.Mountpoint) >= len(v.Mountpoint) {
nm = append(nm, mv)
}
}
m = nm

// add entry only if we didn't already find something closer
if len(nm) == 0 || len(v.Mountpoint) >= len(nm[0].Mountpoint) {
m = append(m, v)
}
}
}

return m, nil
}

func deviceType(m Mount) string {
if isNetworkFs(m) {
return networkDevice
Expand Down
62 changes: 39 additions & 23 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,24 @@ func main() {
os.Exit(0)
}

// read mount table
m, warnings, err := mounts()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

// print JSON
if *jsonOutput {
err := renderJSON(m)
if err != nil {
fmt.Fprintln(os.Stderr, err)
}

return
}

// validate theme
var err error
theme, err = loadTheme(*themeOpt)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down Expand Up @@ -174,23 +190,21 @@ func main() {
OnlyFilesystems: parseCommaSeparatedValues(*onlyFs),
}

// detect terminal width
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
if isTerminal && *width == 0 {
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err == nil {
*width = uint(w)
// validate arguments
if len(flag.Args()) > 0 {
var mounts []Mount

for _, v := range flag.Args() {
fm, err := findMounts(m, v)
if err != nil {
fmt.Println(err)
os.Exit(1)
}

mounts = append(mounts, fm...)
}
}
if *width == 0 {
*width = 80
}

// read mount table
m, warnings, err := mounts()
if err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
m = mounts
}

// print out warnings
Expand All @@ -200,14 +214,16 @@ func main() {
}
}

// print JSON
if *jsonOutput {
err := renderJSON(m)
if err != nil {
fmt.Fprintln(os.Stderr, err)
// detect terminal width
isTerminal := terminal.IsTerminal(int(os.Stdout.Fd()))
if isTerminal && *width == 0 {
w, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err == nil {
*width = uint(w)
}

return
}
if *width == 0 {
*width = 80
}

// print tables
Expand Down

0 comments on commit 9335e29

Please sign in to comment.