Skip to content

Commit

Permalink
table: fix title auto-sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed Oct 5, 2024
1 parent 8216ef0 commit d36a459
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
32 changes: 32 additions & 0 deletions cmd/foo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"fmt"
"os"

"github.com/jedib0t/go-pretty/v6/table"
"golang.org/x/term"
)

func main() {
w, h, err := term.GetSize(int(os.Stdin.Fd()))
if err != nil {
panic(err.Error())
}
fmt.Println(w, h)

tw := table.NewWriter()
tw.SetTitle("Title")
tw.AppendHeader(table.Row{"#", "First Name", "Last Name", "Salary"})
tw.AppendRows([]table.Row{
{1, "Arya", "Stark", 3000},
{20, "Jon", "Snow", 2000, "You know nothing, Jon Snow!"},
{300, "Tyrion", "Lannister", 5000},
})
tw.AppendFooter(table.Row{"", "", "Total", 10000})
tw.SetStyle(table.StyleLight)
tw.Style().Size = table.SizeOptions{
WidthMin: w,
}
fmt.Println(tw.Render())
}
13 changes: 8 additions & 5 deletions table/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func (t *Table) renderLine(out *strings.Builder, row rowStr, hint renderHint) {

// use a brand-new strings.Builder if a row length limit has been set
var outLine *strings.Builder
if t.getRowWidthMax() > 0 {
if t.style.Size.WidthMax > 0 {
outLine = &strings.Builder{}
} else {
outLine = out
Expand Down Expand Up @@ -227,8 +227,8 @@ func (t *Table) renderLine(out *strings.Builder, row rowStr, hint renderHint) {

func (t *Table) renderLineMergeOutputs(out *strings.Builder, outLine *strings.Builder) {
outLineStr := outLine.String()
if text.RuneWidthWithoutEscSequences(outLineStr) > t.getRowWidthMax() {
trimLength := t.getRowWidthMax() - utf8.RuneCountInString(t.style.Box.UnfinishedRow)
if text.RuneWidthWithoutEscSequences(outLineStr) > t.style.Size.WidthMax {
trimLength := t.style.Size.WidthMax - utf8.RuneCountInString(t.style.Box.UnfinishedRow)
if trimLength > 0 {
out.WriteString(text.Trim(outLineStr, trimLength))
out.WriteString(t.style.Box.UnfinishedRow)
Expand Down Expand Up @@ -385,8 +385,11 @@ func (t *Table) renderTitle(out *strings.Builder) {
colors := t.style.Title.Colors
colorsBorder := t.getBorderColors(renderHint{isTitleRow: true})
rowLength := t.maxRowLength
if t.getRowWidthMax() != 0 && t.getRowWidthMax() < rowLength {
rowLength = t.getRowWidthMax()
if wm := t.style.Size.WidthMax; wm > 0 && wm < rowLength {
rowLength = wm
}
if wm := t.style.Size.WidthMin; wm > 0 && wm > rowLength {
rowLength = wm
}
if t.style.Options.DrawBorder {
lenBorder := rowLength - text.RuneWidthWithoutEscSequences(t.style.Box.TopLeft+t.style.Box.TopRight)
Expand Down
8 changes: 1 addition & 7 deletions table/render_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,16 +194,10 @@ func (t *Table) initForRenderMaxRowLength() {
if t.style.Options.DrawBorder {
t.maxRowLength += text.RuneWidthWithoutEscSequences(t.style.Box.Left + t.style.Box.Right)
}

}

func (t *Table) initForRenderPaddedColumns() {
minWidth := t.getRowWidthMin()
if minWidth == 0 || t.maxRowLength >= minWidth {
return
}

paddingSize := minWidth - t.maxRowLength
paddingSize := t.style.Size.WidthMin - t.maxRowLength
for paddingSize > 0 {
// distribute padding equally among all columns
numColumnsPadded := 0
Expand Down
6 changes: 5 additions & 1 deletion table/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1370,12 +1370,15 @@ func TestTable_Render_AutoWidth(t *testing.T) {
β”‚ β”‚ β”‚ TOTAL β”‚ 10000 β”‚ β”‚
β””β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜`)

tw.SetTitle("Game of Thrones")
tw.Style().Size = SizeOptions{
WidthMax: 0,
WidthMin: 100,
}
compareOutput(t, tw.Render(), `
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚ Game of Thrones β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ # β”‚ FIRST NAME β”‚ LAST NAME β”‚ SALARY β”‚ β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1 β”‚ Arya β”‚ Stark β”‚ 3000 β”‚ β”‚
Expand All @@ -1385,6 +1388,7 @@ func TestTable_Render_AutoWidth(t *testing.T) {
β”‚ β”‚ β”‚ TOTAL β”‚ 10000 β”‚ β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜`)

tw.SetTitle("")
tw.Style().Size = SizeOptions{
WidthMax: 0,
WidthMin: 120,
Expand Down
16 changes: 5 additions & 11 deletions table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,11 @@ func (t *Table) Style() *Style {
tempStyle := StyleDefault
t.style = &tempStyle
}
// override WidthMax with allowedRowLength until allowedRowLength is
// removed from code
if t.allowedRowLength > 0 {
t.style.Size.WidthMax = t.allowedRowLength
}
return t.style
}

Expand Down Expand Up @@ -636,17 +641,6 @@ func (t *Table) getRowConfig(hint renderHint) RowConfig {
}
}

func (t *Table) getRowWidthMax() int {
if t.allowedRowLength > 0 {
return t.allowedRowLength
}
return t.Style().Size.WidthMax
}

func (t *Table) getRowWidthMin() int {
return t.Style().Size.WidthMin
}

func (t *Table) getSeparatorColors(hint renderHint) text.Colors {
if t.style.Options.DoNotColorBordersAndSeparators {
return nil
Expand Down

0 comments on commit d36a459

Please # to comment.