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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
package uitcell
import (
"fmt"
"path/filepath"
"strings"
tcell "github.com/gdamore/tcell/v2"
"github.com/prodhe/poe/editor"
)
const (
FnMessageWin = "+poe"
FnEmptyWin = ""
RuneWidthZero = '?'
)
var (
screen tcell.Screen
ed editor.Editor
workspace *Workspace
CurWin *Window
poecmds map[string]commandFunc
quit chan bool
events chan tcell.Event
)
type commandFunc func()
type Tcell struct{}
func (t *Tcell) Init(e editor.Editor) error {
ed = e
if ed.Len() == 0 {
ed.LoadBuffers([]string{"."})
}
if err := initScreen(); err != nil {
return err
}
if err := initStyles(); err != nil {
return err
}
if err := setStyleAcme(); err != nil {
return err
}
if err := initWorkspace(); err != nil {
return err
}
if err := initWindows(); err != nil {
return err
}
initCommands()
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
poename := CurWin.Dir() + string(filepath.Separator) + FnMessageWin
poename = filepath.Clean(poename)
poewin := FindWindow(poename)
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 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)
CurWin = win
}
return nil
}
func initCommands() {
poecmds = map[string]commandFunc{
"New": CmdNew,
"Newcol": CmdNewcol,
"Del": CmdDel,
"Exit": CmdExit,
}
}
func (t *Tcell) redraw() {
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()
workspace.Resize(0, 0, w, h)
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 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
}
}
if CurWin != nil {
CurWin.Focus()
CurWin.HandleEvent(e)
}
}
select {
case event = <-events:
default:
event = nil
}
}
}
}
func Cmd(input string) string {
if input == "" {
return ""
}
input = strings.Trim(input, "\t\n ")
// check poe default commands
cmd := strings.Split(string(input), " ")
if fn, ok := poecmds[cmd[0]]; ok {
fn()
return ""
}
// Edit shortcuts for external commands and piping
switch input[0] {
case '!', '<', '>', '|':
return ed.Edit(CurWin.bufid, input)
}
return ed.Edit(CurWin.bufid, "!"+input)
}
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() {
screen.Clear()
id, _ := ed.NewBuffer()
win := NewWindow(id)
workspace.LastCol().AddWindow(win)
}
func CmdDel() {
if len(AllWindows()) == 1 {
CmdExit()
return
}
CurWin.Close()
}
func CmdNewcol() {
workspace.AddCol()
CmdNew()
}
func CmdExit() {
exit := true
wins := AllWindows()
for _, win := range wins {
if !win.CanClose() {
exit = false
}
}
if exit {
quit <- true
}
}
|