Skip to content

Commit 34a0fba

Browse files
committed
feat: move cursor to item selected in filtered state
Pressing enter in a filtered state will move the cursor to the task that was selected.
1 parent bf8f956 commit 34a0fba

File tree

5 files changed

+86
-61
lines changed

5 files changed

+86
-61
lines changed

cmd/assets/guide/actions-filtering-tasks.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ prompt, which will match your query with task prefixes.
44
Try it out now. You get out of the filtered state by pressing `q/esc/<ctrl+c>`.
55

66
Note: You cannot add tasks or move them around in a filtered state. But, you can
7-
move a task to the top or the bottom of the list (by pressing ``/`E`). Doing
8-
this will also get you out of the filtered state.
7+
press `` to go back to the main list and have the cursor be moved to the task
8+
you had selected in the filtered state.

cmd/assets/guide/actions-quick-filtering-via-a-list.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
You can also choose the prefix you want to filter by with the means of a list,
2-
hereby called as the **Quick Filter List**. Press `ctrl+p` to open up a set of
3-
task prefixes contained in the currently active task list. Press `` to
4-
pre-populate the task list's search prompt with your selection.
1+
You can also choose the prefix you want to filter by using the **Prefix
2+
Selection List**. Press `ctrl+p` to open up this list. Press `` to pre-populate
3+
the task list's search prompt with your selection.
54

65
Try it out now.
76

internal/ui/assets/help.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,10 @@ omm has 6 components:
5555
J move task one position down
5656
K move task one position up
5757

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

6163
### Task Creation/Update Pane
6264

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

7476
⏎ open URL in browser
75-
76-
### Prefix Selection List
77-
78-
⏎ pre-populate task list's search prompt with chosen prefix

internal/ui/model.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,6 @@ const (
2121
taskSummaryWidth = 120
2222
)
2323

24-
func (m model) Init() tea.Cmd {
25-
return tea.Batch(
26-
fetchTasks(m.db, true, pers.TaskNumLimit),
27-
fetchTasks(m.db, false, pers.TaskNumLimit),
28-
hideHelp(time.Minute*1),
29-
)
30-
}
31-
3224
type taskChangeType uint
3325

3426
const (
@@ -71,6 +63,7 @@ type model struct {
7163
taskBMList list.Model
7264
prefixSearchList list.Model
7365
tlIndexMap map[uint64]int
66+
atlIndexMap map[uint64]int
7467
taskIndex int
7568
taskId uint64
7669
taskChange taskChangeType
@@ -102,3 +95,11 @@ type model struct {
10295
taskDetailsMdRenderer *glamour.TermRenderer
10396
prefixSearchUse prefixUse
10497
}
98+
99+
func (m model) Init() tea.Cmd {
100+
return tea.Batch(
101+
fetchTasks(m.db, true, pers.TaskNumLimit),
102+
fetchTasks(m.db, false, pers.TaskNumLimit),
103+
hideHelp(time.Minute*1),
104+
)
105+
}

internal/ui/update.go

Lines changed: 68 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
470470
m.taskList.SetItem(ci+1, currentItem)
471471
m.taskList.Select(ci + 1)
472472

473-
cmd = m.updateTaskSequence()
473+
cmd = m.updateActiveTasksSequence()
474474
cmds = append(cmds, cmd)
475475

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

497-
cmd = m.updateTaskSequence()
497+
cmd = m.updateActiveTasksSequence()
498498
cmds = append(cmds, cmd)
499499

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

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

578578
if m.archivedTaskList.IsFiltered() {
579-
m.errorMsg = "Cannot archive items when the task list is filtered"
579+
m.errorMsg = "Cannot unarchive items when the task list is filtered"
580580
break
581581
}
582582

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

696696
case "enter":
697-
if m.activeView != taskListView && m.activeView != contextBookmarksView && m.activeView != prefixSelectionView {
697+
if m.activeView != taskListView && m.activeView != archivedTaskListView && m.activeView != contextBookmarksView && m.activeView != prefixSelectionView {
698698
break
699699
}
700700
switch m.activeView {
@@ -703,39 +703,64 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
703703
break
704704
}
705705

706-
var index int
707706
if m.taskList.IsFiltered() {
708707
selected, ok := m.taskList.SelectedItem().(types.Task)
709708
if !ok {
709+
m.errorMsg = "Something went wrong"
710710
break
711711
}
712+
712713
listIndex, ok := m.tlIndexMap[selected.ID]
713714
if !ok {
714-
m.errorMsg = "Something went wrong; cannot move item to the top"
715+
m.errorMsg = "Something went wrong"
716+
break
715717
}
716-
index = listIndex
717-
} else {
718-
index = m.taskList.Index()
718+
719+
m.taskList.ResetFilter()
720+
m.taskList.Select(listIndex)
721+
break
719722
}
720723

724+
index := m.taskList.Index()
725+
721726
if index == 0 {
722727
m.errorMsg = "This item is already at the top of the list"
723728
break
724729
}
725730

726-
if m.taskList.IsFiltered() {
727-
m.taskList.ResetFilter()
728-
m.taskList.Select(index)
729-
}
730-
731731
listItem := m.taskList.SelectedItem()
732732
m.taskList.RemoveItem(index)
733733
cmd = m.taskList.InsertItem(0, listItem)
734734
cmds = append(cmds, cmd)
735735
m.taskList.Select(0)
736736

737-
cmd = m.updateTaskSequence()
737+
cmd = m.updateActiveTasksSequence()
738738
cmds = append(cmds, cmd)
739+
740+
case archivedTaskListView:
741+
if len(m.archivedTaskList.Items()) == 0 {
742+
break
743+
}
744+
745+
if !m.archivedTaskList.IsFiltered() {
746+
break
747+
}
748+
749+
selected, ok := m.archivedTaskList.SelectedItem().(types.Task)
750+
if !ok {
751+
m.errorMsg = "Something went wrong"
752+
break
753+
}
754+
755+
listIndex, ok := m.atlIndexMap[selected.ID]
756+
if !ok {
757+
m.errorMsg = "Something went wrong"
758+
break
759+
}
760+
761+
m.archivedTaskList.ResetFilter()
762+
m.archivedTaskList.Select(listIndex)
763+
739764
case contextBookmarksView:
740765
url := m.taskBMList.SelectedItem().FilterValue()
741766
cmds = append(cmds, openURL(url))
@@ -801,21 +826,13 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
801826
break
802827
}
803828

804-
var index int
805829
if m.taskList.IsFiltered() {
806-
selected, ok := m.taskList.SelectedItem().(types.Task)
807-
if !ok {
808-
break
809-
}
810-
listIndex, ok := m.tlIndexMap[selected.ID]
811-
if !ok {
812-
m.errorMsg = "Something went wrong; cannot move item to the end"
813-
}
814-
index = listIndex
815-
} else {
816-
index = m.taskList.Index()
830+
m.errorMsg = "Cannot move items when the task list is filtered"
831+
break
817832
}
818833

834+
index := m.taskList.Index()
835+
819836
lastIndex := len(m.taskList.Items()) - 1
820837

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

837-
cmd = m.updateTaskSequence()
854+
cmd = m.updateActiveTasksSequence()
838855
cmds = append(cmds, cmd)
839856

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

911-
for i, li := range m.taskList.Items() {
912-
m.taskList.SetItem(i, li)
913-
}
914-
915-
for i, li := range m.archivedTaskList.Items() {
916-
m.archivedTaskList.SetItem(i, li)
917-
}
918-
919928
if m.cfg.ShowContext {
920929
m.taskList.SetHeight(m.shortenedListHt)
921930
m.archivedTaskList.SetHeight(m.shortenedListHt)
@@ -1148,7 +1157,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
11481157
cmds = append(cmds, cmd)
11491158
m.taskList.Select(m.taskIndex)
11501159

1151-
cmd = m.updateTaskSequence()
1160+
cmd = m.updateActiveTasksSequence()
11521161
cmds = append(cmds, cmd)
11531162

11541163
case taskDeletedMsg:
@@ -1160,10 +1169,11 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
11601169
switch msg.active {
11611170
case true:
11621171
m.taskList.RemoveItem(msg.listIndex)
1163-
cmd = m.updateTaskSequence()
1172+
cmd = m.updateActiveTasksSequence()
11641173
cmds = append(cmds, cmd)
11651174
case false:
11661175
m.archivedTaskList.RemoveItem(msg.listIndex)
1176+
m.updateArchivedTasksIndex()
11671177
}
11681178

11691179
case taskSequenceUpdatedMsg:
@@ -1264,7 +1274,8 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
12641274
m.archivedTaskList.InsertItem(0, list.Item(t))
12651275
m.taskList.RemoveItem(msg.listIndex)
12661276
}
1267-
cmd = m.updateTaskSequence()
1277+
cmd = m.updateActiveTasksSequence()
1278+
m.updateArchivedTasksIndex()
12681279
cmds = append(cmds, cmd)
12691280
}
12701281

@@ -1298,6 +1309,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
12981309
}
12991310
m.archivedTaskList.SetItems(archivedTaskItems)
13001311
m.archivedTaskList.Select(0)
1312+
m.updateArchivedTasksIndex()
13011313
}
13021314
}
13031315
case textEditorClosed:
@@ -1437,7 +1449,7 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
14371449
return m, tea.Batch(cmds...)
14381450
}
14391451

1440-
func (m *model) updateTaskSequence() tea.Cmd {
1452+
func (m *model) updateActiveTasksSequence() tea.Cmd {
14411453
sequence := make([]uint64, len(m.taskList.Items()))
14421454
tlIndexMap := make(map[uint64]int)
14431455

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

1469+
func (m *model) updateArchivedTasksIndex() {
1470+
sequence := make([]uint64, len(m.archivedTaskList.Items()))
1471+
tlIndexMap := make(map[uint64]int)
1472+
1473+
for i, ti := range m.archivedTaskList.Items() {
1474+
t, ok := ti.(types.Task)
1475+
if ok {
1476+
sequence[i] = t.ID
1477+
tlIndexMap[t.ID] = i
1478+
}
1479+
}
1480+
1481+
m.atlIndexMap = tlIndexMap
1482+
}
1483+
14571484
func (m model) isSpaceAvailable() bool {
14581485
return len(m.taskList.Items()) < pers.TaskNumLimit
14591486
}

0 commit comments

Comments
 (0)