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

     1  package spacex
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	"github.com/rivo/tview"
     8  	"github.com/wtfutil/wtf/view"
     9  	"github.com/wtfutil/wtf/wtf"
    10  )
    11  
    12  type Widget struct {
    13  	view.TextWidget
    14  	settings *Settings
    15  	err      error
    16  }
    17  
    18  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget {
    19  	widget := &Widget{
    20  		TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.Common),
    21  		settings:   settings,
    22  	}
    23  	return widget
    24  }
    25  
    26  func (widget *Widget) Refresh() {
    27  	if widget.Disabled() {
    28  		return
    29  	}
    30  	widget.Redraw(widget.content)
    31  }
    32  
    33  func (widget *Widget) Render() {
    34  	widget.Redraw(widget.content)
    35  }
    36  
    37  func (widget *Widget) content() (string, string, bool) {
    38  	var title = "Next SpaceX 🚀"
    39  	if widget.CommonSettings().Title != "" {
    40  		title = widget.CommonSettings().Title
    41  	}
    42  
    43  	launch, err := NextLaunch()
    44  	var str string
    45  	if err != nil {
    46  		handleError(widget, err)
    47  	} else {
    48  
    49  		str = fmt.Sprintf("[%s]Mission[white]\n", widget.settings.Colors.Subheading)
    50  		str += fmt.Sprintf("%s: %s\n", "Name", launch.MissionName)
    51  		str += fmt.Sprintf("%s: %s\n", "Date", wtf.UnixTime(launch.LaunchDate).Format(time.RFC822))
    52  		str += fmt.Sprintf("%s: %s\n", "Site", launch.LaunchSite.Name)
    53  		str += "\n"
    54  
    55  		str += fmt.Sprintf("[%s]Links[white]\n", widget.settings.Colors.Subheading)
    56  		str += fmt.Sprintf("%s: %s\n", "YouTube", launch.Links.YouTubeLink)
    57  		str += fmt.Sprintf("%s: %s\n", "Reddit", launch.Links.RedditLink)
    58  
    59  		if widget.CommonSettings().Height >= 2 {
    60  			str += "\n"
    61  			str += fmt.Sprintf("[%s]Details[white]\n", widget.settings.Colors.Subheading)
    62  			str += fmt.Sprintf("%s: %s\n", "RocketName", launch.Rocket.Name)
    63  			str += fmt.Sprintf("%s: %s\n", "Details", launch.Details)
    64  		}
    65  	}
    66  	return title, str, true
    67  }
    68  
    69  func handleError(widget *Widget, err error) {
    70  	widget.err = err
    71  }