-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminalui.go
More file actions
140 lines (114 loc) · 2.72 KB
/
terminalui.go
File metadata and controls
140 lines (114 loc) · 2.72 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
package main
import (
"time"
"github.com/dcbishop/jkl/service"
"github.com/nsf/termbox-go"
)
// Color is red and yellow and pink and green...
type Color interface{}
// ConsoleDriver handles the input/output of the TerminalUI to/from the terminal.
type ConsoleDriver interface {
Size() (width int, height int)
Init()
Close()
SetCell(x, y int, r rune, fg Color, bg Color)
Events() chan Event
SetCursor(x, y int)
AfterDraw()
}
// TerminalUI a text based user interface renderer.
type TerminalUI struct {
quit chan bool
state service.State
Console ConsoleDriver
}
// NewTerminalUI constructs a new TerminalUI.
func NewTerminalUI(driver ConsoleDriver) TerminalUI {
tui := TerminalUI{
Console: driver,
}
tui.initializeQuitChannel()
return tui
}
// Run enters the main UI loop untill Stop() is called.
func (tui *TerminalUI) Run() {
if tui.state.SetRunning() != nil {
panic("UI already running.")
return
}
defer tui.state.SetStopped()
tui.initialize()
tui.waitForQuit()
tui.cleanUp()
}
// Running returns true if ui.Run() was called but ui.Stop() hasn't been.
func (tui *TerminalUI) Running() bool {
return tui.state.Running()
}
// Stop terminates the Run loop.
func (tui *TerminalUI) Stop() {
if !tui.Running() {
return
}
tui.quit <- true
service.WaitUntilStopped(tui, time.Second)
}
// Events gets the channel that emits events
func (tui *TerminalUI) Events() <-chan Event {
return tui.Console.Events()
}
// Redraw updates the display
func (tui *TerminalUI) Redraw(editor *Editor) {
if !tui.state.Running() {
return
}
defer tui.Console.AfterDraw()
// [TODO]: Cache runegrid and change on resize only - 2014-09-27 10:10pm
width, height := tui.Console.Size()
grid := NewRuneGrid(width, height)
grid.RenderEditor(editor)
tui.renderGrid(&grid)
if editor.CurrentPane().Cursor() == nil {
return
}
xPos, linePos := editor.CurrentPane().Cursor().Position()
linePos = linePos - editor.CurrentPane().TopLine() + 1
if editor.Settings().Borders && editor.Settings().OuterBorder {
xPos++
linePos++
}
tui.Console.SetCursor(xPos, linePos-1)
}
func (tui *TerminalUI) renderGrid(grid *RuneGrid) {
for y, l := range grid.Cells() {
for x, r := range l {
tui.Console.SetCell(x, y, r, termbox.ColorWhite, termbox.ColorRed)
}
}
}
func (tui *TerminalUI) initialize() {
tui.initializeConsoleDriver()
tui.Console.Init()
}
func (tui *TerminalUI) cleanUp() {
tui.Console.Close()
}
func (tui *TerminalUI) initializeConsoleDriver() {
if tui.Console != nil {
return
}
tbd := NewTermboxDriver()
tui.Console = &tbd
}
func (tui *TerminalUI) initializeQuitChannel() {
if tui.quit != nil {
return
}
tui.quit = make(chan bool)
}
func (tui *TerminalUI) waitForQuit() {
select {
case <-tui.quit:
return
}
}