Skip to content

Commit

Permalink
First pass at creating a generic checklist component
Browse files Browse the repository at this point in the history
The idea is that checklist-like modules would all share an underlying
checklist implementation (ie: Todo and Todoist) to avoid duplication.
  • Loading branch information
Chris Cummer committed Jul 12, 2018
1 parent 236005a commit 4ad25ed
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 211 deletions.
28 changes: 12 additions & 16 deletions todo/display.go
Expand Up @@ -3,56 +3,52 @@ package todo
import (
"fmt"

"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
)

const checkWidth = 4

func (widget *Widget) display() {
str := ""
newList := List{selected: -1}

selectedItem := widget.list.Selected()
maxLineLen := widget.list.LongestLine()
newList := wtf.NewChecklist()

for _, item := range widget.list.UncheckedItems() {
str = str + widget.formattedItemLine(item, selectedItem, maxLineLen)
str = str + widget.formattedItemLine(item, widget.list.SelectedItem(), widget.list.LongestLine())
newList.Items = append(newList.Items, item)
}

for _, item := range widget.list.CheckedItems() {
str = str + widget.formattedItemLine(item, selectedItem, maxLineLen)
str = str + widget.formattedItemLine(item, widget.list.SelectedItem(), widget.list.LongestLine())
newList.Items = append(newList.Items, item)
}

newList.SetSelectedByItem(widget.list.Selected())
widget.SetList(&newList)
newList.SetSelectedByItem(widget.list.SelectedItem())
widget.SetList(newList)

widget.View.Clear()
widget.View.SetText(str)
}

func (widget *Widget) formattedItemLine(item *Item, selectedItem *Item, maxLen int) string {
func (widget *Widget) formattedItemLine(item *wtf.ChecklistItem, selectedItem *wtf.ChecklistItem, maxLen int) string {
foreColor, backColor := "white", wtf.Config.UString("wtf.colors.background", "black")

if item.Checked {
foreColor = wtf.Config.UString("wtf.mods.todo.colors.checked", "white")
foreColor = wtf.Config.UString("wtf.colors.checked", "white")
}

if widget.View.HasFocus() && (item == selectedItem) {
foreColor = wtf.Config.UString("wtf.mods.todo.colors.highlight.fore", "black")
backColor = wtf.Config.UString("wtf.mods.todo.colors.highlight.back", "white")
foreColor = wtf.Config.UString("wtf.colors.highlight.fore", "black")
backColor = wtf.Config.UString("wtf.colors.highlight.back", "orange")
}

str := fmt.Sprintf(
"[%s:%s]|%s| %s[white]",
foreColor,
backColor,
item.CheckMark(),
item.Text,
tview.Escape(item.Text),
)

str = str + wtf.PadRow((checkWidth+len(item.Text)), (checkWidth+maxLen)) + "\n"

return str
return str + wtf.PadRow((checkWidth+len(item.Text)), (checkWidth+maxLen+1)) + "\n"
}
22 changes: 0 additions & 22 deletions todo/item.go

This file was deleted.

165 changes: 0 additions & 165 deletions todo/list.go

This file was deleted.

12 changes: 6 additions & 6 deletions todo/widget.go
Expand Up @@ -39,7 +39,7 @@ type Widget struct {

app *tview.Application
filePath string
list *List
list wtf.Checklist
pages *tview.Pages
}

Expand All @@ -49,7 +49,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {

app: app,
filePath: wtf.Config.UString("wtf.mods.todo.filename"),
list: &List{selected: -1},
list: wtf.NewChecklist(),
pages: pages,
}

Expand All @@ -67,19 +67,19 @@ func (widget *Widget) Refresh() {
widget.display()
}

func (widget *Widget) SetList(newList *List) {
func (widget *Widget) SetList(newList wtf.Checklist) {
widget.list = newList
}

/* -------------------- Unexported Functions -------------------- */

// edit opens a modal dialog that permits editing the text of the currently-selected item
func (widget *Widget) editItem() {
if widget.list.Selected() == nil {
if widget.list.SelectedItem() == nil {
return
}

form := widget.modalForm("Edit:", widget.list.Selected().Text)
form := widget.modalForm("Edit:", widget.list.SelectedItem().Text)

saveFctn := func() {
text := form.GetFormItem(0).(*tview.InputField).GetText()
Expand Down Expand Up @@ -191,7 +191,7 @@ func (widget *Widget) newItem() {
saveFctn := func() {
text := form.GetFormItem(0).(*tview.InputField).GetText()

widget.list.Add(text)
widget.list.Add(false, text)
widget.persist()
widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View)
Expand Down
20 changes: 18 additions & 2 deletions todoist/display.go
Expand Up @@ -8,6 +8,8 @@ import (
"github.com/senorprogrammer/wtf/wtf"
)

const checkWidth = 4

func (w *Widget) display() {
if len(w.list) == 0 {
return
Expand All @@ -17,11 +19,25 @@ func (w *Widget) display() {
w.View.SetTitle(fmt.Sprintf("%s- [green]%s[white] ", w.Name, list.Project.Name))
str := wtf.SigilStr(len(w.list), w.idx, w.View) + "\n"

maxLen := w.list[w.idx].LongestLine()

for index, item := range list.items {
foreColor, backColor := "white", wtf.Config.UString("wtf.colors.background", "black")

if index == list.index {
str = str + fmt.Sprintf("[%s]", wtf.Config.UString("wtf.colors.border.focused", "grey"))
foreColor = wtf.Config.UString("wtf.colors.highlight.fore", "black")
backColor = wtf.Config.UString("wtf.colors.highlight.back", "orange")
}
str = str + fmt.Sprintf("| | %s[white]\n", tview.Escape(item.Content))

row := fmt.Sprintf(
"[%s:%s]| | %s[white]",
foreColor,
backColor,
tview.Escape(item.Content),
)

row = row + wtf.PadRow((checkWidth+len(item.Content)), (checkWidth+maxLen+1)) + "\n"
str = str + row
}

w.View.Clear()
Expand Down
12 changes: 12 additions & 0 deletions todoist/list.go
Expand Up @@ -62,6 +62,18 @@ func (l *List) loadItems() {
l.items = tasks
}

func (list *List) LongestLine() int {
maxLen := 0

for _, item := range list.items {
if len(item.Content) > maxLen {
maxLen = len(item.Content)
}
}

return maxLen
}

func (l *List) close() {
if err := l.items[l.index].Close(); err != nil {
panic(err)
Expand Down
18 changes: 18 additions & 0 deletions wtf/checklist_item.go
@@ -0,0 +1,18 @@
package wtf

type ChecklistItem struct {
Checked bool
Text string
}

func (item *ChecklistItem) CheckMark() string {
if item.Checked {
return Config.UString("wtf.mods.todo.checkedIcon", "x")
} else {
return " "
}
}

func (item *ChecklistItem) Toggle() {
item.Checked = !item.Checked
}

0 comments on commit 4ad25ed

Please sign in to comment.