forked from jojomi/gonsole
/
dialog.go
103 lines (86 loc) · 2.51 KB
/
dialog.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
package main
import (
"github.com/nsf/termbox-go"
g "github.com/quantum/gonsole"
)
func openConfirmExit(app *g.App) {
title := "Exit"
message := "Are you sure you want to exit"
d := g.NewMessageDialog(app, "exit", title, message, []string{"Yes", "No"})
d.SetPosition(g.Position{"30%", "30%", "40%", "30%"})
d.OnClose(func() {
if d.SelectedButton() == 0 {
app.Stop()
}
})
}
func openConfirmInput(app *g.App, value string) {
title := "Are you sure?"
message := "Are you sure you want " + value
d := g.NewMessageDialog(app, "confirminput", title, message, []string{"Yes", "No"})
d.SetPosition(g.Position{"30%", "30%", "40%", "30%"})
d.OnClose(func() {
if d.SelectedButton() == 0 {
openSelectionDialog(app)
} else {
openInputDialog(app)
}
})
}
func openConfirmColor(app *g.App, value string) {
title := "Are you sure?"
message := "Are you sure you like the color " + value
d := g.NewMessageDialog(app, "confirmcolor", title, message, []string{"Yes", "No"})
d.SetPosition(g.Position{"30%", "30%", "40%", "30%"})
d.OnClose(func() {
if d.SelectedButton() == 0 {
app.Stop()
} else {
openSelectionDialog(app)
}
})
}
func openSelectionDialog(app *g.App) {
title := "Choose a color"
message := "What is your favorite color?"
options := []string{"Red", "Blue", "Green"}
d := g.NewSelectionDialog(app, "selection", title, message, []string{"OK", "Cancel"}, options)
d.SetPosition(g.Position{"15%", "25%", "70%", "40%"})
d.OnClose(func() {
if d.SelectedButton() == 0 {
openConfirmColor(app, options[d.SelectedItem()])
} else {
app.Stop()
}
})
}
func openInputDialog(app *g.App) {
title := "Enter Something"
message := "What do you like?"
d := g.NewInputDialog(app, "input", title, message, []string{"OK", "Cancel"})
d.SetPosition(g.Position{"15%", "25%", "70%", "40%"})
d.OnClose(func() {
if d.SelectedButton() == 0 {
openConfirmInput(app, d.InputValue())
} else {
app.Stop()
}
})
}
func main() {
app := g.NewApp()
app.CloseKey = termbox.KeyCtrlQ
app.AddEventListener(termbox.KeyF10, func() {
openConfirmExit(app)
})
infoBar := g.NewWindow(app, "info")
infoBar.SetBorderType(g.LineNone)
infoBar.SetShadowType(g.LineNone)
infoBar.Theme().SetBorder("focused.border", g.LineNone)
infoBar.SetPosition(g.Position{"0", "100%-1", "100%", "1"})
infoBarLabel := g.NewLabel(infoBar, infoBar, "infobar")
infoBarLabel.SetText("[Tab] to move, [Enter] to select, [F10] to exit")
infoBarLabel.SetPosition(g.Position{"0", "0", "100%", "100%"})
openInputDialog(app)
app.Run()
}