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

     1  package bamboohr
     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  	"github.com/wtfutil/wtf/wtf"
    11  )
    12  
    13  const apiURI = "https://api.bamboohr.com/api/gateway.php"
    14  
    15  type Widget struct {
    16  	view.TextWidget
    17  
    18  	settings *Settings
    19  	items    []Item
    20  }
    21  
    22  func NewWidget(tviewApp *tview.Application, redrawChan chan bool, settings *Settings) *Widget {
    23  	widget := Widget{
    24  		TextWidget: view.NewTextWidget(tviewApp, redrawChan, nil, settings.Common),
    25  
    26  		settings: settings,
    27  	}
    28  
    29  	return &widget
    30  }
    31  
    32  /* -------------------- Exported Functions -------------------- */
    33  
    34  func (widget *Widget) Refresh() {
    35  	client := NewClient(
    36  		apiURI,
    37  		widget.settings.apiKey,
    38  		widget.settings.subdomain,
    39  	)
    40  
    41  	widget.items = client.Away(
    42  		"timeOff",
    43  		time.Now().Local().Format(wtf.DateFormat),
    44  		time.Now().Local().Format(wtf.DateFormat),
    45  	)
    46  
    47  	widget.Redraw(widget.content)
    48  }
    49  
    50  /* -------------------- Unexported Functions -------------------- */
    51  
    52  func (widget *Widget) content() (string, string, bool) {
    53  	str := ""
    54  	if len(widget.items) == 0 {
    55  		str = fmt.Sprintf("\n\n\n\n\n\n\n\n%s", utils.CenterText("[grey]no one[white]", 50))
    56  	} else {
    57  		for _, item := range widget.items {
    58  			str += widget.format(item)
    59  		}
    60  	}
    61  
    62  	return widget.CommonSettings().Title, str, false
    63  }
    64  
    65  func (widget *Widget) format(item Item) string {
    66  	var str string
    67  
    68  	if item.IsOneDay() {
    69  		str = fmt.Sprintf(" [green]%s[white]\n %s\n\n", item.Name(), item.PrettyEnd())
    70  	} else {
    71  		str = fmt.Sprintf(" [green]%s[white]\n %s - %s\n\n", item.Name(), item.PrettyStart(), item.PrettyEnd())
    72  	}
    73  
    74  	return str
    75  }