Persist Task Ids when Archiving

This commit is contained in:
Brian Buller 2019-02-22 17:29:23 -06:00
parent ff8a0ec0b7
commit 7c0edd1871
3 changed files with 44 additions and 59 deletions

View File

@ -39,15 +39,14 @@ func (screen *AboutScreen) initialize(bundle Bundle) error {
Command{"j,↓", "down"}, Command{"j,↓", "down"},
Command{"k,↑", "up"}, Command{"k,↑", "up"},
Command{"l,→", "open task"}, Command{"l,→", "open task"},
Command{"[space]", "toggle task complete"},
Command{"g", "goto top"}, Command{"g", "goto top"},
Command{"G", "goto bottom"}, Command{"G", "goto bottom"},
Command{"ctrl+f", "jump down"}, Command{"ctrl+f", "jump down"},
Command{"ctrl+b", "jump up"}, Command{"ctrl+b", "jump up"},
} }
screen.commandsCol2 = []Command{ screen.commandsCol2 = []Command{
Command{"r", "rename pair/bucket"},
Command{"D", "move task to done.txt"}, Command{"D", "move task to done.txt"},
Command{"x,X", "export as string/json to file"},
Command{"?", "this screen"}, Command{"?", "this screen"},
Command{"q", "quit program"}, Command{"q", "quit program"},
} }
@ -79,7 +78,7 @@ func (screen *AboutScreen) drawScreen() {
} }
screen.drawCommandsAtPoint(screen.commandsCol1, col1XPos, yPos) screen.drawCommandsAtPoint(screen.commandsCol1, col1XPos, yPos)
screen.drawCommandsAtPoint(screen.commandsCol2, col2XPos, yPos) screen.drawCommandsAtPoint(screen.commandsCol2, col2XPos, yPos)
exitTxt := "Press any key to return to tasks " + fmt.Sprintf("%d", width) exitTxt := "Press any key to return to tasks"
termboxUtil.DrawStringAtPoint(exitTxt, (width-len(exitTxt))/2, height-1, TitleFg, TitleBg) termboxUtil.DrawStringAtPoint(exitTxt, (width-len(exitTxt))/2, height-1, TitleFg, TitleBg)
} }

View File

@ -23,8 +23,8 @@ type MainScreen struct {
currentList string currentList string
currentFilter string currentFilter string
activeList todotxt.TaskList activeList *todotxt.TaskList
displayList []todotxt.Task displayList *todotxt.TaskList
undoQueue []string undoQueue []string
redoQueue []string redoQueue []string
@ -38,9 +38,6 @@ const (
MainBundleListTodo = "mainscreen.list.todo" MainBundleListTodo = "mainscreen.list.todo"
MainBundleListDone = "mainscreen.list.done" MainBundleListDone = "mainscreen.list.done"
MainModalIdFilter = "mainscreen.filter"
MainModalIdAddTask = "mainscreen.addtask"
MainBackspaceNothing = iota MainBackspaceNothing = iota
MainBackspaceMain MainBackspaceMain
MainBackspaceFilter MainBackspaceFilter
@ -68,49 +65,50 @@ func (screen *MainScreen) initialize(bundle Bundle) error {
} }
func (screen *MainScreen) reloadList(bundle Bundle) error { func (screen *MainScreen) reloadList(bundle Bundle) error {
screen.displayList = []todotxt.Task{} // We add tasks to the display list using append because we want to persist task Ids
screen.displayList = todotxt.NewTaskList()
screen.currentList = bundle.getString(MainBundleListKey, MainBundleListTodo) screen.currentList = bundle.getString(MainBundleListKey, MainBundleListTodo)
switch screen.currentList { switch screen.currentList {
case MainBundleListTodo: case MainBundleListTodo:
screen.setActiveList(*app.TaskList) screen.setActiveList(app.TaskList)
if screen.currentFilter = bundle.getString(MainBundleFilterKey, ""); screen.currentFilter != "" { if screen.currentFilter = bundle.getString(MainBundleFilterKey, ""); screen.currentFilter != "" {
filteredList := app.filterList(&screen.activeList, screen.currentFilter) filteredList := app.filterList(screen.activeList, screen.currentFilter)
for _, av := range screen.activeList { for _, av := range *screen.activeList {
for _, fv := range *filteredList { for _, fv := range *filteredList {
if av.String() == fv.String() { if av.String() == fv.String() {
screen.displayList = append(screen.displayList, av) (*screen.displayList) = append(*screen.displayList, av)
break break
} }
} }
} }
} else { } else {
for _, av := range screen.activeList { for _, av := range *screen.activeList {
screen.displayList = append(screen.displayList, av) (*screen.displayList) = append(*screen.displayList, av)
} }
} }
case MainBundleListDone: case MainBundleListDone:
if err := app.LoadDoneList(); err != nil { if err := app.LoadDoneList(); err != nil {
return err return err
} }
screen.setActiveList(*app.DoneList) screen.setActiveList(app.DoneList)
if screen.currentFilter = bundle.getString(MainBundleFilterKey, ""); screen.currentFilter != "" { if screen.currentFilter = bundle.getString(MainBundleFilterKey, ""); screen.currentFilter != "" {
filteredList := app.filterList(&screen.activeList, screen.currentFilter) filteredList := app.filterList(screen.activeList, screen.currentFilter)
for _, av := range screen.activeList { for _, av := range *screen.activeList {
for _, fv := range *filteredList { for _, fv := range *filteredList {
if av.String() == fv.String() { if av.String() == fv.String() {
screen.displayList = append(screen.displayList, av) (*screen.displayList) = append(*screen.displayList, av)
break break
} }
} }
} }
} else { } else {
for _, av := range screen.activeList { for _, av := range *screen.activeList {
screen.displayList = append(screen.displayList, av) (*screen.displayList) = append(*screen.displayList, av)
} }
} }
} }
if screen.cursor[screen.currentList] > len(screen.displayList)-1 { if screen.cursor[screen.currentList] > len(*screen.displayList)-1 {
screen.cursor[screen.currentList] = len(screen.displayList) - 1 screen.cursor[screen.currentList] = len(*screen.displayList) - 1
} }
return nil return nil
} }
@ -148,14 +146,17 @@ func (screen *MainScreen) handleKeyEvent(event termbox.Event) int {
screen.cursor[screen.currentList] = 0 screen.cursor[screen.currentList] = 0
} else if event.Ch == 'G' { } else if event.Ch == 'G' {
screen.cursor[screen.currentList] = len(screen.displayList) - 1 screen.cursor[screen.currentList] = len(*screen.displayList) - 1
} else if event.Key == termbox.KeyCtrlR {
screen.reloadList(screen.buildBundle(screen.currentList, screen.currentFilter))
} else if event.Key == termbox.KeyCtrlF { } else if event.Key == termbox.KeyCtrlF {
// Jump forward half a screen // Jump forward half a screen
_, h := termbox.Size() _, h := termbox.Size()
screen.cursor[screen.currentList] += (h / 2) screen.cursor[screen.currentList] += (h / 2)
if screen.cursor[screen.currentList] >= len(screen.displayList) { if screen.cursor[screen.currentList] >= len(*screen.displayList) {
screen.cursor[screen.currentList] = len(screen.displayList) - 1 screen.cursor[screen.currentList] = len(*screen.displayList) - 1
} }
} else if event.Key == termbox.KeyCtrlB { } else if event.Key == termbox.KeyCtrlB {
@ -254,11 +255,8 @@ func (screen *MainScreen) handleInputKeyEvent(event termbox.Event) int {
return ScreenMain return ScreenMain
} }
func (screen *MainScreen) setActiveList(list todotxt.TaskList) { func (screen *MainScreen) setActiveList(list *todotxt.TaskList) {
screen.activeList = todotxt.NewTaskList() screen.activeList = list
for _, v := range list {
screen.activeList.AddTask(&v)
}
} }
func (screen *MainScreen) drawScreen() { func (screen *MainScreen) drawScreen() {
@ -279,13 +277,13 @@ func (screen *MainScreen) drawScreen() {
} }
screen.drawHeader() screen.drawHeader()
topId := 0 topId := 0
for _, v := range screen.displayList { for _, v := range *screen.displayList {
if v.Id > topId { if v.Id > topId {
topId = v.Id topId = v.Id
} }
} }
padCnt := fmt.Sprintf("%d", topId) padCnt := fmt.Sprintf("%d", topId)
for k, v := range screen.displayList { for k, v := range *screen.displayList {
pad := strings.Repeat(" ", len(padCnt)-len(fmt.Sprintf("%d", v.Id))) pad := strings.Repeat(" ", len(padCnt)-len(fmt.Sprintf("%d", v.Id)))
useFg, useBg := DefaultFg, DefaultBg useFg, useBg := DefaultFg, DefaultBg
if k == screen.cursor[screen.currentList] { if k == screen.cursor[screen.currentList] {
@ -335,8 +333,8 @@ func (screen *MainScreen) confirmArchiveItem() int {
return ScreenMain return ScreenMain
} }
// Find the task under the cursor // Find the task under the cursor
if screen.cursor[screen.currentList] < len(screen.displayList) { if screen.cursor[screen.currentList] < len(*screen.displayList) {
t := screen.displayList[screen.cursor[screen.currentList]] t := (*screen.displayList)[screen.cursor[screen.currentList]]
if !t.Completed { if !t.Completed {
// Task isn't completed, verify that the user wants to archive it // Task isn't completed, verify that the user wants to archive it
screen.inputField.SetID(InputIDIncompleteArchive) screen.inputField.SetID(InputIDIncompleteArchive)
@ -353,8 +351,8 @@ func (screen *MainScreen) archiveCurrentItem() int {
return ScreenMain return ScreenMain
} }
// Find the task under the cursor // Find the task under the cursor
if len(screen.displayList) > screen.cursor[screen.currentList] { if len(*screen.displayList) > screen.cursor[screen.currentList] {
t := screen.displayList[screen.cursor[screen.currentList]] t := (*screen.displayList)[screen.cursor[screen.currentList]]
if err := app.archiveTask(t.Id); err != nil { if err := app.archiveTask(t.Id); err != nil {
screen.setMessage(err.Error()) screen.setMessage(err.Error())
return ScreenMain return ScreenMain
@ -374,8 +372,8 @@ func (screen *MainScreen) unarchiveCurrentItem() int {
return ScreenMain return ScreenMain
} }
// Find the task under the cursor // Find the task under the cursor
if len(screen.displayList) > screen.cursor[screen.currentList] { if len(*screen.displayList) > screen.cursor[screen.currentList] {
t := screen.displayList[screen.cursor[screen.currentList]] t := (*screen.displayList)[screen.cursor[screen.currentList]]
if err := app.unarchiveTask(t.Id); err != nil { if err := app.unarchiveTask(t.Id); err != nil {
screen.setMessage(err.Error()) screen.setMessage(err.Error())
return ScreenMain return ScreenMain
@ -391,13 +389,14 @@ func (screen *MainScreen) unarchiveCurrentItem() int {
func (screen *MainScreen) startEditTaskScreen() int { func (screen *MainScreen) startEditTaskScreen() int {
// Find the task under the cursor // Find the task under the cursor
if len(screen.displayList) > screen.cursor[screen.currentList] { if len(*screen.displayList) > screen.cursor[screen.currentList] {
t := screen.displayList[screen.cursor[screen.currentList]] t := (*screen.displayList)[screen.cursor[screen.currentList]]
// Load the task screen with this task // Load the task screen with this task
b := Bundle{} b := Bundle{}
b.setValue(TaskBundleTaskIdKey, t.Id) b.setValue(TaskBundleTaskIdKey, t.Id)
if err := app.screens[ScreenTask].initialize(b); err != nil { if err := app.screens[ScreenTask].initialize(b); err != nil {
screen.setMessage(err.Error()) screen.setMessage(err.Error())
return ScreenMain
} }
return ScreenTask return ScreenTask
} }
@ -436,8 +435,8 @@ func (screen *MainScreen) toggleTaskComplete() int {
} }
// Find the task under the cursor // Find the task under the cursor
if len(screen.displayList) > screen.cursor[screen.currentList] { if len(*screen.displayList) > screen.cursor[screen.currentList] {
t := screen.displayList[screen.cursor[screen.currentList]] t := (*screen.displayList)[screen.cursor[screen.currentList]]
err := app.toggleTaskComplete(t.Id) err := app.toggleTaskComplete(t.Id)
if err != nil { if err != nil {
screen.setMessage(err.Error()) screen.setMessage(err.Error())
@ -450,8 +449,8 @@ func (screen *MainScreen) toggleTaskComplete() int {
func (screen *MainScreen) moveCursorDown() bool { func (screen *MainScreen) moveCursorDown() bool {
screen.cursor[screen.currentList]++ screen.cursor[screen.currentList]++
if screen.cursor[screen.currentList] >= len(screen.displayList) { if screen.cursor[screen.currentList] >= len(*screen.displayList) {
screen.cursor[screen.currentList] = len(screen.displayList) - 1 screen.cursor[screen.currentList] = len(*screen.displayList) - 1
return false return false
} }
return true return true
@ -487,7 +486,7 @@ func (screen *MainScreen) setMessageWithTimeout(msg string, timeout time.Duratio
} }
func (screen *MainScreen) clearMessage() { func (screen *MainScreen) clearMessage() {
screen.message = fmt.Sprintf("%d Total Tasks", len(screen.activeList)) screen.message = fmt.Sprintf("%d Total Tasks", len(*screen.activeList))
screen.messageTimeout = -1 screen.messageTimeout = -1
} }

View File

@ -46,11 +46,6 @@ func (a *AppState) opAddTask(args []string) int {
fmt.Println(err.Error()) fmt.Println(err.Error())
return 1 return 1
} }
if err := a.WriteList(); err != nil {
fmt.Println("Error saving task list")
fmt.Println(err.Error())
return 1
}
return 0 return 0
} }
@ -118,14 +113,6 @@ func (a *AppState) opArchiveTasks(args []string) int {
} }
} }
} }
if a.WriteDoneList() != nil {
fmt.Println("Error saving archive list")
return 1
}
if a.WriteList() != nil {
fmt.Println("Error saving task list")
return 1
}
return 0 return 0
} }