From 2d957c4be01d1d81305f6dd70694aae647cf0192 Mon Sep 17 00:00:00 2001 From: Oliver Date: Thu, 21 Nov 2019 20:56:45 +0100 Subject: [PATCH] Added an "evaluate all rows" flag to Table. Resolves #345 --- table.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/table.go b/table.go index 54eb6a7..9d0db29 100644 --- a/table.go +++ b/table.go @@ -227,6 +227,10 @@ type Table struct { // The rightmost column in the data set. lastColumn int + // If true, when calculating the widths of the columns, all rows are evaluated + // instead of only the visible ones. + evaluateAllRows bool + // The number of fixed rows / columns. fixedRows, fixedColumns int @@ -383,6 +387,17 @@ func (t *Table) GetOffset() (row, column int) { return t.rowOffset, t.columnOffset } +// SetEvaluateAllRows sets a flag which determines the rows to be evaluated when +// calculating the widths of the table's columns. When false, only visible rows +// are evaluated. When true, all rows in the table are evaluated. +// +// Set this flag to true to avoid shifting column widths when the table is +// scrolled. (May be slower for large tables.) +func (t *Table) SetEvaluateAllRows(all bool) *Table { + t.evaluateAllRows = all + return t +} + // SetSelectedFunc sets a handler which is called whenever the user presses the // Enter key on a selected cell/row/column. The handler receives the position of // the selection and its cell contents. If entire rows are selected, the column @@ -637,14 +652,20 @@ func (t *Table) Draw(screen tcell.Screen) { // Determine the indices and widths of the columns and rows which fit on the // screen. var ( - columns, rows, widths []int - tableHeight, tableWidth int + columns, rows, allRows, widths []int + tableHeight, tableWidth int ) rowStep := 1 if t.borders { rowStep = 2 // With borders, every table row takes two screen rows. tableWidth = 1 // We start at the second character because of the left table border. } + if t.evaluateAllRows { + allRows = make([]int, len(t.cells)) + for row := range t.cells { + allRows[row] = row + } + } indexRow := func(row int) bool { // Determine if this row is visible, store its index. if tableHeight >= height { return false @@ -701,7 +722,11 @@ ColumnLoop: // What's this column's width (without expansion)? maxWidth := -1 expansion := 0 - for _, row := range rows { + evaluationRows := rows + if t.evaluateAllRows { + evaluationRows = allRows + } + for _, row := range evaluationRows { if cell := getCell(row, column); cell != nil { _, _, _, _, _, _, cellWidth := decomposeString(cell.Text, true, false) if cell.MaxWidth > 0 && cell.MaxWidth < cellWidth {