Skip to content

Commit

Permalink
Let ESC skip back to where the search started
Browse files Browse the repository at this point in the history
When searching in the pager, pressing ESC will now skip back to where
the search started. This is useful when you want to quickly jump back to
where you started searching from.

Pressing RETURN will still stop the search and leave the cursor where it
is.

Being able to press ESC to go back somewhat improves on the first half
of #257, related to searching from
the current position to the end.
  • Loading branch information
walles committed Nov 26, 2024
1 parent 8c6f666 commit 02a6f11
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion m/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ Searching
---------
* Type / to start searching, then type what you want to find
* Type ? to search backwards, then type what you want to find
* Type RETURN to stop searching
* Type RETURN to stop searching, or ESC to skip back to where the search started
* Find next by typing 'n' (for "next")
* Find previous by typing SHIFT-N or 'p' (for "previous")
* Search is case sensitive if it contains any UPPER CASE CHARACTERS
Expand Down
11 changes: 8 additions & 3 deletions m/pagermode-search.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ import (
)

type PagerModeSearch struct {
pager *Pager
backwards bool
pager *Pager
initialScrollPosition scrollPosition // Pager position before search started
backwards bool
}

func (m PagerModeSearch) drawFooter(_ string, _ string) {
Expand Down Expand Up @@ -101,10 +102,14 @@ func removeLastChar(s string) string {

func (m PagerModeSearch) onKey(key twin.KeyCode) {
switch key {
case twin.KeyEscape, twin.KeyEnter:
case twin.KeyEnter:
//nolint:gosimple // The linter's advice is just wrong here
m.pager.mode = PagerModeViewing{pager: m.pager}

case twin.KeyEscape:
m.pager.mode = PagerModeViewing{pager: m.pager}
m.pager.scrollPosition = m.initialScrollPosition

case twin.KeyBackspace, twin.KeyDelete:
if len(m.pager.searchString) == 0 {
return
Expand Down
4 changes: 2 additions & 2 deletions m/pagermode-viewing.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ func (m PagerModeViewing) onRune(char rune) {
p.handleScrolledDown()

case '/':
p.mode = PagerModeSearch{pager: p, backwards: false}
p.mode = PagerModeSearch{pager: p, backwards: false, initialScrollPosition: p.scrollPosition}
p.TargetLineNumber = nil
p.searchString = ""
p.searchPattern = nil

case '?':
p.mode = PagerModeSearch{pager: p, backwards: true}
p.mode = PagerModeSearch{pager: p, backwards: true, initialScrollPosition: p.scrollPosition}
p.TargetLineNumber = nil
p.searchString = ""
p.searchPattern = nil
Expand Down

0 comments on commit 02a6f11

Please sign in to comment.