mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
82 lines
1.6 KiB
Go
82 lines
1.6 KiB
Go
package bamboohr
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/gdamore/tcell"
|
|
"github.com/olebedev/config"
|
|
"github.com/rivo/tview"
|
|
"github.com/senorprogrammer/wtf/wtf"
|
|
)
|
|
|
|
var Config *config.Config
|
|
|
|
type Widget struct {
|
|
wtf.TextWidget
|
|
}
|
|
|
|
func NewWidget() *Widget {
|
|
widget := Widget{
|
|
TextWidget: wtf.NewTextWidget("BambooHR", "bamboohr"),
|
|
}
|
|
|
|
widget.addView()
|
|
|
|
return &widget
|
|
}
|
|
|
|
/* -------------------- Exported Functions -------------------- */
|
|
|
|
func (widget *Widget) Refresh() {
|
|
url, _ := Config.String("wtf.bamboohr.url")
|
|
|
|
client := NewClient(url)
|
|
items := client.Away("timeOff", wtf.Today(), wtf.Today())
|
|
|
|
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.SetBorderColor(tcell.ColorGray)
|
|
view.SetDynamicColors(true)
|
|
view.SetTitle(widget.Name)
|
|
view.SetWrap(false)
|
|
|
|
widget.View = view
|
|
}
|
|
|
|
func (widget *Widget) contentFrom(items []Item) string {
|
|
if len(items) == 0 {
|
|
return fmt.Sprintf("\n\n\n\n\n\n\n\n%s", wtf.CenterText("[grey]no one[white]", 50))
|
|
}
|
|
|
|
str := ""
|
|
for _, item := range items {
|
|
str = str + widget.format(item)
|
|
}
|
|
|
|
return str
|
|
}
|
|
|
|
func (widget *Widget) format(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
|
|
}
|