mirror of
				https://github.com/taigrr/wtf
				synced 2025-01-18 04:03:14 -08:00 
			
		
		
		
	Widgetized BambooHR
This commit is contained in:
		
							parent
							
								
									c56e99fcec
								
							
						
					
					
						commit
						6cfa0ef632
					
				@ -2,29 +2,60 @@ package bamboohr
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/rivo/tview"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func Widget() tview.Primitive {
 | 
			
		||||
	items := Fetch()
 | 
			
		||||
 | 
			
		||||
	widget := tview.NewTextView()
 | 
			
		||||
	widget.SetBorder(true)
 | 
			
		||||
	widget.SetDynamicColors(true)
 | 
			
		||||
	widget.SetTitle(fmt.Sprintf(" 🐨 Away (%d)", len(items)))
 | 
			
		||||
 | 
			
		||||
	data := ""
 | 
			
		||||
	for _, item := range items {
 | 
			
		||||
		data = data + display(item)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	fmt.Fprintf(widget, "%s", data)
 | 
			
		||||
 | 
			
		||||
	return widget
 | 
			
		||||
type Widget struct {
 | 
			
		||||
	RefreshedAt time.Time
 | 
			
		||||
	View        *tview.TextView
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func display(item Item) string {
 | 
			
		||||
func NewWidget() *Widget {
 | 
			
		||||
	widget := Widget{
 | 
			
		||||
		RefreshedAt: time.Now(),
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	widget.addView()
 | 
			
		||||
 | 
			
		||||
	return &widget
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- Exported Functions -------------------- */
 | 
			
		||||
 | 
			
		||||
func (widget *Widget) Refresh() {
 | 
			
		||||
	items := Fetch()
 | 
			
		||||
 | 
			
		||||
	widget.View.SetTitle(fmt.Sprintf(" 🐨 Away (%d) ", len(items)))
 | 
			
		||||
	widget.RefreshedAt = time.Now()
 | 
			
		||||
 | 
			
		||||
	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(" BambooHR ")
 | 
			
		||||
 | 
			
		||||
	widget.View = view
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (widget *Widget) contentFrom(items []Item) string {
 | 
			
		||||
	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() {
 | 
			
		||||
 | 
			
		||||
@ -29,8 +29,9 @@ func (widget *Widget) Refresh() {
 | 
			
		||||
	data := Fetch()
 | 
			
		||||
 | 
			
		||||
	widget.View.SetTitle(fmt.Sprintf(" %s Weather - %s ", icon(data), data.Name))
 | 
			
		||||
	widget.RefreshedAt = time.Now()
 | 
			
		||||
 | 
			
		||||
	fmt.Fprintf(widget.View, " %s ", widget.content(data))
 | 
			
		||||
	fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/* -------------------- Unexported Functions -------------------- */
 | 
			
		||||
@ -40,22 +41,27 @@ func (widget *Widget) addView() {
 | 
			
		||||
 | 
			
		||||
	view.SetBorder(true)
 | 
			
		||||
	view.SetDynamicColors(true)
 | 
			
		||||
	view.SetTitle("Weather")
 | 
			
		||||
	view.SetTitle(" Weather ")
 | 
			
		||||
 | 
			
		||||
	widget.View = view
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (widget *Widget) content(data *owm.CurrentWeatherData) string {
 | 
			
		||||
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(data *owm.CurrentWeatherData) string {
 | 
			
		||||
	str := fmt.Sprintf("\n")
 | 
			
		||||
 | 
			
		||||
	for _, weather := range data.Weather {
 | 
			
		||||
		str = str + fmt.Sprintf(" %16s\n\n", weather.Description)
 | 
			
		||||
		str = str + fmt.Sprintf(" %s\n\n", weather.Description)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	str = str + fmt.Sprintf("%10s: %4.1f° C\n\n", "Current", data.Main.Temp)
 | 
			
		||||
	str = str + fmt.Sprintf("%10s: %4.1f° C\n", "High", data.Main.TempMax)
 | 
			
		||||
	str = str + fmt.Sprintf("%10s: %4.1f° C\n", "Low", data.Main.TempMin)
 | 
			
		||||
	str = str + "\n\n\n\n"
 | 
			
		||||
	str = str + fmt.Sprintf(" Refreshed at %s", widget.RefreshedAt)
 | 
			
		||||
	str = str + centerText(fmt.Sprintf("Refreshed at %s", widget.refreshedAt()), 38)
 | 
			
		||||
 | 
			
		||||
	return str
 | 
			
		||||
}
 | 
			
		||||
@ -100,3 +106,7 @@ func icon(data *owm.CurrentWeatherData) string {
 | 
			
		||||
 | 
			
		||||
	return icon
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func (widget *Widget) refreshedAt() string {
 | 
			
		||||
	return widget.RefreshedAt.Format("15:04:05")
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										7
									
								
								wtf.go
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								wtf.go
									
									
									
									
									
								
							@ -1,6 +1,8 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	//"time"
 | 
			
		||||
 | 
			
		||||
	"github.com/rivo/tview"
 | 
			
		||||
	"github.com/senorprogrammer/wtf/bamboohr"
 | 
			
		||||
	"github.com/senorprogrammer/wtf/gcal"
 | 
			
		||||
@ -9,6 +11,9 @@ import (
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
	bamboo := bamboohr.NewWidget()
 | 
			
		||||
	bamboo.Refresh()
 | 
			
		||||
 | 
			
		||||
	weather := weather.NewWidget()
 | 
			
		||||
	weather.Refresh()
 | 
			
		||||
 | 
			
		||||
@ -19,7 +24,7 @@ func main() {
 | 
			
		||||
	grid.SetColumns(40, 40) // How _wide_ the column is, in terminal columns
 | 
			
		||||
	grid.SetBorder(false)
 | 
			
		||||
 | 
			
		||||
	grid.AddItem(bamboohr.Widget(), 0, 0, 1, 1, 0, 0, false)
 | 
			
		||||
	grid.AddItem(bamboo.View, 0, 0, 1, 1, 0, 0, false)
 | 
			
		||||
	grid.AddItem(gcal.Widget(), 1, 0, 1, 1, 0, 0, false)
 | 
			
		||||
	grid.AddItem(status.Widget(), 2, 0, 2, 3, 0, 0, false)
 | 
			
		||||
	grid.AddItem(weather.View, 0, 1, 1, 1, 0, 0, false)
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user