gal/ui_mode.go

95 lines
2.0 KiB
Go
Raw Permalink Normal View History

package main
import (
2017-03-23 16:54:35 +00:00
"fmt"
"log"
2017-03-23 16:54:35 +00:00
"time"
"github.com/jroimartin/gocui"
)
2017-03-23 16:54:35 +00:00
type UIState struct {
SelCalMonth time.Month
SelCalDay int
SelCalYear int
}
var uiState *UIState
func DoUIMode() {
2017-03-23 16:54:35 +00:00
uiState = &UIState{
SelCalMonth: time.Now().Month(),
SelCalDay: time.Now().Day(),
SelCalYear: time.Now().Year(),
}
g, err := gocui.NewGui(gocui.Output256)
if err != nil {
log.Panicln(err)
}
defer g.Close()
g.Cursor = true
g.SetManagerFunc(uiLayout)
2017-03-23 16:54:35 +00:00
// Set up some keybindings
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
if err := g.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
log.Panicln(err)
}
// And now the main loop
if err := g.MainLoop(); err != nil && err != gocui.ErrQuit {
log.Panicln(err)
}
}
2017-03-23 16:54:35 +00:00
func uiLayout(g *gocui.Gui) error {
if v, err := g.SetView("cal_view", 0, 0, 20, 62); err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.SelBgColor = gocui.ColorGreen
v.SelFgColor = gocui.ColorBlack
drawCalendar(g, v)
if _, err = g.SetCurrentView("cal_view"); err != nil {
return err
}
}
return nil
}
func updateUiViews(g *gocui.Gui) error {
for _, v := range g.Views() {
v.Clear()
2017-03-23 16:54:35 +00:00
switch v.Name() {
case "cal_view":
if err := drawCalendar(g, v); err != nil {
return err
}
}
}
2017-03-23 16:54:35 +00:00
return nil
}
2017-03-23 16:54:35 +00:00
func drawCalendar(g *gocui.Gui, v *gocui.View) error {
// Print the 'Today' String
fmt.Fprintln(v, "<", uiState.SelCalMonth.String()[:3], uiState.SelCalDay, ",", uiState.SelCalYear, ">")
// Find the first of the month's week day
for wrkDay := 1; wrkDay <= 31; wrkDay++ {
wrkCalString := fmt.Sprintf("%04d%02d%02d", uiState.SelCalYear, uiState.SelCalMonth, wrkDay)
wrkTime, _ := time.Parse("20060102", wrkCalString)
fmt.Fprintf(v, "%04d%02d%02d\n", wrkTime.Year(), wrkTime.Month(), wrkTime.Day())
fmt.Fprintln(v, wrkTime.Weekday())
if wrkDay == 1 {
// For the first day, we need to space it in wrkTime.Weekday() spaces
for i := 0; i < int(wrkTime.Weekday()); i++ {
}
}
}
2017-03-23 16:54:35 +00:00
return nil
}