github.com/wtfutil/wtf@v0.43.0/modules/digitalocean/display.go (about)

     1  package digitalocean
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/wtfutil/wtf/utils"
     7  )
     8  
     9  const maxColWidth = 12
    10  
    11  func (widget *Widget) content() (string, string, bool) {
    12  	columnSet := widget.settings.columns
    13  
    14  	title := widget.CommonSettings().Title
    15  	if widget.err != nil {
    16  		return title, widget.err.Error(), true
    17  	}
    18  
    19  	if len(columnSet) < 1 {
    20  		return title, " no columns defined", false
    21  	}
    22  
    23  	str := fmt.Sprintf(" [::b][%s]", widget.settings.Colors.Subheading)
    24  
    25  	for _, colName := range columnSet {
    26  		truncName := utils.Truncate(colName, maxColWidth, false)
    27  
    28  		str += fmt.Sprintf("%-12s", truncName)
    29  	}
    30  
    31  	str += "\n"
    32  
    33  	for idx, droplet := range widget.droplets {
    34  		// This defines the formatting for the row, one tab-separated string for each defined column
    35  		fmtStr := " [%s]"
    36  
    37  		for range columnSet {
    38  			fmtStr += "%-12s"
    39  		}
    40  
    41  		vals := []interface{}{
    42  			widget.RowColor(idx),
    43  		}
    44  
    45  		// Dynamically access the droplet to get the requested columns values
    46  		for _, colName := range columnSet {
    47  			val, err := droplet.StringValueForProperty(colName)
    48  			if err != nil {
    49  				val = "???"
    50  			}
    51  
    52  			truncVal := utils.Truncate(val, maxColWidth, false)
    53  
    54  			vals = append(vals, truncVal)
    55  		}
    56  
    57  		// And format, print, and color the row
    58  		row := fmt.Sprintf(fmtStr, vals...)
    59  		str += utils.HighlightableHelper(widget.View, row, idx, 33)
    60  	}
    61  
    62  	return title, str, false
    63  }
    64  
    65  func (widget *Widget) display() {
    66  	widget.ScrollableWidget.Redraw(widget.content)
    67  }