package main import ( "fmt" "log" "strings" "time" "github.com/jroimartin/gocui" ) // BEGIN CONFIG SCREEN FUNCTIONS // func DoConfig() { g, err := gocui.NewGui(gocui.Output256) if err != nil { log.Panicln(err) } defer g.Close() g.Cursor = true g.SetManagerFunc(configLayout) // Set up some keybindings if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { log.Panicln(err) } if err := g.SetKeybinding("default_calendars", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil { log.Panicln(err) } if err := g.SetKeybinding("default_calendars", 'k', gocui.ModNone, cursorUp); err != nil { log.Panicln(err) } if err := g.SetKeybinding("default_calendars", gocui.KeyArrowDown, gocui.ModNone, cursorDown); err != nil { log.Panicln(err) } if err := g.SetKeybinding("default_calendars", 'j', gocui.ModNone, cursorDown); err != nil { log.Panicln(err) } if err := g.SetKeybinding("default_calendars", gocui.KeySpace, gocui.ModNone, toggleCalendar); err != nil { log.Panicln(err) } if err := g.SetKeybinding("default_calendars", gocui.KeyEnter, gocui.ModNone, toggleCalendar); err != nil { log.Panicln(err) } // Kick off the main loop if err := g.MainLoop(); err != nil && err != gocui.ErrQuit { log.Panicln(err) } } func configLayout(g *gocui.Gui) error { list := state.account.GetCalendarList() height := len(list) + 4 width, _ := g.Size() if v, err := g.SetView("default_calendars", 0, 0, width-1, height); err != nil { if err != gocui.ErrUnknownView { return err } v.Highlight = true v.SelBgColor = gocui.ColorGreen v.SelFgColor = gocui.ColorBlack v.Clear() drawCalList(g, v) _, err := g.SetCurrentView("default_calendars") return err } return nil } func quit(g *gocui.Gui, v *gocui.View) error { return gocui.ErrQuit } func toggleCalendar(g *gocui.Gui, v *gocui.View) error { _, cy := v.Cursor() calList := state.account.GetCalendarList() if v != nil { for i := range state.defaultCalendars { state.StatusMsg = time.Now().Format(time.RFC3339) if state.defaultCalendars[i] == calList[cy].Id { // Remove calendar from defaults state.defaultCalendars = append(state.defaultCalendars[:i], state.defaultCalendars[i+1:]...) } } } // Add calendar to defaults state.defaultCalendars = append(state.defaultCalendars, calList[cy].Id) return nil } func cursorUp(g *gocui.Gui, v *gocui.View) error { if v != nil { ox, oy := v.Origin() cx, cy := v.Cursor() if err := v.SetCursor(cx, cy-1); err != nil && oy > 0 { if err := v.SetOrigin(ox, oy-1); err != nil { return err } } } return nil } func cursorDown(g *gocui.Gui, v *gocui.View) error { if v != nil { cx, cy := v.Cursor() if err := v.SetCursor(cx, cy+1); err != nil { ox, oy := v.Origin() if err := v.SetOrigin(ox, oy+1); err != nil { return err } } } return nil } func drawCalList(g *gocui.Gui, v *gocui.View) error { width, _ := g.Size() list := state.account.GetCalendarList() for i := range list { isDef := false for defIdx := range state.defaultCalendars { if list[i].Id == state.defaultCalendars[defIdx] { isDef = true break } } calSumTxt := "[" if isDef { calSumTxt += "*" } else { calSumTxt += " " } calSumTxt += "] " + list[i].Summary spc := strings.Repeat(" ", width-len(calSumTxt)-2) fmt.Fprintln(v, calSumTxt+spc) } fmt.Fprintln(v, "Status: "+time.Now().Format(time.RFC3339)) return nil } // END CONFIG SCREEN FUNCTIONS //