1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Rough draft of adding new items to Todo

This commit is contained in:
Chris Cummer 2018-04-28 21:32:06 -07:00
parent c63d92a1f7
commit 4a30de5d51
4 changed files with 73 additions and 30 deletions

View File

@ -1,8 +1,6 @@
package todo package todo
import ( import ()
"fmt"
)
type List struct { type List struct {
Items []*Item Items []*Item
@ -19,7 +17,6 @@ func (list *List) Add(text string) {
} }
list.Items = append([]*Item{&item}, list.Items...) list.Items = append([]*Item{&item}, list.Items...)
fmt.Println("added")
} }
func (list *List) Delete() { func (list *List) Delete() {
@ -77,6 +74,16 @@ func (list *List) Unselect() {
list.selected = -1 list.selected = -1
} }
func (list *List) Update(text string) {
item := list.Selected()
if item == nil {
return
}
item.Text = text
}
/* -------------------- Sort Interface -------------------- */ /* -------------------- Sort Interface -------------------- */
func (list *List) Len() int { func (list *List) Len() int {

View File

@ -66,15 +66,22 @@ func (widget *Widget) editItem() {
SetButtonsAlign(tview.AlignCenter). SetButtonsAlign(tview.AlignCenter).
SetButtonTextColor(tview.Styles.PrimaryTextColor) SetButtonTextColor(tview.Styles.PrimaryTextColor)
form.AddInputField("New text:", widget.selectedText(), 60, nil, nil) form.AddInputField("Edit item:", widget.list.Selected().Text, 60, nil, nil)
form.AddButton("Save", func() { form.AddButton("Save", func() {
fld := form.GetFormItem(0) fld := form.GetFormItem(0)
text := fld.(*tview.InputField).GetText()
widget.updateItem(fld.(*tview.InputField).GetText()) widget.list.Update(text)
widget.persist()
widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View)
widget.display()
})
form.AddButton("Cancel", func() {
widget.pages.RemovePage("modal") widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View) widget.app.SetFocus(widget.View)
widget.persist()
widget.display() widget.display()
}) })
@ -87,7 +94,37 @@ func (widget *Widget) editItem() {
} }
func (widget *Widget) newItem() { func (widget *Widget) newItem() {
_, _, w, h := widget.View.GetInnerRect()
form := tview.NewForm().
SetButtonsAlign(tview.AlignCenter).
SetButtonTextColor(tview.Styles.PrimaryTextColor)
form.AddInputField("New item:", "", 60, nil, nil)
form.AddButton("Save", func() {
fld := form.GetFormItem(0)
text := fld.(*tview.InputField).GetText()
widget.list.Add(text)
widget.persist()
widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View)
widget.display()
})
form.AddButton("Cancel", func() {
widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View)
widget.display()
})
frame := tview.NewFrame(form).SetBorders(0, 0, 0, 0, 0, 0)
frame.SetBorder(true)
frame.SetRect(w+20, h+2, 80, 7)
widget.pages.AddPage("modal", frame, false, true)
widget.app.SetFocus(frame)
} }
func (widget *Widget) init() { func (widget *Widget) init() {
@ -194,25 +231,3 @@ func (widget *Widget) persist() {
panic(err) panic(err)
} }
} }
func (widget *Widget) selectedText() string {
selectedItem := widget.list.Selected()
if selectedItem == nil {
return ""
} else {
return selectedItem.Text
}
}
func (widget *Widget) updateItem(text string) {
selectedItem := widget.list.Selected()
if selectedItem == nil {
// Create a new item
widget.list.Add(text)
} else {
// Update the text of the existing item
selectedItem.Text = text
}
}

View File

@ -17,6 +17,10 @@ type FocusTracker struct {
// Next sets the focus on the next widget in the widget list. If the current widget is // Next sets the focus on the next widget in the widget list. If the current widget is
// the last widget, sets focus on the first widget. // the last widget, sets focus on the first widget.
func (tracker *FocusTracker) Next() { func (tracker *FocusTracker) Next() {
//if tracker.widgetHasFocus() == false {
//return
//}
tracker.blur(tracker.Idx) tracker.blur(tracker.Idx)
tracker.increment() tracker.increment()
tracker.focus(tracker.Idx) tracker.focus(tracker.Idx)
@ -30,12 +34,16 @@ func (tracker *FocusTracker) None() {
// Prev sets the focus on the previous widget in the widget list. If the current widget is // Prev sets the focus on the previous widget in the widget list. If the current widget is
// the last widget, sets focus on the last widget. // the last widget, sets focus on the last widget.
func (tracker *FocusTracker) Prev() { func (tracker *FocusTracker) Prev() {
//if tracker.widgetHasFocus() == false {
//return
//}
tracker.blur(tracker.Idx) tracker.blur(tracker.Idx)
tracker.decrement() tracker.decrement()
tracker.focus(tracker.Idx) tracker.focus(tracker.Idx)
} }
/* -------------------- Exported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (tracker *FocusTracker) blur(idx int) { func (tracker *FocusTracker) blur(idx int) {
view := tracker.Widgets[idx].TextView() view := tracker.Widgets[idx].TextView()
@ -64,3 +72,15 @@ func (tracker *FocusTracker) increment() {
tracker.Idx = 0 tracker.Idx = 0
} }
} }
// widgetHasFocus returns true if one of the widgets currently has the app's focus,
// false if none of them do (ie: perhaps a modal dialog currently has it instead)
//func (tracker *FocusTracker) widgetHasFocus() bool {
//for _, widget := range tracker.Widgets {
//if widget.TextView() == tracker.App.GetFocus() {
//return true
//}
//}
//return false
//}

View File

@ -66,6 +66,7 @@ func NamesFromEmails(emails []string) []string {
return names return names
} }
// OpenFile opens the file defined in `path` via the operating system
func OpenFile(path string) { func OpenFile(path string) {
confDir, _ := ConfigDir() confDir, _ := ConfigDir()
filePath := fmt.Sprintf("%s/%s", confDir, path) filePath := fmt.Sprintf("%s/%s", confDir, path)