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) } defer g.Close() 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 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() 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 }