github.com/wtfutil/wtf@v0.43.0/modules/rollbar/widget.go (about) 1 package rollbar 2 3 import ( 4 "fmt" 5 6 "github.com/rivo/tview" 7 "github.com/wtfutil/wtf/utils" 8 "github.com/wtfutil/wtf/view" 9 ) 10 11 // A Widget represents a Rollbar widget 12 type Widget struct { 13 view.ScrollableWidget 14 15 items *Result 16 settings *Settings 17 err error 18 } 19 20 // NewWidget creates a new instance of a widget 21 func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget { 22 widget := Widget{ 23 ScrollableWidget: view.NewScrollableWidget(tviewApp, redrawChan, pages, settings.Common), 24 25 settings: settings, 26 } 27 28 widget.SetRenderFunction(widget.Render) 29 widget.initializeKeyboardControls() 30 31 return &widget 32 } 33 34 /* -------------------- Exported Functions -------------------- */ 35 36 func (widget *Widget) Refresh() { 37 if widget.Disabled() { 38 return 39 } 40 41 items, err := CurrentActiveItems( 42 widget.settings.accessToken, 43 widget.settings.assignedToName, 44 widget.settings.activeOnly, 45 ) 46 47 if err != nil { 48 widget.err = err 49 widget.items = nil 50 widget.SetItemCount(0) 51 } else { 52 widget.items = &items.Results 53 widget.SetItemCount(len(widget.items.Items)) 54 } 55 56 widget.Render() 57 } 58 59 /* -------------------- Unexported Functions -------------------- */ 60 61 func (widget *Widget) Render() { 62 widget.Redraw(widget.content) 63 } 64 65 func (widget *Widget) content() (string, string, bool) { 66 title := fmt.Sprintf("%s - %s", widget.CommonSettings().Title, widget.settings.projectName) 67 if widget.err != nil { 68 return widget.CommonSettings().Title, widget.err.Error(), true 69 } 70 result := widget.items 71 if result == nil || len(result.Items) == 0 { 72 return title, "No results", false 73 } 74 var str string 75 if len(result.Items) > widget.settings.count { 76 result.Items = result.Items[:widget.settings.count] 77 } 78 for idx, item := range result.Items { 79 80 row := fmt.Sprintf( 81 "[%s] [%s] %s [%s] %s [%s]count: %d [%s]%s", 82 widget.RowColor(idx), 83 levelColor(&item), 84 item.Level, 85 statusColor(&item), 86 item.Title, 87 widget.RowColor(idx), 88 item.TotalOccurrences, 89 "blue", 90 item.Environment, 91 ) 92 str += utils.HighlightableHelper(widget.View, row, idx, len(item.Title)) 93 } 94 95 return title, str, false 96 } 97 98 func statusColor(item *Item) string { 99 switch item.Status { 100 case "active": 101 return "red" 102 case "resolved": 103 return "green" 104 default: 105 return "red" 106 } 107 } 108 func levelColor(item *Item) string { 109 switch item.Level { 110 case "error": 111 return "red" 112 case "critical": 113 return "green" 114 case "warning": 115 return "yellow" 116 default: 117 return "grey" 118 } 119 } 120 121 func (widget *Widget) openBuild() { 122 if widget.GetSelected() >= 0 && widget.items != nil && widget.GetSelected() < len(widget.items.Items) { 123 item := &widget.items.Items[widget.GetSelected()] 124 125 utils.OpenFile( 126 fmt.Sprintf( 127 "https://rollbar.com/%s/%s/%s/%d", 128 widget.settings.projectOwner, 129 widget.settings.projectName, 130 "items", 131 item.ID, 132 ), 133 ) 134 } 135 }