From a777e4298111385c470b13351c6964393360385c Mon Sep 17 00:00:00 2001 From: Sören Tempel Date: Mon, 24 May 2021 05:28:13 +0200 Subject: Make sure history does not go out-of-bounds Return None if no history entry was found and reset the offset if so. Without this patch the offset would be continuously incremented/decrement. --- saneterm/history.py | 12 ++++++++---- saneterm/terminal.py | 7 ++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/saneterm/history.py b/saneterm/history.py index 1226446..2b99af8 100644 --- a/saneterm/history.py +++ b/saneterm/history.py @@ -53,13 +53,17 @@ class History(): self.__con.commit() - def get_entry(self, fd, relidx): + def get_entry(self, fd, offset): exe = self.__get_exec(fd) + # Select an entry by the given offset. If the offset exceeds the + # amount of available entries, select nothing and return None. self.__cur.execute(""" - SELECT entry FROM history WHERE exe=:exe LIMIT 1 OFFSET - (( SELECT count(*) FROM history WHERE exe=:exe ) - :relidx); - """, {"exe": exe, "relidx": relidx}) + SELECT entry FROM history WHERE exe=:exe AND + ( SELECT count(*) FROM history WHERE exe=:exe ) >= :offset + LIMIT 1 OFFSET + (( SELECT count(*) FROM history WHERE exe=:exe ) - :offset); + """, {"exe": exe, "offset": offset}) res = self.__cur.fetchone() if res is None: diff --git a/saneterm/terminal.py b/saneterm/terminal.py index e31a8ce..8bd3120 100644 --- a/saneterm/terminal.py +++ b/saneterm/terminal.py @@ -147,10 +147,15 @@ class Terminal(Gtk.Window): self.hist_index = 0 def history(self, termview, idx): + # Backup index and restore it if no entry with new index exists. + backup_index = self.hist_index + self.hist_index += idx entry = self.hist.get_entry(self.pty.master, self.hist_index) - if not entry is None: + if entry is None: + self.hist_index = backup_index + else: self.termview.emit("kill-after-output") self.termview.emit("insert-at-cursor", entry) -- cgit 1.4.1