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

     1  package system
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/rivo/tview"
     8  	"github.com/wtfutil/wtf/utils"
     9  	"github.com/wtfutil/wtf/view"
    10  )
    11  
    12  type Widget struct {
    13  	view.TextWidget
    14  
    15  	Date    string
    16  	Version string
    17  
    18  	settings   *Settings
    19  	systemInfo *SystemInfo
    20  }
    21  
    22  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, date, version string, settings *Settings) *Widget {
    23  	widget := Widget{
    24  		TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.Common),
    25  
    26  		Date: date,
    27  
    28  		settings: settings,
    29  		Version:  version,
    30  	}
    31  
    32  	widget.systemInfo = NewSystemInfo()
    33  
    34  	return &widget
    35  }
    36  
    37  func (widget *Widget) display() (string, string, bool) {
    38  	content := fmt.Sprintf(
    39  		"%8s: %s\n%8s: %s\n\n%8s: %s\n%8s: %s",
    40  		"Built",
    41  		widget.prettyDate(),
    42  		"Vers",
    43  		widget.Version,
    44  		"OS",
    45  		widget.systemInfo.ProductVersion,
    46  		"Build",
    47  		widget.systemInfo.BuildVersion,
    48  	)
    49  
    50  	return widget.CommonSettings().Title, content, false
    51  }
    52  
    53  func (widget *Widget) Refresh() {
    54  	widget.Redraw(widget.display)
    55  }
    56  
    57  func (widget *Widget) prettyDate() string {
    58  	str, err := time.Parse(utils.TimestampFormat, widget.Date)
    59  
    60  	if err != nil {
    61  		return err.Error()
    62  	}
    63  
    64  	return str.Format("Jan _2, 15:04")
    65  }