Skip to content

Commit

Permalink
[fmtutil/table] Improve separator rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Feb 7, 2023
1 parent 7b6ab80 commit fab38b1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
## Changelog

### 12.59.0

* `[fmtutil/table]` Improved separator rendering

### 12.58.0

* `[system]` Added system arch name to `SystemInfo`
Expand Down
3 changes: 3 additions & 0 deletions fmtutil/table/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func ExampleNewTable() {
t.Add(1, "{g}Bob{!}", 1.42)
t.Add(2, "John", 73.1)
t.Add(3, "Mary", 2.29)
t.Separator()
t.Add(4, "Bob", 6.6)
t.Add(5, "Matilda", 0.0)

t.Render()
}
39 changes: 31 additions & 8 deletions fmtutil/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ const (

// ////////////////////////////////////////////////////////////////////////////////// //

// _SEPARATOR_TAG is tag used for rendering separator
const _SEPARATOR_TAG = "[@SEPARATOR@]"

// ////////////////////////////////////////////////////////////////////////////////// //

// Table is struct which can be used for table rendering
type Table struct {
Sizes []int // Custom columns sizes
Expand Down Expand Up @@ -153,12 +158,12 @@ func (t *Table) Separator() *Table {
return nil
}

if t.separator == "" {
t.separator = strings.Repeat(SeparatorSymbol, getSeparatorSize(t))
if !t.HasData() {
renderSeparator(t)
} else {
t.Add(_SEPARATOR_TAG)
}

fmtc.Println("{s}" + t.separator + "{!}")

return t
}

Expand All @@ -185,7 +190,7 @@ func (t *Table) Render() *Table {
prepareRender(t)

if len(t.Headers) == 0 {
t.Separator()
renderSeparator(t)
}

if t.data != nil {
Expand Down Expand Up @@ -222,7 +227,7 @@ func renderHeaders(t *Table) {
return
}

t.Separator()
renderSeparator(t)

totalHeaders := len(t.Headers)
totalColumns := len(t.columnSizes)
Expand All @@ -249,18 +254,23 @@ func renderHeaders(t *Table) {
}
}

t.Separator()
renderSeparator(t)
}

// renderData render table data
func renderData(t *Table) {
totalColumns := len(t.columnSizes)

for _, rowData := range t.data {
if rowData[0] == _SEPARATOR_TAG {
renderSeparator(t)
continue
}

renderRowData(t, rowData, totalColumns)
}

t.Separator()
renderSeparator(t)
}

// renderRowData render data in row
Expand Down Expand Up @@ -288,6 +298,15 @@ func renderRowData(t *Table, rowData []string, totalColumns int) {
fmtc.NewLine()
}

// renderSeparator prints separator
func renderSeparator(t *Table) {
if t.separator == "" {
t.separator = strings.Repeat(SeparatorSymbol, getSeparatorSize(t))
}

fmtc.Println("{s}" + t.separator + "{!}")
}

// convertSlice convert slice with interface{} to slice with strings
func convertSlice(data []interface{}) []string {
var result []string
Expand Down Expand Up @@ -315,6 +334,10 @@ func calculateColumnSizes(t *Table) {
if len(t.data) > 0 {
for _, row := range t.data {
for index, item := range row {
if item == _SEPARATOR_TAG {
continue
}

itemSizes := strutil.Len(fmtc.Clean(item))

if itemSizes > t.columnSizes[index] {
Expand Down
7 changes: 7 additions & 0 deletions fmtutil/table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,15 @@ func (s *TableSuite) TestSeparator(c *C) {
c.Assert(t.Separator(), IsNil)

t = NewTable()
c.Assert(t.Separator(), NotNil)

t = NewTable()
c.Assert(t.Add(1), NotNil)
c.Assert(t.Add(2), NotNil)
c.Assert(t.Separator(), NotNil)
c.Assert(t.Add(3), NotNil)
c.Assert(t.Add(4), NotNil)
t.Render()
}

func (s *TableSuite) TestRender(c *C) {
Expand Down

0 comments on commit fab38b1

Please sign in to comment.