2019-02-15 17:15:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-02-26 19:22:37 +00:00
|
|
|
"errors"
|
2019-02-15 17:15:26 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-02-20 13:49:41 +00:00
|
|
|
todotxt "github.com/br0xen/go-todotxt"
|
2019-02-28 13:44:03 +00:00
|
|
|
"github.com/br0xen/termbox-screen"
|
2019-02-15 17:15:26 +00:00
|
|
|
"github.com/br0xen/termbox-util"
|
|
|
|
termbox "github.com/nsf/termbox-go"
|
|
|
|
)
|
|
|
|
|
2019-02-28 13:44:03 +00:00
|
|
|
type ViewPort struct {
|
|
|
|
bytesPerRow int
|
|
|
|
numberOfRows int
|
|
|
|
firstRow int
|
|
|
|
}
|
|
|
|
|
2019-02-15 17:15:26 +00:00
|
|
|
// MainScreen holds all that's going on
|
|
|
|
type MainScreen struct {
|
2019-02-21 16:47:08 +00:00
|
|
|
viewPort ViewPort
|
2019-02-15 17:15:26 +00:00
|
|
|
message string
|
|
|
|
messageTimeout time.Duration
|
|
|
|
messageTime time.Time
|
2019-02-26 19:22:37 +00:00
|
|
|
messageColorBg termbox.Attribute
|
|
|
|
messageColorFg termbox.Attribute
|
2019-02-15 17:15:26 +00:00
|
|
|
mode int
|
2019-02-21 16:47:08 +00:00
|
|
|
cursor map[string]int
|
2019-02-15 17:15:26 +00:00
|
|
|
|
|
|
|
inputField *termboxUtil.InputField
|
|
|
|
|
|
|
|
currentList string
|
|
|
|
currentFilter string
|
2019-02-22 23:29:23 +00:00
|
|
|
activeList *todotxt.TaskList
|
|
|
|
displayList *todotxt.TaskList
|
2019-02-15 17:15:26 +00:00
|
|
|
|
|
|
|
undoQueue []string
|
|
|
|
redoQueue []string
|
|
|
|
backspaceDoes int
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
MainBundleListKey = "mainscreen.list"
|
|
|
|
MainBundleFilterKey = "mainscreen.filter"
|
|
|
|
|
|
|
|
MainBundleListTodo = "mainscreen.list.todo"
|
|
|
|
MainBundleListDone = "mainscreen.list.done"
|
|
|
|
|
2019-03-14 15:29:33 +00:00
|
|
|
MainBundleListCurrent = "mainscreen.list.current"
|
|
|
|
|
2019-02-15 17:15:26 +00:00
|
|
|
MainBackspaceNothing = iota
|
|
|
|
MainBackspaceMain
|
|
|
|
MainBackspaceFilter
|
2019-02-21 16:47:08 +00:00
|
|
|
|
|
|
|
InputIDFilter = "filter"
|
|
|
|
InputIDAddTask = "add task"
|
|
|
|
InputIDIncompleteArchive = "archive incomplete task? (y/n)"
|
|
|
|
InputIDUnArchiveTask = "move task to active list? (y/n)"
|
2019-02-15 17:15:26 +00:00
|
|
|
)
|
|
|
|
|
2019-03-14 15:29:33 +00:00
|
|
|
func (screen *MainScreen) Id() int { return ScreenIdMain }
|
2019-02-28 13:44:03 +00:00
|
|
|
|
|
|
|
func (screen *MainScreen) Initialize(bundle termboxScreen.Bundle) error {
|
2019-02-15 17:15:26 +00:00
|
|
|
width, height := termbox.Size()
|
2019-03-15 12:29:57 +00:00
|
|
|
screen.inputField = termboxUtil.CreateInputField(0, (height - 2), width, 1, DefaultFg, DefaultBg)
|
2019-02-21 16:47:08 +00:00
|
|
|
|
|
|
|
screen.cursor = make(map[string]int)
|
|
|
|
if bundle != nil {
|
2019-03-14 15:29:33 +00:00
|
|
|
if bundle.GetString(MainBundleListKey, MainBundleListCurrent) == MainBundleListCurrent {
|
|
|
|
bundle.SetValue(MainBundleListKey, screen.currentList)
|
|
|
|
bundle.SetValue(MainBundleFilterKey, screen.currentFilter)
|
|
|
|
}
|
2019-02-21 16:47:08 +00:00
|
|
|
if err := screen.reloadList(bundle); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
screen.inputField.SetID("")
|
|
|
|
screen.inputField.SetBordered(false)
|
|
|
|
}
|
2019-02-15 17:15:26 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-02-28 13:44:03 +00:00
|
|
|
func (screen *MainScreen) ResizeScreen() { screen.Initialize(nil) }
|
2019-02-15 17:15:26 +00:00
|
|
|
|
2019-02-28 13:44:03 +00:00
|
|
|
func (screen *MainScreen) refreshList(bundle termboxScreen.Bundle) error {
|
|
|
|
whichList := bundle.GetString(MainBundleListKey, MainBundleListTodo)
|
2019-02-26 19:22:37 +00:00
|
|
|
switch whichList {
|
|
|
|
case MainBundleListTodo:
|
|
|
|
return app.LoadTaskList()
|
|
|
|
case MainBundleListDone:
|
|
|
|
return app.LoadDoneList()
|
|
|
|
}
|
|
|
|
return errors.New("Invalid refresh request.")
|
|
|
|
}
|
|
|
|
|
2019-02-28 13:44:03 +00:00
|
|
|
func (screen *MainScreen) reloadList(bundle termboxScreen.Bundle) error {
|
2019-02-22 23:29:23 +00:00
|
|
|
// We add tasks to the display list using append because we want to persist task Ids
|
|
|
|
screen.displayList = todotxt.NewTaskList()
|
2019-02-28 13:44:03 +00:00
|
|
|
screen.currentList = bundle.GetString(MainBundleListKey, MainBundleListTodo)
|
2019-02-15 17:15:26 +00:00
|
|
|
switch screen.currentList {
|
|
|
|
case MainBundleListTodo:
|
2019-02-22 23:29:23 +00:00
|
|
|
screen.setActiveList(app.TaskList)
|
2019-02-28 13:44:03 +00:00
|
|
|
if screen.currentFilter = bundle.GetString(MainBundleFilterKey, ""); screen.currentFilter != "" {
|
2019-02-22 23:29:23 +00:00
|
|
|
filteredList := app.filterList(screen.activeList, screen.currentFilter)
|
|
|
|
for _, av := range *screen.activeList {
|
2019-02-15 17:15:26 +00:00
|
|
|
for _, fv := range *filteredList {
|
|
|
|
if av.String() == fv.String() {
|
2019-02-22 23:29:23 +00:00
|
|
|
(*screen.displayList) = append(*screen.displayList, av)
|
2019-02-15 17:15:26 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-22 23:29:23 +00:00
|
|
|
for _, av := range *screen.activeList {
|
|
|
|
(*screen.displayList) = append(*screen.displayList, av)
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-23 13:16:28 +00:00
|
|
|
screen.displayList.Sort(todotxt.SORT_CREATED_DATE_DESC)
|
|
|
|
screen.displayList.Sort(todotxt.SORT_PRIORITY_ASC)
|
2019-02-15 17:15:26 +00:00
|
|
|
case MainBundleListDone:
|
|
|
|
if err := app.LoadDoneList(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-02-22 23:29:23 +00:00
|
|
|
screen.setActiveList(app.DoneList)
|
2019-02-28 13:44:03 +00:00
|
|
|
if screen.currentFilter = bundle.GetString(MainBundleFilterKey, ""); screen.currentFilter != "" {
|
2019-02-22 23:29:23 +00:00
|
|
|
filteredList := app.filterList(screen.activeList, screen.currentFilter)
|
|
|
|
for _, av := range *screen.activeList {
|
2019-02-15 17:15:26 +00:00
|
|
|
for _, fv := range *filteredList {
|
|
|
|
if av.String() == fv.String() {
|
2019-02-22 23:29:23 +00:00
|
|
|
(*screen.displayList) = append(*screen.displayList, av)
|
2019-02-15 17:15:26 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-02-22 23:29:23 +00:00
|
|
|
for _, av := range *screen.activeList {
|
|
|
|
(*screen.displayList) = append(*screen.displayList, av)
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 23:29:23 +00:00
|
|
|
if screen.cursor[screen.currentList] > len(*screen.displayList)-1 {
|
|
|
|
screen.cursor[screen.currentList] = len(*screen.displayList) - 1
|
2019-02-21 16:47:08 +00:00
|
|
|
}
|
2019-02-15 17:15:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-28 13:44:03 +00:00
|
|
|
func (screen *MainScreen) HandleKeyEvent(event termbox.Event) int {
|
2019-02-15 17:15:26 +00:00
|
|
|
if screen.inputField.GetID() != "" {
|
|
|
|
return screen.handleInputKeyEvent(event)
|
|
|
|
}
|
|
|
|
if event.Ch == '?' {
|
|
|
|
// Go to About Screen
|
2019-02-28 13:44:03 +00:00
|
|
|
b := termboxScreen.Bundle{}
|
2019-03-14 15:29:33 +00:00
|
|
|
if err := app.uiManager.InitializeScreen(ScreenIdAbout, b); err != nil {
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.setErrorMessage(err.Error())
|
2019-02-21 16:47:08 +00:00
|
|
|
}
|
2019-03-14 15:29:33 +00:00
|
|
|
return ScreenIdAbout
|
2019-02-15 17:15:26 +00:00
|
|
|
} else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
|
|
|
|
|
|
|
|
if screen.backspaceDoes == MainBackspaceNothing {
|
|
|
|
if screen.currentList == MainBundleListDone {
|
|
|
|
screen.reloadList(screen.buildBundle(MainBundleListTodo, screen.currentFilter))
|
|
|
|
} else if screen.currentFilter != "" {
|
|
|
|
screen.reloadList(screen.buildBundle(screen.currentList, ""))
|
|
|
|
}
|
|
|
|
} else if screen.backspaceDoes == MainBackspaceMain {
|
|
|
|
screen.reloadList(screen.buildBundle(MainBundleListTodo, screen.currentFilter))
|
|
|
|
} else if screen.backspaceDoes == MainBackspaceFilter {
|
|
|
|
screen.reloadList(screen.buildBundle(screen.currentList, ""))
|
|
|
|
}
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
|
|
|
|
} else if event.Key == termbox.KeySpace {
|
|
|
|
return screen.toggleTaskComplete()
|
|
|
|
|
2019-02-21 16:47:08 +00:00
|
|
|
} else if event.Ch == 'g' {
|
|
|
|
screen.cursor[screen.currentList] = 0
|
|
|
|
|
|
|
|
} else if event.Ch == 'G' {
|
2019-02-22 23:29:23 +00:00
|
|
|
screen.cursor[screen.currentList] = len(*screen.displayList) - 1
|
|
|
|
|
|
|
|
} else if event.Key == termbox.KeyCtrlR {
|
2019-02-26 19:22:37 +00:00
|
|
|
b := screen.buildBundle(screen.currentList, screen.currentFilter)
|
|
|
|
screen.refreshList(b)
|
|
|
|
screen.reloadList(b)
|
2019-02-21 16:47:08 +00:00
|
|
|
|
|
|
|
} else if event.Key == termbox.KeyCtrlF {
|
|
|
|
// Jump forward half a screen
|
|
|
|
_, h := termbox.Size()
|
|
|
|
screen.cursor[screen.currentList] += (h / 2)
|
2019-02-22 23:29:23 +00:00
|
|
|
if screen.cursor[screen.currentList] >= len(*screen.displayList) {
|
|
|
|
screen.cursor[screen.currentList] = len(*screen.displayList) - 1
|
2019-02-21 16:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if event.Key == termbox.KeyCtrlB {
|
|
|
|
// Jump back half a screen
|
|
|
|
_, h := termbox.Size()
|
|
|
|
screen.cursor[screen.currentList] -= (h / 2)
|
|
|
|
if screen.cursor[screen.currentList] < 0 {
|
|
|
|
screen.cursor[screen.currentList] = 0
|
|
|
|
}
|
|
|
|
|
2019-02-15 17:15:26 +00:00
|
|
|
} else if event.Ch == 'L' {
|
|
|
|
return screen.toggleViewList()
|
|
|
|
|
|
|
|
} else if event.Ch == 'a' {
|
|
|
|
return screen.startAddNewTask()
|
|
|
|
|
2019-02-21 16:47:08 +00:00
|
|
|
} else if event.Ch == 'l' || event.Key == termbox.KeyEnter || event.Key == termbox.KeyArrowRight {
|
2019-02-15 17:15:26 +00:00
|
|
|
return screen.startEditTaskScreen()
|
|
|
|
|
|
|
|
} else if event.Ch == 'j' || event.Key == termbox.KeyArrowDown {
|
|
|
|
screen.moveCursorDown()
|
|
|
|
|
|
|
|
} else if event.Ch == 'k' || event.Key == termbox.KeyArrowUp {
|
|
|
|
screen.moveCursorUp()
|
|
|
|
|
2019-02-25 13:44:42 +00:00
|
|
|
} else if event.Ch == 'G' {
|
|
|
|
screen.cursor[screen.currentList] = len(*screen.displayList) - 1
|
|
|
|
|
|
|
|
} else if event.Ch == 'g' {
|
|
|
|
screen.cursor[screen.currentList] = 0
|
|
|
|
|
2019-02-15 17:15:26 +00:00
|
|
|
} else if event.Ch == '/' {
|
|
|
|
screen.startFilter()
|
|
|
|
|
|
|
|
} else if event.Ch == 'D' {
|
2019-02-21 16:47:08 +00:00
|
|
|
screen.confirmArchiveItem()
|
2019-02-15 17:15:26 +00:00
|
|
|
|
|
|
|
} else if event.Ch == 'q' {
|
2019-03-14 15:29:33 +00:00
|
|
|
return ScreenIdExit
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
2019-03-14 15:29:33 +00:00
|
|
|
func (screen *MainScreen) HandleNoneEvent(event termbox.Event) int { return screen.Id() }
|
2019-02-15 17:15:26 +00:00
|
|
|
|
|
|
|
func (screen *MainScreen) handleInputKeyEvent(event termbox.Event) int {
|
2019-02-21 16:47:08 +00:00
|
|
|
switch screen.inputField.GetID() {
|
|
|
|
case InputIDFilter:
|
2019-02-15 17:15:26 +00:00
|
|
|
if event.Key == termbox.KeyEnter {
|
|
|
|
// Apply the filter
|
2019-02-20 13:49:41 +00:00
|
|
|
filter := screen.inputField.GetValue()
|
2019-02-15 17:15:26 +00:00
|
|
|
screen.inputField.SetID("")
|
|
|
|
screen.inputField.SetValue("")
|
2019-02-20 13:49:41 +00:00
|
|
|
screen.backspaceDoes = MainBackspaceFilter
|
|
|
|
screen.reloadList(screen.buildBundle(screen.currentList, filter))
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
2019-02-21 16:47:08 +00:00
|
|
|
case InputIDAddTask:
|
2019-02-15 17:15:26 +00:00
|
|
|
if event.Key == termbox.KeyEnter {
|
|
|
|
// Create the new item
|
|
|
|
err := app.addTask(screen.inputField.GetValue())
|
|
|
|
if err != nil {
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.setErrorMessage(err.Error())
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
2019-02-20 13:49:41 +00:00
|
|
|
screen.inputField.SetID("")
|
|
|
|
screen.inputField.SetValue("")
|
2019-02-15 17:15:26 +00:00
|
|
|
screen.reloadList(screen.buildBundle(screen.currentList, screen.currentFilter))
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
2019-02-21 16:47:08 +00:00
|
|
|
case InputIDIncompleteArchive:
|
|
|
|
if event.Ch == 'y' || event.Ch == 'Y' {
|
|
|
|
return screen.archiveCurrentItem()
|
|
|
|
}
|
|
|
|
screen.inputField.SetID("")
|
|
|
|
screen.inputField.SetValue("")
|
|
|
|
screen.reloadList(screen.buildBundle(screen.currentList, screen.currentFilter))
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-21 16:47:08 +00:00
|
|
|
case InputIDUnArchiveTask:
|
|
|
|
if event.Ch == 'y' || event.Ch == 'Y' {
|
|
|
|
screen.inputField.SetID("")
|
|
|
|
screen.inputField.SetValue("")
|
|
|
|
screen.reloadList(screen.buildBundle(screen.currentList, screen.currentFilter))
|
|
|
|
return screen.unarchiveCurrentItem()
|
|
|
|
}
|
|
|
|
screen.inputField.SetID("")
|
|
|
|
screen.inputField.SetValue("")
|
|
|
|
screen.reloadList(screen.buildBundle(screen.currentList, screen.currentFilter))
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
|
|
|
|
if screen.inputField.GetValue() == "" {
|
|
|
|
screen.reloadList(screen.buildBundle(screen.currentList, screen.inputField.GetValue()))
|
|
|
|
screen.inputField.SetID("")
|
|
|
|
screen.inputField.SetValue("")
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
2019-02-21 16:47:08 +00:00
|
|
|
} else if event.Key == termbox.KeyEsc {
|
|
|
|
screen.reloadList(screen.buildBundle(screen.currentList, screen.currentFilter))
|
|
|
|
screen.inputField.SetID("")
|
|
|
|
screen.inputField.SetValue("")
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
screen.inputField.HandleEvent(event)
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 23:29:23 +00:00
|
|
|
func (screen *MainScreen) setActiveList(list *todotxt.TaskList) {
|
|
|
|
screen.activeList = list
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 13:44:03 +00:00
|
|
|
func (screen *MainScreen) DrawScreen() {
|
2019-02-21 16:47:08 +00:00
|
|
|
_, height := termbox.Size()
|
|
|
|
screen.viewPort.numberOfRows = height - 1
|
|
|
|
if screen.inputField.GetID() != "" {
|
|
|
|
screen.viewPort.numberOfRows--
|
|
|
|
}
|
|
|
|
screen.viewPort.firstRow = 1
|
|
|
|
displayOffset := 0
|
|
|
|
maxCursor := screen.viewPort.numberOfRows * 2 / 3
|
|
|
|
if screen.cursor[screen.currentList] > maxCursor {
|
|
|
|
displayOffset = screen.cursor[screen.currentList] - maxCursor
|
|
|
|
}
|
|
|
|
|
2019-02-15 17:15:26 +00:00
|
|
|
if screen.message == "" {
|
|
|
|
screen.setMessageWithTimeout("Press '?' for help", -1)
|
|
|
|
}
|
|
|
|
screen.drawHeader()
|
2019-02-20 13:49:41 +00:00
|
|
|
topId := 0
|
2019-02-22 23:29:23 +00:00
|
|
|
for _, v := range *screen.displayList {
|
2019-02-20 13:49:41 +00:00
|
|
|
if v.Id > topId {
|
|
|
|
topId = v.Id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
padCnt := fmt.Sprintf("%d", topId)
|
2019-02-22 23:29:23 +00:00
|
|
|
for k, v := range *screen.displayList {
|
2019-02-20 13:49:41 +00:00
|
|
|
pad := strings.Repeat(" ", len(padCnt)-len(fmt.Sprintf("%d", v.Id)))
|
2019-02-15 17:15:26 +00:00
|
|
|
useFg, useBg := DefaultFg, DefaultBg
|
2019-02-21 16:47:08 +00:00
|
|
|
if k == screen.cursor[screen.currentList] {
|
2019-02-15 17:15:26 +00:00
|
|
|
useFg, useBg = CursorFg, CursorBg
|
|
|
|
}
|
2019-02-21 16:47:08 +00:00
|
|
|
lineY := k + 1 - displayOffset
|
|
|
|
if lineY > 0 && lineY < screen.viewPort.numberOfRows {
|
|
|
|
termboxUtil.DrawStringAtPoint(pad+app.getTaskString(v), 0, lineY, useFg, useBg)
|
|
|
|
}
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
screen.drawFooter()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *MainScreen) drawHeader() {
|
|
|
|
width, _ := termbox.Size()
|
|
|
|
headerString := screen.currentFilter
|
|
|
|
if headerString == "" {
|
|
|
|
if screen.currentList == MainBundleListTodo {
|
|
|
|
headerString = "Todo List"
|
|
|
|
} else if screen.currentList == MainBundleListDone {
|
|
|
|
headerString = "Done List"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spaces := strings.Repeat(" ", ((width-len(headerString))/2)+1)
|
|
|
|
termboxUtil.DrawStringAtPoint(fmt.Sprintf("%s%s%s", spaces, headerString, spaces), 0, 0, TitleFg, TitleBg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *MainScreen) drawFooter() {
|
|
|
|
if screen.messageTimeout > 0 && time.Since(screen.messageTime) > screen.messageTimeout {
|
|
|
|
screen.clearMessage()
|
|
|
|
}
|
2019-02-21 16:47:08 +00:00
|
|
|
width, height := termbox.Size()
|
2019-02-15 17:15:26 +00:00
|
|
|
if screen.inputField.GetID() != "" {
|
2019-02-21 16:47:08 +00:00
|
|
|
screen.inputField.SetX(len(screen.inputField.GetID()) + 2)
|
|
|
|
pad := width - len(screen.inputField.GetID()+":")
|
|
|
|
field := screen.inputField.GetID() + ":" + strings.Repeat(" ", pad)
|
|
|
|
termboxUtil.DrawStringAtPoint(field, 0, height-2, DefaultFg, DefaultBg)
|
2019-02-15 17:15:26 +00:00
|
|
|
screen.inputField.Draw()
|
|
|
|
}
|
|
|
|
// And the 'message'
|
2019-02-26 19:22:37 +00:00
|
|
|
termboxUtil.DrawStringAtPoint(screen.message, 0, height-1, screen.messageColorFg, screen.messageColorBg)
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 16:47:08 +00:00
|
|
|
func (screen *MainScreen) confirmArchiveItem() int {
|
|
|
|
if screen.currentList != MainBundleListTodo {
|
|
|
|
screen.inputField.SetID(InputIDUnArchiveTask)
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-21 16:47:08 +00:00
|
|
|
}
|
|
|
|
// Find the task under the cursor
|
2019-02-22 23:29:23 +00:00
|
|
|
if screen.cursor[screen.currentList] < len(*screen.displayList) {
|
|
|
|
t := (*screen.displayList)[screen.cursor[screen.currentList]]
|
2019-02-21 16:47:08 +00:00
|
|
|
if !t.Completed {
|
|
|
|
// Task isn't completed, verify that the user wants to archive it
|
|
|
|
screen.inputField.SetID(InputIDIncompleteArchive)
|
|
|
|
} else {
|
|
|
|
return screen.archiveCurrentItem()
|
|
|
|
}
|
|
|
|
}
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-21 16:47:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 17:15:26 +00:00
|
|
|
func (screen *MainScreen) archiveCurrentItem() int {
|
2019-02-20 13:49:41 +00:00
|
|
|
if screen.currentList != MainBundleListTodo {
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.setErrorMessage("Task is already archived")
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-20 13:49:41 +00:00
|
|
|
}
|
|
|
|
// Find the task under the cursor
|
2019-02-22 23:29:23 +00:00
|
|
|
if len(*screen.displayList) > screen.cursor[screen.currentList] {
|
|
|
|
t := (*screen.displayList)[screen.cursor[screen.currentList]]
|
2019-02-20 13:49:41 +00:00
|
|
|
if err := app.archiveTask(t.Id); err != nil {
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.setErrorMessage(err.Error())
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-20 13:49:41 +00:00
|
|
|
}
|
2019-02-21 16:47:08 +00:00
|
|
|
// Reload the list
|
|
|
|
b := screen.buildBundle(screen.currentList, screen.currentFilter)
|
|
|
|
if err := screen.reloadList(b); err != nil {
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.setErrorMessage(err.Error())
|
2019-02-21 16:47:08 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-21 16:47:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *MainScreen) unarchiveCurrentItem() int {
|
|
|
|
if screen.currentList == MainBundleListTodo {
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.setErrorMessage("Task is not archived")
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-21 16:47:08 +00:00
|
|
|
}
|
|
|
|
// Find the task under the cursor
|
2019-02-22 23:29:23 +00:00
|
|
|
if len(*screen.displayList) > screen.cursor[screen.currentList] {
|
|
|
|
t := (*screen.displayList)[screen.cursor[screen.currentList]]
|
2019-02-21 16:47:08 +00:00
|
|
|
if err := app.unarchiveTask(t.Id); err != nil {
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.setErrorMessage(err.Error())
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
2019-02-20 13:49:41 +00:00
|
|
|
// Reload the list
|
|
|
|
b := screen.buildBundle(screen.currentList, screen.currentFilter)
|
|
|
|
if err := screen.reloadList(b); err != nil {
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.setErrorMessage(err.Error())
|
2019-02-20 13:49:41 +00:00
|
|
|
}
|
|
|
|
}
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *MainScreen) startEditTaskScreen() int {
|
|
|
|
// Find the task under the cursor
|
2019-02-22 23:29:23 +00:00
|
|
|
if len(*screen.displayList) > screen.cursor[screen.currentList] {
|
|
|
|
t := (*screen.displayList)[screen.cursor[screen.currentList]]
|
2019-02-15 17:15:26 +00:00
|
|
|
// Load the task screen with this task
|
2019-02-28 13:44:03 +00:00
|
|
|
b := termboxScreen.Bundle{}
|
|
|
|
b.SetValue(TaskBundleTaskIdKey, t.Id)
|
2019-03-14 15:29:33 +00:00
|
|
|
if err := app.uiManager.InitializeScreen(ScreenIdTask, b); err != nil {
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.setErrorMessage(err.Error())
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
2019-03-14 15:29:33 +00:00
|
|
|
return ScreenIdTask
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *MainScreen) reloadCurrentView() {
|
2019-02-28 13:44:03 +00:00
|
|
|
bundle := termboxScreen.Bundle{}
|
|
|
|
bundle.SetValue(MainBundleListKey, screen.currentList)
|
|
|
|
bundle.SetValue(MainBundleFilterKey, screen.currentFilter)
|
2019-02-21 16:47:08 +00:00
|
|
|
screen.reloadList(bundle)
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *MainScreen) toggleViewList() int {
|
2019-02-28 13:44:03 +00:00
|
|
|
bundle := termboxScreen.Bundle{}
|
2019-02-15 17:15:26 +00:00
|
|
|
if screen.currentList == MainBundleListTodo {
|
2019-02-28 13:44:03 +00:00
|
|
|
bundle.SetValue(MainBundleListKey, MainBundleListDone)
|
2019-02-15 17:15:26 +00:00
|
|
|
screen.backspaceDoes = MainBackspaceMain
|
|
|
|
} else {
|
2019-02-28 13:44:03 +00:00
|
|
|
bundle.SetValue(MainBundleListKey, MainBundleListTodo)
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
2019-02-28 13:44:03 +00:00
|
|
|
bundle.SetValue(MainBundleFilterKey, screen.currentFilter)
|
2019-02-21 16:47:08 +00:00
|
|
|
screen.reloadList(bundle)
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *MainScreen) startAddNewTask() int {
|
2019-02-21 16:47:08 +00:00
|
|
|
screen.inputField.SetID(InputIDAddTask)
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *MainScreen) toggleTaskComplete() int {
|
2019-02-21 16:47:08 +00:00
|
|
|
if screen.currentList == MainBundleListDone {
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.setErrorMessage("Task is archived, unable to modify.")
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-21 16:47:08 +00:00
|
|
|
}
|
|
|
|
|
2019-02-15 17:15:26 +00:00
|
|
|
// Find the task under the cursor
|
2019-02-22 23:29:23 +00:00
|
|
|
if len(*screen.displayList) > screen.cursor[screen.currentList] {
|
|
|
|
t := (*screen.displayList)[screen.cursor[screen.currentList]]
|
2019-02-15 17:15:26 +00:00
|
|
|
err := app.toggleTaskComplete(t.Id)
|
|
|
|
if err != nil {
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.setErrorMessage(err.Error())
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
screen.reloadCurrentView()
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *MainScreen) moveCursorDown() bool {
|
2019-02-21 16:47:08 +00:00
|
|
|
screen.cursor[screen.currentList]++
|
2019-02-22 23:29:23 +00:00
|
|
|
if screen.cursor[screen.currentList] >= len(*screen.displayList) {
|
|
|
|
screen.cursor[screen.currentList] = len(*screen.displayList) - 1
|
2019-02-15 17:15:26 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *MainScreen) moveCursorUp() bool {
|
2019-02-21 16:47:08 +00:00
|
|
|
screen.cursor[screen.currentList]--
|
|
|
|
if screen.cursor[screen.currentList] < 0 {
|
|
|
|
screen.cursor[screen.currentList] = 0
|
2019-02-15 17:15:26 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-02-21 16:47:08 +00:00
|
|
|
func (screen *MainScreen) startFilter() int {
|
|
|
|
screen.inputField.SetID(InputIDFilter)
|
2019-03-14 15:29:33 +00:00
|
|
|
return screen.Id()
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-26 19:22:37 +00:00
|
|
|
func (screen *MainScreen) setErrorMessage(msg string) {
|
|
|
|
screen.message = " " + msg + " "
|
|
|
|
screen.messageTime = time.Now()
|
|
|
|
screen.messageTimeout = time.Second * 2
|
|
|
|
screen.messageColorBg = termbox.ColorRed
|
|
|
|
screen.messageColorFg = termbox.ColorWhite | termbox.AttrBold
|
|
|
|
}
|
|
|
|
|
2019-02-15 17:15:26 +00:00
|
|
|
func (screen *MainScreen) setMessage(msg string) {
|
|
|
|
screen.message = msg
|
|
|
|
screen.messageTime = time.Now()
|
|
|
|
screen.messageTimeout = time.Second * 2
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.messageColorBg = DefaultBg
|
|
|
|
screen.messageColorFg = DefaultFg
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* setMessageWithTimeout lets you specify the timeout for the message
|
|
|
|
* setting it to -1 means it won't timeout
|
|
|
|
*/
|
|
|
|
func (screen *MainScreen) setMessageWithTimeout(msg string, timeout time.Duration) {
|
|
|
|
screen.message = msg
|
|
|
|
screen.messageTime = time.Now()
|
|
|
|
screen.messageTimeout = timeout
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *MainScreen) clearMessage() {
|
2019-02-22 23:29:23 +00:00
|
|
|
screen.message = fmt.Sprintf("%d Total Tasks", len(*screen.activeList))
|
2019-02-15 17:15:26 +00:00
|
|
|
screen.messageTimeout = -1
|
2019-02-26 19:22:37 +00:00
|
|
|
screen.messageColorBg = DefaultBg
|
|
|
|
screen.messageColorFg = DefaultFg
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 13:44:03 +00:00
|
|
|
func (screen *MainScreen) buildBundle(list, filter string) termboxScreen.Bundle {
|
|
|
|
bundle := termboxScreen.Bundle{}
|
|
|
|
bundle.SetValue(MainBundleListKey, list)
|
|
|
|
bundle.SetValue(MainBundleFilterKey, filter)
|
2019-02-15 17:15:26 +00:00
|
|
|
return bundle
|
|
|
|
}
|