diff --git a/todo/list.go b/todo/list.go index 824462f7..cbb43ab6 100644 --- a/todo/list.go +++ b/todo/list.go @@ -24,6 +24,10 @@ func (list *List) Delete() { } func (list *List) Demote() { + if list.isUnselectable() { + return + } + j := list.selected + 1 if j >= len(list.Items) { j = 0 @@ -48,6 +52,10 @@ func (list *List) Prev() { } func (list *List) Promote() { + if list.isUnselectable() { + return + } + j := list.selected - 1 if j < 0 { j = len(list.Items) - 1 @@ -58,7 +66,7 @@ func (list *List) Promote() { } func (list *List) Selected() *Item { - if list.selected < 0 || list.selected >= len(list.Items) { + if list.isUnselectable() { return nil } @@ -67,6 +75,10 @@ func (list *List) Selected() *Item { // Toggle switches the checked state of the selected item func (list *List) Toggle() { + if list.isUnselectable() { + return + } + list.Items[list.selected].Toggle() } @@ -97,3 +109,13 @@ func (list *List) Less(i, j int) bool { func (list *List) Swap(i, j int) { list.Items[i], list.Items[j] = list.Items[j], list.Items[i] } + +/* -------------------- Unexported Functions -------------------- */ + +func (list *List) isSelectable() bool { + return list.selected >= 0 && list.selected < len(list.Items) +} + +func (list *List) isUnselectable() bool { + return !list.isSelectable() +}