From 7bf2ad88cf5a36c5faa405194cf135f41bf94c88 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Wed, 30 May 2018 21:39:21 -0700 Subject: [PATCH] Clean up the Todo display code --- todo/display.go | 98 ++++++++++++++++--------------------------------- todo/list.go | 49 ++++++++++++++++++++++++- todo/widget.go | 4 ++ 3 files changed, 82 insertions(+), 69 deletions(-) diff --git a/todo/display.go b/todo/display.go index 971e2d3a..f66f578c 100644 --- a/todo/display.go +++ b/todo/display.go @@ -9,86 +9,50 @@ import ( const checkWidth = 4 func (widget *Widget) display() { - widget.View.Clear() - - maxLen := widget.longestLineLen(widget.list.Items) - str := "" - checked := []*Item{} - var selectedItem *Item - var newList List - for idx, item := range widget.list.Items { - foreColor, backColor := "white", "black" + newList := List{selected: -1} - // save the selected one - if idx == widget.list.selected { - selectedItem = item - } - - if item.Checked { - checked = append(checked, item) - continue - } - - if widget.View.HasFocus() && item == selectedItem { - foreColor = Config.UString("wtf.mods.todo.colors.highlight.fore", "black") - backColor = Config.UString("wtf.mods.todo.colors.highlight.back", "white") - } - - str = str + fmt.Sprintf( - "[%s:%s]|%s| %s[white]", - foreColor, - backColor, - item.CheckMark(), - item.Text, - ) - - str = str + wtf.PadRow((4+len(item.Text)), (4+maxLen)) + "\n" - - newList.Items = append(newList.Items, item) - } - for _, item := range checked { - foreColor, backColor := Config.UString("wtf.mods.todo.colors.checked", "white"), "black" - - if widget.View.HasFocus() && item == selectedItem { - foreColor = Config.UString("wtf.mods.todo.colors.highlight.fore", "black") - backColor = Config.UString("wtf.mods.todo.colors.highlight.back", "white") - } - str = str + fmt.Sprintf( - "[%s:%s]|%s| %s[white]", - foreColor, - backColor, - item.CheckMark(), - item.Text, - ) - - str = str + wtf.PadRow((4+len(item.Text)), (4+maxLen)) + "\n" + selectedItem := widget.list.Selected() + maxLineLen := widget.list.LongestLine() + for _, item := range widget.list.UncheckedItems() { + str = str + widget.formattedItemLine(item, selectedItem, maxLineLen) newList.Items = append(newList.Items, item) } - // update selected index with new index of selected item - for idx, item := range newList.Items { - if item == selectedItem { - newList.selected = idx - } + for _, item := range widget.list.CheckedItems() { + str = str + widget.formattedItemLine(item, selectedItem, maxLineLen) + newList.Items = append(newList.Items, item) } - // update list with new Items and selected item index - widget.list = &newList + newList.SetSelectedByItem(widget.list.Selected()) + widget.SetList(&newList) + widget.View.Clear() fmt.Fprintf(widget.View, "%s", str) } -// longestLineLen returns the length of the longest todo item line -func (widget *Widget) longestLineLen(items []*Item) int { - maxLen := 0 +func (widget *Widget) formattedItemLine(item *Item, selectedItem *Item, maxLen int) string { + foreColor, backColor := "white", "black" - for _, item := range items { - if len(item.Text) > maxLen { - maxLen = len(item.Text) - } + if item.Checked { + foreColor = Config.UString("wtf.mods.todo.colors.checked", "white") } - return maxLen + if widget.View.HasFocus() && (item == selectedItem) { + foreColor = Config.UString("wtf.mods.todo.colors.highlight.fore", "black") + backColor = Config.UString("wtf.mods.todo.colors.highlight.back", "white") + } + + str := fmt.Sprintf( + "[%s:%s]|%s| %s[white]", + foreColor, + backColor, + item.CheckMark(), + item.Text, + ) + + str = str + wtf.PadRow((checkWidth+len(item.Text)), (checkWidth+maxLen)) + "\n" + + return str } diff --git a/todo/list.go b/todo/list.go index cbb43ab6..9d93beb6 100644 --- a/todo/list.go +++ b/todo/list.go @@ -19,6 +19,18 @@ func (list *List) Add(text string) { list.Items = append([]*Item{&item}, list.Items...) } +func (list *List) CheckedItems() []*Item { + items := []*Item{} + + for _, item := range list.Items { + if item.Checked { + items = append(items, item) + } + } + + return items +} + func (list *List) Delete() { list.Items = append(list.Items[:list.selected], list.Items[list.selected+1:]...) } @@ -44,6 +56,18 @@ func (list *List) Next() { } } +func (list *List) LongestLine() int { + maxLen := 0 + + for _, item := range list.Items { + if len(item.Text) > maxLen { + maxLen = len(item.Text) + } + } + + return maxLen +} + func (list *List) Prev() { list.selected = list.selected - 1 if list.selected < 0 { @@ -73,13 +97,34 @@ func (list *List) Selected() *Item { return list.Items[list.selected] } -// Toggle switches the checked state of the selected item +func (list *List) SetSelectedByItem(selectableItem *Item) { + for idx, item := range list.Items { + if item == selectableItem { + list.selected = idx + break + } + } +} + +// Toggle switches the checked state of the currently-selected item func (list *List) Toggle() { if list.isUnselectable() { return } - list.Items[list.selected].Toggle() + list.Selected().Toggle() +} + +func (list *List) UncheckedItems() []*Item { + items := []*Item{} + + for _, item := range list.Items { + if !item.Checked { + items = append(items, item) + } + } + + return items } func (list *List) Unselect() { diff --git a/todo/widget.go b/todo/widget.go index dfee992f..df66bac4 100644 --- a/todo/widget.go +++ b/todo/widget.go @@ -70,6 +70,10 @@ func (widget *Widget) Refresh() { widget.display() } +func (widget *Widget) SetList(newList *List) { + widget.list = newList +} + /* -------------------- Unexported Functions -------------------- */ // edit opens a modal dialog that permits editing the text of the currently-selected item