forked from taylorskalyo/goreader
/
pager.go
163 lines (141 loc) · 3.76 KB
/
pager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package nav
import (
"github.com/davidwalter0/goreader/parse"
termbox "github.com/nsf/termbox-go"
)
type PageNavigator interface {
Draw() error
MaxScrollX() int
MaxScrollY() int
PageDown() bool
PageUp() bool
Pages() int
ScrollDown()
ScrollLeft()
ScrollRight()
ScrollUp()
SetDoc(parse.Cellbuf)
Size() (int, int)
ToBottom()
ToTop()
}
type Pager struct {
scrollX int
scrollY int
doc parse.Cellbuf
}
// setDoc sets the pager's cell buffer
func (p *Pager) SetDoc(doc parse.Cellbuf) {
p.doc = doc
}
// Draw displays a pager's cell buffer in the terminal.
func (p *Pager) Draw() error {
termbox.Clear(termbox.ColorDefault, termbox.ColorDefault)
width, height := termbox.Size()
var centerOffset int
for y := 0; y < height; y++ {
for x := 0; x < p.doc.Width; x++ {
index := (y+p.scrollY)*p.doc.Width + x
if index >= len(p.doc.Cells) || index <= 0 {
continue
}
cell := p.doc.Cells[index]
if width > p.doc.Width {
centerOffset = (width - p.doc.Width) / 2
}
// Calling SetCell with coordinates outside of the terminal viewport
// results in a no-op.
termbox.SetCell(x+p.scrollX+centerOffset, y, cell.Ch, cell.Fg, cell.Bg)
}
}
return termbox.Flush()
}
// scrollDown pans the pager's viewport down, without exceeding the underlying
// cell buffer document's boundaries.
func (p *Pager) ScrollDown() {
if p.scrollY < p.MaxScrollY() {
p.scrollY++
}
}
// scrollUp pans the pager's viewport up, without exceeding the underlying cell
// buffer document's boundaries.
func (p *Pager) ScrollUp() {
if p.scrollY > 0 {
p.scrollY--
}
}
// scrollLeft pans the pager's viewport left, without exceeding the underlying
// cell buffer document's boundaries.
func (p *Pager) ScrollLeft() {
if p.scrollX < 0 {
p.scrollX++
}
}
// scrollRight pans the pager's viewport right, without exceeding the
// underlying cell buffer document's boundaries.
func (p *Pager) ScrollRight() {
if p.scrollX > -p.MaxScrollX() {
p.scrollX--
}
}
// pageDown pans the pager's viewport down by a full page, without exceeding
// the underlying cell buffer document's boundaries.
func (p *Pager) PageDown() bool {
_, viewHeight := termbox.Size()
if p.scrollY < p.MaxScrollY() {
p.scrollY += viewHeight
return true
}
return false
}
// pageUp pans the pager's viewport up by a full page, without exceeding the
// underlying cell buffer document's boundaries.
func (p *Pager) PageUp() bool {
_, viewHeight := termbox.Size()
if p.scrollY > viewHeight {
p.scrollY -= viewHeight
return true
} else if p.scrollY > 0 {
p.scrollY = 0
return true
}
return false
}
// toTop set's the pager's horizontal and vertical panning distance back to
// zero.
func (p *Pager) ToTop() {
p.scrollX = 0
p.scrollY = 0
}
// toBottom set's the pager's horizontal panning distance back to zero and
// vertical panning distance to the last viewport page.
func (p *Pager) ToBottom() {
_, viewHeight := termbox.Size()
p.scrollX = 0
p.scrollY = p.Pages() * viewHeight
}
// maxScrollX represents the pager's maximum horizontal scroll distance.
func (p *Pager) MaxScrollX() int {
docWidth, _ := p.Size()
viewWidth, _ := termbox.Size()
return docWidth - viewWidth
}
// maxScrollY represents the pager's maximum vertical scroll distance.
func (p *Pager) MaxScrollY() int {
_, docHeight := p.Size()
_, viewHeight := termbox.Size()
return docHeight - viewHeight
}
// size returns the width and height of the pager's underlying cell buffer
// document.
func (p *Pager) Size() (int, int) {
height := len(p.doc.Cells) / p.doc.Width
return p.doc.Width, height
}
// pages returns the number of times the pager's underlying cell buffer
// document can be split into viewport sized pages.
func (p *Pager) Pages() int {
_, docHeight := p.Size()
_, viewHeight := termbox.Size()
return docHeight / viewHeight
}