-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
192 lines (161 loc) · 3.65 KB
/
app.go
File metadata and controls
192 lines (161 loc) · 3.65 KB
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
package main
import (
"fmt"
"io"
"os"
"time"
"github.com/dcbishop/jkl/service"
"github.com/nsf/termbox-go"
"github.com/spf13/afero"
)
// App is the main program.
type App struct {
quit chan interface{}
UI UI
state service.State
editor *Editor
Out io.Writer
ErrOut io.Writer
}
// NewApp constructs a new app from the given options.
func NewApp(options ...Option) App {
editor := NewEditor(afero.OsFs{})
app := App{
editor: &editor,
UI: nil,
Out: os.Stdout,
ErrOut: os.Stderr,
}
app.initializeQuitChannel()
app.LoadOptions(options...)
return app
}
// Editor returns the app's editor.
func (app *App) Editor() *Editor {
return app.editor
}
// SetUI sets the UI to render the app to.
func (app *App) SetUI(ui UI) {
if app.UI != nil {
app.UI.Stop()
service.WaitUntilStopped(app.UI, time.Second)
}
app.UI = ui
if app.UI != nil && app.Running() {
go app.UI.Run()
}
}
// SetOut sets the default output stream of the App.
func (app *App) SetOut(out io.Writer) {
app.Out = out
}
// SetErrOut sets the error output stream of the App.
func (app *App) SetErrOut(eout io.Writer) {
app.ErrOut = eout
}
// SetFS sets the filesystem handler of the App.
func (app *App) SetFS(fs afero.Fs) {
app.editor.SetFS(fs)
}
// LoadOptions loads the given options.
func (app *App) LoadOptions(options ...Option) {
for _, o := range options {
o(app)
}
}
// Run starts the main loop of the app. Will block until finished.
func (app *App) Run() {
if app.state.SetRunning() != nil {
fmt.Fprintf(app.ErrOut, "App already running.")
return
}
if app.UI != nil && !app.UI.Running() {
go app.UI.Run()
if service.WaitUntilRunning(app.UI, time.Second) != nil {
fmt.Fprintf(app.ErrOut, "Could not start UI service.")
}
}
app.loopUntilQuit()
app.UI.Stop()
app.state.SetStopped()
}
// Stop shuts everything down and terminates Run(). Blocks untill clean shutdown.
func (app *App) Stop() {
if !app.Running() {
return
}
app.quit <- true
if service.WaitUntilStopped(app.UI, time.Second) != nil {
fmt.Fprintln(app.ErrOut, "UI service did not stop in under a second.")
}
if service.WaitUntilStopped(app, time.Second) != nil {
fmt.Fprintln(app.ErrOut, "App service did not stop in under a second.")
}
}
// Running returns true if Run() was called but Stop() hasn't been.
func (app *App) Running() bool {
return app.state.Running()
}
func (app *App) initializeQuitChannel() {
if app.quit != nil {
return
}
app.quit = make(chan interface{})
}
func (app *App) loopUntilQuit() {
loop:
for {
var events <-chan Event
if app.UI != nil {
events = app.UI.Events()
}
select {
case <-app.quit:
break loop
case event := <-events:
app.handleEvent(event)
default:
app.Update()
}
}
}
func (app *App) handleEvent(event Event) {
// [TODO]: Convert all Events to an interal format in the UI layer rather than using termbox directly. - 2014-09-27 11:27am
switch data := event.Data.(type) {
case termbox.Event:
app.handleTermboxEvent(data)
}
}
func (app *App) handleTermboxEvent(event termbox.Event) {
switch event.Type {
case termbox.EventKey:
app.handleTermboxKeyEvent(event)
}
}
func (app *App) handleTermboxKeyEvent(event termbox.Event) {
if event.Ch == 'q' {
go app.Stop()
}
cursor := app.editor.CurrentPane().Cursor()
if cursor == nil {
return
}
if event.Ch == 'j' {
cursor.Move(cursor.DownLine())
}
if event.Ch == 'k' {
cursor.Move(cursor.UpLine())
}
if event.Ch == 'h' {
cursor.Move(cursor.BackCharacter())
}
if event.Ch == 'l' {
cursor.Move(cursor.ForwardCharacter())
}
}
// Update processes input and redraws the app.
func (app *App) Update() {
if app.UI != nil {
app.UI.Redraw(app.editor)
}
}