From 37aab9b79bf1c84749cfd9425514bbbd67c53ceb Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 13 Oct 2026 20:35:01 +0200 Subject: [PATCH] fix: only overwrite oldline when going up from last history position --- src/msh.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/msh.c b/src/msh.c index 0970b3b..2138962 100644 --- a/src/msh.c +++ b/src/msh.c @@ -347,13 +347,22 @@ int main(int argc, char** argv) { } } else if (ansi == ANSI_CURSOR_UP || ansi == ANSI_CURSOR_DOWN) { - int changed = 1; + int load_from_hist = 1; if (ansi == ANSI_CURSOR_UP && historyoffset < historyi) { historyoffset++; + + if (historyoffset == 1) { + if (oldline != NULL) { + free(oldline); + } + + oldline = line; + } } else if (ansi == ANSI_CURSOR_DOWN && historyoffset > 0) { historyoffset--; + // We just went back to the last line, load from oldline if (historyoffset == 0) { free(line); line = oldline; @@ -364,20 +373,15 @@ int main(int argc, char** argv) { oldline = NULL; // Don't look up from history - changed = 0; + load_from_hist = 0; } } else { - // We didn't actually change anything - changed = 0; + // We didn't change our line (already at top or bottom) + load_from_hist = 0; } - if (changed == 1) { - if (oldline != NULL) { - free(oldline); - } - - oldline = line; + if (load_from_hist == 1) { char* historyline = history[historyi - (historyoffset)]; size_t historylinelen = strlen(historyline); line = malloc(historylinelen + 1);