-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
table: Pager interface to page through output
- Loading branch information
Showing
7 changed files
with
229 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package table | ||
|
||
import ( | ||
"io" | ||
) | ||
|
||
// Pager lets you interact with the table rendering in a paged manner. | ||
type Pager interface { | ||
// GoTo moves to the given 1-indexed page number. | ||
GoTo(pageNum int) string | ||
// Location returns the current page number in 1-indexed form. | ||
Location() int | ||
// Next moves to the next available page and returns the same. | ||
Next() string | ||
// Prev moves to the previous available page and returns the same. | ||
Prev() string | ||
// Render returns the current page. | ||
Render() string | ||
// SetOutputMirror sets up the writer to which Render() will write the | ||
// output other than returning. | ||
SetOutputMirror(mirror io.Writer) | ||
} | ||
|
||
type pager struct { | ||
index int // 0-indexed | ||
pages []string | ||
outputMirror io.Writer | ||
size int | ||
} | ||
|
||
func (p *pager) GoTo(pageNum int) string { | ||
if pageNum < 1 { | ||
pageNum = 1 | ||
} | ||
if pageNum > len(p.pages) { | ||
pageNum = len(p.pages) | ||
} | ||
p.index = pageNum - 1 | ||
return p.pages[p.index] | ||
} | ||
|
||
func (p *pager) Location() int { | ||
return p.index + 1 | ||
} | ||
|
||
func (p *pager) Next() string { | ||
if p.index < len(p.pages)-1 { | ||
p.index++ | ||
} | ||
return p.pages[p.index] | ||
} | ||
|
||
func (p *pager) Prev() string { | ||
if p.index > 0 { | ||
p.index-- | ||
} | ||
return p.pages[p.index] | ||
} | ||
|
||
func (p *pager) Render() string { | ||
pageToWrite := p.pages[p.index] | ||
if p.outputMirror != nil { | ||
_, _ = p.outputMirror.Write([]byte(pageToWrite)) | ||
} | ||
return pageToWrite | ||
} | ||
|
||
func (p *pager) SetOutputMirror(mirror io.Writer) { | ||
p.outputMirror = mirror | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package table | ||
|
||
// PagerOption helps control Paging. | ||
type PagerOption func(t *Table) | ||
|
||
// PageSize sets the size of each page rendered. | ||
func PageSize(pageSize int) PagerOption { | ||
return func(t *Table) { | ||
t.pager.size = pageSize | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package table | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestPager(t *testing.T) { | ||
expectedOutput := `+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ | ||
| PASSENGERID | SURVIVED | PCLASS | NAME | SEX | AGE | SIBSP | PARCH | TICKET | FARE | CABIN | EMBARKED | | ||
+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ | ||
| 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22 | 1 | 0 | A/5 21171 | 7.25 | | S | | ||
| 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Thayer) | female | 38 | 1 | 0 | PC 17599 | 71.2833 | C85 | C | | ||
| 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26 | 0 | 0 | STON/O2. 3101282 | 7.925 | | S | | ||
| 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35 | 1 | 0 | 113803 | 53.1 | C123 | S | | ||
| 5 | 0 | 3 | Allen, Mr. William Henry | male | 35 | 0 | 0 | 373450 | 8.05 | | S | | ||
| 6 | 0 | 3 | Moran, Mr. James | male | | 0 | 0 | 330877 | 8.4583 | | Q | | ||
| 7 | 0 | 1 | McCarthy, Mr. Timothy J | male | 54 | 0 | 0 | 17463 | 51.8625 | E46 | S | | ||
| 8 | 0 | 3 | Palsson, Master. Gosta Leonard | male | 2 | 3 | 1 | 349909 | 21.075 | | S | | ||
| 9 | 1 | 3 | Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) | female | 27 | 0 | 2 | 347742 | 11.1333 | | S | | ||
| 10 | 1 | 2 | Nasser, Mrs. Nicholas (Adele Achem) | female | 14 | 1 | 0 | 237736 | 30.0708 | | C | | ||
+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+` | ||
expectedOutputP1 := `+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ | ||
| PASSENGERID | SURVIVED | PCLASS | NAME | SEX | AGE | SIBSP | PARCH | TICKET | FARE | CABIN | EMBARKED | | ||
+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ | ||
| 1 | 0 | 3 | Braund, Mr. Owen Harris | male | 22 | 1 | 0 | A/5 21171 | 7.25 | | S | | ||
| 2 | 1 | 1 | Cumings, Mrs. John Bradley (Florence Briggs Thayer) | female | 38 | 1 | 0 | PC 17599 | 71.2833 | C85 | C | | ||
| 3 | 1 | 3 | Heikkinen, Miss. Laina | female | 26 | 0 | 0 | STON/O2. 3101282 | 7.925 | | S | | ||
+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+` | ||
expectedOutputP2 := `+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ | ||
| PASSENGERID | SURVIVED | PCLASS | NAME | SEX | AGE | SIBSP | PARCH | TICKET | FARE | CABIN | EMBARKED | | ||
+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ | ||
| 4 | 1 | 1 | Futrelle, Mrs. Jacques Heath (Lily May Peel) | female | 35 | 1 | 0 | 113803 | 53.1 | C123 | S | | ||
| 5 | 0 | 3 | Allen, Mr. William Henry | male | 35 | 0 | 0 | 373450 | 8.05 | | S | | ||
| 6 | 0 | 3 | Moran, Mr. James | male | | 0 | 0 | 330877 | 8.4583 | | Q | | ||
+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+` | ||
expectedOutputP3 := `+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ | ||
| PASSENGERID | SURVIVED | PCLASS | NAME | SEX | AGE | SIBSP | PARCH | TICKET | FARE | CABIN | EMBARKED | | ||
+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ | ||
| 7 | 0 | 1 | McCarthy, Mr. Timothy J | male | 54 | 0 | 0 | 17463 | 51.8625 | E46 | S | | ||
| 8 | 0 | 3 | Palsson, Master. Gosta Leonard | male | 2 | 3 | 1 | 349909 | 21.075 | | S | | ||
| 9 | 1 | 3 | Johnson, Mrs. Oscar W (Elisabeth Vilhelmina Berg) | female | 27 | 0 | 2 | 347742 | 11.1333 | | S | | ||
+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+` | ||
expectedOutputP4 := `+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ | ||
| PASSENGERID | SURVIVED | PCLASS | NAME | SEX | AGE | SIBSP | PARCH | TICKET | FARE | CABIN | EMBARKED | | ||
+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+ | ||
| 10 | 1 | 2 | Nasser, Mrs. Nicholas (Adele Achem) | female | 14 | 1 | 0 | 237736 | 30.0708 | | C | | ||
+-------------+----------+--------+-----------------------------------------------------+--------+-----+-------+-------+------------------+---------+-------+----------+` | ||
|
||
tw := NewWriter() | ||
tw.AppendHeader(testTitanicHeader) | ||
tw.AppendRows(testTitanicRows) | ||
compareOutput(t, expectedOutput, tw.Render()) | ||
|
||
p := tw.Pager(PageSize(3)) | ||
assert.Equal(t, 1, p.Location()) | ||
compareOutput(t, expectedOutputP1, p.Render()) | ||
compareOutput(t, expectedOutputP2, p.Next()) | ||
compareOutput(t, expectedOutputP2, p.Render()) | ||
assert.Equal(t, 2, p.Location()) | ||
compareOutput(t, expectedOutputP3, p.Next()) | ||
compareOutput(t, expectedOutputP3, p.Render()) | ||
assert.Equal(t, 3, p.Location()) | ||
compareOutput(t, expectedOutputP4, p.Next()) | ||
compareOutput(t, expectedOutputP4, p.Render()) | ||
assert.Equal(t, 4, p.Location()) | ||
compareOutput(t, expectedOutputP4, p.Next()) | ||
compareOutput(t, expectedOutputP4, p.Render()) | ||
assert.Equal(t, 4, p.Location()) | ||
compareOutput(t, expectedOutputP3, p.Prev()) | ||
compareOutput(t, expectedOutputP3, p.Render()) | ||
assert.Equal(t, 3, p.Location()) | ||
compareOutput(t, expectedOutputP2, p.Prev()) | ||
compareOutput(t, expectedOutputP2, p.Render()) | ||
assert.Equal(t, 2, p.Location()) | ||
compareOutput(t, expectedOutputP1, p.Prev()) | ||
compareOutput(t, expectedOutputP1, p.Render()) | ||
assert.Equal(t, 1, p.Location()) | ||
compareOutput(t, expectedOutputP1, p.Prev()) | ||
compareOutput(t, expectedOutputP1, p.Render()) | ||
assert.Equal(t, 1, p.Location()) | ||
|
||
compareOutput(t, expectedOutputP1, p.GoTo(0)) | ||
compareOutput(t, expectedOutputP1, p.Render()) | ||
assert.Equal(t, 1, p.Location()) | ||
compareOutput(t, expectedOutputP1, p.GoTo(1)) | ||
compareOutput(t, expectedOutputP1, p.Render()) | ||
assert.Equal(t, 1, p.Location()) | ||
compareOutput(t, expectedOutputP2, p.GoTo(2)) | ||
compareOutput(t, expectedOutputP2, p.Render()) | ||
assert.Equal(t, 2, p.Location()) | ||
compareOutput(t, expectedOutputP3, p.GoTo(3)) | ||
compareOutput(t, expectedOutputP3, p.Render()) | ||
assert.Equal(t, 3, p.Location()) | ||
compareOutput(t, expectedOutputP4, p.GoTo(4)) | ||
compareOutput(t, expectedOutputP4, p.Render()) | ||
assert.Equal(t, 4, p.Location()) | ||
compareOutput(t, expectedOutputP4, p.GoTo(5)) | ||
compareOutput(t, expectedOutputP4, p.Render()) | ||
assert.Equal(t, 4, p.Location()) | ||
|
||
sb := strings.Builder{} | ||
p.SetOutputMirror(&sb) | ||
p.Render() | ||
compareOutput(t, expectedOutputP4, sb.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters