From bef57d799b01c9f2ca33d51fdaa1fbe258fa3c21 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Tue, 24 Apr 2018 19:41:15 -0700 Subject: [PATCH] Rudimentary modal showing up for Todo on 'e' --- clocks/display.go | 6 ++++-- clocks/widget.go | 3 --- gcal/widget.go | 2 +- system/widget.go | 2 +- todo/widget.go | 44 +++++++++++++++++++++++++++----------------- wtf.go | 16 +++++++++------- wtf/utils.go | 5 +++++ 7 files changed, 47 insertions(+), 31 deletions(-) diff --git a/clocks/display.go b/clocks/display.go index 56e07dbe..114d487d 100644 --- a/clocks/display.go +++ b/clocks/display.go @@ -2,6 +2,8 @@ package clocks import ( "fmt" + + "github.com/senorprogrammer/wtf/wtf" ) func (widget *Widget) display(clocks []Clock) { @@ -16,8 +18,8 @@ func (widget *Widget) display(clocks []Clock) { " [%s]%-12s %-10s %7s[white]\n", widget.rowColor(idx), clock.Label, - clock.LocalTime.Format(TimeFormat), - clock.LocalTime.Format(DateFormat), + clock.LocalTime.Format(wtf.SimpleTimeFormat), + clock.LocalTime.Format(wtf.SimpleDateFormat), ) } diff --git a/clocks/widget.go b/clocks/widget.go index 457d4a55..f4936376 100644 --- a/clocks/widget.go +++ b/clocks/widget.go @@ -7,9 +7,6 @@ import ( "github.com/senorprogrammer/wtf/wtf" ) -const TimeFormat = "15:04 MST" -const DateFormat = "Jan 2" - // Config is a pointer to the global config object var Config *config.Config diff --git a/gcal/widget.go b/gcal/widget.go index b68bba92..0d38e96e 100644 --- a/gcal/widget.go +++ b/gcal/widget.go @@ -113,7 +113,7 @@ func (widget *Widget) eventSummary(event *calendar.Event, conflict bool) string func (widget *Widget) eventTimestamp(event *calendar.Event) string { startTime, _ := time.Parse(time.RFC3339, event.Start.DateTime) - return startTime.Format("Mon, Jan 2, 15:04") + return startTime.Format(wtf.FriendlyDateTimeFormat) } // eventIsNow returns true if the event is happening now, false if it not diff --git a/system/widget.go b/system/widget.go index 34d72103..ec522cf6 100644 --- a/system/widget.go +++ b/system/widget.go @@ -48,7 +48,7 @@ func (widget *Widget) Refresh() { } func (widget *Widget) prettyBuiltAt() string { - str, err := time.Parse("2006-01-02T15:04:05-0700", widget.BuiltAt) + str, err := time.Parse(wtf.TimestampFormat, widget.BuiltAt) if err != nil { return err.Error() } else { diff --git a/todo/widget.go b/todo/widget.go index 71174c1b..bbb5ee3b 100644 --- a/todo/widget.go +++ b/todo/widget.go @@ -3,11 +3,11 @@ package todo import ( "fmt" "io/ioutil" - //"os/exec" "time" "github.com/gdamore/tcell" "github.com/olebedev/config" + "github.com/rivo/tview" "github.com/senorprogrammer/wtf/wtf" "gopkg.in/yaml.v2" ) @@ -18,16 +18,18 @@ var Config *config.Config type Widget struct { wtf.TextWidget - FilePath string + pages *tview.Pages + filePath string list *List } -func NewWidget() *Widget { +func NewWidget(pages *tview.Pages) *Widget { widget := Widget{ TextWidget: wtf.NewTextWidget(" 📝 Todo ", "todo"), - FilePath: Config.UString("wtf.mods.todo.filename"), - list: &List{selected: -1}, + pages: pages, + filePath: Config.UString("wtf.mods.todo.filename"), + list: &List{selected: -1}, } widget.init() @@ -50,8 +52,23 @@ func (widget *Widget) Refresh() { /* -------------------- Unexported Functions -------------------- */ +// edit opens a modal dialog that permits editing the text of the currently-selected item +func (widget *Widget) edit() { + modal := tview.NewModal(). + SetText("Do you want to quit the application?"). + AddButtons([]string{"Quit", "Cancel"}). + SetDoneFunc(func(buttonIndex int, buttonLabel string) { + if buttonLabel == "Quit" { + widget.pages.RemovePage("edit") + } + }) + + widget.pages.AddPage("edit", modal, false, true) + //widget.app.SetFocus(modal) +} + func (widget *Widget) init() { - _, err := wtf.CreateFile(widget.FilePath) + _, err := wtf.CreateFile(widget.filePath) if err != nil { panic(err) } @@ -67,6 +84,7 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { return nil case "e": // Edit selected item + widget.edit() return nil case "h": // Show help menu @@ -86,7 +104,7 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { case "o": // Open the file //widget.openFile() - wtf.OpenFile(widget.FilePath) + wtf.OpenFile(widget.filePath) return nil } @@ -133,24 +151,16 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { // Loads the todo list from Yaml file func (widget *Widget) load() { confDir, _ := wtf.ConfigDir() - filePath := fmt.Sprintf("%s/%s", confDir, widget.FilePath) + filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath) fileData, _ := wtf.ReadFileBytes(filePath) yaml.Unmarshal(fileData, &widget.list) } -//func (widget *Widget) openFile() { - //confDir, _ := wtf.ConfigDir() - //filePath := fmt.Sprintf("%s/%s", confDir, widget.FilePath) - - //cmd := exec.Command("open", filePath) - //wtf.ExecuteCommand(cmd) -//} - // persist writes the todo list to Yaml file func (widget *Widget) persist() { confDir, _ := wtf.ConfigDir() - filePath := fmt.Sprintf("%s/%s", confDir, widget.FilePath) + filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath) fileData, _ := yaml.Marshal(&widget.list) diff --git a/wtf.go b/wtf.go index 49d3331e..b5fe1702 100644 --- a/wtf.go +++ b/wtf.go @@ -138,7 +138,7 @@ var Widgets []wtf.TextViewer var result = wtf.CreateConfigDir() var ( - builtat = "now" + builtat = time.Now().Format(wtf.TimestampFormat) version = "dev" ) @@ -163,7 +163,8 @@ func main() { Config = wtf.LoadConfigFile(*flagConf) - wtf.Config = Config + app := tview.NewApplication() + pages := tview.NewPages() bamboohr.Config = Config clocks.Config = Config @@ -179,6 +180,7 @@ func main() { textfile.Config = Config todo.Config = Config weather.Config = Config + wtf.Config = Config Widgets = []wtf.TextViewer{ bamboohr.NewWidget(), @@ -193,13 +195,10 @@ func main() { status.NewWidget(), system.NewWidget(builtat, version), textfile.NewWidget(), - todo.NewWidget(), + todo.NewWidget(pages), weather.NewWidget(), } - app := tview.NewApplication() - app.SetInputCapture(keyboardIntercept) - FocusTracker = wtf.FocusTracker{ App: app, Idx: 0, @@ -210,7 +209,10 @@ func main() { go redrawApp(app) grid := buildGrid(Widgets) - if err := app.SetRoot(grid, true).Run(); err != nil { + pages.AddPage("grid", grid, true, true) + app.SetInputCapture(keyboardIntercept) + + if err := app.SetRoot(pages, true).Run(); err != nil { os.Exit(1) } } diff --git a/wtf/utils.go b/wtf/utils.go index d35e563f..6dcb35e0 100644 --- a/wtf/utils.go +++ b/wtf/utils.go @@ -10,6 +10,11 @@ import ( "github.com/rivo/tview" ) +const SimpleDateFormat = "Jan 2" +const SimpleTimeFormat = "15:04 MST" +const FriendlyDateTimeFormat = "Mon, Jan 2, 15:04" +const TimestampFormat = "2006-01-02T15:04:05-0700" + func CenterText(str string, width int) string { return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str)) }