1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00
wtf/modules/todo/display.go
Kirill Motkov f0771cd013 Some code improvements
* Some assignments simplified by using assignment operators
* Rewrite switch statement with only one case as if.
* Rewrite if-else chain as a switch statement.
* go fmt `modules/todoist/project.go` file.
2019-05-21 17:29:09 +03:00

67 lines
1.7 KiB
Go

package todo
import (
"fmt"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/checklist"
"github.com/wtfutil/wtf/wtf"
)
const checkWidth = 4
func (widget *Widget) display() {
str := ""
newList := checklist.NewChecklist(
widget.settings.common.Sigils.Checkbox.Checked,
widget.settings.common.Sigils.Checkbox.Unchecked,
)
offset := 0
for idx, item := range widget.list.UncheckedItems() {
str += widget.formattedItemLine(idx, item, widget.list.SelectedItem(), widget.list.LongestLine())
newList.Items = append(newList.Items, item)
offset++
}
for idx, item := range widget.list.CheckedItems() {
str += widget.formattedItemLine(idx+offset, item, widget.list.SelectedItem(), widget.list.LongestLine())
newList.Items = append(newList.Items, item)
}
newList.SetSelectedByItem(widget.list.SelectedItem())
widget.SetList(newList)
widget.Redraw(widget.CommonSettings.Title, str, false)
}
func (widget *Widget) formattedItemLine(idx int, item *checklist.ChecklistItem, selectedItem *checklist.ChecklistItem, maxLen int) string {
foreColor, backColor := widget.settings.common.Colors.Text, widget.settings.common.Colors.Background
if item.Checked {
foreColor = widget.settings.common.Colors.Checked
}
if widget.View.HasFocus() && (item == selectedItem) {
foreColor = widget.settings.common.Colors.HighlightFore
backColor = widget.settings.common.Colors.HighlightBack
}
str := fmt.Sprintf(
`["%d"][""][%s:%s]|%s| %s[white][""]`,
idx,
foreColor,
backColor,
item.CheckMark(),
tview.Escape(item.Text),
)
_, _, w, _ := widget.View.GetInnerRect()
if w > maxLen {
maxLen = w
}
return str + wtf.PadRow((checkWidth+len(item.Text)), (checkWidth+maxLen+1)) + "\n"
}