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

Clean up the Todo display code

This commit is contained in:
Chris Cummer 2018-05-30 21:39:21 -07:00
parent f6c9e9be31
commit 7bf2ad88cf
3 changed files with 82 additions and 69 deletions

View File

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

View File

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

View File

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