Skip to content

Commit

Permalink
Feature suggestion: Jump directly to next/previous URL (#151)
Browse files Browse the repository at this point in the history
Add functionality to jump directly to next/previous URL (J/K) instead of just scrolling the view.
  • Loading branch information
ilekka committed Jun 2, 2024
1 parent c44184e commit 0c2fb20
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,12 @@ The follow actions are supported:
- `down` -- cursor down (default: `j`)
- `help_menu` -- show/hide help menu (default: `F1`)
- `link_handler` -- cycle link handling (webbrowser, xdg-open, --run-safe or --run) (default: `l`)
- `next` -- jump to next URL (default: `J`)
- `open_queue` -- open all URLs in queue (default: `o`)
- `open_queue_win` -- open all URLs in queue in new window (default: `O`)
- `open_url` -- open selected URL (default: `space` or `enter`)
- `palette` -- cycle through palettes (default: `p`)
- `previous` -- jump to previous URL (default: `K`)
- `quit` -- quit (default: `q` or `Q`)
- `reverse` -- reverse display order (default: `R`)
- `shorten` -- toggle shorten highlighted URL (default: `s`)
Expand Down
4 changes: 4 additions & 0 deletions urlscan.1
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ The follow actions are supported:
.TP
\fBlink_handler\fR \-\- cycle link handling (webbrowser, xdg-open or custom) (Default: \fBl\fR)
.TP
\fBnext\fR \-\- jump to next URL (Default: \fBJ\fR)
.TP
\fBopen_queue\fR \-\- open all URLs in queue (Default: \fBo\fR)
.TP
\fBopen_queue_win\fR \-\- open all URLs in queue in new window (Default: \fBO\fR)
Expand All @@ -196,6 +198,8 @@ The follow actions are supported:
.TP
\fBpalette\fR \-\- cycle through palettes (Default: \fBp\fR)
.TP
\fBprevious\fR \-\- jump to previous URL (Default: \fBK\fR)
.TP
\fBquit\fR \-\- quit (Default: \fBq\fR or \fBQ\fR)
.TP
\fBreverse\fR \-\- reverse display order (Default: \fBR\fR)
Expand Down
29 changes: 29 additions & 0 deletions urlscan/urlchoose.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ def __init__(self, extractedurls, compact=False, reverse=False, nohelp=False, de
'g': self._top,
'j': self._down,
'k': self._up,
'J': self._next,
'K': self._previous,
'P': self._clipboard_pri,
'l': self._link_handler,
'o': self._open_queue,
Expand Down Expand Up @@ -446,10 +448,12 @@ def _help_menu(self):
"help_menu -- show/hide help menu\n"
"link_handler -- cycle through xdg-open, webbrowser \n"
" and user-defined function\n"
"next -- jump to next URL\n"
"open_queue -- open all URLs in queue\n"
"open_queue_win-- open all URLs in queue in new window\n"
"open_url -- open selected URL\n"
"palette -- cycle through palettes\n"
"previous -- jump to previous URL\n"
"quit -- quit\n"
"reverse -- reverse order URLs/context\n"
"shorten -- toggle shorten highlighted URL\n"
Expand Down Expand Up @@ -524,6 +528,31 @@ def _bottom(self):
self.top.base_widget.body.focus_position = len(self.items) - 1
self.top.base_widget.keypress(self.size, "") # Trick urwid into redisplaying the cursor

def _selectable_positions(self):
return [i for i, item in enumerate(self.items) if item.selectable()]

def _next(self):
""" J """
current_position = self.top.base_widget.body.focus_position
if current_position >= self._selectable_positions()[-1]:
# Do not jump if focus is on or after the last selectable position
return
# Jump to the first selectable position after the currently focused position
target_position = min(p for p in self._selectable_positions() if p > current_position)
self.top.base_widget.body.focus_position = target_position
self.top.base_widget.keypress(self.size, "") # Trick urwid into redisplaying the cursor

def _previous(self):
""" K """
current_position = self.top.base_widget.body.focus_position
if current_position <= self._selectable_positions()[0]:
# Do not jump if focus is on or before the first selectable position
return
# Jump to the first selectable position before the currently focused position
target_position = max(p for p in self._selectable_positions() if p < current_position)
self.top.base_widget.body.focus_position = target_position
self.top.base_widget.keypress(self.size, "") # Trick urwid into redisplaying the cursor

def _shorten(self):
""" s """
# Toggle shortened URL for selected item
Expand Down

0 comments on commit 0c2fb20

Please sign in to comment.