Skip to content

Commit

Permalink
Merge pull request #809 from 20mattia02/step-backwards
Browse files Browse the repository at this point in the history
Implement backwards navigation by pressing N
  • Loading branch information
jarun authored Feb 12, 2025
2 parents bc4307a + bc6d577 commit 38311cd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 27 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ PROMPT KEYS:
append, set, remove (all or specific) tags
search by taglist id(s) if records are omitted
n show next page of search results
N show previous page of search results
o id|range [...] browse bookmarks by indices and/or ranges
p id|range [...] print bookmarks by indices and/or ranges
w [editor|id] edit and add or update a bookmark
Expand Down
53 changes: 26 additions & 27 deletions buku
Original file line number Diff line number Diff line change
Expand Up @@ -3380,6 +3380,7 @@ PROMPT KEYS:
append, set, remove (all or specific) tags
search by taglist id(s) if records are omitted
n show next page of search results
N show previous page of search results
o id|range [...] browse bookmarks by indices and/or ranges
p id|range [...] print bookmarks by indices and/or ranges
w [editor|id] edit and add or update a bookmark
Expand Down Expand Up @@ -4649,7 +4650,7 @@ def prompt(obj, results, noninteractive=False, deep=False, listtags=False, sugge

new_results = bool(results)
nav = ''
cur_index = next_index = count = 0
cur_index = next_index = prev_index = 0

if listtags:
show_taglist(obj)
Expand All @@ -4661,34 +4662,32 @@ def prompt(obj, results, noninteractive=False, deep=False, listtags=False, sugge

if noninteractive:
try:
for row in results:
count += 1
print_single_rec(row, count, columns)
except Exception:
pass
for i, row in enumerate(results):
print_single_rec(row, i + 1, columns)
except Exception as e:
LOGERR(e)
return

skip_print = False
while True:
if (new_results or nav == 'n') and not skip_print:
count = next_index

if results:
total_results = len(results)
cur_index = next_index
if cur_index < total_results:
next_index = min(cur_index + num, total_results)
print()
for row in results[cur_index:next_index]:
count += 1
print_single_rec(row, count, columns)
print('%d-%d/%d' % (cur_index + 1, next_index, total_results))
else:
print('No more results')
new_results = False
else:
if (new_results or nav in ['n', 'N']) and not skip_print:
_total_results = len(results or [])
cur_index = next_index # used elsewhere as "most recent page start index"
if not results:
print('0 results')
new_results = False
elif cur_index >= _total_results and nav != 'N':
print('No more results')
new_results = False
else:
if nav == 'N':
cur_index = min(cur_index, prev_index)
prev_index = max(0, cur_index - num)
next_index = min(cur_index + num, _total_results)
print()
for i in range(cur_index, next_index):
print_single_rec(results[i], i + 1, columns)
print('%d-%d/%d' % (cur_index + 1, next_index, _total_results))
skip_print = False

try:
Expand All @@ -4703,7 +4702,7 @@ def prompt(obj, results, noninteractive=False, deep=False, listtags=False, sugge
return

# show the next set of results from previous search
if nav == 'n':
if nav in ('n', 'N'):
continue

if (m := re.match(r'^R(?: (-)?([0-9]+))?$', nav.rstrip())) and (n := int(m[2] or 1)) > 0:
Expand Down Expand Up @@ -4844,7 +4843,7 @@ def prompt(obj, results, noninteractive=False, deep=False, listtags=False, sugge
# Copy URL to clipboard
if nav.startswith('c ') and nav[2:].isdigit():
index = int(nav[2:]) - 1
if index < 0 or index >= count:
if index < 0 or index >= next_index:
print('No matching index')
continue
copy_to_clipboard(content=results[index + cur_index][1].encode('utf-8'))
Expand Down Expand Up @@ -4873,7 +4872,7 @@ def prompt(obj, results, noninteractive=False, deep=False, listtags=False, sugge
for nav in nav.split():
if is_int(nav):
index = int(nav) - 1
if index < 0 or index >= count:
if index < 0 or index >= next_index:
print('No matching index %s' % nav)
continue
browse(results[index][1])
Expand All @@ -4884,7 +4883,7 @@ def prompt(obj, results, noninteractive=False, deep=False, listtags=False, sugge
vals[0], vals[-1] = vals[-1], vals[0]

for _id in range(vals[0]-1, vals[-1]):
if 0 <= _id < count:
if 0 <= _id < next_index:
browse(results[_id][1])
else:
print('No matching index %d' % (_id + 1))
Expand Down
6 changes: 6 additions & 0 deletions buku.1
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,12 @@ Search bookmarks by a tag. List all tags alphabetically, if no arguments.
.BI "g" " taglist id|range [...] [>>|>|<<] [record id|range ...]"
Append, set, remove specific or all tags by indices and/or ranges to bookmark indices and/or ranges (see \fBEXAMPLES\fR section below). Search by space-separated taglist id(s) and/or range if records are omitted.
.TP
.BI "n"
Display the next page of search results.
.TP
.BI "N"
Display the previous page of search results.
.TP
.BI "o" " id|range [...]"
Browse bookmarks by indices and/or ranges.
.TP
Expand Down
1 change: 1 addition & 0 deletions tests/test_bukuDb.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sqlite3
import sys
import unittest
from genericpath import exists
from tempfile import NamedTemporaryFile, TemporaryDirectory
from random import shuffle
from unittest import mock
Expand Down

0 comments on commit 38311cd

Please sign in to comment.