Compare commits

...

2 Commits

Author SHA1 Message Date
Lionel Dricot
06b2e178fb history is now a list 2022-01-26 19:35:05 +01:00
Lionel Dricot
41f0900373 implementing the archive list 2022-01-26 18:52:38 +01:00

View File

@ -743,7 +743,6 @@ class GeminiClient(cmd.Cmd):
self.offline_prompt = "\x1b[38;5;76m" + "OFF" + "\x1b[38;5;255m" + "> " + "\x1b[0m"
self.prompt = self.no_cert_prompt
self.gi = None
self.history = []
self.hist_index = 0
self.idx_filename = ""
self.index = []
@ -778,7 +777,9 @@ class GeminiClient(cmd.Cmd):
"gopher_proxy" : None,
"tls_mode" : "tofu",
"http_proxy": None,
"https_everywhere": True
"https_everywhere": True,
"archives_size" : 100,
"history_size" : 100
}
self.log = {
@ -1391,12 +1392,17 @@ you'll be able to transparently follow links to Gopherspace!""")
print(self._format_geminiitem(n+offset+1, gi, url))
def _update_history(self, gi):
# Don't duplicate
if self.history and self.history[self.hist_index] == gi:
histlist = self.get_list("history")
links = self.list_get_links("history")
# avoid duplicate
length = len(links)
if length > self.options["history_size"]:
length = self.options["history_size"]
if length > 0 and links[self.hist_index] == gi:
return
self.history = self.history[0:self.hist_index+1]
self.history.append(gi)
self.hist_index = len(self.history) - 1
self.list_add_top("history",limit=self.options["history_size"],truncate_lines=self.hist_index)
self.hist_index = 0
def _log_visit(self, gi, address, size):
if not address:
@ -1750,18 +1756,22 @@ Use with "raw" to copy the content as seen in your terminal (not gemtext)"""
def do_back(self, *args):
"""Go back to the previous gemini item."""
if not self.history or self.hist_index == 0:
histfile = self.get_list("history")
links = self.list_get_links("history")
if self.hist_index >= len(links) -1:
return
self.hist_index -= 1
gi = self.history[self.hist_index]
self.hist_index += 1
gi = links[self.hist_index]
self._go_to_gi(gi, update_hist=False)
def do_forward(self, *args):
"""Go forward to the next gemini item."""
if not self.history or self.hist_index == len(self.history) - 1:
histfile = self.get_list("history")
links = self.list_get_links("history")
if self.hist_index <= 0:
return
self.hist_index += 1
gi = self.history[self.hist_index]
self.hist_index -= 1
gi = links[self.hist_index]
self._go_to_gi(gi, update_hist=False)
def do_next(self, *args):
@ -1869,9 +1879,7 @@ Use 'ls -l' to see URLs."""
def do_history(self, *args):
"""Display history."""
self.lookup = self.history
self._show_lookup(url=True)
self.page_index = 0
self.list_show("history")
def do_find(self, searchterm):
"""Find in the list of links (case insensitive)."""
@ -2064,6 +2072,18 @@ Bookmarks are stored using the 'add' command."""
else:
self.list_show("bookmarks")
def do_archive(self,args):
"""Archive current page by removing it from every list and adding it to
archives, which is a special historical list limited in size. It is similar to `move archives`."""
for li in self.list_lists():
if li not in ["archives", "history"]:
deleted = self.list_rm_url(self.gi.url,li)
if deleted:
print("Removed from %s"%li)
self.list_add_top("archives",limit=self.options["archives_size"])
print("%s was added to your archives (size limited to %s last items)"\
%(self.gi.url,self.options["archives_size"]))
def list_add_line(self,list,gi=None,verbose=True):
list_path = self.list_path(list)
if not list_path:
@ -2088,6 +2108,32 @@ Bookmarks are stored using the 'add' command."""
if verbose:
print("%s added to %s" %(gi.url,list))
return True
def list_add_top(self,list,limit=0,truncate_lines=0):
stri = self.gi.to_map_line().strip("\n")
if list == "archives":
stri += ", archived on "
elif list == "history":
stri += ", visited on "
else:
stri += ", added to %s on "%list
stri += time.ctime() + "\n"
list_path = self.get_list(list)
with open(list_path,"r") as l_file:
lines = l_file.readlines()
l_file.close()
with open(list_path,"w") as l_file:
l_file.write(stri)
counter = 0
to_truncate = truncate_lines
for l in lines:
if to_truncate > 0:
to_truncate -= 1
elif limit == 0 or counter < limit:
l_file.write(l)
counter += 1
l_file.close()
# remove an url from a list.
# return True if the URL was removed
@ -2180,21 +2226,20 @@ With a major twist: current page will be removed from all other lists.
If current page was not in a list, this command is similar to `add LIST`."""
if not arg:
print("LIST argument is required as the target for your move")
elif arg[0] == "archives":
self.do_archive()
else:
args = arg.split()
list_path = self.list_path(args[0])
if not list_path:
print("%s is not a list, aborting the move" %args[0])
else:
listdir = os.path.join(_DATA_DIR,"lists")
lists = os.listdir(listdir)
lists = self.list_lists()
for l in lists:
# remove the .gmi at the end of the filename (4char)
lname = l[:-4]
if lname != args[0]:
isremoved = self.list_rm_url(self.gi.url,lname)
if l != args[0] and l not in ["archives", "history"]:
isremoved = self.list_rm_url(self.gi.url,l)
if isremoved:
print("Removed from %s"%lname)
print("Removed from %s"%l)
self.list_add_line(args[0])
def list_lists(self):