Quick Add and Config Screen Work

This commit is contained in:
Brian Buller 2017-03-09 09:32:40 -06:00
parent 23b8a190ec
commit 1e0e954a38
4 changed files with 109 additions and 34 deletions

3
.gitignore vendored
View File

@ -1,2 +1,3 @@
client_secret.json client_secret.json
gocal client_secret.go
gal

19
client_secret.go.example Normal file
View File

@ -0,0 +1,19 @@
package main
/**
* = API Set Up Process =
* 1. Use [this wizard](https://console.developers.google.com/start/api?id=calendar) to create or select a project in the Google Developers Console and automatically turn on the API. Click Continue, then Go to credentials.
* 2. On the Add credentials to your project page, click the Cancel button.
* 3. At the top of the page, select the OAuth consent screen tab. Select an Email address, enter a Product name if not already set, and click the Save button.
* 4. Select the Credentials tab, click the Create credentials button and select OAuth client ID.
* 5. Select the application type Other, enter the name "Google Calendar API Quickstart", and click the Create button.
* 6. Click OK to dismiss the resulting dialog.
* 7. Click the (Download JSON) button to the right of the client ID.
* 8. Copy the contents of the JSON file into the string variable above.
* Alternatively just input the path/filename of the downloaded json file when prompted on first run of galendar.
* 9. Rename this file to `client_secret.go`
**/
// Put the entire json downloaded from the Google API Manager here
var GoogleClientJson = ``

View File

@ -4,13 +4,13 @@ import (
"fmt" "fmt"
"log" "log"
"strings" "strings"
"time"
"github.com/jroimartin/gocui" "github.com/jroimartin/gocui"
) )
// BEGIN CONFIG SCREEN FUNCTIONS // // BEGIN CONFIG SCREEN FUNCTIONS //
func DoConfig() { func DoConfig() {
state.StatusMsg = "Galendar Configuration"
g, err := gocui.NewGui(gocui.Output256) g, err := gocui.NewGui(gocui.Output256)
if err != nil { if err != nil {
log.Panicln(err) log.Panicln(err)
@ -23,6 +23,9 @@ func DoConfig() {
if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil { if err := g.SetKeybinding("", gocui.KeyCtrlC, gocui.ModNone, quit); err != nil {
log.Panicln(err) 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 { if err := g.SetKeybinding("default_calendars", gocui.KeyArrowUp, gocui.ModNone, cursorUp); err != nil {
log.Panicln(err) log.Panicln(err)
} }
@ -61,11 +64,34 @@ func configLayout(g *gocui.Gui) error {
v.SelBgColor = gocui.ColorGreen v.SelBgColor = gocui.ColorGreen
v.SelFgColor = gocui.ColorBlack v.SelFgColor = gocui.ColorBlack
v.Clear()
drawCalList(g, v) drawCalList(g, v)
_, err := g.SetCurrentView("default_calendars") if _, err = g.SetCurrentView("default_calendars"); err != nil {
return err 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 return nil
} }
@ -75,18 +101,24 @@ func quit(g *gocui.Gui, v *gocui.View) error {
func toggleCalendar(g *gocui.Gui, v *gocui.View) error { func toggleCalendar(g *gocui.Gui, v *gocui.View) error {
_, cy := v.Cursor() _, cy := v.Cursor()
var removed bool
calList := state.account.GetCalendarList() calList := state.account.GetCalendarList()
if v != nil { if v != nil {
for i := range state.defaultCalendars { for i := range state.defaultCalendars {
state.StatusMsg = time.Now().Format(time.RFC3339)
if state.defaultCalendars[i] == calList[cy].Id { if state.defaultCalendars[i] == calList[cy].Id {
// Remove calendar from defaults // Remove calendar from defaults
state.defaultCalendars = append(state.defaultCalendars[:i], state.defaultCalendars[i+1:]...) state.defaultCalendars = append(state.defaultCalendars[:i], state.defaultCalendars[i+1:]...)
state.StatusMsg = "Calendar Removed from Defaults"
removed = true
} }
} }
} }
if !removed {
// Add calendar to defaults // Add calendar to defaults
state.StatusMsg = "Calendar Added to Defaults"
state.defaultCalendars = append(state.defaultCalendars, calList[cy].Id) state.defaultCalendars = append(state.defaultCalendars, calList[cy].Id)
}
updateViews(g)
return nil return nil
} }
@ -116,6 +148,11 @@ func cursorDown(g *gocui.Gui, v *gocui.View) error {
return nil 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 { func drawCalList(g *gocui.Gui, v *gocui.View) error {
width, _ := g.Size() width, _ := g.Size()
list := state.account.GetCalendarList() list := state.account.GetCalendarList()
@ -137,7 +174,6 @@ func drawCalList(g *gocui.Gui, v *gocui.View) error {
spc := strings.Repeat(" ", width-len(calSumTxt)-2) spc := strings.Repeat(" ", width-len(calSumTxt)-2)
fmt.Fprintln(v, calSumTxt+spc) fmt.Fprintln(v, calSumTxt+spc)
} }
fmt.Fprintln(v, "Status: "+time.Now().Format(time.RFC3339))
return nil return nil
} }

51
main.go
View File

@ -15,7 +15,7 @@ import (
) )
const ( const (
AppName = "gocal" AppName = "galendar"
AppVersion = 1 AppVersion = 1
) )
@ -26,6 +26,7 @@ type AppState struct {
cfg *userConfig.Config cfg *userConfig.Config
account *Account account *Account
defaultCalendars []string defaultCalendars []string
quickAddCalendar string
StatusMsg string StatusMsg string
} }
@ -103,6 +104,7 @@ func main() {
} }
case "config": case "config":
// Open the Configuration CUI screen
InitComm() InitComm()
DoConfig() DoConfig()
@ -113,6 +115,7 @@ func main() {
case "add": case "add":
// Quick event add to primary calendar // Quick event add to primary calendar
quickAddText := strings.Join(os.Args[2:], " ") quickAddText := strings.Join(os.Args[2:], " ")
InitComm()
e, err := state.account.GetDefaultCalendar().QuickAdd(quickAddText) e, err := state.account.GetDefaultCalendar().QuickAdd(quickAddText)
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
@ -140,19 +143,6 @@ func InitComm() {
// Save the Raw Token // Save the Raw Token
state.cfg.SetBytes("Token", state.account.CC.GetRawToken()) state.cfg.SetBytes("Token", state.account.CC.GetRawToken())
// If the 'defaultCalendars' cfg is set to 'primary' (or not at all)
// we need to actually but the primary cal id in there
var defCal []string
defCal, err = state.cfg.GetArray("defaultCalendars")
if len(defCal) == 0 || (len(defCal) == 1 && defCal[0] == "primary") {
gCal, err := state.account.Service.CalendarList.Get("primary").Do()
if err == nil {
defCal = []string{gCal.Id}
state.cfg.SetArray("defaultCalendars", defCal)
}
}
state.defaultCalendars = defCal
loadAccountState() loadAccountState()
} }
@ -163,8 +153,10 @@ func DoVersionCheck() {
switch confVer { switch confVer {
case 0: // Initializing the app case 0: // Initializing the app
fmt.Println("Initializing gocal") fmt.Println("Initializing gocal")
if GoogleClientJson == "" {
for { for {
fmt.Println("Client Secret Filename (Ctrl-C to exit): ") fmt.Println("Client Secret Filename (Ctrl-C to exit): ")
var fn string var fn string
if _, err := fmt.Scan(&fn); err == nil { if _, err := fmt.Scan(&fn); err == nil {
var b []byte var b []byte
@ -175,8 +167,12 @@ func DoVersionCheck() {
} }
} }
} }
} else {
state.cfg.Set("ClientSecret", GoogleClientJson)
}
state.cfg.SetInt("version", 1) state.cfg.SetInt("version", 1)
state.cfg.SetArray("defaultCalendars", []string{"primary"}) state.cfg.SetArray("defaultCalendars", []string{"primary"})
state.cfg.Set("quickAddCalendar", "")
} }
state.ClientSecret = state.cfg.GetBytes("ClientSecret") state.ClientSecret = state.cfg.GetBytes("ClientSecret")
// Refetch the version from the config // Refetch the version from the config
@ -200,10 +196,12 @@ func saveAccountState() {
if err = state.cfg.SetArray("defaultCalendars", state.defaultCalendars); err != nil { if err = state.cfg.SetArray("defaultCalendars", state.defaultCalendars); err != nil {
fmt.Println("saveAccountState:defaultCalendars error:", err) fmt.Println("saveAccountState:defaultCalendars error:", err)
} }
if err = state.cfg.Set("quickAddCalendar", state.quickAddCalendar); err != nil {
fmt.Println("saveAccountState:quickAddCalendar error:", err)
}
} }
// loadAccountState loads all the bits of cache from the config // loadAccountState loads all of the config into the state struct
// into the account
func loadAccountState() { func loadAccountState() {
var err error var err error
var calListJson []byte var calListJson []byte
@ -220,4 +218,25 @@ func loadAccountState() {
} else { } else {
fmt.Println("error: ", err) fmt.Println("error: ", err)
} }
// If the 'defaultCalendars' cfg is set to 'primary' (or not at all)
// we need to actually but the primary cal id in there
var defCal []string
defCal, err = state.cfg.GetArray("defaultCalendars")
if len(defCal) == 0 || (len(defCal) == 1 && defCal[0] == "primary") {
gCal, err := state.account.Service.CalendarList.Get("primary").Do()
if err == nil {
defCal = []string{gCal.Id}
}
}
state.defaultCalendars = defCal
// Verify/Set QuickAdd Calendar
state.quickAddCalendar = state.cfg.Get("quickAddCalendar")
if state.quickAddCalendar == "" {
gCal, err := state.account.Service.CalendarList.Get("primary").Do()
if err == nil {
state.quickAddCalendar = gCal.Id
}
}
} }