Skip to content

Commit

Permalink
feat: move cursor to item selected in filtered state
Browse files Browse the repository at this point in the history
Pressing enter in a filtered state will move the cursor to the task that
was selected.
  • Loading branch information
dhth committed Jul 26, 2024
1 parent bf8f956 commit 34a0fba
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 61 deletions.
4 changes: 2 additions & 2 deletions cmd/assets/guide/actions-filtering-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ prompt, which will match your query with task prefixes.
Try it out now. You get out of the filtered state by pressing `q/esc/<ctrl+c>`.

Note: You cannot add tasks or move them around in a filtered state. But, you can
move a task to the top or the bottom of the list (by pressing ``/`E`). Doing
this will also get you out of the filtered state.
press `` to go back to the main list and have the cursor be moved to the task
you had selected in the filtered state.
7 changes: 3 additions & 4 deletions cmd/assets/guide/actions-quick-filtering-via-a-list.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
You can also choose the prefix you want to filter by with the means of a list,
hereby called as the **Quick Filter List**. Press `ctrl+p` to open up a set of
task prefixes contained in the currently active task list. Press `` to
pre-populate the task list's search prompt with your selection.
You can also choose the prefix you want to filter by using the **Prefix
Selection List**. Press `ctrl+p` to open up this list. Press `` to pre-populate
the task list's search prompt with your selection.

Try it out now.

Expand Down
10 changes: 4 additions & 6 deletions internal/ui/assets/help.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ omm has 6 components:
J move task one position down
K move task one position up

**Note**: Moving tasks around is not allowed when the active tasks list is in a
filtered state, however, you can still use `` to move a task to the top.
**Note**: Most actions on tasks are not allowed when the tasks list is in a
filtered state. You can press `` to go back to the main list and have the
cursor be moved to the task you had selected in the filtered state, and run the
action from there.

### Task Creation/Update Pane

Expand All @@ -72,7 +74,3 @@ filtered state, however, you can still use `⏎` to move a task to the top.
### Task Bookmarks List

⏎ open URL in browser

### Prefix Selection List

⏎ pre-populate task list's search prompt with chosen prefix
17 changes: 9 additions & 8 deletions internal/ui/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,6 @@ const (
taskSummaryWidth = 120
)

func (m model) Init() tea.Cmd {
return tea.Batch(
fetchTasks(m.db, true, pers.TaskNumLimit),
fetchTasks(m.db, false, pers.TaskNumLimit),
hideHelp(time.Minute*1),
)
}

type taskChangeType uint

const (
Expand Down Expand Up @@ -71,6 +63,7 @@ type model struct {
taskBMList list.Model
prefixSearchList list.Model
tlIndexMap map[uint64]int
atlIndexMap map[uint64]int
taskIndex int
taskId uint64
taskChange taskChangeType
Expand Down Expand Up @@ -102,3 +95,11 @@ type model struct {
taskDetailsMdRenderer *glamour.TermRenderer
prefixSearchUse prefixUse
}

func (m model) Init() tea.Cmd {
return tea.Batch(
fetchTasks(m.db, true, pers.TaskNumLimit),
fetchTasks(m.db, false, pers.TaskNumLimit),
hideHelp(time.Minute*1),
)
}
109 changes: 68 additions & 41 deletions internal/ui/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.taskList.SetItem(ci+1, currentItem)
m.taskList.Select(ci + 1)

cmd = m.updateTaskSequence()
cmd = m.updateActiveTasksSequence()
cmds = append(cmds, cmd)

case "K":
Expand All @@ -494,7 +494,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.taskList.SetItem(ci-1, currentItem)
m.taskList.Select(ci - 1)

cmd = m.updateTaskSequence()
cmd = m.updateActiveTasksSequence()
cmds = append(cmds, cmd)

case "u":
Expand Down Expand Up @@ -563,7 +563,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
index := m.taskList.Index()
t, ok := listItem.(types.Task)
if !ok {
m.errorMsg = "Something went wrong"
m.errorMsg = "Something went wrong; cannot archive item"
break
}

Expand All @@ -576,7 +576,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

if m.archivedTaskList.IsFiltered() {
m.errorMsg = "Cannot archive items when the task list is filtered"
m.errorMsg = "Cannot unarchive items when the task list is filtered"
break
}

Expand Down Expand Up @@ -694,7 +694,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.prefixSearchUse = prefixFilter

case "enter":
if m.activeView != taskListView && m.activeView != contextBookmarksView && m.activeView != prefixSelectionView {
if m.activeView != taskListView && m.activeView != archivedTaskListView && m.activeView != contextBookmarksView && m.activeView != prefixSelectionView {
break
}
switch m.activeView {
Expand All @@ -703,39 +703,64 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
break
}

var index int
if m.taskList.IsFiltered() {
selected, ok := m.taskList.SelectedItem().(types.Task)
if !ok {
m.errorMsg = "Something went wrong"
break
}

listIndex, ok := m.tlIndexMap[selected.ID]
if !ok {
m.errorMsg = "Something went wrong; cannot move item to the top"
m.errorMsg = "Something went wrong"
break
}
index = listIndex
} else {
index = m.taskList.Index()

m.taskList.ResetFilter()
m.taskList.Select(listIndex)
break
}

index := m.taskList.Index()

if index == 0 {
m.errorMsg = "This item is already at the top of the list"
break
}

if m.taskList.IsFiltered() {
m.taskList.ResetFilter()
m.taskList.Select(index)
}

listItem := m.taskList.SelectedItem()
m.taskList.RemoveItem(index)
cmd = m.taskList.InsertItem(0, listItem)
cmds = append(cmds, cmd)
m.taskList.Select(0)

cmd = m.updateTaskSequence()
cmd = m.updateActiveTasksSequence()
cmds = append(cmds, cmd)

case archivedTaskListView:
if len(m.archivedTaskList.Items()) == 0 {
break
}

if !m.archivedTaskList.IsFiltered() {
break
}

selected, ok := m.archivedTaskList.SelectedItem().(types.Task)
if !ok {
m.errorMsg = "Something went wrong"
break
}

listIndex, ok := m.atlIndexMap[selected.ID]
if !ok {
m.errorMsg = "Something went wrong"
break
}

m.archivedTaskList.ResetFilter()
m.archivedTaskList.Select(listIndex)

case contextBookmarksView:
url := m.taskBMList.SelectedItem().FilterValue()
cmds = append(cmds, openURL(url))
Expand Down Expand Up @@ -801,21 +826,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
break
}

var index int
if m.taskList.IsFiltered() {
selected, ok := m.taskList.SelectedItem().(types.Task)
if !ok {
break
}
listIndex, ok := m.tlIndexMap[selected.ID]
if !ok {
m.errorMsg = "Something went wrong; cannot move item to the end"
}
index = listIndex
} else {
index = m.taskList.Index()
m.errorMsg = "Cannot move items when the task list is filtered"
break
}

index := m.taskList.Index()

lastIndex := len(m.taskList.Items()) - 1

if index == lastIndex {
Expand All @@ -834,7 +851,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, cmd)
m.taskList.Select(lastIndex)

cmd = m.updateTaskSequence()
cmd = m.updateActiveTasksSequence()
cmds = append(cmds, cmd)

case "c":
Expand Down Expand Up @@ -908,14 +925,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.taskList.SetDelegate(tlDel)
m.archivedTaskList.SetDelegate(atlDel)

for i, li := range m.taskList.Items() {
m.taskList.SetItem(i, li)
}

for i, li := range m.archivedTaskList.Items() {
m.archivedTaskList.SetItem(i, li)
}

if m.cfg.ShowContext {
m.taskList.SetHeight(m.shortenedListHt)
m.archivedTaskList.SetHeight(m.shortenedListHt)
Expand Down Expand Up @@ -1148,7 +1157,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
cmds = append(cmds, cmd)
m.taskList.Select(m.taskIndex)

cmd = m.updateTaskSequence()
cmd = m.updateActiveTasksSequence()
cmds = append(cmds, cmd)

case taskDeletedMsg:
Expand All @@ -1160,10 +1169,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.active {
case true:
m.taskList.RemoveItem(msg.listIndex)
cmd = m.updateTaskSequence()
cmd = m.updateActiveTasksSequence()
cmds = append(cmds, cmd)
case false:
m.archivedTaskList.RemoveItem(msg.listIndex)
m.updateArchivedTasksIndex()
}

case taskSequenceUpdatedMsg:
Expand Down Expand Up @@ -1264,7 +1274,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.archivedTaskList.InsertItem(0, list.Item(t))
m.taskList.RemoveItem(msg.listIndex)
}
cmd = m.updateTaskSequence()
cmd = m.updateActiveTasksSequence()
m.updateArchivedTasksIndex()
cmds = append(cmds, cmd)
}

Expand Down Expand Up @@ -1298,6 +1309,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}
m.archivedTaskList.SetItems(archivedTaskItems)
m.archivedTaskList.Select(0)
m.updateArchivedTasksIndex()
}
}
case textEditorClosed:
Expand Down Expand Up @@ -1437,7 +1449,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}

func (m *model) updateTaskSequence() tea.Cmd {
func (m *model) updateActiveTasksSequence() tea.Cmd {
sequence := make([]uint64, len(m.taskList.Items()))
tlIndexMap := make(map[uint64]int)

Expand All @@ -1454,6 +1466,21 @@ func (m *model) updateTaskSequence() tea.Cmd {
return updateTaskSequence(m.db, sequence)
}

func (m *model) updateArchivedTasksIndex() {
sequence := make([]uint64, len(m.archivedTaskList.Items()))
tlIndexMap := make(map[uint64]int)

for i, ti := range m.archivedTaskList.Items() {
t, ok := ti.(types.Task)
if ok {
sequence[i] = t.ID
tlIndexMap[t.ID] = i
}
}

m.atlIndexMap = tlIndexMap
}

func (m model) isSpaceAvailable() bool {
return len(m.taskList.Items()) < pers.TaskNumLimit
}
Expand Down

0 comments on commit 34a0fba

Please sign in to comment.