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
|
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"runtime"
"github.com/gdamore/tcell"
"github.com/prodhe/poe/gapbuffer"
)
const (
FnMessageWin = "+poe"
FnEmptyWin = ""
RuneWidthZero = '?'
)
var (
// Main screen terminal
screen tcell.Screen
// menu is the main tagline above everything else
menu *View
// workspace contains windows.
workspace *Workspace
// CurWin is a pointer to the currently focused window.
CurWin *Window
// Channels
events chan tcell.Event
quit chan bool
// baseDir stores the dir from which the program started in
baseDir string
)
// InitScreen initializes the tcell terminal.
func InitScreen() {
var err error
screen, err = tcell.NewScreen()
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
if err = screen.Init(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
screen.SetStyle(bodyStyle)
screen.EnableMouse()
screen.Clear()
screen.Sync()
}
func InitMenu() {
menu = &View{
text: &Text{buf: gapbuffer.New()},
what: ViewMenu,
style: bodyStyle,
cursorStyle: bodyCursorStyle,
hilightStyle: bodyHilightStyle,
tabstop: 4,
}
fmt.Fprintf(menu, "Exit New Newcol")
}
func InitWorkspace() {
workspace = &Workspace{} // first resize event will set proper dimensions
workspace.AddCol()
}
// LoadBuffers reads files from disk and loads them into windows. Screen need to be initialized.
func LoadBuffers(fns []string) {
if len(fns) < 1 {
fns = append(fns, FnEmptyWin)
}
// setup windows
for i, fn := range fns {
win := NewWindow(fn)
win.LoadBuffer()
if i == 0 { // first window gets focus
CurWin = win
}
workspace.Col(0).AddWindow(win)
}
// add a base directory listing in new col
workspace.AddCol()
CmdOpen(baseDir)
}
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.NameAbs() == poename && CurWin.Dir() == win.Dir() {
poewin = win
}
}
if poewin == nil {
poewin = NewWindow(CurWin.Dir() + string(filepath.Separator) + FnMessageWin)
poewin.body.what = ViewScratch
if len(workspace.cols) < 2 {
workspace.AddCol()
}
workspace.LastCol().AddWindow(poewin)
}
poewin.body.SetCursor(poewin.body.text.buf.Len(), 0)
if a == nil {
fmt.Fprintf(poewin.body, format)
return
}
fmt.Fprintf(poewin.body, format, a...)
}
func Redraw() {
menu.Draw()
workspace.Draw()
screen.Show()
}
func main() {
flag.Parse()
// store current working dir
var err error
baseDir, err = filepath.Abs(".")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
//baseDir += string(filepath.Separator)
// Init
InitStyles()
InitScreen()
// proper closing and terminal cleanup on exit and error message on a possible panic
defer func() {
if screen != nil {
screen.Clear()
screen.Fini()
}
if err := recover(); err != nil {
buf := make([]byte, 1<<16)
n := runtime.Stack(buf, true)
fmt.Println("poe error:", err)
fmt.Printf("%s", buf[:n+1])
os.Exit(1)
}
}()
// Setup top menu
InitMenu()
// Create initial workspace
InitWorkspace()
InitCommands()
// This loads all buffers reading file names from command line and populates the workspace.
LoadBuffers(flag.Args())
events = make(chan tcell.Event, 100)
quit = make(chan bool, 1)
go func() {
for {
if screen != nil {
events <- screen.PollEvent()
}
}
}()
// main loop
loop:
for {
Redraw()
// Check for events
var event tcell.Event
select {
case <-quit:
break loop
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
}
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
}
CurWin.HandleEvent(e)
}
event = nil
// check tcell event queue before returning to main event check
select {
case event = <-events:
default:
event = nil
}
}
}
}
|