-
Notifications
You must be signed in to change notification settings - Fork 12
/
itemview.go
229 lines (205 loc) · 6.98 KB
/
itemview.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package views
import (
"bytes"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/lawrencegripper/azbrowse/internal/pkg/eventing"
"github.com/lawrencegripper/azbrowse/internal/pkg/expanders"
"github.com/stuartleeks/colorjson"
"github.com/stuartleeks/gocui"
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/quick"
"github.com/alecthomas/chroma/styles"
)
// ItemWidget is response for showing the text response from the Rest requests
type ItemWidget struct {
x, y int
w, h int
hideGuids bool
content string
contentType expanders.ExpanderResponseType
view *gocui.View
g *gocui.Gui
}
// NewItemWidget creates a new instance of ItemWidget
func NewItemWidget(x, y, w, h int, hideGuids bool, content string) *ItemWidget {
configureYAMLHighlighting()
return &ItemWidget{x: x, y: y, w: w, h: h, hideGuids: hideGuids, content: content}
}
// Layout draws the widget in the gocui view
func (w *ItemWidget) Layout(g *gocui.Gui) error {
w.g = g
v, err := g.SetView("itemWidget", w.x, w.y, w.x+w.w, w.y+w.h)
if err != nil && err != gocui.ErrUnknownView {
return err
}
v.Editable = true
v.Wrap = true
w.view = v
width, height := v.Size()
expanders.ItemWidgetHeight = height
expanders.ItemWidgetWidth = width
v.Clear()
if w.content == "" {
return nil
}
if w.hideGuids {
if w.hideGuids {
w.content = stripSecretVals(w.content)
}
}
switch w.contentType {
case expanders.ResponseJSON:
d := json.NewDecoder(strings.NewReader(w.content))
d.UseNumber()
var obj interface{}
err = d.Decode(&obj)
if err != nil {
eventing.SendStatusEvent(eventing.StatusEvent{
InProgress: false,
Failure: true,
Message: "Failed to display as JSON: " + err.Error(),
Timeout: time.Duration(time.Second * 4),
})
fmt.Fprint(v, w.content)
return nil
}
f := colorjson.NewFormatter()
f.Indent = 2
s, err := f.Marshal(obj)
if err != nil {
fmt.Fprint(v, w.content)
} else {
fmt.Fprint(v, string(s))
}
case expanders.ResponseYAML:
var buf bytes.Buffer
err = quick.Highlight(&buf, w.content, "YAML-azbrowse", "terminal", "azbrowse")
if err != nil {
fmt.Fprint(v, w.content)
} else {
fmt.Fprint(v, buf.String())
}
default:
fmt.Fprint(v, w.content)
}
return nil
}
// PageDown move the view down a page
func (w *ItemWidget) PageDown() {
_, maxHeight := w.view.Size()
x, y := w.view.Origin()
w.view.SetCursor(0, 0) //nolint: errcheck
maxY := strings.Count(w.content, "\n")
y = y + maxHeight
if y > maxY {
y = maxY
}
err := w.view.SetOrigin(x, y)
if err != nil {
eventing.SendStatusEvent(eventing.StatusEvent{
InProgress: false,
Failure: true,
Message: "Failed to execute pagedown: " + err.Error(),
Timeout: time.Duration(time.Second * 4),
})
}
}
// PageUp move the view a page up
func (w *ItemWidget) PageUp() {
_, maxHeight := w.view.Size()
x, y := w.view.Origin()
w.view.SetCursor(0, 0) //nolint: errcheck
y = y - maxHeight
// Check we haven't overshot
if y < 0 {
y = 0
}
err := w.view.SetOrigin(x, y)
if err != nil {
eventing.SendStatusEvent(eventing.StatusEvent{
InProgress: false,
Failure: true,
Message: "Failed to execute pagedown: " + err.Error(),
Timeout: time.Duration(time.Second * 4),
})
}
}
// SetContent displays the string in the itemview
func (w *ItemWidget) SetContent(content string, contentType expanders.ExpanderResponseType, title string) {
w.g.Update(func(g *gocui.Gui) error {
w.content = content
w.contentType = contentType
// Reset the cursor and origin (scroll poisition)
// so we don't start at the bottom of a new doc
w.view.SetCursor(0, 0) //nolint: errcheck
w.view.SetOrigin(0, 0) //nolint: errcheck
w.view.Title = title
return nil
})
}
// GetContent returns the current content
func (w *ItemWidget) GetContent() string {
return w.content
}
// GetContentType returns the current content type
func (w *ItemWidget) GetContentType() expanders.ExpanderResponseType {
return w.contentType
}
func configureYAMLHighlighting() {
lexer := chroma.MustNewLexer(
&chroma.Config{
Name: "YAML-azbrowse",
Aliases: []string{"yaml"},
Filenames: []string{"*.yaml", "*.yml"},
MimeTypes: []string{"text/x-yaml"},
},
chroma.Rules{
"root": {
chroma.Include("whitespace"),
{`#.*`, chroma.Comment, nil}, //nolint:govet
{`!![^\s]+`, chroma.CommentPreproc, nil}, //nolint:govet
{`&[^\s]+`, chroma.CommentPreproc, nil}, //nolint:govet
{`\*[^\s]+`, chroma.CommentPreproc, nil}, //nolint:govet
{`^%include\s+[^\n\r]+`, chroma.CommentPreproc, nil}, //nolint:govet
{`([>|+-]\s+)(\s+)((?:(?:.*?$)(?:[\n\r]*?)?)*)`, chroma.ByGroups(chroma.StringDoc, chroma.StringDoc, chroma.StringDoc), nil}, //nolint:govet
chroma.Include("value"), //nolint:govet
{`[?:,\[\]]`, chroma.Punctuation, nil}, //nolint:govet
{`.`, chroma.Text, nil}, //nolint:govet
},
"value": {
{chroma.Words(``, `\b`, "true", "false", "null"), chroma.KeywordConstant, nil}, //nolint:govet
{`"(?:\\.|[^"])*"`, chroma.StringDouble, nil}, //nolint:govet
{`'(?:\\.|[^'])*'`, chroma.StringSingle, nil}, //nolint:govet
{`\d\d\d\d-\d\d-\d\d([T ]\d\d:\d\d:\d\d(\.\d+)?(Z|\s+[-+]\d+)?)?`, chroma.LiteralDate, nil}, //nolint:govet
{`\b[+\-]?(0x[\da-f]+|0o[0-7]+|(\d+\.?\d*|\.?\d+)(e[\+\-]?\d+)?|\.inf|\.nan)\b`, chroma.Number, nil}, //nolint:govet
{`\b([\w]+)([ \t]*)([:]+)([ \t]*)(\d+\.?\d*|\.?\d+)(\s)`, chroma.ByGroups(chroma.Text, chroma.Whitespace, chroma.Punctuation, chroma.Whitespace, chroma.Number, chroma.Whitespace), nil}, //nolint:govet
{`\b([\w]+)([ \t]*)([:]+)([ \t]*)(true)\b`, chroma.ByGroups(chroma.Text, chroma.Whitespace, chroma.Punctuation, chroma.Whitespace, chroma.LiteralStringBoolean), nil}, //nolint:govet
{`\b([\w]+)([ \t]*)([:]+)([ \t]*)(false)\b`, chroma.ByGroups(chroma.Text, chroma.Whitespace, chroma.Punctuation, chroma.Whitespace, chroma.LiteralStringBoolean), nil}, //nolint:govet
{`\b([\w]+)([ \t]*)([:]+)([ \t]*)([\w\./\-_]+)\b`, chroma.ByGroups(chroma.Text, chroma.Whitespace, chroma.Punctuation, chroma.Whitespace, chroma.StringDouble), nil}, //nolint:govet
// {`\b(?:[\w]+)(?:[:\b]+)(?:[\w+])\b`, ByGroups(KeywordReserved, Punctuation, StringDouble), nil},
{`\b[\w]+\b`, chroma.Text, nil}, //nolint:govet
},
"whitespace": {
{`\s+`, chroma.Whitespace, nil}, //nolint:govet
},
},
)
lexers.Register(lexer)
style := chroma.MustNewStyle(
"azbrowse",
chroma.StyleEntries{
chroma.LiteralString: "#00aa00",
chroma.LiteralStringBoolean: "#b3760e",
chroma.LiteralNumber: "#0099ff",
},
)
styles.Register(style)
}
// SetHideGuids sets the HideGuids option
func (w *ItemWidget) SetHideGuids(value bool) {
w.hideGuids = value
}