about summary refs log tree commit diff
path: root/saneterm
diff options
context:
space:
mode:
authorSören Tempel <soeren+git@soeren-tempel.net>2021-05-24 05:28:13 +0200
committerSören Tempel <soeren+git@soeren-tempel.net>2021-05-24 05:28:13 +0200
commita777e4298111385c470b13351c6964393360385c (patch)
tree8b555aadc2b1434c12cd98caacd580b62742fd94 /saneterm
parent9ab521202855bcf4ea82ae37d89a9f1eae06f43d (diff)
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.
Diffstat (limited to 'saneterm')
-rw-r--r--saneterm/history.py12
-rw-r--r--saneterm/terminal.py7
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)