Skip to content

Commit

Permalink
[fmtutil/table] Add automatic breaks feature
Browse files Browse the repository at this point in the history
  • Loading branch information
andyone committed Oct 29, 2024
1 parent 5dbeb81 commit 7dd7ba1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
16 changes: 16 additions & 0 deletions fmtutil/table/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type Table struct {
// Width is table maximum width
Width int

// Breaks is an interval for separators between given number of rows
Breaks int

// SeparatorSymbol is symbol used for borders rendering
BorderSymbol string

Expand Down Expand Up @@ -91,6 +94,9 @@ type Table struct {

// Slice with auto calculated sizes
columnSizes []int

// Cursor is number of the latest record
cursor int
}

// ////////////////////////////////////////////////////////////////////////////////// //
Expand All @@ -116,6 +122,9 @@ var SeparatorColorTag = "{s}"
// ColumnSeparatorSymbol is default column separator symbol
var ColumnSeparatorSymbol = "|"

// Breaks is an interval for separators between given number of rows
var Breaks = -1

// FullScreen is a flag for full-screen table by default
var FullScreen = true

Expand All @@ -126,6 +135,7 @@ func NewTable(headers ...string) *Table {
return &Table{
HeaderCapitalize: HeaderCapitalize,
FullScreen: FullScreen,
Breaks: Breaks,
Headers: headers,
Processor: convertSlice,
}
Expand Down Expand Up @@ -354,6 +364,10 @@ func renderData(t *Table) {

// renderRowData render data in row
func renderRowData(t *Table, data []string, totalColumns int) {
if t.Breaks > 0 && t.cursor > 0 && t.cursor%t.Breaks == 0 {
renderSeparator(t)
}

for columnIndex, columnData := range data {
if columnIndex == totalColumns {
break
Expand All @@ -377,6 +391,8 @@ func renderRowData(t *Table, data []string, totalColumns int) {
}
}

t.cursor++

fmtc.NewLine()
}

Expand Down
1 change: 1 addition & 0 deletions fmtutil/table/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (s *TableSuite) TestRender(c *C) {
t.BorderColorTag = "{b}"
t.SeparatorColorTag = "{g}"
t.HeaderCapitalize = true
t.Breaks = 2

c.Assert(t.Render(), NotNil)

Expand Down

0 comments on commit 7dd7ba1

Please sign in to comment.