-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
118 lines (96 loc) · 2.15 KB
/
main.go
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
package main
// TODO: add data editing and file save commands
// TODO: add data editor for generic data (binary, ints, floats, time, guid, disasm?)
// TODO: add data editor for defined data (file formats, structs, protobufs?)
// TODO: add simple endianness switch in data editors
// TODO: add some settings from within the editor to change flags
import (
"os"
"github.com/gdamore/tcell"
)
type editor struct {
flags flags
term term
areas areas
err error // error to print after closing editor
}
var app = editor{
areas: areas{
all: map[string]area{},
},
}
func main() {
defer app.close()
app.flags.init()
app.term.init()
app.must(app.areas.add("editor", &editorArea{}))
app.areas.focus("editor")
loop()
}
func loop() {
for {
// flush terminal if modified
app.term.flush()
ev := app.term.screen.PollEvent()
switch v := ev.(type) {
case *tcell.EventResize:
// set new terminal size
app.term.w, app.term.h = v.Size()
// pass event to current area
if app.areas.current != nil {
app.must(app.areas.current.onEvent(ev))
}
case *tcell.EventKey:
// handle keypress
switch v.Key() {
case tcell.KeyCtrlC, tcell.KeyF10:
// close editor on C-c or F10
return
}
// pass event to current area
if app.areas.current != nil {
app.must(app.areas.current.onEvent(ev))
}
default:
// pass event to current area
if app.areas.current != nil {
app.must(app.areas.current.onEvent(ev))
}
}
}
}
func (e *editor) close() {
// recover any panic that happened naturally
r := recover()
// reset terminal so any further output will be okay
e.term.reset()
e.term.close()
e.areas.close()
// panic to dump error with stack trace
if r != nil {
panic(r)
}
if e.err != nil {
panic(e.err)
}
os.Exit(0)
}
func (e *editor) error(err error) {
e.err = err
e.close()
}
// must checks if the last return value of a function is an error
// and panics if it is a non-nil error, otherwise returning
// the first return value.
func (e *editor) must(v ...interface{}) interface{} {
if len(v) == 0 {
return nil
}
switch x := v[len(v)-1].(type) {
case error:
if x != nil {
e.error(x)
}
}
return v[0]
}