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:
parent
f6c9e9be31
commit
7bf2ad88cf
@ -9,86 +9,50 @@ import (
|
|||||||
const checkWidth = 4
|
const checkWidth = 4
|
||||||
|
|
||||||
func (widget *Widget) display() {
|
func (widget *Widget) display() {
|
||||||
widget.View.Clear()
|
|
||||||
|
|
||||||
maxLen := widget.longestLineLen(widget.list.Items)
|
|
||||||
|
|
||||||
str := ""
|
str := ""
|
||||||
checked := []*Item{}
|
newList := List{selected: -1}
|
||||||
var selectedItem *Item
|
|
||||||
var newList List
|
|
||||||
for idx, item := range widget.list.Items {
|
|
||||||
foreColor, backColor := "white", "black"
|
|
||||||
|
|
||||||
// save the selected one
|
selectedItem := widget.list.Selected()
|
||||||
if idx == widget.list.selected {
|
maxLineLen := widget.list.LongestLine()
|
||||||
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"
|
|
||||||
|
|
||||||
|
for _, item := range widget.list.UncheckedItems() {
|
||||||
|
str = str + widget.formattedItemLine(item, selectedItem, maxLineLen)
|
||||||
newList.Items = append(newList.Items, item)
|
newList.Items = append(newList.Items, item)
|
||||||
}
|
}
|
||||||
|
|
||||||
// update selected index with new index of selected item
|
for _, item := range widget.list.CheckedItems() {
|
||||||
for idx, item := range newList.Items {
|
str = str + widget.formattedItemLine(item, selectedItem, maxLineLen)
|
||||||
if item == selectedItem {
|
newList.Items = append(newList.Items, item)
|
||||||
newList.selected = idx
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// update list with new Items and selected item index
|
newList.SetSelectedByItem(widget.list.Selected())
|
||||||
widget.list = &newList
|
widget.SetList(&newList)
|
||||||
|
|
||||||
|
widget.View.Clear()
|
||||||
fmt.Fprintf(widget.View, "%s", str)
|
fmt.Fprintf(widget.View, "%s", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// longestLineLen returns the length of the longest todo item line
|
func (widget *Widget) formattedItemLine(item *Item, selectedItem *Item, maxLen int) string {
|
||||||
func (widget *Widget) longestLineLen(items []*Item) int {
|
foreColor, backColor := "white", "black"
|
||||||
maxLen := 0
|
|
||||||
|
|
||||||
for _, item := range items {
|
if item.Checked {
|
||||||
if len(item.Text) > maxLen {
|
foreColor = Config.UString("wtf.mods.todo.colors.checked", "white")
|
||||||
maxLen = len(item.Text)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
49
todo/list.go
49
todo/list.go
@ -19,6 +19,18 @@ func (list *List) Add(text string) {
|
|||||||
list.Items = append([]*Item{&item}, list.Items...)
|
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() {
|
func (list *List) Delete() {
|
||||||
list.Items = append(list.Items[:list.selected], list.Items[list.selected+1:]...)
|
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() {
|
func (list *List) Prev() {
|
||||||
list.selected = list.selected - 1
|
list.selected = list.selected - 1
|
||||||
if list.selected < 0 {
|
if list.selected < 0 {
|
||||||
@ -73,13 +97,34 @@ func (list *List) Selected() *Item {
|
|||||||
return list.Items[list.selected]
|
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() {
|
func (list *List) Toggle() {
|
||||||
if list.isUnselectable() {
|
if list.isUnselectable() {
|
||||||
return
|
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() {
|
func (list *List) Unselect() {
|
||||||
|
@ -70,6 +70,10 @@ func (widget *Widget) Refresh() {
|
|||||||
widget.display()
|
widget.display()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (widget *Widget) SetList(newList *List) {
|
||||||
|
widget.list = newList
|
||||||
|
}
|
||||||
|
|
||||||
/* -------------------- Unexported Functions -------------------- */
|
/* -------------------- Unexported Functions -------------------- */
|
||||||
|
|
||||||
// edit opens a modal dialog that permits editing the text of the currently-selected item
|
// edit opens a modal dialog that permits editing the text of the currently-selected item
|
||||||
|
Loading…
x
Reference in New Issue
Block a user