mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
100 lines
1.9 KiB
Go
100 lines
1.9 KiB
Go
package bamboohr
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/rivo/tview"
|
|
"github.com/senorprogrammer/wtf/wtf"
|
|
)
|
|
|
|
type Widget struct {
|
|
wtf.BaseWidget
|
|
View *tview.TextView
|
|
}
|
|
|
|
func NewWidget() *Widget {
|
|
widget := Widget{
|
|
BaseWidget: wtf.BaseWidget{
|
|
Name: "BambooHR",
|
|
RefreshedAt: time.Now(),
|
|
RefreshInterval: 15,
|
|
},
|
|
}
|
|
|
|
widget.addView()
|
|
go widget.refresher()
|
|
|
|
return &widget
|
|
}
|
|
|
|
/* -------------------- Exported Functions -------------------- */
|
|
|
|
func (widget *Widget) Refresh() {
|
|
items := Fetch()
|
|
|
|
widget.View.SetTitle(fmt.Sprintf(" 👽 Away (%d) ", len(items)))
|
|
widget.RefreshedAt = time.Now()
|
|
|
|
widget.View.Clear()
|
|
fmt.Fprintf(widget.View, "%s", widget.contentFrom(items))
|
|
}
|
|
|
|
/* -------------------- Unexported Functions -------------------- */
|
|
|
|
func (widget *Widget) addView() {
|
|
view := tview.NewTextView()
|
|
|
|
view.SetBorder(true)
|
|
view.SetDynamicColors(true)
|
|
view.SetTitle(widget.Name)
|
|
view.SetWrap(false)
|
|
|
|
widget.View = view
|
|
}
|
|
|
|
func centerText(str string, width int) string {
|
|
return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str))
|
|
}
|
|
|
|
func (widget *Widget) contentFrom(items []Item) string {
|
|
if len(items) == 0 {
|
|
return fmt.Sprintf("\n\n\n\n\n\n\n%s", centerText("[grey]no one[white]", 52))
|
|
}
|
|
|
|
str := ""
|
|
|
|
for _, item := range items {
|
|
str = str + widget.display(item)
|
|
}
|
|
|
|
return str
|
|
}
|
|
|
|
func (widget *Widget) display(item Item) string {
|
|
var str string
|
|
|
|
if item.IsOneDay() {
|
|
str = fmt.Sprintf(" [green]%s[white]\n %s\n\n", item.Name(), item.PrettyEnd())
|
|
} else {
|
|
str = fmt.Sprintf(" [green]%s[white]\n %s - %s\n\n", item.Name(), item.PrettyStart(), item.PrettyEnd())
|
|
}
|
|
|
|
return str
|
|
}
|
|
|
|
func (widget *Widget) refresher() {
|
|
tick := time.NewTicker(time.Duration(widget.RefreshInterval) * time.Minute)
|
|
quit := make(chan struct{})
|
|
|
|
for {
|
|
select {
|
|
case <-tick.C:
|
|
widget.Refresh()
|
|
case <-quit:
|
|
tick.Stop()
|
|
return
|
|
}
|
|
}
|
|
}
|