-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
147 lines (120 loc) · 2.81 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
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
// Demo code for the TreeView primitive.
package main
import (
"fmt"
"github.com/detunized/retool/shape"
"github.com/detunized/retool/float"
tc "github.com/gdamore/tcell/v2"
t "github.com/rivo/tview"
)
var application *t.Application
var rootView *t.Flex
var contentView *t.Flex
type hotkeyInfo struct {
key tc.Key
label string
handler func()
}
var hotkeys = []hotkeyInfo{
{
key: tc.KeyEsc,
label: "Quit",
handler: quit,
},
}
func makeHotkeyLine() t.Primitive {
flex := NewFlexRow()
for _, hk := range hotkeys {
flex.AddItem(makeHotkeyButton(hk.key, hk.label), 0, 1, false)
}
return flex
}
func makeHotkeyButton(key tc.Key, title string) t.Primitive {
label := formatHotkeyText(key, title)
return t.NewBox().
SetDrawFunc(func(screen tc.Screen, x, y, width, height int) (int, int, int, int) {
t.Print(screen, label, x, y+height/2, width, t.AlignLeft, tc.ColorWhite)
return x, y, width, height
})
}
func formatHotkeyText(key tc.Key, title string) string {
return fmt.Sprintf("[:#7f0000:b] %s [:-:-] %s", tc.KeyNames[key], title)
}
func quit() {
application.Stop()
}
//
// Panes
//
type paneInfo struct {
name string
view t.Primitive
}
var panes []paneInfo
func addPane(hotkey tc.Key, makePane func() (t.Primitive, string)) {
p, name := makePane()
panes = append(panes, paneInfo{name: name, view: p})
// Add to hotkeys
hotkeys = append(hotkeys, hotkeyInfo{
key: hotkey,
label: name,
handler: func() {
showPane(p)
},
})
}
func showPane(pane t.Primitive) {
contentView.Clear()
contentView.AddItem(pane, 0, 1, true)
application.SetFocus(pane)
}
func showPaneByName(name string) {
for _, p := range panes {
if p.name == name {
showPane(p.view)
return
}
}
panic(fmt.Sprintf("Pane '%s' not found\n", name))
}
func main() {
application = t.NewApplication()
// Build panes
// TODO: Consider to make this lazy
addPane(tc.KeyF1, makeHashPane)
addPane(tc.KeyF2, makeHmacPane)
addPane(tc.KeyF3, makeEncodePane)
addPane(tc.KeyF4, makeCalcPane)
addPane(tc.KeyF5, makeIeeePane)
addPane(tc.KeyF6, func() (t.Primitive, string) {
p := float.MakePane()
return p.GetView(), p.GetName()
})
addPane(tc.KeyF7, func() (t.Primitive, string) {
p := shape.MakePane()
return p.GetView(), p.GetName()
})
contentView = NewFlexRow()
rootView = NewFlexColumn().
AddItem(t.NewTextView().SetText("retool v0.0.1 beta =)"), 1, 0, false).
AddItem(contentView, 0, 1, false).
AddItem(makeHotkeyLine(), 1, 0, true)
rootView.SetInputCapture(func(event *tc.EventKey) *tc.EventKey {
for _, hk := range hotkeys {
if event.Key() == hk.key {
if hk.handler != nil {
hk.handler()
}
return nil
}
}
return event
})
application.
SetRoot(rootView, true).
EnableMouse(true)
showPaneByName("Shape")
if err := application.Run(); err != nil {
panic(err)
}
}