From 4fb308c555168ed74ee45eb86d5fe361676112a1 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sun, 22 Apr 2018 21:31:02 -0700 Subject: [PATCH] Can delete items from Todo list --- todo/item.go | 9 +-------- todo/list.go | 29 ++++++++++++++++++----------- todo/widget.go | 29 ++++++++++++++++++----------- wtf/utils.go | 5 ++--- 4 files changed, 39 insertions(+), 33 deletions(-) diff --git a/todo/item.go b/todo/item.go index de5498c3..4f996749 100644 --- a/todo/item.go +++ b/todo/item.go @@ -1,16 +1,12 @@ package todo -import( - "time" +import ( ) type Item struct { Checked bool Index int Text string - - createdAt time.Time - updatedAt time.Time } func (item *Item) CheckMark() string { @@ -23,7 +19,4 @@ func (item *Item) CheckMark() string { func (item *Item) Toggle() { item.Checked = !item.Checked - item.updatedAt = time.Now() } - - diff --git a/todo/list.go b/todo/list.go index d3ccf9ed..5b71fecb 100644 --- a/todo/list.go +++ b/todo/list.go @@ -1,6 +1,6 @@ package todo -import () +import ("fmt") type List struct { Items []*Item @@ -8,16 +8,9 @@ type List struct { selected int } -func (list *List) Len() int { - return len(list.Items) -} - -func (list *List) Less(i, j int) bool { - return list.Items[i].Index < list.Items[j].Index -} - -func (list *List) Swap(i, j int) { - list.Items[i], list.Items[j] = list.Items[j], list.Items[i] +func (list *List) Delete() { + fmt.Println("del") + list.Items = append(list.Items[:list.selected], list.Items[list.selected+1:]...) } func (list *List) Next() { @@ -42,3 +35,17 @@ func (list *List) Toggle() { func (list *List) Unselect() { list.selected = -1 } + +/* -------------------- Sort Interface -------------------- */ + +func (list *List) Len() int { + return len(list.Items) +} + +func (list *List) Less(i, j int) bool { + return list.Items[i].Index < list.Items[j].Index +} + +func (list *List) Swap(i, j int) { + list.Items[i], list.Items[j] = list.Items[j], list.Items[i] +} diff --git a/todo/widget.go b/todo/widget.go index 3ff54a49..85a11bf6 100644 --- a/todo/widget.go +++ b/todo/widget.go @@ -2,8 +2,8 @@ package todo import ( "fmt" - "time" "io/ioutil" + "time" "github.com/gdamore/tcell" "github.com/olebedev/config" @@ -42,11 +42,7 @@ func (widget *Widget) Refresh() { return } - confDir, _ := wtf.ConfigDir() - - fileData, _ := wtf.ReadYamlFile(fmt.Sprintf("%s/%s", confDir, widget.FilePath)) - yaml.Unmarshal(fileData, &widget.list) - + widget.load() widget.display() widget.RefreshedAt = time.Now() } @@ -79,29 +75,40 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { switch event.Key() { case tcell.KeyCtrlD: // Delete selected item + widget.list.Delete() + widget.persist() + widget.display() return nil case tcell.KeyDown: + // Select the next item down widget.list.Next() widget.display() return nil - //case tcell.KeySpac: - //// Check/uncheck an item - //return nil case tcell.KeyEsc: - // Unselect the current row and pass the key on through to unselect the widget + // Unselect the current row (pass it on) widget.list.Unselect() widget.display() return event case tcell.KeyUp: - // Select next item up + // Select the next item up widget.list.Prev() widget.display() return nil default: + // Pass it along return event } } +// Loads the todo list from Yaml file +func (widget *Widget) load() { + confDir, _ := wtf.ConfigDir() + filePath := fmt.Sprintf("%s/%s", confDir, widget.FilePath) + + fileData, _ := wtf.ReadYamlFile(filePath) + yaml.Unmarshal(fileData, &widget.list) +} + // persist writes the todo list to Yaml file func (widget *Widget) persist() { confDir, _ := wtf.ConfigDir() diff --git a/wtf/utils.go b/wtf/utils.go index 59e4b066..7b6cbb57 100644 --- a/wtf/utils.go +++ b/wtf/utils.go @@ -83,13 +83,12 @@ func Now() time.Time { } func ReadYamlFile(filePath string) ([]byte, error) { - file, err := ioutil.ReadFile(filePath) - + fileData, err := ioutil.ReadFile(filePath) if err != nil { return []byte{}, err } - return file, nil + return fileData, nil } func RightAlignFormat(view *tview.TextView) string {