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) { } 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) { if (ansi == ANSI_CURSOR_UP && historyoffset < historyi) {
historyoffset++; historyoffset++;
if (historyoffset == 1) {
if (oldline != NULL) {
free(oldline);
}
oldline = line;
}
} else if (ansi == ANSI_CURSOR_DOWN && historyoffset > 0) { } else if (ansi == ANSI_CURSOR_DOWN && historyoffset > 0) {
historyoffset--; historyoffset--;
// We just went back to the last line, load from oldline
if (historyoffset == 0) { if (historyoffset == 0) {
free(line); free(line);
line = oldline; line = oldline;
@@ -364,20 +373,15 @@ int main(int argc, char** argv) {
oldline = NULL; oldline = NULL;
// Don't look up from history // Don't look up from history
changed = 0; load_from_hist = 0;
} }
} else { } else {
// We didn't actually change anything // We didn't change our line (already at top or bottom)
changed = 0; load_from_hist = 0;
} }
if (changed == 1) { if (load_from_hist == 1) {
if (oldline != NULL) {
free(oldline);
}
oldline = line;
char* historyline = history[historyi - (historyoffset)]; char* historyline = history[historyi - (historyoffset)];
size_t historylinelen = strlen(historyline); size_t historylinelen = strlen(historyline);
line = malloc(historylinelen + 1); line = malloc(historylinelen + 1);