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

     1  package victorops
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rivo/tview"
     7  	"github.com/wtfutil/wtf/view"
     8  )
     9  
    10  // Widget contains text info
    11  type Widget struct {
    12  	view.TextWidget
    13  
    14  	teams    []OnCallTeam
    15  	settings *Settings
    16  	err      error
    17  }
    18  
    19  // NewWidget creates a new widget
    20  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget {
    21  	widget := Widget{
    22  		TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.Common),
    23  		settings:   settings,
    24  	}
    25  
    26  	widget.View.SetScrollable(true)
    27  	widget.View.SetRegions(true)
    28  
    29  	return &widget
    30  }
    31  
    32  // Refresh gets latest content for the widget
    33  func (widget *Widget) Refresh() {
    34  	if widget.Disabled() {
    35  		return
    36  	}
    37  
    38  	teams, err := Fetch(widget.settings.apiID, widget.settings.apiKey)
    39  
    40  	widget.err = err
    41  	widget.teams = teams
    42  
    43  	widget.Redraw(widget.content)
    44  }
    45  
    46  func (widget *Widget) content() (string, string, bool) {
    47  	title := widget.CommonSettings().Title
    48  	if widget.err != nil {
    49  		return title, widget.err.Error(), true
    50  	}
    51  	teams := widget.teams
    52  	var str string
    53  
    54  	if len(teams) == 0 {
    55  		return title, "No teams specified", false
    56  	}
    57  
    58  	for _, team := range teams {
    59  		if len(widget.settings.team) > 0 && widget.settings.team != team.Slug {
    60  			continue
    61  		}
    62  
    63  		str = fmt.Sprintf("%s[green]%s\n", str, team.Name)
    64  		if len(team.OnCall) == 0 {
    65  			str = fmt.Sprintf("%s[grey]no one\n", str)
    66  		}
    67  		for _, onCall := range team.OnCall {
    68  			str = fmt.Sprintf("%s[white]%s - %s\n", str, onCall.Policy, onCall.Userlist)
    69  		}
    70  
    71  		str = fmt.Sprintf("%s\n", str)
    72  	}
    73  
    74  	if str == "" {
    75  		str = "Could not find any teams to display"
    76  	}
    77  	return title, str, false
    78  }