forked from taylorskalyo/goreader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pager.go
150 lines (126 loc) · 3.5 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
package main
import termbox "github.com/nsf/termbox-go"
type pager struct {
scrollX int
scrollY int
doc cellbuf
}
// 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() bool {
if p.scrollY < p.maxScrollY() {
p.scrollY++
return true
}
return false
}
// scrollUp pans the pager's viewport up, without exceeding the underlying cell
// buffer document's boundaries.
func (p *pager) scrollUp() bool {
if p.scrollY > 0 {
p.scrollY--
return true
}
return false
}
// scrollLeft pans the pager's viewport left, without exceeding the underlying
// cell buffer document's boundaries.
func (p *pager) scrollLeft() bool {
if p.scrollX < 0 {
p.scrollX++
return true
}
return false
}
// scrollRight pans the pager's viewport right, without exceeding the
// underlying cell buffer document's boundaries.
func (p *pager) scrollRight() bool {
if p.scrollX > -p.maxScrollX() {
p.scrollX--
return true
}
return false
}
// 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
}