/
view.go
150 lines (128 loc) · 2.86 KB
/
view.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 view
import (
"log"
termbox "github.com/nsf/termbox-go"
)
type Render interface {
Draw()
}
type Layer struct {
win *Window
cursorPos *Position
drawPos *Position
}
func NewLayer(x, y, width, height int) *Layer {
return &Layer{
win: newWindow(x, y, width, height),
cursorPos: newPosition(0, 0),
drawPos: newPosition(0, 0),
}
}
func (l *Layer) Resize(x, y, width, height int) {
l.win.Resize(x, y, width, height)
}
func (l *Layer) getCursorY() int {
return l.win.DrawY(l.cursorPos.Y) - l.drawPos.Y
}
func (l *Layer) getDrawY(i int) int {
return l.win.DrawY(i) - l.drawPos.Y
}
func (l *Layer) DrawBackGround(fg, bg termbox.Attribute) {
for i := 0; i < l.win.Box.Height; i++ {
drawStr := PadRight("", l.win.Box.Width, " ")
drawX := l.win.DrawX(0)
drawY := l.win.DrawY(i)
tbPrint(drawX, drawY, fg, bg, drawStr)
}
}
func (l *Layer) DrawContents(
lines []string,
cursorFG, cursorBG termbox.Attribute,
defaultFG, defaultBG termbox.Attribute,
) {
for i, line := range lines {
if i >= l.drawPos.Y {
drawStr := PadRight(line, l.win.Box.Width, " ")
drawX := l.win.DrawX(0)
drawY := l.getDrawY(i)
var fg, bg termbox.Attribute
if drawY == l.getCursorY() {
fg = cursorFG
bg = cursorBG
} else {
fg = defaultFG
bg = defaultBG
}
tbPrint(drawX, drawY, fg, bg, drawStr)
}
}
}
func (l *Layer) UpCursor(val int) int {
l.cursorPos.Y -= val
if l.cursorPos.Y < 0 {
l.cursorPos.Y = 0
}
if l.cursorPos.Y < l.drawPos.Y {
l.drawPos.Y = l.cursorPos.Y
}
log.Printf("Up detail. CursorPosition:%d, DrawPosition:%d", l.cursorPos.Y, l.drawPos.Y)
return l.cursorPos.Y
}
func (l *Layer) DownCursor(val int, contentNum int) int {
l.cursorPos.Y += val
if l.cursorPos.Y > (contentNum - 1) {
l.cursorPos.Y = contentNum - 1
}
if l.cursorPos.Y > (l.drawPos.Y + l.win.Box.Height - 1) {
l.drawPos.Y = l.cursorPos.Y - l.win.Box.Height + 1
}
log.Printf("Down detail. CursorPosition:%d, DrawPosition:%d", l.cursorPos.Y, l.drawPos.Y)
return l.cursorPos.Y
}
func (l *Layer) HalfPageUpCursor() int {
_, height := termbox.Size()
halfPage := height / 2
return l.UpCursor(halfPage)
}
func (l *Layer) HalfPageDownCursor(contentNum int) int {
_, height := termbox.Size()
halfPage := height / 2
return l.DownCursor(halfPage, contentNum)
}
type Position struct {
X, Y int
}
func newPosition(x, y int) *Position {
return &Position{x, y}
}
type Box struct {
Width, Height int
}
type Window struct {
Pos *Position
Box *Box
}
func newWindow(x, y, width, height int) *Window {
return &Window{
Pos: &Position{
X: x,
Y: y,
},
Box: &Box{
Width: width,
Height: height,
},
}
}
func (w *Window) Resize(x, y, width, height int) {
w.Pos.X = x
w.Pos.Y = y
w.Box.Width = width
w.Box.Height = height
}
func (w *Window) DrawX(x int) int {
return w.Pos.X + x
}
func (w *Window) DrawY(y int) int {
return w.Pos.Y + y
}