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{"k,↑", "up"},
Command{"l,→", "open task"},
Command{"[space]", "toggle task complete"},
Command{"g", "goto top"},
Command{"G", "goto bottom"},
Command{"ctrl+f", "jump down"},
Command{"ctrl+b", "jump up"},
}
screen.commandsCol2 = []Command{
Command{"r", "rename pair/bucket"},
Command{"D", "move task to done.txt"},
Command{"x,X", "export as string/json to file"},
Command{"?", "this screen"},
Command{"q", "quit program"},
}
@ -79,7 +78,7 @@ func (screen *AboutScreen) drawScreen() {
}
screen.drawCommandsAtPoint(screen.commandsCol1, col1XPos, 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)
}

View File

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

View File

@ -46,11 +46,6 @@ func (a *AppState) opAddTask(args []string) int {
fmt.Println(err.Error())
return 1
}
if err := a.WriteList(); err != nil {
fmt.Println("Error saving task list")
fmt.Println(err.Error())
return 1
}
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
}