1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Cleaner code around widget creation in wtf.go

This commit is contained in:
Chris Cummer 2018-04-13 12:00:29 -07:00 committed by Chris Cummer
parent 31e17da358
commit 8e3287ba5c
3 changed files with 22 additions and 22 deletions

View File

@ -39,10 +39,12 @@ func (widget *Widget) Refresh() {
widget.RefreshedAt = time.Now() widget.RefreshedAt = time.Now()
_, _, w, _ := widget.View.GetInnerRect()
widget.View.Clear() widget.View.Clear()
fmt.Fprintf( fmt.Fprintf(
widget.View, widget.View,
"%107s\n%123s", fmt.Sprintf("%%%ds\n%%%ds", w-2, w-1),
widget.animation(), widget.animation(),
widget.timezones(), widget.timezones(),
) )
@ -86,5 +88,5 @@ func (widget *Widget) timezones() string {
formattedTimes = append(formattedTimes, time.Format(wtf.TimeFormat)) formattedTimes = append(formattedTimes, time.Format(wtf.TimeFormat))
} }
return strings.Join(formattedTimes, " [yellow][white] ") return strings.Join(formattedTimes, " • ")
} }

34
wtf.go
View File

@ -1,7 +1,6 @@
package main package main
import ( import (
//"fmt"
"os" "os"
"time" "time"
@ -20,7 +19,7 @@ import (
"github.com/senorprogrammer/wtf/wtf" "github.com/senorprogrammer/wtf/wtf"
) )
func addToApp(grid *tview.Grid, widget wtf.TextViewer) { func addToGrid(grid *tview.Grid, widget wtf.TextViewer) {
if widget.Disabled() { if widget.Disabled() {
return return
} }
@ -37,6 +36,16 @@ func addToApp(grid *tview.Grid, widget wtf.TextViewer) {
) )
} }
// Grid stores all the widgets onscreen (like an HTML table)
func buildGrid() *tview.Grid {
grid := tview.NewGrid()
grid.SetColumns(wtf.ToInts(Config.UList("wtf.grid.columns"))...)
grid.SetRows(wtf.ToInts(Config.UList("wtf.grid.rows"))...)
grid.SetBorder(false)
return grid
}
func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { func keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
// Ctrl-R: force-refreshes every widget // Ctrl-R: force-refreshes every widget
if event.Key() == tcell.KeyCtrlR { if event.Key() == tcell.KeyCtrlR {
@ -73,52 +82,36 @@ var Modules []wtf.TextViewer
func main() { func main() {
wtf.Config = Config wtf.Config = Config
// Grid stores all the widgets onscreen (like an HTML table)
grid := tview.NewGrid()
grid.SetColumns(wtf.ToInts(Config.UList("wtf.grid.columns"))...)
grid.SetRows(wtf.ToInts(Config.UList("wtf.grid.rows"))...)
grid.SetBorder(false)
// TODO: Really need to generalize all of these. This don't scale // TODO: Really need to generalize all of these. This don't scale
bamboohr.Config = Config bamboohr.Config = Config
bamboo := bamboohr.NewWidget() bamboo := bamboohr.NewWidget()
go wtf.Schedule(bamboo)
gcal.Config = Config gcal.Config = Config
cal := gcal.NewWidget() cal := gcal.NewWidget()
go wtf.Schedule(cal)
git.Config = Config git.Config = Config
git := git.NewWidget() git := git.NewWidget()
go wtf.Schedule(git)
github.Config = Config github.Config = Config
github := github.NewWidget() github := github.NewWidget()
go wtf.Schedule(github)
jira.Config = Config jira.Config = Config
jira := jira.NewWidget() jira := jira.NewWidget()
go wtf.Schedule(jira)
newrelic.Config = Config newrelic.Config = Config
newrelic := newrelic.NewWidget() newrelic := newrelic.NewWidget()
go wtf.Schedule(newrelic)
opsgenie.Config = Config opsgenie.Config = Config
opsgenie := opsgenie.NewWidget() opsgenie := opsgenie.NewWidget()
go wtf.Schedule(opsgenie)
security.Config = Config security.Config = Config
sec := security.NewWidget() sec := security.NewWidget()
go wtf.Schedule(sec)
status.Config = Config status.Config = Config
stat := status.NewWidget() stat := status.NewWidget()
go wtf.Schedule(stat)
weather.Config = Config weather.Config = Config
weather := weather.NewWidget() weather := weather.NewWidget()
go wtf.Schedule(weather)
Modules = []wtf.TextViewer{ Modules = []wtf.TextViewer{
bamboo, bamboo,
@ -133,8 +126,11 @@ func main() {
weather, weather,
} }
grid := buildGrid()
for _, module := range Modules { for _, module := range Modules {
addToApp(grid, module) addToGrid(grid, module)
go wtf.Schedule(module)
} }
app := tview.NewApplication() app := tview.NewApplication()

View File

@ -4,10 +4,12 @@ import (
"github.com/rivo/tview" "github.com/rivo/tview"
) )
// TODO: I really need to come up with a better name for this now
type TextViewer interface { type TextViewer interface {
Enabler Enabler
Scheduler
Refresh() //Refresh()
TextView() *tview.TextView TextView() *tview.TextView
Top() int Top() int