Skip to content

Commit

Permalink
fix(backlog): Insert new task before on "O"
Browse files Browse the repository at this point in the history
  • Loading branch information
ja-he committed Dec 4, 2023
1 parent b775dcf commit e476d38
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
24 changes: 24 additions & 0 deletions internal/control/cli/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,30 @@ func NewController(
log.Debug().Msg("could not find parent, so not changing current task")
}
}),
"O": action.NewSimple(func() string { return "add a new task above the current one" }, func() {
if currentTask == nil {
log.Debug().Msgf("asked to add a task after to nil current task, adding as first")
newTask := backlog.AddLast()
newTask.Name = "" // user should be hinted to change this quite quickly, i.e. via immediate editor activation
newTask.Category = controller.data.CurrentCategory
currentTask = newTask
createAndEnableTaskEditor(currentTask)
return
}
newTask, parent, err := backlog.AddBefore(currentTask)
if err != nil {
log.Error().Err(err).Msgf("was unable to add a task after '%s'", currentTask.Name)
return
}
newTask.Name = "" // user should be hinted to change this quite quickly, i.e. via immediate editor activation
if parent != nil {
newTask.Category = parent.Category
} else {
newTask.Category = controller.data.CurrentCategory
}
currentTask = newTask
createAndEnableTaskEditor(currentTask)
}),
"o": action.NewSimple(func() string { return "add a new task below the current one" }, func() {
if currentTask == nil {
log.Debug().Msgf("asked to add a task after to nil current task, adding as first")
Expand Down
32 changes: 32 additions & 0 deletions internal/model/backlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,38 @@ func (b *Backlog) AddAfter(anchorTask *Task) (newTask *Task, parent *Task, err e
return newTask, parent, nil
}

// AddBefore adds a new task before the given anchorTask.
func (b *Backlog) AddBefore(anchorTask *Task) (newTask *Task, parent *Task, err error) {
_, _, parentage, index, err := b.Locate(anchorTask)
if err != nil {
return nil, nil, fmt.Errorf("could not locate anchor task (%s)", err.Error())
}
taskList := b.Tasks
if len(parentage) > 0 {
parent = parentage[0]
taskList = parent.Subtasks
}

// sanity check
{
if taskList[index] != anchorTask {
return nil, nil, fmt.Errorf("implementation error: task[%d].Name == '%s' != '%s", index, taskList[index].Name, anchorTask.Name)
}
}

newTask = new(Task)

// insert new task after given index
taskList = append(taskList[:index], append([]*Task{newTask}, taskList[index:]...)...)
if parent != nil {
parent.Subtasks = taskList
} else {
b.Tasks = taskList
}

return newTask, parent, nil
}

func (t *Task) toEvent(startTime time.Time, namePrefix string) Event {
return Event{
Start: *NewTimestampFromGotime(startTime),
Expand Down

0 comments on commit e476d38

Please sign in to comment.