Skip to content

Commit

Permalink
table: support paging tables by number of lines (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
jedib0t committed May 23, 2018
1 parent 2cbe4b7 commit b28165e
Show file tree
Hide file tree
Showing 10 changed files with 341 additions and 108 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Pretty-print tables into ASCII/Unicode strings.
- Add Header(s) and Footer(s)
- Auto Index Rows (1, 2, 3 ...) and Columns (A, B, C, ...)
- Limit the length of the Rows; limit the length of individual Columns
- Page results by a specified number of Lines
- Alignment - Horizontal & Vertical
- Auto (horizontal) Align (numeric columns are aligned Right)
- Custom (horizontal) Align per column
Expand Down
25 changes: 25 additions & 0 deletions cmd/demo-table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,31 @@ Table with a Multi-line Row with VAlign and changed Align.
+-----+------------+-----------+--------+-----------------------------+
Starting afresh with a Simple Table again.
+-----+------------+-----------+--------+-----------------------------+
| # | FIRST NAME | LAST NAME | SALARY | |
+-----+------------+-----------+--------+-----------------------------+
| 1 | Arya | Stark | 3000 | |
+-----+------------+-----------+--------+-----------------------------+
| | | TOTAL | 10000 | |
+-----+------------+-----------+--------+-----------------------------+
... page break ...
+-----+------------+-----------+--------+-----------------------------+
| # | FIRST NAME | LAST NAME | SALARY | |
+-----+------------+-----------+--------+-----------------------------+
| 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! |
+-----+------------+-----------+--------+-----------------------------+
| | | TOTAL | 10000 | |
+-----+------------+-----------+--------+-----------------------------+
... page break ...
+-----+------------+-----------+--------+-----------------------------+
| # | FIRST NAME | LAST NAME | SALARY | |
+-----+------------+-----------+--------+-----------------------------+
| 300 | Tyrion | Lannister | 5000 | |
+-----+------------+-----------+--------+-----------------------------+
| | | TOTAL | 10000 | |
+-----+------------+-----------+--------+-----------------------------+
Table with a PageSize of 1.
+-----+------------+-----------+--------+------- ~
| # | FIRST NAME | LAST NAME | SALARY | ~
+-----+------------+-----------+--------+------- ~
Expand Down
34 changes: 34 additions & 0 deletions cmd/demo-table/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,40 @@ func demoTableFeatures() {
//Starting afresh with a Simple Table again.
//==========================================================================

//==========================================================================
// Does it support paging?
//==========================================================================
t.SetPageSize(1)
t.Style().Box.PageSeparator = "\n... page break ..."
t.SetCaption("Table with a PageSize of 1.\n")
fmt.Println(t.Render())
//+-----+------------+-----------+--------+-----------------------------+
//| # | FIRST NAME | LAST NAME | SALARY | |
//+-----+------------+-----------+--------+-----------------------------+
//| 1 | Arya | Stark | 3000 | |
//+-----+------------+-----------+--------+-----------------------------+
//| | | TOTAL | 10000 | |
//+-----+------------+-----------+--------+-----------------------------+
//... page break ...
//+-----+------------+-----------+--------+-----------------------------+
//| # | FIRST NAME | LAST NAME | SALARY | |
//+-----+------------+-----------+--------+-----------------------------+
//| 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! |
//+-----+------------+-----------+--------+-----------------------------+
//| | | TOTAL | 10000 | |
//+-----+------------+-----------+--------+-----------------------------+
//... page break ...
//+-----+------------+-----------+--------+-----------------------------+
//| # | FIRST NAME | LAST NAME | SALARY | |
//+-----+------------+-----------+--------+-----------------------------+
//| 300 | Tyrion | Lannister | 5000 | |
//+-----+------------+-----------+--------+-----------------------------+
//| | | TOTAL | 10000 | |
//+-----+------------+-----------+--------+-----------------------------+
//Table with a PageSize of 1.
t.SetPageSize(0) // disables paging
//==========================================================================

//==========================================================================
// How about limiting the length of every Row?
//==========================================================================
Expand Down
36 changes: 36 additions & 0 deletions table/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Pretty-print tables into ASCII/Unicode strings.
- Add Header(s) and Footer(s)
- Auto Index Rows (1, 2, 3 ...) and Columns (A, B, C, ...)
- Limit the length of the Rows; limit the length of individual Columns
- Page results by a specified number of Lines
- Alignment - Horizontal & Vertical
- Auto (horizontal) Align (numeric columns are aligned Right)
- Custom (horizontal) Align per column
Expand Down Expand Up @@ -174,6 +175,41 @@ Or you can use one of the ready-to-use Styles, and just make a few tweaks:
t.Style().Options.DrawBorder = false
```

## Paging

You can limit then number of lines rendered in a single "Page". This logic
can handle rows with multiple lines too. Here is a simple example:
```go
t.SetPageSize(1)
t.Render()
```
to get:
```
+-----+------------+-----------+--------+-----------------------------+
| # | FIRST NAME | LAST NAME | SALARY | |
+-----+------------+-----------+--------+-----------------------------+
| 1 | Arya | Stark | 3000 | |
+-----+------------+-----------+--------+-----------------------------+
| | | TOTAL | 10000 | |
+-----+------------+-----------+--------+-----------------------------+
+-----+------------+-----------+--------+-----------------------------+
| # | FIRST NAME | LAST NAME | SALARY | |
+-----+------------+-----------+--------+-----------------------------+
| 20 | Jon | Snow | 2000 | You know nothing, Jon Snow! |
+-----+------------+-----------+--------+-----------------------------+
| | | TOTAL | 10000 | |
+-----+------------+-----------+--------+-----------------------------+
+-----+------------+-----------+--------+-----------------------------+
| # | FIRST NAME | LAST NAME | SALARY | |
+-----+------------+-----------+--------+-----------------------------+
| 300 | Tyrion | Lannister | 5000 | |
+-----+------------+-----------+--------+-----------------------------+
| | | TOTAL | 10000 | |
+-----+------------+-----------+--------+-----------------------------+
```

## Wrapping (or) Row/Column Width restrictions

You can restrict the maximum (text) width for a Row:
Expand Down

0 comments on commit b28165e

Please sign in to comment.