Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do some effort to maximize the width of the table #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 24 additions & 4 deletions table.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,30 @@ func printTable(title string, m []Mount, opts TableOptions) {
columns[4].Width = barWidth() + 7
columns[8].Width = barWidth() + 7
}
twidth := tableWidth(opts.Columns, tab.Style().Options.SeparateColumns)

twidthInt := int(*width) - tableWidth(opts.Columns, tab.Style().Options.SeparateColumns)
twidth := float64(twidthInt)

// colMaxWidths is a mapping from percentage to maximum number of columns available for that percentage.
colMaxWidths := map[int]int{
20: int(twidth * 0.2),
40: int(twidth * 0.4),
}
if diff := twidthInt - (colMaxWidths[20] + 2*colMaxWidths[40]); diff > 0 {
switch diff {
case 1:
colMaxWidths[20]++
case 2:
colMaxWidths[40]++
default:
if *warns {
fmt.Fprintf(os.Stderr, "diff > 2 (%v), not adjusting maximum column widths", diff)
}
}
}

tab.SetColumnConfigs([]table.ColumnConfig{
{Number: 1, Hidden: !inColumns(opts.Columns, 1), WidthMax: int(float64(twidth) * 0.4)},
{Number: 1, Hidden: !inColumns(opts.Columns, 1), WidthMax: colMaxWidths[40]},
{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},
Expand All @@ -65,8 +85,8 @@ func printTable(title string, m []Mount, opts TableOptions) {
{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: 10, Hidden: !inColumns(opts.Columns, 10), WidthMax: colMaxWidths[20]},
{Number: 11, Hidden: !inColumns(opts.Columns, 11), WidthMax: colMaxWidths[40]},
{Number: 12, Hidden: true}, // sortBy helper for size
{Number: 13, Hidden: true}, // sortBy helper for used
{Number: 14, Hidden: true}, // sortBy helper for avail
Expand Down