Skip to content

Commit

Permalink
Bound current page when WithRows is called with fewer rows (#56)
Browse files Browse the repository at this point in the history
* If WithRows is called with fewer rows, make sure to bound within pages correctly

* Use newer golangci version
  • Loading branch information
Evertras authored Apr 6, 2022
1 parent 534c809 commit c54ce22
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
uses: golangci/golangci-lint-action@v3
with:
version: v1.44.2

80 changes: 56 additions & 24 deletions examples/pagination/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,11 @@ import (
type Model struct {
tableDefault table.Model
tableWithRowIndices table.Model
}

func genTable(columnCount int, rowCount int) table.Model {
columns := []table.Column{}

for column := 0; column < columnCount; column++ {
columnStr := fmt.Sprintf("%d", column+1)
columns = append(columns, table.NewColumn(columnStr, columnStr, 8))
}
rowCount int
}

func genRows(columnCount int, rowCount int) []table.Row {
rows := []table.Row{}

for row := 1; row <= rowCount; row++ {
Expand All @@ -36,14 +31,39 @@ func genTable(columnCount int, rowCount int) table.Model {
rows = append(rows, table.NewRow(rowData))
}

return rows
}

func genTable(columnCount int, rowCount int) table.Model {
columns := []table.Column{}

for column := 0; column < columnCount; column++ {
columnStr := fmt.Sprintf("%d", column+1)
columns = append(columns, table.NewColumn(columnStr, columnStr, 8))
}

rows := genRows(columnCount, rowCount)

return table.New(columns).WithRows(rows).HeaderStyle(lipgloss.NewStyle().Bold(true))
}

func NewModel() Model {
return Model{
tableDefault: genTable(3, 105).WithPageSize(10).Focused(true),
tableWithRowIndices: genTable(3, 105).WithPageSize(10).Focused(false),
const startingRowCount = 105

m := Model{
rowCount: startingRowCount,
tableDefault: genTable(3, startingRowCount).WithPageSize(10).Focused(true),
tableWithRowIndices: genTable(3, startingRowCount).WithPageSize(10).Focused(false),
}

m.regenTableRows()

return m
}

func (m *Model) regenTableRows() {
m.tableDefault = m.tableDefault.WithRows(genRows(3, m.rowCount))
m.tableWithRowIndices = m.tableWithRowIndices.WithRows(genRows(3, m.rowCount))
}

func (m Model) Init() tea.Cmd {
Expand All @@ -56,18 +76,6 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds []tea.Cmd
)

m.tableDefault, cmd = m.tableDefault.Update(msg)
cmds = append(cmds, cmd)

m.tableWithRowIndices, cmd = m.tableWithRowIndices.Update(msg)
cmds = append(cmds, cmd)

// Write a custom footer
start, end := m.tableWithRowIndices.VisibleIndices()
m.tableWithRowIndices = m.tableWithRowIndices.WithStaticFooter(
fmt.Sprintf("%d-%d of %d", start+1, end+1, m.tableWithRowIndices.TotalRows()),
)

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
Expand All @@ -81,16 +89,40 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "b":
m.tableDefault = m.tableDefault.Focused(false)
m.tableWithRowIndices = m.tableWithRowIndices.Focused(true)

case "z":
if m.rowCount < 10 {
break
}

m.rowCount -= 10
m.regenTableRows()

case "x":
m.rowCount += 10
m.regenTableRows()
}
}

m.tableDefault, cmd = m.tableDefault.Update(msg)
cmds = append(cmds, cmd)

m.tableWithRowIndices, cmd = m.tableWithRowIndices.Update(msg)
cmds = append(cmds, cmd)

// Write a custom footer
start, end := m.tableWithRowIndices.VisibleIndices()
m.tableWithRowIndices = m.tableWithRowIndices.WithStaticFooter(
fmt.Sprintf("%d-%d of %d", start+1, end+1, m.tableWithRowIndices.TotalRows()),
)

return m, tea.Batch(cmds...)
}

func (m Model) View() string {
body := strings.Builder{}

body.WriteString("Table demo with pagination! Press left/right to move pages, or use page up/down\nPress 'a' for left table, 'b' for right table\nPress q or ctrl+c to quit\n\n")
body.WriteString("Table demo with pagination! Press left/right to move pages, or use page up/down\nPress 'a' for left table, 'b' for right table\nPress 'z' to reduce rows by 10, 'y' to increase rows by 10\nPress q or ctrl+c to quit\n\n")

pad := lipgloss.NewStyle().Padding(1)

Expand Down
9 changes: 9 additions & 0 deletions table/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ func (m Model) HeaderStyle(style lipgloss.Style) Model {
func (m Model) WithRows(rows []Row) Model {
m.rows = rows

if m.pageSize != 0 {
maxPage := m.MaxPages()

// MaxPages is 1-index, currentPage is 0 index
if maxPage <= m.currentPage {
m.pageLast()
}
}

return m
}

Expand Down
8 changes: 0 additions & 8 deletions table/pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,11 @@ func (m *Model) pageUp() {
}

func (m *Model) pageFirst() {
if m.pageSize == 0 || len(m.GetVisibleRows()) <= m.pageSize {
return
}

m.currentPage = 0
m.rowCursorIndex = 0
}

func (m *Model) pageLast() {
if m.pageSize == 0 || len(m.GetVisibleRows()) <= m.pageSize {
return
}

m.currentPage = m.MaxPages() - 1
m.rowCursorIndex = m.currentPage * m.pageSize
}
Expand Down
25 changes: 25 additions & 0 deletions table/pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,3 +363,28 @@ func TestClearPagination(t *testing.T) {

assert.Equal(t, 0, model.expectedPageForRowIndex(11))
}

func TestPaginationSetsLastPageWithFewerRows(t *testing.T) {
const (
pageSize = 10
numRowsOriginal = 30
numRowsAfter = 18
)

model := genPaginationTable(numRowsOriginal, pageSize)
model.pageUp()

assert.Equal(t, 3, model.CurrentPage())

rows := []Row{}

for i := 1; i <= numRowsAfter; i++ {
rows = append(rows, NewRow(RowData{
"id": i,
}))
}

model = model.WithRows(rows)

assert.Equal(t, 2, model.CurrentPage())
}

0 comments on commit c54ce22

Please sign in to comment.