1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/digitalocean/display.go
Chris Cummer f84142553c
WTF-986 User-definable DigitalOcean columns (#1001)
* WTF-986 Wrap the DigitalOcean droplet in our own droplet

This gives us something to build off while still providing the
underlying functionality of the original droplet instance.

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-986 Dynamically display droplet attributes based on defined column names

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-986 Read DigitalOcean column configuration from settings

Signed-off-by: Chris Cummer <chriscummer@me.com>

* WTF-986 Extract the reflection bits into a Reflective package

Signed-off-by: Chris Cummer <chriscummer@me.com>
2020-10-14 09:29:58 -07:00

68 lines
1.4 KiB
Go

package digitalocean
import (
"fmt"
"github.com/wtfutil/wtf/utils"
)
const maxColWidth = 12
func (widget *Widget) content() (string, string, bool) {
columnSet := widget.settings.columns
title := widget.CommonSettings().Title
if widget.err != nil {
return title, widget.err.Error(), true
}
if len(columnSet) < 1 {
return title, " no columns defined", false
}
str := fmt.Sprintf(" [::b][%s]", widget.settings.common.Colors.Subheading)
for _, colName := range columnSet {
truncName := utils.Truncate(colName, maxColWidth, false)
str += fmt.Sprintf("%-12s", truncName)
}
str += "\n"
for idx, droplet := range widget.droplets {
// This defines the formatting for the row, one tab-seperated string for each defined column
fmtStr := " [%s]"
for range columnSet {
fmtStr += "%-12s"
}
vals := []interface{}{
widget.RowColor(idx),
}
// Dynamically access the droplet to get the requested columns values
for _, colName := range columnSet {
val, err := droplet.StringValueForProperty(colName)
if err != nil {
val = "???"
}
truncVal := utils.Truncate(val, maxColWidth, false)
vals = append(vals, truncVal)
}
// And format, print, and color the row
row := fmt.Sprintf(fmtStr, vals...)
str += utils.HighlightableHelper(widget.View, row, idx, 33)
}
return title, str, false
}
func (widget *Widget) display() {
widget.ScrollableWidget.Redraw(widget.content)
}