/
screen.go
190 lines (176 loc) · 4.57 KB
/
screen.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package screen
import (
"log"
"github.com/nsf/termbox-go"
gott "github.com/timburks/gott/types"
)
// Screen draws the state of an editor.
type Screen struct {
size gott.Size // screen size
editor gott.Editor
needsLayout bool
}
// NewScreen creates a screen for use with a specified editor.
func NewScreen(e gott.Editor) *Screen {
// Open the terminal.
err := termbox.Init()
if err != nil {
log.Output(1, err.Error())
return nil
}
termbox.SetOutputMode(termbox.Output256)
s := &Screen{editor: e}
s.needsLayout = true
return s
}
// Close shuts down the display of a screen.
func (s *Screen) Close() {
termbox.Close()
}
func (s *Screen) layout() {
var screenSize gott.Size
screenSize.Cols, screenSize.Rows = termbox.Size()
s.size = screenSize
editSize := screenSize
// Save the last row on the screen for the message bar.
editSize.Rows -= 1
s.editor.SetSize(editSize)
s.editor.LayoutWindows()
}
func (s *Screen) Render(e gott.Editor, c gott.Commander) {
if s.needsLayout {
s.layout()
s.needsLayout = false
}
termbox.Clear(termbox.ColorWhite, termbox.ColorBlack)
e.RenderWindows(s)
s.renderMessageBar(c)
termbox.Flush()
}
func (s *Screen) SetCell(j int, i int, c rune, color gott.Color) {
termbox.SetCell(j, i, c, termbox.Attribute(color), 0x01)
}
func (s *Screen) SetCellReversed(j int, i int, c rune, color gott.Color) {
termbox.SetCell(j, i, c, termbox.Attribute(color), 0x08)
}
func (s *Screen) SetCursor(position gott.Point) {
termbox.SetCursor(position.Col, position.Row)
}
// The message bar is a single line at the bottom of the screen.
func (s *Screen) renderMessageBar(c gott.Commander) {
text := c.GetMessageBarText(s.size.Cols)
for x, ch := range text {
termbox.SetCell(x, s.size.Rows-1,
rune(ch),
termbox.ColorWhite,
termbox.ColorBlack)
}
}
func (s *Screen) GetNextEvent() *gott.Event {
event := termbox.PollEvent()
if event.Type == termbox.EventResize {
s.needsLayout = true
termbox.Flush()
}
return &gott.Event{
Type: int(event.Type),
Key: key(event.Key),
Ch: event.Ch,
}
}
// This conversion seems silly, but it keeps termbox dependencies isolated here.
func key(k termbox.Key) gott.Key {
switch k {
case termbox.KeyArrowDown:
return gott.KeyArrowDown
case termbox.KeyArrowLeft:
return gott.KeyArrowLeft
case termbox.KeyArrowRight:
return gott.KeyArrowRight
case termbox.KeyArrowUp:
return gott.KeyArrowUp
case termbox.KeyBackspace2:
return gott.KeyBackspace2
case termbox.KeyCtrlA:
return gott.KeyCtrlA
case termbox.KeyCtrlB:
return gott.KeyCtrlB
case termbox.KeyCtrlC:
return gott.KeyCtrlC
case termbox.KeyCtrlD:
return gott.KeyCtrlD
case termbox.KeyCtrlE:
return gott.KeyCtrlE
case termbox.KeyCtrlF:
return gott.KeyCtrlF
case termbox.KeyCtrlG:
return gott.KeyCtrlG
case termbox.KeyCtrlH:
return gott.KeyCtrlH
// skip termbox.KeyCtrlI, it is handled as KeyTab
case termbox.KeyCtrlJ:
return gott.KeyCtrlJ
case termbox.KeyCtrlK:
return gott.KeyCtrlK
case termbox.KeyCtrlL:
return gott.KeyCtrlL
// skip termbox.KeyCtrlM, it is handled as KeyEnter
case termbox.KeyCtrlN:
return gott.KeyCtrlN
case termbox.KeyCtrlO:
return gott.KeyCtrlO
case termbox.KeyCtrlP:
return gott.KeyCtrlP
case termbox.KeyCtrlQ:
return gott.KeyCtrlQ
case termbox.KeyCtrlR:
return gott.KeyCtrlR
case termbox.KeyCtrlS:
return gott.KeyCtrlS
case termbox.KeyCtrlT:
return gott.KeyCtrlT
case termbox.KeyCtrlU:
return gott.KeyCtrlU
case termbox.KeyCtrlV:
return gott.KeyCtrlV
case termbox.KeyCtrlW:
return gott.KeyCtrlW
case termbox.KeyCtrlX:
return gott.KeyCtrlX
case termbox.KeyCtrlY:
return gott.KeyCtrlY
case termbox.KeyCtrlZ:
return gott.KeyCtrlZ
case termbox.KeyEnd:
return gott.KeyEnd
case termbox.KeyEnter:
return gott.KeyEnter
case termbox.KeyEsc:
return gott.KeyEsc
case termbox.KeyHome:
return gott.KeyHome
case termbox.KeyPgdn:
return gott.KeyPgdn
case termbox.KeyPgup:
return gott.KeyPgup
case termbox.KeySpace:
return gott.KeySpace
case termbox.KeyTab:
return gott.KeyTab
default:
return gott.KeyUnsupported
}
}