-
Notifications
You must be signed in to change notification settings - Fork 34
/
screen_dialog.go
121 lines (106 loc) Β· 3.04 KB
/
screen_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
// "fmt"
"github.com/nsf/termbox-go"
"strings"
)
const HEAVY_VERTICAL_RIGHT = "β£"
const HEAVY_VERTICAL_LEFT = "β«"
const HEAVY_VERTICAL = "β"
const HEAVY_HORIZONTAL = "β"
const HEAVY_TOP_LEFT = "β"
const HEAVY_TOP_RIGHT = "β"
const HEAVY_BOTTOM_LEFT = "β"
const HEAVY_BOTTOM_RIGHT = "β"
type DialogProc func(event termbox.Event, output chan<- interface{}) bool
type DialogScreen struct {
width int
height int
top int
left int
bottom int
right int
caption string
text string
callback DialogProc
}
func (screen *DialogScreen) receiveEvents(input <-chan termbox.Event, output chan<- interface{}, quit <-chan bool) {
for {
do_quit := false
select {
case event := <-input:
do_quit = screen.callback(event, output)
case <-quit:
do_quit = true
}
if do_quit {
break
}
}
}
func (screen *DialogScreen) performLayout() {
width, height := termbox.Size()
screen.top = Max((height-screen.height)/2, 0)
screen.left = Max((width-screen.width)/2, 0)
screen.bottom = Min(screen.top+screen.height, height)
screen.right = Min(screen.left+screen.width, width)
}
func (screen *DialogScreen) drawScreen(style Style) {
fg := style.default_fg
bg := style.default_bg
drawStringAtPoint(screen.caption, screen.left+2, screen.top+1, fg, bg)
for i, s := range strings.Split(screen.text, "\n") {
drawStringAtPoint(s, screen.left+5, screen.top+4+i, fg, bg)
}
for row := screen.top; row < screen.bottom; row++ {
if row == screen.bottom-1 {
drawStringAtPoint(HEAVY_BOTTOM_LEFT, screen.left, row, fg, bg)
drawStringAtPoint(HEAVY_BOTTOM_RIGHT, screen.right, row, fg, bg)
} else if row == screen.top+2 {
drawStringAtPoint(HEAVY_VERTICAL_RIGHT, screen.left, row, fg, bg)
drawStringAtPoint(HEAVY_VERTICAL_LEFT, screen.right, row, fg, bg)
} else if row == screen.top {
drawStringAtPoint(HEAVY_TOP_LEFT, screen.left, row, fg, bg)
drawStringAtPoint(HEAVY_TOP_RIGHT, screen.right, row, fg, bg)
} else {
drawStringAtPoint(HEAVY_VERTICAL, screen.left, row, fg, bg)
drawStringAtPoint(HEAVY_VERTICAL, screen.right, row, fg, bg)
}
for col := screen.left + 1; col < screen.right; col++ {
if row == screen.top || row == screen.top+2 || row == screen.bottom-1 {
drawStringAtPoint(HEAVY_HORIZONTAL, col, row, fg, bg)
}
}
}
}
func NewDialogScreen(caption, text string, width, height int, callback DialogProc) *DialogScreen {
return &DialogScreen{
text: text,
width: width,
height: height,
caption: caption,
callback: callback,
}
}
func ShowModal(caption, text string, callback DialogProc) *DialogScreen {
return NewDialogScreen(caption, text, 50, 8, callback)
}
func ShowMessage(caption, text string) *DialogScreen {
return NewDialogScreen(caption, text, 70, 8, defaultDialogCloseCallback)
}
func defaultDialogCloseCallback(event termbox.Event, output chan<- interface{}) bool {
output <- ScreenIndex(DATA_SCREEN_INDEX)
return true
}
func Max(a, b int) int {
if a > b {
return a
}
return b
}
func Min(a, b int) int {
if a < b {
return a
}
return b
}