2017-03-02 16:26:34 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jroimartin/gocui"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BEGIN CONFIG SCREEN FUNCTIONS //
|
|
|
|
func DoConfig() {
|
2017-03-09 15:32:40 +00:00
|
|
|
state.StatusMsg = "Galendar Configuration"
|
2017-03-02 16:26:34 +00:00
|
|
|
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)
|
|
|
|
}
|
2017-03-09 15:32:40 +00:00
|
|
|
if err := g.SetKeybinding("", 'q', gocui.ModNone, quit); err != nil {
|
|
|
|
log.Panicln(err)
|
|
|
|
}
|
2017-03-02 16:26:34 +00:00
|
|
|
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)
|
2017-03-09 15:32:40 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
2017-03-02 16:26:34 +00:00
|
|
|
}
|
|
|
|
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()
|
2017-03-09 15:32:40 +00:00
|
|
|
var removed bool
|
2017-03-02 16:26:34 +00:00
|
|
|
calList := state.account.GetCalendarList()
|
2017-03-16 21:11:39 +00:00
|
|
|
for i := range state.account.ActiveCalendars {
|
|
|
|
if state.account.ActiveCalendars[i] == calList[cy].Id {
|
2017-03-15 19:38:37 +00:00
|
|
|
// Remove calendar from defaults
|
2017-03-16 21:11:39 +00:00
|
|
|
state.account.ActiveCalendars = append(state.account.ActiveCalendars[:i], state.account.ActiveCalendars[i+1:]...)
|
2017-03-15 19:38:37 +00:00
|
|
|
state.StatusMsg = "Calendar '" + calList[cy].Summary + "' Removed from Defaults"
|
|
|
|
removed = true
|
|
|
|
break
|
2017-03-02 16:26:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-03-09 15:32:40 +00:00
|
|
|
if !removed {
|
|
|
|
// Add calendar to defaults
|
2017-03-15 19:38:37 +00:00
|
|
|
state.StatusMsg = "Calendar '" + calList[cy].Summary + "' Added to Defaults"
|
2017-03-16 21:11:39 +00:00
|
|
|
state.account.ActiveCalendars = append(state.account.ActiveCalendars, calList[cy].Id)
|
2017-03-09 15:32:40 +00:00
|
|
|
}
|
|
|
|
updateViews(g)
|
2017-03-02 16:26:34 +00:00
|
|
|
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()
|
2017-03-15 19:38:37 +00:00
|
|
|
calList := state.account.GetCalendarList()
|
|
|
|
if cy+1 > len(calList)-1 {
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-02 16:26:34 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-03-09 15:32:40 +00:00
|
|
|
func drawStatusLine(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
fmt.Fprintln(v, state.StatusMsg)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-02 16:26:34 +00:00
|
|
|
func drawCalList(g *gocui.Gui, v *gocui.View) error {
|
|
|
|
width, _ := g.Size()
|
|
|
|
list := state.account.GetCalendarList()
|
|
|
|
for i := range list {
|
|
|
|
isDef := false
|
2017-03-16 21:11:39 +00:00
|
|
|
for defIdx := range state.account.ActiveCalendars {
|
|
|
|
if list[i].Id == state.account.ActiveCalendars[defIdx] {
|
2017-03-02 16:26:34 +00:00
|
|
|
isDef = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
calSumTxt := "["
|
|
|
|
if isDef {
|
|
|
|
calSumTxt += "*"
|
|
|
|
} else {
|
|
|
|
calSumTxt += " "
|
|
|
|
}
|
2017-03-23 12:44:45 +00:00
|
|
|
calSumTxt += "] " + list[i].Summary + " " + list[i].BackgroundColor
|
2017-03-02 16:26:34 +00:00
|
|
|
spc := strings.Repeat(" ", width-len(calSumTxt)-2)
|
|
|
|
fmt.Fprintln(v, calSumTxt+spc)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// END CONFIG SCREEN FUNCTIONS //
|