From 76662a1ebda0f34595a5d13ddc3519f764a5cdf5 Mon Sep 17 00:00:00 2001 From: Ahmad Karlam Date: Wed, 12 May 2021 07:25:37 +0700 Subject: [PATCH 1/3] feat: text wrap on table --- widgets/table.go | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/widgets/table.go b/widgets/table.go index 05391ad5..ea88d0b7 100644 --- a/widgets/table.go +++ b/widgets/table.go @@ -6,6 +6,7 @@ package widgets import ( "image" + "strings" . "github.com/gizak/termui/v3" ) @@ -24,6 +25,7 @@ type Table struct { Rows [][]string ColumnWidths []int TextStyle Style + TextWrap bool RowSeparator bool TextAlignment Alignment RowStyles map[int]Style @@ -62,6 +64,7 @@ func (self *Table) Draw(buf *Buffer) { // draw rows for i := 0; i < len(self.Rows) && yCoordinate < self.Inner.Max.Y; i++ { row := self.Rows[i] + textWrap := false colXCoordinate := self.Inner.Min.X rowStyle := self.TextStyle @@ -77,6 +80,10 @@ func (self *Table) Draw(buf *Buffer) { // draw row cells for j := 0; j < len(row); j++ { + if len(row[j]) > columnWidths[j] && self.TextWrap { + row = self.transformForTextWrap(row, i, j, columnWidths[j]) + textWrap = true + } col := ParseStyles(row[j], rowStyle) // draw row cell if len(col) > columnWidths[j] || self.TextAlignment == AlignLeft { @@ -127,6 +134,9 @@ func (self *Table) Draw(buf *Buffer) { yCoordinate++ // draw horizontal separator + if textWrap { + continue + } horizontalCell := NewCell(HORIZONTAL_LINE, separatorStyle) if self.RowSeparator && yCoordinate < self.Inner.Max.Y && i != len(self.Rows)-1 { buf.Fill(horizontalCell, image.Rect(self.Inner.Min.X, yCoordinate, self.Inner.Max.X, yCoordinate+1)) @@ -134,3 +144,27 @@ func (self *Table) Draw(buf *Buffer) { } } } + +func (self *Table) transformForTextWrap(row []string, i, j, columnWidth int) []string { + words := strings.Split(row[j], " ") + newWords := []string{} + nextWords := []string{} + totalChar := 0 + for i := 0; i < len(words); i++ { + word := words[i] + if totalChar+len(word)+1 > columnWidth { + nextWords = words[i:] + break + } + + newWords = append(newWords, word) + totalChar += len(word) + 1 + } + row[j] = strings.Join(newWords, " ") + last := len(self.Rows) - 1 + self.Rows = append(self.Rows, self.Rows[last]) + copy(self.Rows[i+1:], self.Rows[i:last]) + self.Rows[i+1] = make([]string, len(self.Rows[i])) + self.Rows[i+1][j] = strings.Join(nextWords, " ") + return row +} From df7a6fc8593ded1b5f9f860f9de90ad764525501 Mon Sep 17 00:00:00 2001 From: Ahmad Karlam Date: Wed, 12 May 2021 18:58:20 -0700 Subject: [PATCH 2/3] docs: add comment --- widgets/table.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/widgets/table.go b/widgets/table.go index ea88d0b7..9fbf7c9d 100644 --- a/widgets/table.go +++ b/widgets/table.go @@ -146,6 +146,7 @@ func (self *Table) Draw(buf *Buffer) { } func (self *Table) transformForTextWrap(row []string, i, j, columnWidth int) []string { + // Split words by column width words := strings.Split(row[j], " ") newWords := []string{} nextWords := []string{} @@ -161,6 +162,8 @@ func (self *Table) transformForTextWrap(row []string, i, j, columnWidth int) []s totalChar += len(word) + 1 } row[j] = strings.Join(newWords, " ") + + // Insert new sentence last := len(self.Rows) - 1 self.Rows = append(self.Rows, self.Rows[last]) copy(self.Rows[i+1:], self.Rows[i:last]) From d03838471030c1303c2240a3e37a315b47ba177f Mon Sep 17 00:00:00 2001 From: Ahmad Karlam Date: Thu, 13 May 2021 22:29:20 +0700 Subject: [PATCH 3/3] refactor: rename function to textWrapTransformer --- widgets/table.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/widgets/table.go b/widgets/table.go index ea88d0b7..19fafa33 100644 --- a/widgets/table.go +++ b/widgets/table.go @@ -81,7 +81,7 @@ func (self *Table) Draw(buf *Buffer) { // draw row cells for j := 0; j < len(row); j++ { if len(row[j]) > columnWidths[j] && self.TextWrap { - row = self.transformForTextWrap(row, i, j, columnWidths[j]) + row = self.textWrapTransformer(row, i, j, columnWidths[j]) textWrap = true } col := ParseStyles(row[j], rowStyle) @@ -145,7 +145,7 @@ func (self *Table) Draw(buf *Buffer) { } } -func (self *Table) transformForTextWrap(row []string, i, j, columnWidth int) []string { +func (self *Table) textWrapTransformer(row []string, i, j, columnWidth int) []string { words := strings.Split(row[j], " ") newWords := []string{} nextWords := []string{}