-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreqmgr.go
121 lines (111 loc) · 2.87 KB
/
reqmgr.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 goublu ReqManager is the gocui manager for requestors.
package main
import (
"fmt"
"strings"
"github.com/jroimartin/gocui"
)
// ReqManager is a gocui Manager for Requestors.
type ReqManager struct {
UM *UbluManager
G *gocui.Gui
X int
Y int
Title string
BgColor gocui.Attribute
FgColor gocui.Attribute
ReqEditor gocui.Editor
Text string
UbluinBuf string
UbluoutBuf string
}
// NewReqManager instances a new manager.
func NewReqManager(um *UbluManager, g *gocui.Gui, x int, y int, title string, bgc gocui.Attribute, fgc gocui.Attribute, text string) (rm *ReqManager) {
rm = &ReqManager{
UM: um,
G: g,
X: x,
Y: y,
Title: title,
BgColor: bgc,
FgColor: fgc,
Text: text,
}
rm.ReqEditor = gocui.EditorFunc(func(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
gx, gy := rm.G.Size()
_, y1, _, y2 := rm.calcPos(gx, gy)
pgHeight := y2 - y1
switch {
case key == gocui.KeyArrowDown:
v.MoveCursor(0, 1, false)
case key == gocui.KeyArrowUp:
v.MoveCursor(0, -1, false)
case key == gocui.KeyArrowLeft:
v.MoveCursor(-1, 0, false)
case key == gocui.KeyArrowRight:
v.MoveCursor(1, 0, false)
case key == gocui.KeyPgup:
ox, oy := v.Origin()
v.SetOrigin(ox, Max(0, oy-(pgHeight-1)))
case key == gocui.KeyPgdn:
ox, oy := v.Origin()
v.SetOrigin(ox, oy+(pgHeight-1))
/*
case key == gocui.MouseWheelUp:
v.MoveCursor(0, -1, false)
case key == gocui.MouseWheelDown:
v.MoveCursor(0, 1, false)
*/
case key == gocui.KeyF3:
rm.EndReq()
}
})
return rm
}
func (rm *ReqManager) calcPos(maxX int, maxY int) (x1 int, y1 int, x2 int, y2 int) {
x1 = Max((maxX-rm.X)/2, 0)
x2 = maxX - x1
y1 = Max((maxY-rm.Y)/2, 0)
y2 = maxY - y1
return x1, y1, x2, y2
}
// EndReq unshows the requestor.
func (rm *ReqManager) EndReq() {
rm.G.SetManager(rm.UM)
rm.UM.Layout(rm.G)
v, _ := rm.G.View("Ubluin")
v.Clear()
for _, ch := range strings.Trim(strings.TrimSpace(rm.UbluinBuf), "\000") {
v.EditWrite(ch)
}
v, _ = rm.G.View("Ubluout")
fmt.Fprintf(v, "%s\n", strings.Trim(strings.TrimSpace(rm.UbluoutBuf), "\000"))
}
// StartReq shows the requestor.
func (rm *ReqManager) StartReq() {
v, _ := rm.G.View("Ubluin")
rm.UbluinBuf = v.Buffer()
v, _ = rm.G.View("Ubluout")
rm.UbluoutBuf = v.Buffer()
rm.G.SetManager(rm.UM, rm)
}
// Layout is the obligatory gocui layout redraw function.
func (rm *ReqManager) Layout(g *gocui.Gui) error {
x1, y1, x2, y2 := rm.calcPos(g.Size())
if v, err := g.SetView(rm.Title, x1, y1, x2, y2); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = " " + rm.Title + " "
v.Editable = true
v.Editor = rm.ReqEditor
v.Wrap = true
v.BgColor = rm.BgColor
v.FgColor = rm.FgColor
fmt.Fprintf(v, "%s", rm.Text)
}
if _, err := g.SetCurrentView(rm.Title); err != nil {
return err
}
return nil
}