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
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()
}

View File

@ -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]
}

View File

@ -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()

View File

@ -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 {