Skip to content

Commit 6b2acc9

Browse files
committed
Save current line for recall during history commands
1 parent c9f044e commit 6b2acc9

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

PSReadLine/ReadLine.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ static KeyHandler MakeKeyHandler(Action<ConsoleKeyInfo?, object> action, string
135135
private int _currentHistoryIndex;
136136
private int _searchHistoryCommandCount;
137137
private string _searchHistoryPrefix;
138+
// When cycling through history, the current line (not yet added to history)
139+
// is saved here so it can be restored.
140+
private HistoryItem _savedCurrentLine;
138141

139142
// Yank/Kill state
140143
private readonly List<string> _killRing;
@@ -489,6 +492,7 @@ private PSConsoleReadLine()
489492
_chordDispatchTable = new Dictionary<ConsoleKeyInfo, Dictionary<ConsoleKeyInfo, KeyHandler>>();
490493

491494
_buffer = new StringBuilder();
495+
_savedCurrentLine = new HistoryItem {_buffer = new StringBuilder()};
492496

493497
_tokenForegroundColors = new ConsoleColor[(int)TokenClassification.Member + 1];
494498
_tokenBackgroundColors = new ConsoleColor[_tokenForegroundColors.Length];
@@ -856,20 +860,33 @@ public static void ClearHistory()
856860

857861
private void UpdateFromHistory(bool moveCursor)
858862
{
863+
var buffer = (_currentHistoryIndex == _history.Count)
864+
? _savedCurrentLine._buffer
865+
: _history[_currentHistoryIndex]._buffer;
859866
_buffer.Clear();
860-
_buffer.Append(_history[_currentHistoryIndex]._buffer);
867+
_buffer.Append(buffer);
861868
if (moveCursor)
862869
{
863870
_current = _buffer.Length;
864871
}
865872
Render();
866873
}
867874

875+
private void SaveCurrentLine()
876+
{
877+
if (_singleton._currentHistoryIndex == _history.Count)
878+
{
879+
_savedCurrentLine._buffer.Clear();
880+
_savedCurrentLine._buffer.Append(_buffer.ToString());
881+
}
882+
}
883+
868884
/// <summary>
869885
/// Replace the current input with the 'previous' item from PSReadline history.
870886
/// </summary>
871887
public static void PreviousHistory(ConsoleKeyInfo? key = null, object arg = null)
872888
{
889+
_singleton.SaveCurrentLine();
873890
if (_singleton._currentHistoryIndex > 0)
874891
{
875892
_singleton._currentHistoryIndex -= 1;
@@ -882,7 +899,8 @@ public static void PreviousHistory(ConsoleKeyInfo? key = null, object arg = null
882899
/// </summary>
883900
public static void NextHistory(ConsoleKeyInfo? key = null, object arg = null)
884901
{
885-
if (_singleton._currentHistoryIndex < (_singleton._history.Count - 1))
902+
_singleton.SaveCurrentLine();
903+
if (_singleton._currentHistoryIndex < _singleton._history.Count)
886904
{
887905
_singleton._currentHistoryIndex += 1;
888906
_singleton.UpdateFromHistory(moveCursor: true);
@@ -915,6 +933,7 @@ private void HistorySearch(bool backward)
915933
/// </summary>
916934
public static void HistorySearchBackward(ConsoleKeyInfo? key = null, object arg = null)
917935
{
936+
_singleton.SaveCurrentLine();
918937
_singleton.HistorySearch(backward: true);
919938
}
920939

@@ -924,6 +943,7 @@ public static void HistorySearchBackward(ConsoleKeyInfo? key = null, object arg
924943
/// </summary>
925944
public static void HistorySearchForward(ConsoleKeyInfo? key = null, object arg = null)
926945
{
946+
_singleton.SaveCurrentLine();
927947
_singleton.HistorySearch(backward: false);
928948
}
929949

0 commit comments

Comments
 (0)