Skip to content

Commit 4cb0f6c

Browse files
committed
Handle history with safe render
1 parent e390233 commit 4cb0f6c

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

PSReadLine/History.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -846,6 +846,8 @@ private void UpdateFromHistory(HistoryMoveCursor moveCursor)
846846
}
847847
_buffer.Clear();
848848
_buffer.Append(line);
849+
// Deletes the whole line (from start of line)
850+
SafeRender("\x1b[K", 0);
849851

850852
switch (moveCursor)
851853
{
@@ -864,7 +866,8 @@ private void UpdateFromHistory(HistoryMoveCursor moveCursor)
864866
}
865867

866868
using var _ = _prediction.DisableScoped();
867-
Render();
869+
// Renders the line and re-adjusts the cursor to account for the above options
870+
SafeRender(line, cursorBefore: null, cursorAfter: _current);
868871
}
869872

870873
private void SaveCurrentLine()

PSReadLine/Render.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ private void RenderWithPredictionQueryPaused()
218218
Render();
219219
}
220220

221-
private void SafeRender(string s, int? cursor = null)
221+
private void SafeRender(string s, int? cursorBefore = null, int? cursorAfter = null)
222222
{
223223
// Render as usual if we're not supporting a screen reader
224224
if (!_singleton.Options.ScreenReader)
@@ -227,13 +227,17 @@ private void SafeRender(string s, int? cursor = null)
227227
return;
228228
}
229229

230-
// Move the cursor if we have to
231-
if (cursor.HasValue)
232-
MoveCursor(cursor.Value);
230+
// Many commands will need the cursor set before writing
231+
if (cursorBefore.HasValue)
232+
MoveCursor(cursorBefore.Value);
233233

234234
// Directly write without re-rendering
235235
// This means using ANSI escapes for movement
236236
_console.Write(s);
237+
238+
// Some commands adjust the cursor after writing
239+
if (cursorAfter.HasValue)
240+
MoveCursor(cursorAfter.Value);
237241
}
238242

239243
private void Render()

0 commit comments

Comments
 (0)