From db162feddd857ec6b81a5a828c8764ec5a4a7090 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sun, 22 Apr 2018 21:56:36 -0700 Subject: [PATCH] Can promote/demote todo list items --- todo/list.go | 20 ++++++++++++++++++++ todo/widget.go | 16 ++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/todo/list.go b/todo/list.go index 5b71fecb..d581c4a9 100644 --- a/todo/list.go +++ b/todo/list.go @@ -13,6 +13,16 @@ func (list *List) Delete() { list.Items = append(list.Items[:list.selected], list.Items[list.selected+1:]...) } +func (list *List) Demote() { + j := list.selected + 1 + if j >= len(list.Items) { + j = 0 + } + + list.Swap(list.selected, j) + list.selected = j +} + func (list *List) Next() { list.selected = list.selected + 1 if list.selected >= len(list.Items) { @@ -27,6 +37,16 @@ func (list *List) Prev() { } } +func (list *List) Promote() { + j := list.selected - 1 + if j < 0 { + j = len(list.Items) - 1 + } + + list.Swap(list.selected, j) + list.selected = j +} + // Toggle switches the checked state of the selected item func (list *List) Toggle() { list.Items[list.selected].Toggle() diff --git a/todo/widget.go b/todo/widget.go index 85a11bf6..510ea382 100644 --- a/todo/widget.go +++ b/todo/widget.go @@ -73,19 +73,31 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { } switch event.Key() { + case tcell.KeyCtrlA: + // Move the selected item up + widget.list.Promote() + widget.persist() + widget.display() + return nil case tcell.KeyCtrlD: - // Delete selected item + // Delete the selected item widget.list.Delete() widget.persist() widget.display() return nil + case tcell.KeyCtrlZ: + // Move the selected item down + widget.list.Demote() + widget.persist() + widget.display() + return nil case tcell.KeyDown: // Select the next item down widget.list.Next() widget.display() return nil case tcell.KeyEsc: - // Unselect the current row (pass it on) + // Unselect the current row widget.list.Unselect() widget.display() return event