mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Add Scrollable to todoist widget
This is now a multi-source scrollable widget, trying to leverage as much of existing functionality as possible for consistency
This commit is contained in:
parent
44a2634a85
commit
2fb1a06ca0
@ -24,18 +24,12 @@ func (widget *Widget) display() {
|
|||||||
maxLen := proj.LongestLine()
|
maxLen := proj.LongestLine()
|
||||||
|
|
||||||
for index, item := range proj.tasks {
|
for index, item := range proj.tasks {
|
||||||
foreColor, backColor := widget.settings.common.Colors.Text, widget.settings.common.Colors.Background
|
|
||||||
|
|
||||||
if index == proj.index {
|
|
||||||
foreColor = widget.settings.common.Colors.HighlightFore
|
|
||||||
backColor = widget.settings.common.Colors.HighlightBack
|
|
||||||
}
|
|
||||||
|
|
||||||
row := fmt.Sprintf(
|
row := fmt.Sprintf(
|
||||||
"[%s:%s]| | %s[white]",
|
`["%d"][""][%s]| | %s[%s]`,
|
||||||
foreColor,
|
index,
|
||||||
backColor,
|
widget.RowColor(index),
|
||||||
tview.Escape(item.Content),
|
tview.Escape(item.Content),
|
||||||
|
widget.RowColor(index),
|
||||||
)
|
)
|
||||||
|
|
||||||
_, _, w, _ := widget.View.GetInnerRect()
|
_, _, w, _ := widget.View.GetInnerRect()
|
||||||
@ -43,8 +37,8 @@ func (widget *Widget) display() {
|
|||||||
maxLen = w
|
maxLen = w
|
||||||
}
|
}
|
||||||
|
|
||||||
str = str + row + wtf.PadRow((checkWidth+len(item.Content)), (checkWidth+maxLen+1)) + "\n"
|
str = str + row + wtf.PadRow((checkWidth+len(item.Content)), (checkWidth+maxLen+1)) + `[""]` + "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
widget.TextWidget.Redraw(title, str, false)
|
widget.ScrollableWidget.Redraw(title, str, false)
|
||||||
}
|
}
|
||||||
|
@ -5,15 +5,17 @@ import "github.com/gdamore/tcell"
|
|||||||
func (widget *Widget) initializeKeyboardControls() {
|
func (widget *Widget) initializeKeyboardControls() {
|
||||||
widget.SetKeyboardChar("/", widget.ShowHelp, "Show/hide this help prompt")
|
widget.SetKeyboardChar("/", widget.ShowHelp, "Show/hide this help prompt")
|
||||||
widget.SetKeyboardChar("r", widget.Refresh, "Refresh widget")
|
widget.SetKeyboardChar("r", widget.Refresh, "Refresh widget")
|
||||||
widget.SetKeyboardChar("j", widget.Down, "Select next item")
|
|
||||||
widget.SetKeyboardChar("k", widget.Up, "Select previous item")
|
|
||||||
widget.SetKeyboardChar("c", widget.Close, "Close item")
|
|
||||||
widget.SetKeyboardChar("d", widget.Delete, "Delete item")
|
widget.SetKeyboardChar("d", widget.Delete, "Delete item")
|
||||||
|
widget.SetKeyboardChar("j", widget.Prev, "Select previous item")
|
||||||
|
widget.SetKeyboardChar("k", widget.Next, "Select next item")
|
||||||
widget.SetKeyboardChar("h", widget.PrevSource, "Select previous project")
|
widget.SetKeyboardChar("h", widget.PrevSource, "Select previous project")
|
||||||
|
widget.SetKeyboardChar("c", widget.Close, "Close item")
|
||||||
widget.SetKeyboardChar("l", widget.NextSource, "Select next project")
|
widget.SetKeyboardChar("l", widget.NextSource, "Select next project")
|
||||||
|
widget.SetKeyboardChar("u", widget.Unselect, "Clear selection")
|
||||||
|
|
||||||
widget.SetKeyboardKey(tcell.KeyRight, widget.NextSource, "Select next project")
|
widget.SetKeyboardKey(tcell.KeyDown, widget.Next, "Select next item")
|
||||||
|
widget.SetKeyboardKey(tcell.KeyUp, widget.Prev, "Select previous item")
|
||||||
|
widget.SetKeyboardKey(tcell.KeyEsc, widget.Unselect, "Clear selection")
|
||||||
widget.SetKeyboardKey(tcell.KeyLeft, widget.PrevSource, "Select previous project")
|
widget.SetKeyboardKey(tcell.KeyLeft, widget.PrevSource, "Select previous project")
|
||||||
widget.SetKeyboardKey(tcell.KeyDown, widget.Down, "Select next item")
|
widget.SetKeyboardKey(tcell.KeyRight, widget.NextSource, "Select next project")
|
||||||
widget.SetKeyboardKey(tcell.KeyUp, widget.Up, "Select previous item")
|
|
||||||
}
|
}
|
||||||
|
@ -32,34 +32,10 @@ func NewProject(id int) *Project {
|
|||||||
return proj
|
return proj
|
||||||
}
|
}
|
||||||
|
|
||||||
func (proj *Project) isFirst() bool {
|
|
||||||
return proj.index == 0
|
|
||||||
}
|
|
||||||
|
|
||||||
func (proj *Project) isLast() bool {
|
func (proj *Project) isLast() bool {
|
||||||
return proj.index >= len(proj.tasks)-1
|
return proj.index >= len(proj.tasks)-1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (proj *Project) up() {
|
|
||||||
proj.index--
|
|
||||||
|
|
||||||
if proj.index < 0 {
|
|
||||||
proj.index = len(proj.tasks) - 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (proj *Project) down() {
|
|
||||||
if proj.index == -1 {
|
|
||||||
proj.index = 0
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
proj.index++
|
|
||||||
if proj.index >= len(proj.tasks) {
|
|
||||||
proj.index = 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (proj *Project) loadTasks() {
|
func (proj *Project) loadTasks() {
|
||||||
tasks, err := todoist.ListTask(todoist.QueryParam{"project_id": fmt.Sprintf("%d", proj.ID)})
|
tasks, err := todoist.ListTask(todoist.QueryParam{"project_id": fmt.Sprintf("%d", proj.ID)})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package todoist
|
package todoist
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/darkSasori/todoist"
|
"github.com/darkSasori/todoist"
|
||||||
"github.com/gdamore/tcell"
|
|
||||||
"github.com/rivo/tview"
|
"github.com/rivo/tview"
|
||||||
"github.com/wtfutil/wtf/wtf"
|
"github.com/wtfutil/wtf/wtf"
|
||||||
)
|
)
|
||||||
@ -10,8 +10,8 @@ import (
|
|||||||
// A Widget represents a Todoist widget
|
// A Widget represents a Todoist widget
|
||||||
type Widget struct {
|
type Widget struct {
|
||||||
wtf.KeyboardWidget
|
wtf.KeyboardWidget
|
||||||
wtf.TextWidget
|
|
||||||
wtf.MultiSourceWidget
|
wtf.MultiSourceWidget
|
||||||
|
wtf.ScrollableWidget
|
||||||
|
|
||||||
projects []*Project
|
projects []*Project
|
||||||
settings *Settings
|
settings *Settings
|
||||||
@ -21,8 +21,8 @@ type Widget struct {
|
|||||||
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
|
||||||
widget := Widget{
|
widget := Widget{
|
||||||
KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common),
|
KeyboardWidget: wtf.NewKeyboardWidget(app, pages, settings.common),
|
||||||
TextWidget: wtf.NewTextWidget(app, settings.common, true),
|
|
||||||
MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common, "project", "projects"),
|
MultiSourceWidget: wtf.NewMultiSourceWidget(settings.common, "project", "projects"),
|
||||||
|
ScrollableWidget: wtf.NewScrollableWidget(app, settings.common, true),
|
||||||
|
|
||||||
settings: settings,
|
settings: settings,
|
||||||
}
|
}
|
||||||
@ -30,6 +30,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
|
|||||||
widget.loadAPICredentials()
|
widget.loadAPICredentials()
|
||||||
widget.loadProjects()
|
widget.loadProjects()
|
||||||
|
|
||||||
|
widget.SetRenderFunction(widget.display)
|
||||||
widget.initializeKeyboardControls()
|
widget.initializeKeyboardControls()
|
||||||
widget.View.SetInputCapture(widget.InputCapture)
|
widget.View.SetInputCapture(widget.InputCapture)
|
||||||
widget.SetDisplayFunction(widget.display)
|
widget.SetDisplayFunction(widget.display)
|
||||||
@ -55,9 +56,11 @@ func (widget *Widget) ProjectAt(idx int) *Project {
|
|||||||
|
|
||||||
func (w *Widget) Refresh() {
|
func (w *Widget) Refresh() {
|
||||||
if w.Disabled() || w.CurrentProject() == nil {
|
if w.Disabled() || w.CurrentProject() == nil {
|
||||||
|
w.SetItemCount(0)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
w.SetItemCount(len(w.CurrentProject().tasks))
|
||||||
w.display()
|
w.display()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,30 +68,48 @@ func (widget *Widget) HelpText() string {
|
|||||||
return widget.KeyboardWidget.HelpText()
|
return widget.KeyboardWidget.HelpText()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (widget *Widget) NextSource() {
|
||||||
|
widget.MultiSourceWidget.NextSource()
|
||||||
|
widget.Selected = widget.CurrentProject().index
|
||||||
|
widget.SetItemCount(len(widget.CurrentProject().tasks))
|
||||||
|
widget.RenderFunction()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (widget *Widget) PrevSource() {
|
||||||
|
widget.MultiSourceWidget.PrevSource()
|
||||||
|
widget.Selected = widget.CurrentProject().index
|
||||||
|
widget.SetItemCount(len(widget.CurrentProject().tasks))
|
||||||
|
widget.RenderFunction()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (widget *Widget) Prev() {
|
||||||
|
widget.ScrollableWidget.Prev()
|
||||||
|
widget.CurrentProject().index = widget.Selected
|
||||||
|
}
|
||||||
|
|
||||||
|
func (widget *Widget) Next() {
|
||||||
|
widget.ScrollableWidget.Next()
|
||||||
|
widget.CurrentProject().index = widget.Selected
|
||||||
|
}
|
||||||
|
|
||||||
|
func (widget *Widget) Unselect() {
|
||||||
|
widget.ScrollableWidget.Unselect()
|
||||||
|
widget.CurrentProject().index = -1
|
||||||
|
widget.RenderFunction()
|
||||||
|
}
|
||||||
|
|
||||||
/* -------------------- Keyboard Movement -------------------- */
|
/* -------------------- Keyboard Movement -------------------- */
|
||||||
|
|
||||||
// Down selects the next item in the list
|
|
||||||
func (w *Widget) Down() {
|
|
||||||
w.CurrentProject().down()
|
|
||||||
w.display()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Up selects the previous item in the list
|
|
||||||
func (w *Widget) Up() {
|
|
||||||
w.CurrentProject().up()
|
|
||||||
w.display()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close closes the currently-selected task in the currently-selected project
|
// Close closes the currently-selected task in the currently-selected project
|
||||||
func (w *Widget) Close() {
|
func (w *Widget) Close() {
|
||||||
w.CurrentProject().closeSelectedTask()
|
w.CurrentProject().closeSelectedTask()
|
||||||
|
|
||||||
if w.CurrentProject().isLast() {
|
if w.CurrentProject().isLast() {
|
||||||
w.Up()
|
w.Prev()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Down()
|
w.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete deletes the currently-selected task in the currently-selected project
|
// Delete deletes the currently-selected task in the currently-selected project
|
||||||
@ -96,11 +117,11 @@ func (w *Widget) Delete() {
|
|||||||
w.CurrentProject().deleteSelectedTask()
|
w.CurrentProject().deleteSelectedTask()
|
||||||
|
|
||||||
if w.CurrentProject().isLast() {
|
if w.CurrentProject().isLast() {
|
||||||
w.Up()
|
w.Prev()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Down()
|
w.Next()
|
||||||
}
|
}
|
||||||
|
|
||||||
/* -------------------- Unexported Functions -------------------- */
|
/* -------------------- Unexported Functions -------------------- */
|
||||||
@ -119,17 +140,3 @@ func (widget *Widget) loadProjects() {
|
|||||||
|
|
||||||
widget.projects = projects
|
widget.projects = projects
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *Widget) vimBindings(event *tcell.EventKey) tcell.Key {
|
|
||||||
switch string(event.Rune()) {
|
|
||||||
case "h":
|
|
||||||
return tcell.KeyLeft
|
|
||||||
case "l":
|
|
||||||
return tcell.KeyRight
|
|
||||||
case "k":
|
|
||||||
return tcell.KeyUp
|
|
||||||
case "j":
|
|
||||||
return tcell.KeyDown
|
|
||||||
}
|
|
||||||
return event.Key()
|
|
||||||
}
|
|
||||||
|
@ -10,7 +10,7 @@ import (
|
|||||||
type ScrollableWidget struct {
|
type ScrollableWidget struct {
|
||||||
TextWidget
|
TextWidget
|
||||||
|
|
||||||
selected int
|
Selected int
|
||||||
maxItems int
|
maxItems int
|
||||||
RenderFunction func()
|
RenderFunction func()
|
||||||
}
|
}
|
||||||
@ -36,11 +36,11 @@ func (widget *ScrollableWidget) SetItemCount(items int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (widget *ScrollableWidget) GetSelected() int {
|
func (widget *ScrollableWidget) GetSelected() int {
|
||||||
return widget.selected
|
return widget.Selected
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *ScrollableWidget) RowColor(idx int) string {
|
func (widget *ScrollableWidget) RowColor(idx int) string {
|
||||||
if widget.View.HasFocus() && (idx == widget.selected) {
|
if widget.View.HasFocus() && (idx == widget.Selected) {
|
||||||
return widget.CommonSettings.DefaultFocussedRowColor()
|
return widget.CommonSettings.DefaultFocussedRowColor()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,23 +48,23 @@ func (widget *ScrollableWidget) RowColor(idx int) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (widget *ScrollableWidget) Next() {
|
func (widget *ScrollableWidget) Next() {
|
||||||
widget.selected++
|
widget.Selected++
|
||||||
if widget.selected >= widget.maxItems {
|
if widget.Selected >= widget.maxItems {
|
||||||
widget.selected = 0
|
widget.Selected = 0
|
||||||
}
|
}
|
||||||
widget.RenderFunction()
|
widget.RenderFunction()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *ScrollableWidget) Prev() {
|
func (widget *ScrollableWidget) Prev() {
|
||||||
widget.selected--
|
widget.Selected--
|
||||||
if widget.selected < 0 {
|
if widget.Selected < 0 {
|
||||||
widget.selected = widget.maxItems - 1
|
widget.Selected = widget.maxItems - 1
|
||||||
}
|
}
|
||||||
widget.RenderFunction()
|
widget.RenderFunction()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (widget *ScrollableWidget) Unselect() {
|
func (widget *ScrollableWidget) Unselect() {
|
||||||
widget.selected = -1
|
widget.Selected = -1
|
||||||
if widget.RenderFunction != nil {
|
if widget.RenderFunction != nil {
|
||||||
widget.RenderFunction()
|
widget.RenderFunction()
|
||||||
}
|
}
|
||||||
@ -74,6 +74,6 @@ func (widget *ScrollableWidget) Redraw(title, content string, wrap bool) {
|
|||||||
|
|
||||||
widget.TextWidget.Redraw(title, content, wrap)
|
widget.TextWidget.Redraw(title, content, wrap)
|
||||||
widget.app.QueueUpdateDraw(func() {
|
widget.app.QueueUpdateDraw(func() {
|
||||||
widget.View.Highlight(strconv.Itoa(widget.selected)).ScrollToHighlight()
|
widget.View.Highlight(strconv.Itoa(widget.Selected)).ScrollToHighlight()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user