-
Notifications
You must be signed in to change notification settings - Fork 392
/
screen.go
212 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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// Copyright (C) 2019 Storj Labs, Inc.
// See LICENSE for copying information.
package cui
import (
"bufio"
"bytes"
"context"
"errors"
"sync"
termbox "github.com/nsf/termbox-go"
)
var initialized = false
const padding = 2
// Point is a 2D coordinate in console
// X is the column
// Y is the row
type Point struct{ X, Y int }
// Rect is a 2D rectangle in console, excluding Max edge
type Rect struct{ Min, Max Point }
// Screen is a writable area on screen
type Screen struct {
rendering sync.Mutex
blitting sync.Mutex
closed bool
flushed frame
pending frame
}
type frame struct {
size Point
content []byte
}
// NewScreen returns a new screen, only one screen can be use at a time.
func NewScreen() (*Screen, error) {
if initialized {
return nil, errors.New("only one screen allowed at a time")
}
initialized = true
if err := termbox.Init(); err != nil {
initialized = false
return nil, err
}
termbox.SetInputMode(termbox.InputEsc)
termbox.HideCursor()
screen := &Screen{}
screen.flushed.size.X, screen.flushed.size.Y = termbox.Size()
screen.pending.size = screen.flushed.size
return screen, nil
}
func (screen *Screen) markClosed() {
screen.blitting.Lock()
screen.closed = true
screen.blitting.Unlock()
}
func (screen *Screen) isClosed() bool {
screen.blitting.Lock()
defer screen.blitting.Unlock()
return screen.closed
}
// Close closes the screen.
func (screen *Screen) Close() error {
screen.markClosed()
// shutdown termbox
termbox.Close()
initialized = false
return nil
}
// Run runs the event loop
func (screen *Screen) Run() error {
defer screen.markClosed()
for !screen.isClosed() {
switch ev := termbox.PollEvent(); ev.Type {
case termbox.EventInterrupt:
// either screen refresh or close
case termbox.EventKey:
switch ev.Key {
case termbox.KeyCtrlC, termbox.KeyEsc:
return nil
default:
// ignore key presses
}
case termbox.EventError:
return ev.Err
case termbox.EventResize:
screen.blitting.Lock()
screen.flushed.size.X, screen.flushed.size.Y = ev.Width, ev.Height
err := screen.blit(&screen.flushed)
screen.blitting.Unlock()
if err != nil {
return err
}
}
}
return nil
}
// Size returns the current size of the screen.
func (screen *Screen) Size() (width, height int) {
width, height = screen.pending.size.X-2*padding, screen.pending.size.Y-2*padding
if width < 0 {
width = 0
}
if height < 0 {
height = 0
}
return width, height
}
// Lock screen for exclusive rendering
func (screen *Screen) Lock() { screen.rendering.Lock() }
// Unlock screen
func (screen *Screen) Unlock() { screen.rendering.Unlock() }
// Write writes to the screen.
func (screen *Screen) Write(data []byte) (int, error) {
screen.pending.content = append(screen.pending.content, data...)
return len(data), nil
}
// Flush flushes pending content to the console and clears for new frame.
func (screen *Screen) Flush() error {
screen.blitting.Lock()
var err error
if !screen.closed {
err = screen.blit(&screen.pending)
} else {
err = context.Canceled
}
screen.pending.content = nil
screen.pending.size = screen.flushed.size
screen.blitting.Unlock()
return err
}
// blit writes content to the console
func (screen *Screen) blit(frame *frame) error {
screen.flushed.content = frame.content
size := screen.flushed.size
if err := termbox.Clear(termbox.ColorDefault, termbox.ColorDefault); err != nil {
return err
}
drawRect(Rect{
Min: Point{0, 0},
Max: size,
}, lightStyle)
scanner := bufio.NewScanner(bytes.NewReader(frame.content))
y := padding
for scanner.Scan() && y <= size.Y-2*padding {
x := padding
for _, r := range scanner.Text() {
if x > size.X-2*padding {
break
}
termbox.SetCell(x, y, r, termbox.ColorDefault, termbox.ColorDefault)
x++
}
y++
}
return termbox.Flush()
}
type rectStyle [3][3]rune
var lightStyle = rectStyle{
{'┌', '─', '┐'},
{'│', ' ', '│'},
{'└', '─', '┘'},
}
// drawRect draws a rectangle using termbox
func drawRect(r Rect, style rectStyle) {
attr := termbox.ColorDefault
termbox.SetCell(r.Min.X, r.Min.Y, style[0][0], attr, attr)
termbox.SetCell(r.Max.X-1, r.Min.Y, style[0][2], attr, attr)
termbox.SetCell(r.Max.X-1, r.Max.Y-1, style[2][2], attr, attr)
termbox.SetCell(r.Min.X, r.Max.Y-1, style[2][0], attr, attr)
for x := r.Min.X + 1; x < r.Max.X-1; x++ {
termbox.SetCell(x, r.Min.Y, style[0][1], attr, attr)
termbox.SetCell(x, r.Max.Y-1, style[2][1], attr, attr)
}
for y := r.Min.Y + 1; y < r.Max.Y-1; y++ {
termbox.SetCell(r.Min.X, y, style[1][0], attr, attr)
termbox.SetCell(r.Max.X-1, y, style[1][2], attr, attr)
}
}