181 lines
4.2 KiB
Go
181 lines
4.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
"github.com/jroimartin/gocui"
|
|
)
|
|
|
|
// BEGIN CONFIG SCREEN FUNCTIONS //
|
|
func DoConfig() {
|
|
state.StatusMsg = "Galendar Configuration"
|
|
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("", 'q', 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
|
|
|
|
drawCalList(g, v)
|
|
if _, err = g.SetCurrentView("default_calendars"); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if v, err := g.SetView("status_line", 0, height+1, width-1, height+3); err != nil {
|
|
if err != gocui.ErrUnknownView {
|
|
return err
|
|
}
|
|
drawStatusLine(g, v)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func updateViews(g *gocui.Gui) error {
|
|
for _, v := range g.Views() {
|
|
v.Clear()
|
|
switch v.Name() {
|
|
case "default_calendars":
|
|
if err := drawCalList(g, v); err != nil {
|
|
return err
|
|
}
|
|
case "status_line":
|
|
if err := drawStatusLine(g, v); err != nil {
|
|
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()
|
|
var removed bool
|
|
calList := state.account.GetCalendarList()
|
|
if v != nil {
|
|
for i := range state.defaultCalendars {
|
|
if state.defaultCalendars[i] == calList[cy].Id {
|
|
// Remove calendar from defaults
|
|
state.defaultCalendars = append(state.defaultCalendars[:i], state.defaultCalendars[i+1:]...)
|
|
state.StatusMsg = "Calendar Removed from Defaults"
|
|
removed = true
|
|
}
|
|
}
|
|
}
|
|
if !removed {
|
|
// Add calendar to defaults
|
|
state.StatusMsg = "Calendar Added to Defaults"
|
|
state.defaultCalendars = append(state.defaultCalendars, calList[cy].Id)
|
|
}
|
|
updateViews(g)
|
|
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 drawStatusLine(g *gocui.Gui, v *gocui.View) error {
|
|
fmt.Fprintln(v, state.StatusMsg)
|
|
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)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// END CONFIG SCREEN FUNCTIONS //
|