-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathviews.go
191 lines (162 loc) · 4.16 KB
/
views.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
package main
import (
"fmt"
"github.com/jroimartin/gocui"
)
const (
logView = "Log"
input = "Input"
info = "Info"
popup = "Popup"
prompt = "Prompt"
promptSymbol = "PromptSymbol"
errorView = "Errors"
search = "Search"
searchSymbol = "SearchSymbol"
searchOutline = "SearchOutline"
)
const stockDivisor = 2
const (
inputHeight = 4
inputCursorPos = 12
searchEntryHeight = 3
searchCursorPos = 4
)
// viewDrawer represents the user's screen size according to the number of
// rows and columns of the console's fixed-width font.
type viewDrawer struct {
maxX int
maxY int
}
// layout handles the creation of all gocui views.
func (vd *viewDrawer) layout(g *gocui.Gui) (err error) {
vd.maxX, vd.maxY = g.Size()
if err = vd.makeLogPanel(); err != nil {
logFile.Fatal(err)
return
}
if err = vd.makePromptPanels(); err != nil {
logFile.Fatal(err)
return
}
if err = vd.makeInfoPanel(); err != nil {
logFile.Fatal(err)
return
}
if err = vd.makeSelectOptionsPopup(); err != nil {
logFile.Fatal(err)
return
}
return
}
// makeLogPanel creates the logging view, which logs all user actions.
func (vd *viewDrawer) makeLogPanel() error {
viewHeight := vd.maxY - inputHeight
logWidth := float64(vd.maxX) - float64(vd.maxX)/stockDivisor
x0 := 0
x1 := int(logWidth)
y0 := 0
y1 := viewHeight
if v, err := g.SetView(logView, x0, y0, x1, y1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Wrap = true
v.Title = "Log"
v.Autoscroll = true
logGui.Out = v
}
return nil
}
// makeSelectOptionsPopup creates the popup view for option selection.
func (vd *viewDrawer) makeSelectOptionsPopup() error {
w := vd.maxX / 2
h := vd.maxY / 4
x0 := (vd.maxX / 2) - (w / 2)
y0 := (vd.maxY / 2) - (h / 2)
x1 := x0 + w
y1 := y0 + h
if v, err := g.SetView(popup, x0, y0, x1, y1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = true
v.Highlight = true
//TODO: Set Selected FG and BG colors
g.SetViewOnBottom(popup)
}
if v, err := g.SetView(search, x0+searchCursorPos, y0+h+1, x1, y0+h+searchEntryHeight); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Editable = true
v.Wrap = false
v.Frame = false
v.Editor = gocui.EditorFunc(promptEditor)
g.SetViewOnBottom(search)
}
if v, err := g.SetView(searchSymbol, x0, y0+h+1, x0+searchCursorPos, y0+h+searchEntryHeight); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
fmt.Fprintf(v, ">>")
g.SetViewOnBottom(searchSymbol)
}
if v, err := g.SetView(searchOutline, x0, y0+h, x1, y0+h+searchEntryHeight); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = true
g.SetViewOnBottom(searchOutline)
}
return nil
}
// makeInfoPanel creates the info view.
func (vd *viewDrawer) makeInfoPanel() error {
viewHeight := vd.maxY - inputHeight
infoStart := float64(vd.maxX) - float64(vd.maxX)/stockDivisor
if v, err := g.SetView(info, int(infoStart), 0, vd.maxX-2, viewHeight); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = "Stock"
v.Wrap = true
refreshInventory()
}
return nil
}
// makePromptPanels creates the main prompt view, which contains the input
// line and the keybinding hints.
func (vd *viewDrawer) makePromptPanels() error {
promptStartHeight := vd.maxY - inputHeight
promptDividerHeight := vd.maxY - (inputHeight / 2)
promptString := generateKeybindString(quantity)
if v, err := g.SetView(prompt, 0, promptStartHeight, vd.maxX, promptDividerHeight); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
fmt.Fprintf(v, promptString)
}
if v, err := g.SetView(input, inputCursorPos, promptDividerHeight, vd.maxX, vd.maxY); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
v.Editable = true
v.Wrap = false
v.Editor = gocui.EditorFunc(promptEditor)
if _, err := g.SetCurrentView(input); err != nil {
return err
}
}
if v, err := g.SetView(promptSymbol, 0, promptDividerHeight, inputCursorPos, vd.maxY); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Frame = false
updatePromptSymbol()
}
return nil
}