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
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"
"log"
"strings"
"time"
"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)
@ -23,6 +23,9 @@ func DoConfig() {
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)
}
@ -61,10 +64,33 @@ func configLayout(g *gocui.Gui) error {
v.SelBgColor = gocui.ColorGreen
v.SelFgColor = gocui.ColorBlack
v.Clear()
drawCalList(g, v)
_, err := g.SetCurrentView("default_calendars")
return err
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
}
@ -75,18 +101,24 @@ func quit(g *gocui.Gui, v *gocui.View) error {
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 {
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:]...)
state.StatusMsg = "Calendar Removed from Defaults"
removed = true
}
}
}
// Add calendar to defaults
state.defaultCalendars = append(state.defaultCalendars, calList[cy].Id)
if !removed {
// Add calendar to defaults
state.StatusMsg = "Calendar Added to Defaults"
state.defaultCalendars = append(state.defaultCalendars, calList[cy].Id)
}
updateViews(g)
return nil
}
@ -116,6 +148,11 @@ func cursorDown(g *gocui.Gui, v *gocui.View) error {
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()
@ -137,7 +174,6 @@ func drawCalList(g *gocui.Gui, v *gocui.View) error {
spc := strings.Repeat(" ", width-len(calSumTxt)-2)
fmt.Fprintln(v, calSumTxt+spc)
}
fmt.Fprintln(v, "Status: "+time.Now().Format(time.RFC3339))
return nil
}

69
main.go
View File

@ -15,7 +15,7 @@ import (
)
const (
AppName = "gocal"
AppName = "galendar"
AppVersion = 1
)
@ -26,6 +26,7 @@ type AppState struct {
cfg *userConfig.Config
account *Account
defaultCalendars []string
quickAddCalendar string
StatusMsg string
}
@ -103,6 +104,7 @@ func main() {
}
case "config":
// Open the Configuration CUI screen
InitComm()
DoConfig()
@ -113,6 +115,7 @@ func main() {
case "add":
// Quick event add to primary calendar
quickAddText := strings.Join(os.Args[2:], " ")
InitComm()
e, err := state.account.GetDefaultCalendar().QuickAdd(quickAddText)
if err != nil {
fmt.Println(err.Error())
@ -140,19 +143,6 @@ func InitComm() {
// Save the Raw Token
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()
}
@ -163,20 +153,26 @@ func DoVersionCheck() {
switch confVer {
case 0: // Initializing the app
fmt.Println("Initializing gocal")
for {
fmt.Println("Client Secret Filename (Ctrl-C to exit): ")
var fn string
if _, err := fmt.Scan(&fn); err == nil {
var b []byte
b, err = ioutil.ReadFile(fn)
if len(b) > 0 && err == nil {
state.cfg.SetBytes("ClientSecret", b)
break
if GoogleClientJson == "" {
for {
fmt.Println("Client Secret Filename (Ctrl-C to exit): ")
var fn string
if _, err := fmt.Scan(&fn); err == nil {
var b []byte
b, err = ioutil.ReadFile(fn)
if len(b) > 0 && err == nil {
state.cfg.SetBytes("ClientSecret", b)
break
}
}
}
} else {
state.cfg.Set("ClientSecret", GoogleClientJson)
}
state.cfg.SetInt("version", 1)
state.cfg.SetArray("defaultCalendars", []string{"primary"})
state.cfg.Set("quickAddCalendar", "")
}
state.ClientSecret = state.cfg.GetBytes("ClientSecret")
// Refetch the version from the config
@ -200,10 +196,12 @@ func saveAccountState() {
if err = state.cfg.SetArray("defaultCalendars", state.defaultCalendars); err != nil {
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
// into the account
// loadAccountState loads all of the config into the state struct
func loadAccountState() {
var err error
var calListJson []byte
@ -220,4 +218,25 @@ func loadAccountState() {
} else {
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
}
}
}