2019-02-15 17:15:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"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-03-14 15:29:33 +00:00
|
|
|
const (
|
|
|
|
FieldTaskTodo = iota
|
|
|
|
FieldTaskPriority
|
|
|
|
FieldTaskProjects
|
|
|
|
FieldTaskContexts
|
|
|
|
FieldTaskTags
|
|
|
|
FieldTaskError
|
2019-02-28 13:44:03 +00:00
|
|
|
|
2019-03-14 15:29:33 +00:00
|
|
|
TaskBundleTaskIdKey = "taskscreen.taskid"
|
|
|
|
)
|
2019-02-15 17:15:26 +00:00
|
|
|
|
2019-03-14 15:29:33 +00:00
|
|
|
type TaskScreen struct {
|
|
|
|
message *Message
|
|
|
|
cursor int
|
2019-02-15 17:15:26 +00:00
|
|
|
|
|
|
|
currentTaskId int
|
|
|
|
displayTask *todotxt.Task
|
2019-03-14 15:29:33 +00:00
|
|
|
fieldLabels map[int]string
|
|
|
|
editing bool
|
|
|
|
|
|
|
|
inputField *termboxUtil.InputField
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
2019-03-14 15:29:33 +00:00
|
|
|
func (screen *TaskScreen) GetFieldValue(fld int) string {
|
|
|
|
switch fld {
|
|
|
|
case FieldTaskTodo:
|
|
|
|
return screen.displayTask.Todo
|
|
|
|
case FieldTaskPriority:
|
|
|
|
return screen.displayTask.Priority
|
|
|
|
case FieldTaskProjects:
|
|
|
|
return strings.Join(screen.displayTask.Projects, ",")
|
|
|
|
case FieldTaskContexts:
|
|
|
|
return strings.Join(screen.displayTask.Contexts, ",")
|
|
|
|
case FieldTaskTags:
|
|
|
|
var ret []string
|
|
|
|
for k, v := range screen.displayTask.AdditionalTags {
|
|
|
|
ret = append(ret, k+":"+v)
|
|
|
|
}
|
|
|
|
return strings.Join(ret, ",")
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2019-02-15 17:15:26 +00:00
|
|
|
|
2019-03-14 15:29:33 +00:00
|
|
|
func (screen *TaskScreen) Id() int { return ScreenIdTask }
|
2019-02-28 13:44:03 +00:00
|
|
|
|
|
|
|
func (screen *TaskScreen) Initialize(bundle termboxScreen.Bundle) error {
|
2019-03-14 15:29:33 +00:00
|
|
|
screen.fieldLabels = make(map[int]string)
|
|
|
|
screen.fieldLabels[FieldTaskTodo] = "Todo"
|
|
|
|
screen.fieldLabels[FieldTaskPriority] = "Priority"
|
|
|
|
screen.fieldLabels[FieldTaskProjects] = "Projects"
|
|
|
|
screen.fieldLabels[FieldTaskContexts] = "Contexts"
|
|
|
|
screen.fieldLabels[FieldTaskTags] = "Tags"
|
2019-02-15 17:15:26 +00:00
|
|
|
var err error
|
2019-03-14 15:29:33 +00:00
|
|
|
width, height := termbox.Size()
|
|
|
|
screen.inputField = termboxUtil.CreateInputField(2, (height - 3), width, 1, DefaultFg, DefaultBg)
|
|
|
|
screen.message = NewMessage("", DefaultFg, DefaultBg, time.Second*2)
|
2019-02-21 16:47:08 +00:00
|
|
|
if bundle != nil {
|
2019-02-28 13:44:03 +00:00
|
|
|
screen.currentTaskId = bundle.GetInt(TaskBundleTaskIdKey, -1)
|
2019-02-21 16:47:08 +00:00
|
|
|
}
|
2019-02-15 17:15:26 +00:00
|
|
|
if screen.currentTaskId == -1 {
|
|
|
|
return errors.New("Task Screen Initialization Failed")
|
|
|
|
}
|
|
|
|
if screen.displayTask, err = app.TaskList.GetTask(screen.currentTaskId); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-14 15:29:33 +00:00
|
|
|
screen.cursor = FieldTaskTodo
|
2019-02-15 17:15:26 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-02-28 13:44:03 +00:00
|
|
|
func (screen *TaskScreen) ResizeScreen() { screen.Initialize(nil) }
|
|
|
|
|
|
|
|
func (screen *TaskScreen) HandleKeyEvent(event termbox.Event) int {
|
2019-03-14 15:29:33 +00:00
|
|
|
if screen.editing {
|
|
|
|
return screen.handleEditingKeyEvent(event)
|
|
|
|
}
|
|
|
|
if event.Key == termbox.KeyEnter {
|
|
|
|
screen.editing = true
|
|
|
|
screen.inputField.SetTitle(screen.fieldLabels[screen.cursor] + ": ")
|
|
|
|
screen.inputField.SetValue(screen.GetFieldValue(screen.cursor))
|
|
|
|
screen.inputField.SetActive(true)
|
|
|
|
return screen.Id()
|
|
|
|
|
|
|
|
} else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 || event.Ch == 'h' || event.Key == termbox.KeyArrowLeft {
|
|
|
|
bundle := termboxScreen.Bundle{}
|
|
|
|
bundle.SetValue(MainBundleListKey, MainBundleListCurrent)
|
|
|
|
app.uiManager.InitializeScreen(ScreenIdMain, bundle)
|
|
|
|
return ScreenIdMain
|
2019-02-15 17:15:26 +00:00
|
|
|
|
|
|
|
} else if event.Ch == 'j' || event.Key == termbox.KeyArrowDown {
|
|
|
|
screen.moveCursorDown()
|
|
|
|
|
|
|
|
} else if event.Ch == 'k' || event.Key == termbox.KeyArrowUp {
|
|
|
|
screen.moveCursorUp()
|
|
|
|
}
|
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 *TaskScreen) HandleNoneEvent(event termbox.Event) int { return screen.Id() }
|
2019-02-15 17:15:26 +00:00
|
|
|
|
2019-02-28 13:44:03 +00:00
|
|
|
func (screen *TaskScreen) DrawScreen() {
|
2019-02-15 17:15:26 +00:00
|
|
|
screen.drawHeader()
|
|
|
|
|
|
|
|
yPos := 1
|
2019-03-14 15:29:33 +00:00
|
|
|
if screen.cursor == FieldTaskTodo {
|
|
|
|
termboxUtil.DrawStringAtPoint(screen.displayTask.Todo, 0, yPos, CursorBg, CursorFg)
|
|
|
|
} else {
|
|
|
|
termboxUtil.DrawStringAtPoint(screen.displayTask.Todo, 0, yPos, DefaultFg, DefaultBg)
|
|
|
|
}
|
2019-02-15 17:15:26 +00:00
|
|
|
yPos++
|
2019-03-14 15:29:33 +00:00
|
|
|
if screen.cursor == FieldTaskPriority {
|
|
|
|
termboxUtil.DrawStringAtPoint(fmt.Sprintf("Priority: %s", screen.displayTask.Priority), 0, yPos, CursorBg, CursorFg)
|
|
|
|
} else {
|
|
|
|
termboxUtil.DrawStringAtPoint(fmt.Sprintf("Priority: %s", screen.displayTask.Priority), 0, yPos, DefaultFg, DefaultBg)
|
|
|
|
}
|
2019-02-15 17:15:26 +00:00
|
|
|
yPos++
|
2019-03-14 15:29:33 +00:00
|
|
|
if screen.cursor == FieldTaskProjects {
|
|
|
|
termboxUtil.DrawStringAtPoint(fmt.Sprintf("Projects: %s", screen.displayTask.Projects), 0, yPos, CursorBg, CursorFg)
|
|
|
|
} else {
|
|
|
|
termboxUtil.DrawStringAtPoint(fmt.Sprintf("Projects: %s", screen.displayTask.Projects), 0, yPos, DefaultFg, DefaultBg)
|
|
|
|
}
|
2019-02-15 17:15:26 +00:00
|
|
|
yPos++
|
2019-03-14 15:29:33 +00:00
|
|
|
if screen.cursor == FieldTaskContexts {
|
|
|
|
termboxUtil.DrawStringAtPoint(fmt.Sprintf("Contexts: %s", screen.displayTask.Contexts), 0, yPos, CursorBg, CursorFg)
|
|
|
|
} else {
|
|
|
|
termboxUtil.DrawStringAtPoint(fmt.Sprintf("Contexts: %s", screen.displayTask.Contexts), 0, yPos, DefaultFg, DefaultBg)
|
|
|
|
}
|
2019-02-15 17:15:26 +00:00
|
|
|
yPos++
|
2019-03-14 15:29:33 +00:00
|
|
|
if screen.cursor == FieldTaskTags {
|
|
|
|
termboxUtil.DrawStringAtPoint("Additional Tags: ", 0, yPos, CursorBg, CursorFg)
|
|
|
|
} else {
|
|
|
|
termboxUtil.DrawStringAtPoint("Additional Tags: ", 0, yPos, DefaultFg, DefaultBg)
|
|
|
|
}
|
2019-02-15 17:15:26 +00:00
|
|
|
yPos++
|
|
|
|
for k, v := range screen.displayTask.AdditionalTags {
|
|
|
|
termboxUtil.DrawStringAtPoint(fmt.Sprintf("%s: %s", k, v), 0, yPos, DefaultFg, DefaultBg)
|
|
|
|
yPos++
|
|
|
|
}
|
|
|
|
|
2019-03-14 15:29:33 +00:00
|
|
|
if screen.editing {
|
|
|
|
w, h := termbox.Size()
|
|
|
|
termboxUtil.DrawBorder(0, h-4, w-1, h-2, CursorBg, CursorFg)
|
|
|
|
termboxUtil.FillWithChar(' ', 1, h-3, w-2, h-3, DefaultFg, DefaultBg)
|
|
|
|
screen.inputField.Draw()
|
|
|
|
}
|
|
|
|
|
2019-02-15 17:15:26 +00:00
|
|
|
screen.drawFooter()
|
|
|
|
}
|
|
|
|
|
2019-03-14 15:29:33 +00:00
|
|
|
func (screen *TaskScreen) handleEditingKeyEvent(event termbox.Event) int {
|
|
|
|
if event.Key == termbox.KeyEnter {
|
|
|
|
var needsSave bool
|
|
|
|
// Save the change
|
|
|
|
switch screen.cursor {
|
|
|
|
case FieldTaskTodo:
|
|
|
|
//screen.displayTask.Todo = screen.inputField.GetValue()
|
|
|
|
if screen.inputField.GetValue() != screen.displayTask.Todo {
|
|
|
|
screen.displayTask.Todo = screen.inputField.GetValue()
|
|
|
|
needsSave = true
|
|
|
|
}
|
|
|
|
case FieldTaskPriority:
|
|
|
|
val := screen.inputField.GetValue()
|
|
|
|
if len(val) > 0 {
|
|
|
|
val = string(val[0])
|
|
|
|
}
|
|
|
|
if val != screen.displayTask.Priority {
|
|
|
|
screen.displayTask.Priority = val
|
|
|
|
needsSave = true
|
|
|
|
}
|
|
|
|
case FieldTaskProjects:
|
|
|
|
projects := splitFields(screen.inputField.GetValue())
|
|
|
|
if !isCombination(projects, screen.displayTask.Projects) {
|
|
|
|
needsSave = true
|
|
|
|
screen.displayTask.Projects = []string{}
|
|
|
|
for _, v := range projects {
|
|
|
|
screen.displayTask.Projects = append(screen.displayTask.Projects, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case FieldTaskContexts:
|
|
|
|
contexts := splitFields(screen.inputField.GetValue())
|
|
|
|
if !isCombination(contexts, screen.displayTask.Contexts) {
|
|
|
|
needsSave = true
|
|
|
|
screen.displayTask.Contexts = []string{}
|
|
|
|
for _, v := range contexts {
|
|
|
|
screen.displayTask.Contexts = append(screen.displayTask.Contexts, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case FieldTaskTags:
|
|
|
|
tagsSlice := splitFields(screen.inputField.GetValue())
|
|
|
|
if !sliceIsValidTags(tagsSlice) {
|
|
|
|
screen.message.Set("Tags should be in format <key>:<val>")
|
|
|
|
return screen.Id()
|
|
|
|
}
|
|
|
|
if !isCombination(tagsSlice, tagsToSlice(screen.displayTask.AdditionalTags)) {
|
|
|
|
needsSave = true
|
|
|
|
screen.displayTask.AdditionalTags = make(map[string]string)
|
|
|
|
for k, v := range sliceToTags(tagsSlice) {
|
|
|
|
screen.displayTask.AdditionalTags[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if needsSave {
|
|
|
|
if err := app.saveTask(screen.displayTask); err != nil {
|
|
|
|
screen.message.SetError(err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
screen.editing = false
|
|
|
|
screen.inputField.SetActive(false)
|
|
|
|
} else {
|
|
|
|
if screen.inputField.HandleEvent(event) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return screen.Id()
|
|
|
|
}
|
|
|
|
|
2019-02-15 17:15:26 +00:00
|
|
|
func (screen *TaskScreen) drawHeader() {
|
|
|
|
width, _ := termbox.Size()
|
|
|
|
headerString := screen.displayTask.Todo
|
|
|
|
spaces := strings.Repeat(" ", ((width-len(headerString))/2)+1)
|
|
|
|
termboxUtil.DrawStringAtPoint(fmt.Sprintf("%s%s%s", spaces, headerString, spaces), 0, 0, TitleFg, TitleBg)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *TaskScreen) drawFooter() {
|
|
|
|
_, height := termbox.Size()
|
2019-03-14 15:29:33 +00:00
|
|
|
screen.message.DrawAt(0, height-1)
|
2019-02-15 17:15:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *TaskScreen) moveCursorDown() bool {
|
2019-03-14 15:29:33 +00:00
|
|
|
screen.cursor = (screen.cursor + 1) % FieldTaskError
|
2019-02-15 17:15:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (screen *TaskScreen) moveCursorUp() bool {
|
2019-03-14 15:29:33 +00:00
|
|
|
if screen.cursor > 0 {
|
|
|
|
screen.cursor--
|
|
|
|
} else {
|
|
|
|
screen.cursor = FieldTaskError - 1
|
|
|
|
}
|
2019-02-15 17:15:26 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-03-14 15:29:33 +00:00
|
|
|
func (screen *TaskScreen) editCurrentField() {
|
2019-02-15 17:15:26 +00:00
|
|
|
|
|
|
|
}
|