Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions duf.1
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ style: unicode, ascii
\fB-theme\fP
color themes: dark, light, ansi
.TP
\fB-totals\fP
print size totals for every group, as well as grand totals
.TP
\fB-usage-threshold\fP
specifies the coloring threshold (yellow, red) of the usage bars as a floating point number from 0 to 1
.TP
Expand Down
14 changes: 14 additions & 0 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ func renderTables(m []Mount, filters FilterOptions, opts TableOptions) {
}

// print tables
var totalSize uint64
var totalUsed uint64
var totalAvail uint64
for _, devType := range groups {
mounts := deviceMounts[devType]

Expand All @@ -125,6 +128,17 @@ func renderTables(m []Mount, filters FilterOptions, opts TableOptions) {

if shouldPrint {
printTable(devType, mounts, opts)
if opts.Totals {
for _, v := range mounts {
totalSize += v.Total
totalUsed += v.Used
totalAvail += v.Free
}
}
}
}

if opts.Totals {
printTotals(totalSize, totalUsed, totalAvail, opts)
}
}
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ var (
sortBy = flag.String("sort", "mountpoint", "sort output by: "+strings.Join(columnIDs(), ", "))
width = flag.Uint("width", 0, "max output width")
themeOpt = flag.String("theme", defaultThemeName(), "color themes: dark, light, ansi")
totals = flag.Bool("totals", false, "print size totals for every group, as well as grand totals")
styleOpt = flag.String("style", defaultStyleName(), "style: unicode, ascii")

availThreshold = flag.String("avail-threshold", "10G,1G", "specifies the coloring threshold (yellow, red) of the avail column, must be integer with optional SI prefixes")
Expand Down Expand Up @@ -311,5 +312,6 @@ func main() {
Columns: columns,
SortBy: sortCol,
Style: style,
Totals: *totals,
})
}
51 changes: 51 additions & 0 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type TableOptions struct {
Columns []int
SortBy int
Style table.Style
Totals bool
}

// Column defines a column.
Expand All @@ -43,6 +44,38 @@ var columns = []Column{
{ID: "filesystem", Name: "Filesystem", SortIndex: 11},
}

// printTotals prints a small table with total sizes for all previous mounts.
func printTotals(totalSize uint64, totalUsed uint64, totalAvail uint64, opts TableOptions) {
tab := table.NewWriter()
tab.SetAllowedRowLength(int(*width))
tab.SetOutputMirror(os.Stdout)
tab.Style().Options.SeparateColumns = true
tab.SetStyle(opts.Style)

tab.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, WidthMin: len(columns[0].Name)},
{Number: 2, Transformer: sizeTransformer, Align: text.AlignRight, AlignHeader: text.AlignRight},
{Number: 3, Transformer: sizeTransformer, Align: text.AlignRight, AlignHeader: text.AlignRight},
{Number: 4, Transformer: spaceTransformer, Align: text.AlignRight, AlignHeader: text.AlignRight},
})

tab.SetTitle("grand totals")
tab.AppendHeader(table.Row{
"",
"Size",
"Used",
"Avail",
})
tab.AppendRow(table.Row{
"TOTAL",
totalSize,
totalUsed,
totalAvail,
})

tab.Render()
}

// printTable prints an individual table of mounts.
func printTable(title string, m []Mount, opts TableOptions) {
tab := table.NewWriter()
Expand Down Expand Up @@ -85,9 +118,19 @@ func printTable(title string, m []Mount, opts TableOptions) {
}
tab.AppendHeader(headers)

var totalSize uint64
var totalUsed uint64
var totalAvail uint64

for _, v := range m {
// spew.Dump(v)

if opts.Totals {
totalSize += v.Total
totalUsed += v.Used
totalAvail += v.Free
}

var usage, inodeUsage float64
if v.Total > 0 {
usage = float64(v.Used) / float64(v.Total)
Expand Down Expand Up @@ -136,6 +179,14 @@ func printTable(title string, m []Mount, opts TableOptions) {
tab.SetTitle("%d %s %s", tab.Length(), title, suffix)

// tab.AppendFooter(table.Row{fmt.Sprintf("%d %s", tab.Length(), title)})
if opts.Totals {
tab.AppendFooter(table.Row{
"TOTAL",
sizeToString(totalSize),
sizeToString(totalUsed),
sizeToString(totalAvail),
})
}
sortMode := table.Asc
if opts.SortBy >= 12 {
sortMode = table.AscNumeric
Expand Down