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

Can delete items from Todo list

This commit is contained in:
Chris Cummer 2018-04-22 21:31:02 -07:00
parent a6e8d64284
commit 4fb308c555
4 changed files with 39 additions and 33 deletions

View File

@ -1,16 +1,12 @@
package todo package todo
import ( import (
"time"
) )
type Item struct { type Item struct {
Checked bool Checked bool
Index int Index int
Text string Text string
createdAt time.Time
updatedAt time.Time
} }
func (item *Item) CheckMark() string { func (item *Item) CheckMark() string {
@ -23,7 +19,4 @@ func (item *Item) CheckMark() string {
func (item *Item) Toggle() { func (item *Item) Toggle() {
item.Checked = !item.Checked item.Checked = !item.Checked
item.updatedAt = time.Now()
} }

View File

@ -1,6 +1,6 @@
package todo package todo
import () import ("fmt")
type List struct { type List struct {
Items []*Item Items []*Item
@ -8,16 +8,9 @@ type List struct {
selected int selected int
} }
func (list *List) Len() int { func (list *List) Delete() {
return len(list.Items) fmt.Println("del")
} list.Items = append(list.Items[:list.selected], list.Items[list.selected+1:]...)
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) Next() { func (list *List) Next() {
@ -42,3 +35,17 @@ func (list *List) Toggle() {
func (list *List) Unselect() { func (list *List) Unselect() {
list.selected = -1 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]
}

View File

@ -2,8 +2,8 @@ package todo
import ( import (
"fmt" "fmt"
"time"
"io/ioutil" "io/ioutil"
"time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/olebedev/config" "github.com/olebedev/config"
@ -42,11 +42,7 @@ func (widget *Widget) Refresh() {
return return
} }
confDir, _ := wtf.ConfigDir() widget.load()
fileData, _ := wtf.ReadYamlFile(fmt.Sprintf("%s/%s", confDir, widget.FilePath))
yaml.Unmarshal(fileData, &widget.list)
widget.display() widget.display()
widget.RefreshedAt = time.Now() widget.RefreshedAt = time.Now()
} }
@ -79,29 +75,40 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() { switch event.Key() {
case tcell.KeyCtrlD: case tcell.KeyCtrlD:
// Delete selected item // Delete selected item
widget.list.Delete()
widget.persist()
widget.display()
return nil return nil
case tcell.KeyDown: case tcell.KeyDown:
// Select the next item down
widget.list.Next() widget.list.Next()
widget.display() widget.display()
return nil return nil
//case tcell.KeySpac:
//// Check/uncheck an item
//return nil
case tcell.KeyEsc: 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.list.Unselect()
widget.display() widget.display()
return event return event
case tcell.KeyUp: case tcell.KeyUp:
// Select next item up // Select the next item up
widget.list.Prev() widget.list.Prev()
widget.display() widget.display()
return nil return nil
default: default:
// Pass it along
return event 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 // persist writes the todo list to Yaml file
func (widget *Widget) persist() { func (widget *Widget) persist() {
confDir, _ := wtf.ConfigDir() confDir, _ := wtf.ConfigDir()

View File

@ -83,13 +83,12 @@ func Now() time.Time {
} }
func ReadYamlFile(filePath string) ([]byte, error) { func ReadYamlFile(filePath string) ([]byte, error) {
file, err := ioutil.ReadFile(filePath) fileData, err := ioutil.ReadFile(filePath)
if err != nil { if err != nil {
return []byte{}, err return []byte{}, err
} }
return file, nil return fileData, nil
} }
func RightAlignFormat(view *tview.TextView) string { func RightAlignFormat(view *tview.TextView) string {