Archive/Unarchive

Several other improvements
This commit is contained in:
Brian Buller 2019-02-21 10:47:08 -06:00
parent d8c9e1a26b
commit ff8a0ec0b7
6 changed files with 270 additions and 71 deletions

View File

@ -16,7 +16,7 @@ func (a *AppState) addTask(taskString string) error {
t.CreatedDate = time.Now()
}
a.TaskList.AddTask(t)
return nil
return a.WriteList()
}
func (a *AppState) toggleTaskComplete(id int) error {
@ -30,7 +30,7 @@ func (a *AppState) toggleTaskComplete(id int) error {
} else {
task.Complete()
}
return nil
return a.WriteList()
}
func (a *AppState) archiveTask(id int) error {
@ -39,12 +39,25 @@ func (a *AppState) archiveTask(id int) error {
if task, err = a.TaskList.GetTask(id); err != nil {
return err
}
task.Completed = true
if err := a.TaskList.ArchiveTaskToFile(*task, app.getDoneFile()); err != nil {
return err
}
a.TaskList.RemoveTask(*task)
return nil
return a.WriteList()
}
func (a *AppState) unarchiveTask(id int) error {
var err error
var task *todotxt.Task
if task, err = a.DoneList.GetTask(id); err != nil {
return err
}
a.TaskList.AddTask(task)
if err = a.WriteList(); err != nil {
return err
}
a.DoneList.RemoveTask(*task)
return a.WriteDoneList()
}
func (a *AppState) getFilterPredicate(filter string) func(todotxt.Task) bool {

View File

@ -25,13 +25,9 @@ const (
)
func (a *AppState) BuildScreens() {
mainScreen := MainScreen{
viewPort: &ViewPort{},
}
mainScreen := MainScreen{}
aboutScreen := AboutScreen{}
taskScreen := TaskScreen{
viewPort: &ViewPort{},
}
taskScreen := TaskScreen{}
a.screens = append(a.screens, &mainScreen)
a.screens = append(a.screens, &taskScreen)
a.screens = append(a.screens, &aboutScreen)
@ -47,16 +43,28 @@ func (a *AppState) layoutAndDrawScreen(s Screen) {
termbox.Flush()
}
// ViewPort helps keep track of what's being displayed on the screen
type ViewPort struct {
bytesPerRow int
numberOfRows int
firstRow int
cursor int
}
func readUserInput(e chan termbox.Event) {
for {
e <- termbox.PollEvent()
}
}
func refreshList(e chan termbox.Event) {
/*
for {
time.Sleep(5 * time.Minute)
app.LoadTasklist()
app.LoadDoneList()
e <- termbox.Event{Type: termbox.EventNone}
}
*/
}
/*
* ViewPort helps keep track of what's being displayed on the screen
*/
type ViewPort struct {
bytesPerRow int
numberOfRows int
firstRow int
}

View File

@ -1,6 +1,7 @@
package main
import (
"fmt"
"time"
"github.com/br0xen/termbox-util"
@ -9,13 +10,48 @@ import (
// AboutScreen holds all that's going on
type AboutScreen struct {
viewPort *ViewPort
viewPort ViewPort
message string
messageTimeout time.Duration
messageTime time.Time
titleTemplate []string
commandsCol1 []Command
commandsCol2 []Command
}
type Command struct {
key string
description string
}
func (screen *AboutScreen) initialize(bundle Bundle) error {
screen.titleTemplate = []string{
" __ ",
" _________ _____| | __",
" / ___\\__ \\ / ___/ |/ /",
" / /_/ > __ \\_\\___ \\| < ",
" \\___ (____ /____ >__|_ \\",
"/_____/ \\/ \\/ \\/",
}
screen.commandsCol1 = []Command{
Command{"j,↓", "down"},
Command{"k,↑", "up"},
Command{"l,→", "open task"},
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"},
}
return nil
}
@ -25,6 +61,36 @@ func (screen *AboutScreen) handleKeyEvent(event termbox.Event) int {
func (screen *AboutScreen) drawScreen() {
width, height := termbox.Size()
exitTxt := "Press any key to return to tasks"
xPos := (width - len(screen.titleTemplate[0])) / 2
yPos := 1
for _, line := range screen.titleTemplate {
termboxUtil.DrawStringAtPoint(line, xPos, yPos, DefaultFg, DefaultBg)
yPos++
}
numCols := 2
if width < 80 {
numCols = 1
}
col1XPos := (width - (width * 3 / 4))
col2XPos := (width - (width * 2 / 4))
if numCols == 1 {
col2XPos = col1XPos
}
screen.drawCommandsAtPoint(screen.commandsCol1, col1XPos, yPos)
screen.drawCommandsAtPoint(screen.commandsCol2, col2XPos, yPos)
exitTxt := "Press any key to return to tasks " + fmt.Sprintf("%d", width)
termboxUtil.DrawStringAtPoint(exitTxt, (width-len(exitTxt))/2, height-1, TitleFg, TitleBg)
}
func (screen *AboutScreen) drawCommandsAtPoint(commands []Command, x, y int) {
xPos, yPos := x, y
for index, cmd := range commands {
termboxUtil.DrawStringAtPoint(fmt.Sprintf("%6s", cmd.key), xPos, yPos, DefaultFg, DefaultBg)
termboxUtil.DrawStringAtPoint(cmd.description, xPos+8, yPos, DefaultFg, DefaultBg)
yPos++
if index > 2 && index%2 == 1 {
yPos++
}
}
}

View File

@ -12,11 +12,12 @@ import (
// MainScreen holds all that's going on
type MainScreen struct {
viewPort *ViewPort
viewPort ViewPort
message string
messageTimeout time.Duration
messageTime time.Time
mode int
cursor map[string]int
inputField *termboxUtil.InputField
@ -43,16 +44,25 @@ const (
MainBackspaceNothing = iota
MainBackspaceMain
MainBackspaceFilter
InputIDFilter = "filter"
InputIDAddTask = "add task"
InputIDIncompleteArchive = "archive incomplete task? (y/n)"
InputIDUnArchiveTask = "move task to active list? (y/n)"
)
func (screen *MainScreen) initialize(bundle Bundle) error {
if err := screen.reloadList(bundle); err != nil {
return err
}
width, height := termbox.Size()
screen.inputField = termboxUtil.CreateInputField(0, (height - 3), width, 1, DefaultFg, DefaultBg)
screen.inputField.SetID("")
screen.inputField.SetBordered(false)
screen.cursor = make(map[string]int)
if bundle != nil {
if err := screen.reloadList(bundle); err != nil {
return err
}
screen.inputField.SetID("")
screen.inputField.SetBordered(false)
}
return nil
}
@ -99,6 +109,9 @@ func (screen *MainScreen) reloadList(bundle Bundle) error {
}
}
}
if screen.cursor[screen.currentList] > len(screen.displayList)-1 {
screen.cursor[screen.currentList] = len(screen.displayList) - 1
}
return nil
}
@ -108,6 +121,10 @@ func (screen *MainScreen) handleKeyEvent(event termbox.Event) int {
}
if event.Ch == '?' {
// Go to About Screen
b := Bundle{}
if err := app.screens[ScreenAbout].initialize(b); err != nil {
screen.setMessage(err.Error())
}
return ScreenAbout
} else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
@ -127,13 +144,35 @@ func (screen *MainScreen) handleKeyEvent(event termbox.Event) int {
} else if event.Key == termbox.KeySpace {
return screen.toggleTaskComplete()
} else if event.Ch == 'g' {
screen.cursor[screen.currentList] = 0
} else if event.Ch == 'G' {
screen.cursor[screen.currentList] = len(screen.displayList) - 1
} 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
}
} 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
}
} else if event.Ch == 'L' {
return screen.toggleViewList()
} else if event.Ch == 'a' {
return screen.startAddNewTask()
} else if event.Ch == 'e' || event.Ch == 'l' || event.Key == termbox.KeyEnter {
} else if event.Ch == 'l' || event.Key == termbox.KeyEnter || event.Key == termbox.KeyArrowRight {
return screen.startEditTaskScreen()
} else if event.Ch == 'j' || event.Key == termbox.KeyArrowDown {
@ -146,7 +185,7 @@ func (screen *MainScreen) handleKeyEvent(event termbox.Event) int {
screen.startFilter()
} else if event.Ch == 'D' {
screen.archiveCurrentItem()
screen.confirmArchiveItem()
} else if event.Ch == 'q' {
return ScreenExit
@ -154,13 +193,9 @@ func (screen *MainScreen) handleKeyEvent(event termbox.Event) int {
return ScreenMain
}
func (screen *MainScreen) handleConfirmKeyEvent(event termbox.Event) int {
return ScreenMain
}
func (screen *MainScreen) handleInputKeyEvent(event termbox.Event) int {
id := screen.inputField.GetID()
if id == "filter" {
switch screen.inputField.GetID() {
case InputIDFilter:
if event.Key == termbox.KeyEnter {
// Apply the filter
filter := screen.inputField.GetValue()
@ -170,7 +205,7 @@ func (screen *MainScreen) handleInputKeyEvent(event termbox.Event) int {
screen.reloadList(screen.buildBundle(screen.currentList, filter))
return ScreenMain
}
} else if id == "add task" {
case InputIDAddTask:
if event.Key == termbox.KeyEnter {
// Create the new item
err := app.addTask(screen.inputField.GetValue())
@ -179,13 +214,28 @@ func (screen *MainScreen) handleInputKeyEvent(event termbox.Event) int {
}
screen.inputField.SetID("")
screen.inputField.SetValue("")
if err = app.WriteList(); err != nil {
screen.setMessage(err.Error())
return ScreenMain
}
screen.reloadList(screen.buildBundle(screen.currentList, screen.currentFilter))
return ScreenMain
}
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))
return ScreenMain
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))
return ScreenMain
}
if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
if screen.inputField.GetValue() == "" {
@ -194,6 +244,11 @@ func (screen *MainScreen) handleInputKeyEvent(event termbox.Event) int {
screen.inputField.SetValue("")
return ScreenMain
}
} else if event.Key == termbox.KeyEsc {
screen.reloadList(screen.buildBundle(screen.currentList, screen.currentFilter))
screen.inputField.SetID("")
screen.inputField.SetValue("")
return ScreenMain
}
screen.inputField.HandleEvent(event)
return ScreenMain
@ -207,6 +262,18 @@ func (screen *MainScreen) setActiveList(list todotxt.TaskList) {
}
func (screen *MainScreen) drawScreen() {
_, 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
}
if screen.message == "" {
screen.setMessageWithTimeout("Press '?' for help", -1)
}
@ -221,10 +288,13 @@ func (screen *MainScreen) drawScreen() {
for k, v := range screen.displayList {
pad := strings.Repeat(" ", len(padCnt)-len(fmt.Sprintf("%d", v.Id)))
useFg, useBg := DefaultFg, DefaultBg
if k == screen.viewPort.cursor {
if k == screen.cursor[screen.currentList] {
useFg, useBg = CursorFg, CursorBg
}
termboxUtil.DrawStringAtPoint(pad+app.getTaskString(v), 0, k+1, useFg, useBg)
lineY := k + 1 - displayOffset
if lineY > 0 && lineY < screen.viewPort.numberOfRows {
termboxUtil.DrawStringAtPoint(pad+app.getTaskString(v), 0, lineY, useFg, useBg)
}
}
screen.drawFooter()
}
@ -247,28 +317,66 @@ func (screen *MainScreen) drawFooter() {
if screen.messageTimeout > 0 && time.Since(screen.messageTime) > screen.messageTimeout {
screen.clearMessage()
}
_, height := termbox.Size()
width, height := termbox.Size()
if screen.inputField.GetID() != "" {
termboxUtil.DrawStringAtPoint(screen.inputField.GetID()+": ", 0, height-2, DefaultFg, DefaultBg)
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)
screen.inputField.Draw()
}
// And the 'message'
termboxUtil.DrawStringAtPoint(screen.message, 0, height-1, DefaultFg, DefaultBg)
}
func (screen *MainScreen) confirmArchiveItem() int {
if screen.currentList != MainBundleListTodo {
screen.inputField.SetID(InputIDUnArchiveTask)
return ScreenMain
}
// Find the task under the cursor
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)
} else {
return screen.archiveCurrentItem()
}
}
return ScreenMain
}
func (screen *MainScreen) archiveCurrentItem() int {
if screen.currentList != MainBundleListTodo {
screen.setMessage("Task is already archived")
return ScreenMain
}
// Find the task under the cursor
if len(screen.displayList) > screen.viewPort.cursor {
t := screen.displayList[screen.viewPort.cursor]
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
}
if err := app.WriteList(); err != nil {
// Reload the list
b := screen.buildBundle(screen.currentList, screen.currentFilter)
if err := screen.reloadList(b); err != nil {
screen.setMessage(err.Error())
}
}
return ScreenMain
}
func (screen *MainScreen) unarchiveCurrentItem() int {
if screen.currentList == MainBundleListTodo {
screen.setMessage("Task is not archived")
return ScreenMain
}
// Find the task under the cursor
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
}
@ -283,8 +391,8 @@ func (screen *MainScreen) archiveCurrentItem() int {
func (screen *MainScreen) startEditTaskScreen() int {
// Find the task under the cursor
if len(screen.displayList) > screen.viewPort.cursor {
t := screen.displayList[screen.viewPort.cursor]
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)
@ -300,7 +408,7 @@ func (screen *MainScreen) reloadCurrentView() {
bundle := Bundle{}
bundle.setValue(MainBundleListKey, screen.currentList)
bundle.setValue(MainBundleFilterKey, screen.currentFilter)
screen.initialize(bundle)
screen.reloadList(bundle)
}
func (screen *MainScreen) toggleViewList() int {
@ -312,54 +420,55 @@ func (screen *MainScreen) toggleViewList() int {
bundle.setValue(MainBundleListKey, MainBundleListTodo)
}
bundle.setValue(MainBundleFilterKey, screen.currentFilter)
screen.initialize(bundle)
screen.reloadList(bundle)
return ScreenMain
}
func (screen *MainScreen) startAddNewTask() int {
screen.inputField.SetID("add task")
screen.inputField.SetX(len(screen.inputField.GetID()) + 2)
screen.inputField.SetID(InputIDAddTask)
return ScreenMain
}
func (screen *MainScreen) toggleTaskComplete() int {
if screen.currentList == MainBundleListDone {
screen.setMessage("Task is archived, unable to modify.")
return ScreenMain
}
// Find the task under the cursor
if len(screen.displayList) > screen.viewPort.cursor {
t := screen.displayList[screen.viewPort.cursor]
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())
return ScreenMain
}
if err = app.WriteList(); err != nil {
screen.setMessage(err.Error())
}
}
screen.reloadCurrentView()
return ScreenMain
}
func (screen *MainScreen) moveCursorDown() bool {
screen.viewPort.cursor++
if screen.viewPort.cursor >= len(screen.displayList) {
screen.viewPort.cursor = len(screen.displayList) - 1
screen.cursor[screen.currentList]++
if screen.cursor[screen.currentList] >= len(screen.displayList) {
screen.cursor[screen.currentList] = len(screen.displayList) - 1
return false
}
return true
}
func (screen *MainScreen) moveCursorUp() bool {
screen.viewPort.cursor--
if screen.viewPort.cursor < 0 {
screen.viewPort.cursor = 0
screen.cursor[screen.currentList]--
if screen.cursor[screen.currentList] < 0 {
screen.cursor[screen.currentList] = 0
return false
}
return true
}
func (screen *MainScreen) startFilter() {
screen.inputField.SetID("filter")
screen.inputField.SetX(len(screen.inputField.GetID()) + 2)
func (screen *MainScreen) startFilter() int {
screen.inputField.SetID(InputIDFilter)
return ScreenMain
}
func (screen *MainScreen) setMessage(msg string) {
@ -378,7 +487,7 @@ func (screen *MainScreen) setMessageWithTimeout(msg string, timeout time.Duratio
}
func (screen *MainScreen) clearMessage() {
screen.message = ""
screen.message = fmt.Sprintf("%d Total Tasks", len(screen.activeList))
screen.messageTimeout = -1
}

View File

@ -13,10 +13,10 @@ import (
// TaskScreen holds all that's going on
type TaskScreen struct {
viewPort *ViewPort
message string
messageTimeout time.Duration
messageTime time.Time
cursor int
inputModal *termboxUtil.InputModal
confirmModal *termboxUtil.ConfirmModal
@ -31,7 +31,9 @@ const (
func (screen *TaskScreen) initialize(bundle Bundle) error {
var err error
screen.currentTaskId = bundle.getInt(TaskBundleTaskIdKey, -1)
if bundle != nil {
screen.currentTaskId = bundle.getInt(TaskBundleTaskIdKey, -1)
}
if screen.currentTaskId == -1 {
return errors.New("Task Screen Initialization Failed")
}
@ -92,12 +94,12 @@ func (screen *TaskScreen) drawFooter() {
}
func (screen *TaskScreen) moveCursorDown() bool {
screen.viewPort.cursor++
screen.cursor++
return true
}
func (screen *TaskScreen) moveCursorUp() bool {
screen.viewPort.cursor--
screen.cursor--
return true
}

View File

@ -47,6 +47,7 @@ func uiLoop() int {
}
}
if event.Type == termbox.EventResize {
displayScreen.initialize(nil)
app.layoutAndDrawScreen(displayScreen)
}
}