commit c934237cbe3af4bc328ce156747d4bd756993927
parent d5831a131dde7fa59a2b868fbf064e0b07f1e4a9
Author: Solderpunk <solderpunk@sdf.org>
Date: Tue, 24 Mar 2020 20:41:37 +0100
Enhance bookmarks command to accept a numeric index. Closes #4.
Diffstat:
M | av98.py | | | 32 | +++++++++++++++++++++----------- |
1 file changed, 21 insertions(+), 11 deletions(-)
diff --git a/av98.py b/av98.py
@@ -475,7 +475,7 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
self._debug("Using handler: %s" % cmd_str)
return cmd_str
- def _handle_index(self, body, menu_gi):
+ def _handle_index(self, body, menu_gi, display=True):
self.index = []
preformatted = False
if self.idx_filename:
@@ -515,8 +515,9 @@ Slow internet connection? Use 'set timeout' to be more patient.""")
self.page_index = 0
self.index_index = -1
- cmd_str = _MIME_HANDLERS["text/plain"]
- subprocess.call(shlex.split(cmd_str % self.idx_filename))
+ if display:
+ cmd_str = _MIME_HANDLERS["text/plain"]
+ subprocess.call(shlex.split(cmd_str % self.idx_filename))
def _format_geminiitem(self, index, gi, url=False):
line = "[%d] %s" % (index, gi.name or gi.url)
@@ -922,17 +923,26 @@ Optionally, specify the new name for the bookmark."""
with open(os.path.expanduser("~/.av98-bookmarks.txt"), "a") as fp:
fp.write(self.gi.to_map_line(line))
- def do_bookmarks(self, *args):
- """Show the current bookmarks menu.
-Bookmarks are stored in the ~/.av98-bookmarks.txt file."""
+ def do_bookmarks(self, line):
+ """Show or access the bookmarks menu.
+'bookmarks' shows all bookmarks.
+'bookmarks n' navigates immediately to item n in the bookmark menu.
+Bookmarks are stored in the ~/.av98-bookmarks.txt file using the 'add' command."""
bm_file = os.path.expanduser("~/.av98-bookmarks.txt")
if not os.path.exists(bm_file):
print("You need to 'add' some bookmarks, first!")
- else:
- with open(bm_file, "r") as fp:
- body = fp.read()
- gi = GeminiItem("localhost/" + bm_file)
- self._handle_index(body, gi)
+ return
+ args = line.strip()
+ if len(args.split()) > 1 or (args and not args.isnumeric()):
+ print("bookmarks command takes a single integer argument!")
+ return
+ with open(bm_file, "r") as fp:
+ body = fp.read()
+ gi = GeminiItem("localhost/" + bm_file)
+ self._handle_index(body, gi, display = not args)
+ if args:
+ # Use argument as a numeric index
+ self.default(line)
### Help
def do_help(self, arg):