-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.go
169 lines (141 loc) · 2.93 KB
/
app.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
package tui
import (
"fmt"
"github.com/pkg/term"
"github.com/shreve/tui/ansi"
"os"
"sync"
"time"
)
type winSize struct {
rows int
cols int
}
type App struct {
lock sync.Mutex
cond sync.Cond
running bool
term *term.Term
lastRender View
lastSize winSize
watchForResize bool
modes map[int]Mode
mode Mode
Cursor Cursor
OnResize func(int, int)
}
var defaultOnResize = func(int, int) {}
func NewApp() *App {
// Set up the app with non-zero defaults
a := App{}
a.cond = *sync.NewCond(&a.lock)
a.running = true
a.watchForResize = true
a.modes = make(map[int]Mode)
a.mode = &DefaultMode{&a}
a.OnResize = defaultOnResize
// Use term handle of stdin to set mode and read in bytes
var err error
a.term, err = term.Open("/dev/stdin")
if err != nil {
panic(err)
}
return &a
}
func (a *App) AddMode(id int, mode Mode) {
a.modes[id] = mode
if id == 0 {
a.SetMode(0)
}
}
func (a *App) SetMode(id int) {
var ok bool
a.mode, ok = a.modes[id]
if !ok {
a.Panic("Set mode to a mode that doesn't exist.")
}
a.Redraw()
}
// Finish execution by closing render and input loops
func (a *App) Done() {
a.running = false
}
func (a *App) Panic(msg string) {
ansi.RestoreState()
ansi.ShowCursor()
a.term.Restore()
fmt.Println(msg)
os.Exit(1)
}
// Signal renderer
func (a *App) Redraw() {
a.cond.Signal()
}
// Set up the app and run the loops
func (a *App) Run() {
// Save the previous term state and restore it on close
ansi.SaveState()
defer ansi.RestoreState()
// Hide the terminal cursor and restore it on close
ansi.HideCursor()
defer ansi.ShowCursor()
// Set the terminal into raw mode and restore on close
a.term.SetRaw()
a.term.SetCbreak()
defer a.term.Restore()
if a.watchForResize {
go a.resizeWatcher()
}
go a.renderLoop()
a.inputLoop()
}
// Wrap rendering in a condition variable so we can signal at will
func (a *App) renderLoop() {
a.lock.Lock()
for a.running {
a.render()
a.cond.Wait()
}
a.lock.Unlock()
}
// Perform the render
func (a *App) render() {
rows, cols := ansi.WindowSize()
size := winSize{rows, cols}
newRender := a.mode.Render(rows, cols)
if size != a.lastSize {
// If the window is a different size, re-draw everything
a.lastSize = size
newRender.Render()
} else {
// Otherwise, do a diff render based on the last draw
newRender.RenderFrom(a.lastRender)
}
a.lastRender = newRender
}
// Read in inputs one key at a time and pass off to user handler
func (a *App) inputLoop() {
for a.running {
b := make([]byte, 15)
count, err := a.term.Read(b)
if err != nil {
continue
}
input := string(b[0:count])
a.mode.InputHandler(input)
a.Redraw()
}
}
func (a *App) resizeWatcher() {
tick := time.NewTicker(100 * time.Millisecond)
defer tick.Stop()
for {
<-tick.C
rows, cols := ansi.WindowSize()
size := winSize{rows, cols}
if size != a.lastSize {
a.OnResize(rows, cols)
a.render()
}
}
}