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

     1  package hibp
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/rivo/tview"
     7  	"github.com/wtfutil/wtf/view"
     8  )
     9  
    10  // Widget is the container for hibp data
    11  type Widget struct {
    12  	view.TextWidget
    13  
    14  	settings *Settings
    15  	statuses []*Status
    16  	err      error
    17  }
    18  
    19  // NewWidget creates a new instance of a widget
    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  // Fetch retrieves HIBP data from the HIBP API
    33  func (widget *Widget) Fetch(accounts []string) ([]*Status, error) {
    34  	data := []*Status{}
    35  
    36  	for _, account := range accounts {
    37  		stat, err := widget.fetchForAccount(account, widget.settings.since)
    38  		if err != nil {
    39  			return nil, err
    40  		}
    41  
    42  		data = append(data, stat)
    43  	}
    44  
    45  	return data, nil
    46  }
    47  
    48  // Refresh updates the data for this widget and displays it onscreen
    49  func (widget *Widget) Refresh() {
    50  	statuses, err := widget.Fetch(widget.settings.accounts)
    51  
    52  	if err != nil {
    53  		widget.err = err
    54  		widget.statuses = nil
    55  	} else {
    56  		widget.err = nil
    57  		widget.statuses = statuses
    58  	}
    59  
    60  	widget.Redraw(widget.content)
    61  }
    62  
    63  /* -------------------- Unexported Functions -------------------- */
    64  
    65  func (widget *Widget) content() (string, string, bool) {
    66  	title := widget.CommonSettings().Title
    67  	if widget.err != nil {
    68  		return title, widget.err.Error(), true
    69  	}
    70  
    71  	title += widget.sinceDateForTitle()
    72  	str := ""
    73  
    74  	for _, status := range widget.statuses {
    75  		color := widget.settings.colors.ok
    76  
    77  		if status.HasBeenCompromised() {
    78  			color = widget.settings.colors.pwned
    79  		}
    80  
    81  		if status != nil {
    82  			str += fmt.Sprintf(" [%s]%s[white]\n", color, status.Account)
    83  		}
    84  	}
    85  
    86  	return title, str, false
    87  }
    88  
    89  func (widget *Widget) sinceDateForTitle() string {
    90  	dateStr := ""
    91  
    92  	if widget.settings.HasSince() {
    93  		sinceStr := ""
    94  
    95  		dt, err := widget.settings.SinceDate()
    96  		if err != nil {
    97  			sinceStr = widget.settings.since
    98  		} else {
    99  			sinceStr = dt.Format("Jan _2, 2006")
   100  		}
   101  
   102  		dateStr = dateStr + " since " + sinceStr
   103  	}
   104  
   105  	return dateStr
   106  }