From 23b8a190ec80a44cd8c609ab84d9f18b39fd3fac Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 2 Mar 2017 10:26:34 -0600 Subject: [PATCH] Colorize some results * Can toggle default calendars * Though the interactive screen doesn't update :( * Colorize default view depending on time until event --- interactive_mode.go | 144 ++++++++++++++++++++ main.go | 166 ++++------------------- account_struct.go => struct_account.go | 0 calendar_struct.go => struct_calendar.go | 0 event_struct.go => struct_event.go | 7 +- struct_screen.go | 23 ++++ 6 files changed, 198 insertions(+), 142 deletions(-) create mode 100644 interactive_mode.go rename account_struct.go => struct_account.go (100%) rename calendar_struct.go => struct_calendar.go (100%) rename event_struct.go => struct_event.go (97%) create mode 100644 struct_screen.go diff --git a/interactive_mode.go b/interactive_mode.go new file mode 100644 index 0000000..0798440 --- /dev/null +++ b/interactive_mode.go @@ -0,0 +1,144 @@ +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 // diff --git a/main.go b/main.go index 18917a0..6c85a0c 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( "time" "github.com/br0xen/user-config" - "github.com/jroimartin/gocui" + "github.com/fatih/color" ) const ( @@ -50,14 +50,22 @@ func main() { DoVersionCheck() - switch op { - case "--reinit": - // Reset all config - for _, v := range state.cfg.GetKeyList() { - fmt.Println("Deleting Key: " + v) - state.cfg.DeleteKey(v) - } + for i := range os.Args { + switch os.Args[i] { + case "--reinit": + // Reset all config + for _, v := range state.cfg.GetKeyList() { + fmt.Println("Deleting Key: " + v) + state.cfg.DeleteKey(v) + } + case "--refresh": + case "-r": + + } + } + + switch op { case "today": // Show everything on the calendar for today InitComm() @@ -78,10 +86,16 @@ func main() { } sort.Sort(ByStartTime(todayEvents)) for _, e := range todayEvents { - if e.GetStartTime() == "00:00:00" { - fmt.Println("[All Day ] " + e.Summary) + if e.GetStartTimeString() == "00:00:00" { + color.New(color.FgGreen).Println("[All Day ] " + e.Summary) } else { - fmt.Println("[" + e.GetStartTime() + "] " + e.Summary) + if time.Until(e.GetStartTime()) < time.Hour { + color.New(color.FgYellow).Add(color.Bold).Println("[" + e.GetStartTimeString() + "] " + e.Summary) + } else if time.Until(e.GetStartTime()) < time.Minute*30 { + color.New(color.FgRed).Add(color.Bold).Println("[" + e.GetStartTimeString() + "] " + e.Summary) + } else { + fmt.Println("[" + e.GetStartTimeString() + "] " + e.Summary) + } } } } else { @@ -207,133 +221,3 @@ func loadAccountState() { fmt.Println("error: ", err) } } - -// 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 { - v.Highlight = true - v.SelBgColor = gocui.ColorGreen - v.SelFgColor = gocui.ColorBlack - if err != gocui.ErrUnknownView { - return err - } - drawCalList(g, v) - - if _, err := g.SetCurrentView("default_calendars"); 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() - 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 { - } - } - } - drawCalList(g, v) - 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: "+state.StatusMsg) - return nil -} - -// END CONFIG SCREEN FUNCTIONS // diff --git a/account_struct.go b/struct_account.go similarity index 100% rename from account_struct.go rename to struct_account.go diff --git a/calendar_struct.go b/struct_calendar.go similarity index 100% rename from calendar_struct.go rename to struct_calendar.go diff --git a/event_struct.go b/struct_event.go similarity index 97% rename from event_struct.go rename to struct_event.go index 47a66e2..dc21bc7 100644 --- a/event_struct.go +++ b/struct_event.go @@ -48,7 +48,7 @@ func (e *Event) ToCLIString() string { return fmt.Sprintf("%s\n%s\n", e.Summary, e.GetStartTime()) } -func (e *Event) GetStartTime() string { +func (e *Event) GetStartTimeString() string { tm, err := time.Parse(time.RFC3339, e.Start.DateTime) if err != nil { return "00:00:00" @@ -56,6 +56,11 @@ func (e *Event) GetStartTime() string { return tm.Local().Format("15:04:05") } +func (e *Event) GetStartTime() time.Time { + tm, _ := time.Parse(time.RFC3339, e.Start.DateTime) + return tm +} + func GoogleEventToLocal(e *calendar.Event) *Event { return &Event{ //Attachments []EventAttachment // Google Drive files diff --git a/struct_screen.go b/struct_screen.go new file mode 100644 index 0000000..f9732ce --- /dev/null +++ b/struct_screen.go @@ -0,0 +1,23 @@ +package main + +import "github.com/jroimartin/gocui" + +type screen interface { + setViewDefaults(*gocui.View) + setViewTextAndCursor(*gocui.View, string) + getViewValue(*gocui.Gui) +} + +type position struct { + // value = pct * MAX + abs + pct float32 + abs int +} + +func (p position) getCoordinate(max int) int { + return int(p.pct*float32(max)) + p.abs +} + +type viewPosition struct { + x0, y0, x1, y1 position +}