mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	Prefer to have widgets force a draw when their data changes. This should reduce draws (unless the user has a module installed that updates >= 1/sec, the old draw default). This should also remove a source of some of the race conditions that users were experiencing (though not all, there are still many).
		
			
				
	
	
		
			73 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			73 lines
		
	
	
		
			1.5 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package opsgenie
 | |
| 
 | |
| import (
 | |
| 	"fmt"
 | |
| 	"strings"
 | |
| 
 | |
| 	"github.com/rivo/tview"
 | |
| 	"github.com/senorprogrammer/wtf/wtf"
 | |
| )
 | |
| 
 | |
| type Widget struct {
 | |
| 	wtf.TextWidget
 | |
| }
 | |
| 
 | |
| func NewWidget(app *tview.Application) *Widget {
 | |
| 	widget := Widget{
 | |
| 		TextWidget: wtf.NewTextWidget(app, "OpsGenie", "opsgenie", false),
 | |
| 	}
 | |
| 
 | |
| 	return &widget
 | |
| }
 | |
| 
 | |
| /* -------------------- Exported Functions -------------------- */
 | |
| 
 | |
| func (widget *Widget) Refresh() {
 | |
| 	data, err := Fetch()
 | |
| 
 | |
| 	widget.UpdateRefreshedAt()
 | |
| 	widget.View.SetTitle(widget.ContextualTitle(widget.Name))
 | |
| 
 | |
| 	var content string
 | |
| 	if err != nil {
 | |
| 		widget.View.SetWrap(true)
 | |
| 		content = err.Error()
 | |
| 	} else {
 | |
| 		widget.View.SetWrap(false)
 | |
| 		content = widget.contentFrom(data)
 | |
| 	}
 | |
| 
 | |
| 	widget.View.SetText(content)
 | |
| }
 | |
| 
 | |
| /* -------------------- Unexported Functions -------------------- */
 | |
| 
 | |
| func (widget *Widget) contentFrom(onCallResponse *OnCallResponse) string {
 | |
| 	str := ""
 | |
| 
 | |
| 	displayEmpty := wtf.Config.UBool("wtf.mods.opsgenie.displayEmpty", true)
 | |
| 
 | |
| 	for _, data := range onCallResponse.OnCallData {
 | |
| 		if (len(data.Recipients) == 0) && (displayEmpty == false) {
 | |
| 			continue
 | |
| 		}
 | |
| 
 | |
| 		var msg string
 | |
| 		if len(data.Recipients) == 0 {
 | |
| 			msg = " [gray]no one[white]\n\n"
 | |
| 		} else {
 | |
| 			msg = fmt.Sprintf(" %s\n\n", strings.Join(wtf.NamesFromEmails(data.Recipients), ", "))
 | |
| 		}
 | |
| 
 | |
| 		str = str + widget.cleanScheduleName(data.Parent.Name)
 | |
| 		str = str + msg
 | |
| 	}
 | |
| 
 | |
| 	return str
 | |
| }
 | |
| 
 | |
| func (widget *Widget) cleanScheduleName(schedule string) string {
 | |
| 	cleanedName := strings.Replace(schedule, "_", " ", -1)
 | |
| 	return fmt.Sprintf(" [green]%s[white]\n", cleanedName)
 | |
| }
 |