0
0
mirror of https://github.com/crazy-max/diun.git synced 2024-12-22 19:38:28 +00:00
crazy-max_diun/vendor/github.com/jedib0t/go-pretty/v6/table/pager.go
dependabot[bot] 6f7b5b313d
chore(deps): bump github.com/jedib0t/go-pretty/v6 from 6.5.9 to 6.6.5
Bumps [github.com/jedib0t/go-pretty/v6](https://github.com/jedib0t/go-pretty) from 6.5.9 to 6.6.5.
- [Release notes](https://github.com/jedib0t/go-pretty/releases)
- [Commits](https://github.com/jedib0t/go-pretty/compare/v6.5.9...v6.6.5)

---
updated-dependencies:
- dependency-name: github.com/jedib0t/go-pretty/v6
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-12-15 14:33:08 +00:00

71 lines
1.4 KiB
Go

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
}