From f7be01b88a14ccc3d406b13068e92eb9f33baa82 Mon Sep 17 00:00:00 2001 From: sterni Date: Tue, 25 May 2021 06:00:29 +0200 Subject: Clear the line if Down is pressed at the most recent history entry This mimicks the behavior familiar from most shells. We also make sure that this doesn't happen when we hit the oldest history entry -- in that case we will just do nothing if Up is pressed. To implement this correctly we need to distinguish between a position in the history and no position in the history. We encode the latter using `None` and just call `reset_history_index()` in case we encounter it to reacquire a point of reference. --- saneterm/terminal.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/saneterm/terminal.py b/saneterm/terminal.py index c3ae3cf..7ad301a 100644 --- a/saneterm/terminal.py +++ b/saneterm/terminal.py @@ -161,17 +161,28 @@ class Terminal(Gtk.Window): self.hist_index = -1 def history(self, termview, idx): - # Backup index and restore it if no entry with new index exists. + if self.hist_index is None: + self.reset_history_index() + + # Backup index and restore it if we hit the beginning of the history backup_index = self.hist_index self.hist_index += idx entry = self.hist.get_entry(self.pty.master, self.hist_index) if entry is None: - self.hist_index = backup_index - else: - self.termview.emit("kill-after-output") - self.termview.emit("insert-at-cursor", entry) + if idx > 0: + # we are going back in time. if there are no older + # entries, restore history index and bail out. + self.hist_index = backup_index + return + else: + # if we arrive at the present, just clear the line + self.hist_index = None + entry = "" + + self.termview.emit("kill-after-output") + self.termview.emit("insert-at-cursor", entry) def autoscroll(self, widget, rect): if not self.config['autoscroll']: -- cgit 1.4.1