From a6e296dca80ae56fb4b7a3a04897316f7adda3b5 Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Wed, 18 Nov 2020 15:08:31 +0100 Subject: [PATCH] Fixed odd width of tables in command outputs Fix #1040 --- table/table.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/table/table.go b/table/table.go index 0fbfbbf0f4a..278077f623f 100644 --- a/table/table.go +++ b/table/table.go @@ -80,6 +80,7 @@ func (t *Table) Render() string { average := make([]int, t.columnsCount) widths := make([]int, t.columnsCount) count := make([]int, t.columnsCount) + minimum := make([]int, t.columnsCount) for _, row := range t.rows { for x, cell := range row.cells { l := cell.Len() @@ -98,6 +99,15 @@ func (t *Table) Render() string { average[x] = average[x] / count[x] } } + // table headers will dictate the absolute min width + for x := range minimum { + if t.hasHeader { + minimum[x] = t.rows[0].cells[x].Len() + } else { + minimum[x] = 1 + } + } + variance := make([]int, t.columnsCount) for _, row := range t.rows { for x, cell := range row.cells { @@ -128,6 +138,9 @@ func (t *Table) Render() string { selectedWidth = average[x] + variance[x]*3 } } + if selectedWidth < minimum[x] { + selectedWidth = minimum[x] + } res += separator res += cell.Pad(selectedWidth) separator = " "