From 544a57964980b39aeae23abf5a679644e216782b Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 23 Mar 2017 11:54:35 -0500 Subject: [PATCH] Started work on Interactive Mode --- config_mode.go | 4 +-- main.go | 8 +++--- ui_mode.go | 73 ++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 71 insertions(+), 14 deletions(-) diff --git a/config_mode.go b/config_mode.go index e442b40..2a8933b 100644 --- a/config_mode.go +++ b/config_mode.go @@ -78,7 +78,7 @@ func configLayout(g *gocui.Gui) error { return nil } -func updateViews(g *gocui.Gui) error { +func updateConfigViews(g *gocui.Gui) error { for _, v := range g.Views() { v.Clear() switch v.Name() { @@ -117,7 +117,7 @@ func toggleCalendar(g *gocui.Gui, v *gocui.View) error { state.StatusMsg = "Calendar '" + calList[cy].Summary + "' Added to Defaults" state.account.ActiveCalendars = append(state.account.ActiveCalendars, calList[cy].Id) } - updateViews(g) + updateConfigViews(g) return nil } diff --git a/main.go b/main.go index bd8c5a4..8b879dc 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ var state *AppState var parameters []string func main() { - parameters = []string{"add", "colors", "config", "event", "defaults", "delete", "next", "refresh", "today", "ui"} + parameters = []string{"add", "config", "event", "defaults", "delete", "next", "refresh", "today", "ui"} var err error var op string state = &AppState{Name: AppName, Version: AppVersion} @@ -196,9 +196,9 @@ func main() { } } - // case "ui": - // Open the UI mode - // DoUIMode() + case "ui": + // Open the UI mode + DoUIMode() case "help": printHelp() diff --git a/ui_mode.go b/ui_mode.go index f5a624e..55a5051 100644 --- a/ui_mode.go +++ b/ui_mode.go @@ -1,12 +1,27 @@ package main import ( + "fmt" "log" + "time" "github.com/jroimartin/gocui" ) +type UIState struct { + SelCalMonth time.Month + SelCalDay int + SelCalYear int +} + +var uiState *UIState + func DoUIMode() { + 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) @@ -15,23 +30,65 @@ func DoUIMode() { g.Cursor = true g.SetManagerFunc(uiLayout) + // 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) } } -func updateUiViews(g *gocui.Gui) { - for _, v := range g.Views() { - v.Clear() - } -} - func uiLayout(g *gocui.Gui) error { - if v, err := g.SetView("cal_view", 0, 0, 20, 20); err != nil { + if v, err := g.SetView("cal_view", 0, 0, 20, 62); err != nil { if err != gocui.ErrUnknownView { return err } - _ = v + 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() + switch v.Name() { + case "cal_view": + if err := drawCalendar(g, v); err != nil { + return err + } + } + } + return nil +} + +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++ { + + } + } + } + + return nil +}