From a934652b351d90c415a158dc8e0c097ebd900347 Mon Sep 17 00:00:00 2001 From: Dominik Menke Date: Thu, 1 Mar 2018 20:11:23 +0100 Subject: [PATCH] reworked calculation for expandable column width This also fixes two bugs: 1. the background for selected cells/colums now matches the actual column 2. the remaining space was off by one, when the table has no border --- table.go | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/table.go b/table.go index f85d0b1..92cf897 100644 --- a/table.go +++ b/table.go @@ -601,18 +601,26 @@ ColumnLoop: screen.SetContent(x+colX, y+rowY, ch, nil, borderStyle) } + // Helper function which returns the width for a column. If there's enough + // horizontal space, and the given index matches the expandableColumn, + // this will increase a columns width by remaining horizontal space. + expandX := width - tableWidth + colWidth := func(columnIndex int) (w int) { + w = widths[columnIndex] + if t.expandableColumn == columnIndex && tableWidth < width { + w += expandX + } + return w + } + // Draw the cells (and borders). var columnX int if !t.borders { + expandX++ columnX-- } for columnIndex, column := range columns { - columnWidth := widths[columnIndex] - - // Consume remaining horizontal space. - if t.expandableColumn == columnIndex && tableWidth < width { - columnWidth += width - tableWidth - } + columnWidth := colWidth(columnIndex) for rowY, row := range rows { if t.borders { @@ -732,7 +740,7 @@ ColumnLoop: columnX := 0 rowSelected := t.rowsSelectable && !t.columnsSelectable && row == t.selectedRow for columnIndex, column := range columns { - columnWidth := widths[columnIndex] + columnWidth := colWidth(columnIndex) cell := getCell(row, column) if cell == nil { continue