fix: only overwrite oldline when going up from last history position

This commit is contained in:
2026-10-13 20:35:01 +02:00
parent ea1d6f617a
commit 37aab9b79b

View File

@@ -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);