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

     1  package gspreadsheets
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rivo/tview"
     7  	"github.com/wtfutil/wtf/utils"
     8  	"github.com/wtfutil/wtf/view"
     9  	sheets "google.golang.org/api/sheets/v4"
    10  )
    11  
    12  type Widget struct {
    13  	view.TextWidget
    14  
    15  	settings *Settings
    16  	cells    []*sheets.ValueRange
    17  	err      error
    18  }
    19  
    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  
    24  		settings: settings,
    25  	}
    26  
    27  	return &widget
    28  }
    29  
    30  /* -------------------- Exported Functions -------------------- */
    31  
    32  func (widget *Widget) Refresh() {
    33  	cells, err := widget.Fetch()
    34  	widget.err = err
    35  	widget.cells = cells
    36  
    37  	widget.Redraw(widget.content)
    38  }
    39  
    40  /* -------------------- Unexported Functions -------------------- */
    41  
    42  func (widget *Widget) content() (string, string, bool) {
    43  	title := widget.CommonSettings().Title
    44  	if widget.err != nil {
    45  		return title, widget.err.Error(), true
    46  	}
    47  
    48  	if widget.cells == nil {
    49  		return title, "No cells", false
    50  	}
    51  
    52  	res := ""
    53  
    54  	cells := utils.ToStrs(widget.settings.cellNames)
    55  	for i := 0; i < len(widget.cells); i++ {
    56  		res += fmt.Sprintf("%s\t[%s]%s\n", cells[i], widget.settings.colors.values, widget.cells[i].Values[0][0])
    57  	}
    58  
    59  	return title, res, false
    60  }