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

     1  package docker
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/docker/client"
     7  	"github.com/pkg/errors"
     8  	"github.com/rivo/tview"
     9  	"github.com/wtfutil/wtf/view"
    10  )
    11  
    12  type Widget struct {
    13  	view.TextWidget
    14  	cli           *client.Client
    15  	settings      *Settings
    16  	displayBuffer string
    17  }
    18  
    19  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, pages *tview.Pages, settings *Settings) *Widget {
    20  	widget := Widget{
    21  		TextWidget: view.NewTextWidget(tviewApp, redrawChan, pages, settings.Common),
    22  		settings:   settings,
    23  	}
    24  
    25  	widget.View.SetScrollable(true)
    26  
    27  	cli, err := client.NewClientWithOpts()
    28  	if err != nil {
    29  		widget.displayBuffer = errors.Wrap(err, "could not create client").Error()
    30  	} else {
    31  		widget.cli = cli
    32  	}
    33  
    34  	widget.refreshDisplayBuffer()
    35  
    36  	return &widget
    37  }
    38  
    39  /* -------------------- Exported Functions -------------------- */
    40  
    41  func (widget *Widget) Refresh() {
    42  	widget.refreshDisplayBuffer()
    43  	widget.Redraw(widget.display)
    44  }
    45  
    46  /* -------------------- Unexported Functions -------------------- */
    47  
    48  func (widget *Widget) display() (string, string, bool) {
    49  	return widget.CommonSettings().Title, widget.displayBuffer, true
    50  }
    51  
    52  func (widget *Widget) refreshDisplayBuffer() {
    53  	if widget.cli == nil {
    54  		return
    55  	}
    56  
    57  	widget.displayBuffer = ""
    58  
    59  	widget.displayBuffer += fmt.Sprintf("[%s] System[white]\n", widget.settings.Colors.Subheading)
    60  	widget.displayBuffer += widget.getSystemInfo()
    61  
    62  	widget.displayBuffer += "\n"
    63  
    64  	widget.displayBuffer += fmt.Sprintf("[%s] Containers[white]\n", widget.settings.Colors.Subheading)
    65  	widget.displayBuffer += widget.getContainerStates()
    66  }