github.com/wtfutil/wtf@v0.43.0/modules/opsgenie/widget.go (about)

     1  package opsgenie
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/rivo/tview"
     8  	"github.com/wtfutil/wtf/utils"
     9  	"github.com/wtfutil/wtf/view"
    10  )
    11  
    12  type Widget struct {
    13  	view.TextWidget
    14  
    15  	settings *Settings
    16  }
    17  
    18  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget {
    19  	widget := Widget{
    20  		TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.Common),
    21  
    22  		settings: settings,
    23  	}
    24  
    25  	return &widget
    26  }
    27  
    28  /* -------------------- Exported Functions -------------------- */
    29  
    30  func (widget *Widget) Refresh() {
    31  	widget.Redraw(widget.content)
    32  }
    33  
    34  /* -------------------- Unexported Functions -------------------- */
    35  
    36  func (widget *Widget) content() (string, string, bool) {
    37  	onCallResponses, err := widget.Fetch(
    38  		widget.settings.scheduleIdentifierType,
    39  		widget.settings.schedule,
    40  	)
    41  	title := widget.CommonSettings().Title
    42  
    43  	var content string
    44  	wrap := false
    45  	if err != nil {
    46  		wrap = true
    47  		content = err.Error()
    48  	} else {
    49  
    50  		for _, data := range onCallResponses {
    51  			if (len(data.OnCallData.Recipients) == 0) && !widget.settings.displayEmpty {
    52  				continue
    53  			}
    54  
    55  			var msg string
    56  			if len(data.OnCallData.Recipients) == 0 {
    57  				msg = " [gray]no one[white]\n\n"
    58  			} else {
    59  				msg = fmt.Sprintf(" %s\n\n", strings.Join(utils.NamesFromEmails(data.OnCallData.Recipients), ", "))
    60  			}
    61  
    62  			content += widget.cleanScheduleName(data.OnCallData.Parent.Name)
    63  			content += msg
    64  		}
    65  	}
    66  
    67  	return title, content, wrap
    68  }
    69  
    70  func (widget *Widget) cleanScheduleName(schedule string) string {
    71  	cleanedName := strings.ReplaceAll(schedule, "_", " ")
    72  	return fmt.Sprintf(" [green]%s[white]\n", cleanedName)
    73  }