Skip to content

Commit

Permalink
feat: add task at current position/bottom of list
Browse files Browse the repository at this point in the history
  • Loading branch information
singalhimanshu committed Jan 23, 2021
1 parent 52485e5 commit 33efcdd
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
21 changes: 17 additions & 4 deletions parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,16 @@ func (d *Data) GetTasks(listIdx int) ([]string, error) {

// AddNewTask adds a new task to a list provided the list index and the title of that task.
// It returns an error if the index is out of bounds.
func (d *Data) AddNewTask(listIdx int, taskTitle, taskDesc string) error {
func (d *Data) AddNewTask(listIdx int, taskTitle, taskDesc string, taskPos int) error {
listCount := d.GetListCount()
if err := checkBounds(listIdx, listCount); err != nil {
return err
}
d.lists[listIdx].listItems = append(d.lists[listIdx].listItems, ListItem{
newTask := ListItem{
itemName: taskTitle,
itemDescription: taskDesc,
})
}
d.insertTask(listIdx, newTask, taskPos+1)
return nil
}

Expand Down Expand Up @@ -190,7 +191,7 @@ func (d *Data) MoveTask(prevTaskIdx, prevListIdx, newListIdx int) error {
}
taskTitle := d.lists[prevListIdx].listItems[prevTaskIdx].itemName
taskDesc := d.lists[prevListIdx].listItems[prevTaskIdx].itemDescription
err = d.AddNewTask(newListIdx, taskTitle, taskDesc)
err = d.AddNewTask(newListIdx, taskTitle, taskDesc, 0)
if err != nil {
return err
}
Expand Down Expand Up @@ -276,6 +277,18 @@ func (d *Data) GetListCount() int {
return len(d.lists)
}

func (d *Data) insertTask(listIdx int, task ListItem, taskPos int) {
if len(d.lists[listIdx].listItems) < 1 {
d.lists[listIdx].listItems = append(d.lists[listIdx].listItems, task)
return
}

d.lists[listIdx].listItems = append(d.lists[listIdx].listItems, ListItem{})
copy(d.lists[listIdx].listItems[(taskPos+1):],
d.lists[listIdx].listItems[taskPos:])
d.lists[listIdx].listItems[taskPos] = task
}

func swap(first, second *ListItem) {
*second, *first = *first, *second
}
Expand Down
5 changes: 3 additions & 2 deletions ui/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// NewAddPage provides the form to create a new task.
func NewAddPage(p *BoardPage) *tview.Form {
func NewAddPage(p *BoardPage, pos int) *tview.Form {
form := tview.NewForm().
AddInputField("Task", "", 20, nil, nil).
AddInputField("Task Description", "", 20, nil, nil)
Expand All @@ -21,13 +21,14 @@ func NewAddPage(p *BoardPage) *tview.Form {
}
taskDesc := form.GetFormItemByLabel("Task Description").(*tview.InputField).GetText()
taskDesc = strings.TrimSpace(taskDesc)
err := p.data.AddNewTask(p.activeListIdx, taskName, taskDesc)
err := p.data.AddNewTask(p.activeListIdx, taskName, taskDesc, pos)
if err != nil {
app.Stop()
panic(err)
}
p.data.Save(p.fileName)
p.redraw(p.activeListIdx)
p.down()
pages.SwitchToPage("board")
app.SetFocus(p.lists[p.activeListIdx])
}).
Expand Down
10 changes: 9 additions & 1 deletion ui/board.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,15 @@ func (p *BoardPage) setInputCapture(i int) {
case 'L':
p.moveRight()
case 'a':
pages.AddAndSwitchToPage("add", NewAddPage(p), true)
taskPos := p.activeTaskIdxs[p.activeListIdx]
pages.AddAndSwitchToPage("add", NewAddPage(p, taskPos), true)
case 'A':
lastTaskPos, err := p.data.GetTaskCount(p.activeListIdx)
if err != nil {
app.Stop()
panic(err)
}
pages.AddAndSwitchToPage("add", NewAddPage(p, lastTaskPos-1), true)
case 'D':
p.removeTask()
case 'd':
Expand Down
3 changes: 2 additions & 1 deletion ui/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ const helpText = `j: down
k: up
h: left
l: right
a: add task
a: add task under the cursor
A: add task at the end of list
D: delete a task
d: mark a task as done
e: change/edit task
Expand Down

0 comments on commit 33efcdd

Please sign in to comment.