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

     1  package pihole
     2  
     3  import (
     4  	"strings"
     5  	"time"
     6  
     7  	"github.com/rivo/tview"
     8  	"github.com/wtfutil/wtf/view"
     9  )
    10  
    11  type Widget struct {
    12  	view.MultiSourceWidget
    13  	view.TextWidget
    14  
    15  	settings *Settings
    16  }
    17  
    18  // NewWidget creates a new instance of a widget
    19  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, _ *tview.Pages, settings *Settings) *Widget {
    20  	widget := Widget{
    21  		TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.Common),
    22  		settings:   settings,
    23  	}
    24  
    25  	widget.settings.RefreshInterval = 30 * time.Second
    26  	widget.initializeKeyboardControls()
    27  	widget.SetDisplayFunction(widget.Refresh)
    28  	widget.View.SetWordWrap(true)
    29  	widget.View.SetWrap(settings.wrapText)
    30  
    31  	return &widget
    32  }
    33  
    34  /* -------------------- Exported Functions -------------------- */
    35  
    36  func (widget *Widget) Refresh() {
    37  	if widget.Disabled() {
    38  		return
    39  	}
    40  
    41  	widget.Redraw(widget.content)
    42  }
    43  
    44  /* -------------------- Unexported Functions -------------------- */
    45  
    46  func (widget *Widget) content() (string, string, bool) {
    47  	title := widget.CommonSettings().Title
    48  
    49  	c := getClient()
    50  
    51  	if err := checkServer(c, widget.settings.apiUrl); err != nil {
    52  		return title, err.Error(), widget.settings.wrapText
    53  	}
    54  
    55  	var sb strings.Builder
    56  
    57  	if widget.settings.showSummary {
    58  		sb.WriteString(getSummaryView(c, widget.settings))
    59  	}
    60  
    61  	if widget.settings.showTopItems > 0 {
    62  		sb.WriteString(getTopItemsView(c, widget.settings))
    63  	}
    64  
    65  	if widget.settings.showTopClients > 0 {
    66  		sb.WriteString(getTopClientsView(c, widget.settings))
    67  	}
    68  
    69  	output := sb.String()
    70  
    71  	return title, output, widget.settings.wrapText
    72  }
    73  
    74  func (widget *Widget) disable() {
    75  	widget.adblockSwitch("disable")
    76  }
    77  
    78  func (widget *Widget) enable() {
    79  	widget.adblockSwitch("enable")
    80  }