forked from wtfutil/wtf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
widget.go
135 lines (114 loc) · 2.81 KB
/
widget.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
package rollbar
import (
"fmt"
"github.com/rivo/tview"
"github.com/doctorfree/wtf/utils"
"github.com/doctorfree/wtf/view"
)
// A Widget represents a Rollbar widget
type Widget struct {
view.ScrollableWidget
items *Result
settings *Settings
err error
}
// NewWidget creates a new instance of a widget
func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common),
settings: settings,
}
widget.SetRenderFunction(widget.Render)
widget.initializeKeyboardControls()
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
items, err := CurrentActiveItems(
widget.settings.accessToken,
widget.settings.assignedToName,
widget.settings.activeOnly,
)
if err != nil {
widget.err = err
widget.items = nil
widget.SetItemCount(0)
} else {
widget.items = &items.Results
widget.SetItemCount(len(widget.items.Items))
}
widget.Render()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) Render() {
widget.Redraw(widget.content)
}
func (widget *Widget) content() (string, string, bool) {
title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.settings.projectName)
if widget.err != nil {
return widget.CommonSettings().Title, widget.err.Error(), true
}
result := widget.items
if result == nil || len(result.Items) == 0 {
return title, "No results", false
}
var str string
if len(result.Items) > widget.settings.count {
result.Items = result.Items[:widget.settings.count]
}
for idx, item := range result.Items {
row := fmt.Sprintf(
"[%s] [%s] %s [%s] %s [%s]count: %d [%s]%s",
widget.RowColor(idx),
levelColor(&item),
item.Level,
statusColor(&item),
item.Title,
widget.RowColor(idx),
item.TotalOccurrences,
"blue",
item.Environment,
)
str += utils.HighlightableHelper(widget.View, row, idx, len(item.Title))
}
return title, str, false
}
func statusColor(item *Item) string {
switch item.Status {
case "active":
return "red"
case "resolved":
return "green"
default:
return "red"
}
}
func levelColor(item *Item) string {
switch item.Level {
case "error":
return "red"
case "critical":
return "green"
case "warning":
return "yellow"
default:
return "grey"
}
}
func (widget *Widget) openBuild() {
if widget.GetSelected() >= 0 && widget.items != nil && widget.GetSelected() < len(widget.items.Items) {
item := &widget.items.Items[widget.GetSelected()]
utils.OpenFile(
fmt.Sprintf(
"https://rollbar.com/%s/%s/%s/%d",
widget.settings.projectOwner,
widget.settings.projectName,
"items",
item.ID,
),
)
}
}