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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
package uitcell
import (
"fmt"
"path/filepath"
"github.com/gdamore/tcell"
"github.com/prodhe/poe/editor"
)
const (
FnMessageWin = "+poe"
FnEmptyWin = ""
RuneWidthZero = '?'
)
var (
screen tcell.Screen
ed editor.Editor
menu *View
workspace *Workspace
CurWin *Window
quit chan bool
events chan tcell.Event
)
type Tcell struct{}
func (t *Tcell) Init(e editor.Editor) error {
ed = e
if err := initScreen(); err != nil {
return err
}
if err := initStyles(); err != nil {
return err
}
if err := initMenu(); err != nil {
return err
}
if err := initWorkspace(); err != nil {
return err
}
if err := initWindows(); err != nil {
return err
}
quit = make(chan bool, 1)
events = make(chan tcell.Event, 100)
return nil
}
func (t *Tcell) Close() {
if screen == nil {
return
}
screen.DisableMouse()
screen.Fini()
}
func printMsg(format string, a ...interface{}) {
// get output window
var poewin *Window
for _, win := range AllWindows() {
poename := win.Dir() + string(filepath.Separator) + FnMessageWin
poename = filepath.Clean(poename)
if win.Name() == poename && CurWin.Dir() == win.Dir() {
poewin = win
}
}
if poewin == nil {
id, buf := ed.NewBuffer()
buf.NewFile(CurWin.Dir() + string(filepath.Separator) + FnMessageWin)
poewin = NewWindow(id)
poewin.body.what = ViewScratch
if len(workspace.cols) < 2 {
workspace.AddCol()
}
workspace.LastCol().AddWindow(poewin)
}
poewin.body.SetCursor(poewin.body.text.Len(), 0)
if a == nil {
fmt.Fprintf(poewin.body, format)
return
}
fmt.Fprintf(poewin.body, format, a...)
}
func initScreen() error {
var err error
screen, err = tcell.NewScreen()
if err != nil {
return err
}
if err = screen.Init(); err != nil {
return err
}
screen.SetStyle(bodyStyle)
screen.EnableMouse()
screen.Sync()
return nil
}
func initMenu() error {
menu = &View{
text: &editor.Buffer{},
what: ViewMenu,
style: bodyStyle,
cursorStyle: bodyCursorStyle,
hilightStyle: bodyHilightStyle,
tabstop: 4,
}
fmt.Fprintf(menu, "Exit New Newcol")
return nil
}
func initWorkspace() error {
workspace = &Workspace{} // first resize event will set proper dimensions
workspace.AddCol()
return nil
}
func initWindows() error {
ids, _ := ed.Buffers()
for _, id := range ids {
win := NewWindow(id)
workspace.LastCol().AddWindow(win)
}
return nil
}
func (t *Tcell) redraw() {
menu.Draw()
workspace.Draw()
screen.Show()
}
func (t *Tcell) Listen() {
go func() {
for {
events <- screen.PollEvent()
}
}()
outer:
for {
// draw
t.redraw()
var event tcell.Event
select {
case <-quit:
break outer
case event = <-events:
}
for event != nil {
switch e := event.(type) {
case *tcell.EventResize:
w, h := screen.Size()
menu.Resize(0, 0, w, 1)
workspace.Resize(0, 1, w, h-1)
//screen.Clear()
screen.Sync()
case *tcell.EventKey: // system wide shortcuts
switch e.Key() {
case tcell.KeyCtrlL: // refresh terminal
screen.Clear()
screen.Sync()
default: // let the focused view handle event
if menu.focused {
menu.HandleEvent(e)
break
}
if CurWin != nil {
CurWin.HandleEvent(e)
}
}
case *tcell.EventMouse:
mx, my := e.Position()
// find which window to send the event to
for _, win := range AllWindows() {
win.UnFocus()
if mx >= win.x && mx < win.x+win.w &&
my >= win.y && my < win.y+win.h {
CurWin = win
}
}
// check if we are in the menu
menu.focused = false
if my < 1 {
menu.focused = true
menu.HandleEvent(e)
break
}
if CurWin != nil {
CurWin.HandleEvent(e)
}
}
select {
case event = <-events:
default:
event = nil
}
}
}
}
func CmdOpen(fn string) {
screen.Clear()
var win *Window
win = FindWindow(fn)
if win == nil { //only load windows that do no already exists
id, buf := ed.NewBuffer()
buf.NewFile(fn)
buf.ReadFile()
win := NewWindow(id)
workspace.LastCol().AddWindow(win)
}
}
func CmdNew(args string) {
screen.Clear()
id, _ := ed.NewBuffer()
win := NewWindow(id)
workspace.LastCol().AddWindow(win)
}
func CmdDel() {
CurWin.Close()
}
func CmdExit() {
exit := true
wins := AllWindows()
for _, win := range wins {
if !win.CanClose() {
exit = false
}
}
if exit || len(wins) == 0 {
quit <- true
}
}
|