@@ -135,6 +135,9 @@ static KeyHandler MakeKeyHandler(Action<ConsoleKeyInfo?, object> action, string
135
135
private int _currentHistoryIndex ;
136
136
private int _searchHistoryCommandCount ;
137
137
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 ;
138
141
139
142
// Yank/Kill state
140
143
private readonly List < string > _killRing ;
@@ -489,6 +492,7 @@ private PSConsoleReadLine()
489
492
_chordDispatchTable = new Dictionary < ConsoleKeyInfo , Dictionary < ConsoleKeyInfo , KeyHandler > > ( ) ;
490
493
491
494
_buffer = new StringBuilder ( ) ;
495
+ _savedCurrentLine = new HistoryItem { _buffer = new StringBuilder ( ) } ;
492
496
493
497
_tokenForegroundColors = new ConsoleColor [ ( int ) TokenClassification . Member + 1 ] ;
494
498
_tokenBackgroundColors = new ConsoleColor [ _tokenForegroundColors . Length ] ;
@@ -856,20 +860,33 @@ public static void ClearHistory()
856
860
857
861
private void UpdateFromHistory ( bool moveCursor )
858
862
{
863
+ var buffer = ( _currentHistoryIndex == _history . Count )
864
+ ? _savedCurrentLine . _buffer
865
+ : _history [ _currentHistoryIndex ] . _buffer ;
859
866
_buffer . Clear ( ) ;
860
- _buffer . Append ( _history [ _currentHistoryIndex ] . _buffer ) ;
867
+ _buffer . Append ( buffer ) ;
861
868
if ( moveCursor )
862
869
{
863
870
_current = _buffer . Length ;
864
871
}
865
872
Render ( ) ;
866
873
}
867
874
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
+
868
884
/// <summary>
869
885
/// Replace the current input with the 'previous' item from PSReadline history.
870
886
/// </summary>
871
887
public static void PreviousHistory ( ConsoleKeyInfo ? key = null , object arg = null )
872
888
{
889
+ _singleton . SaveCurrentLine ( ) ;
873
890
if ( _singleton . _currentHistoryIndex > 0 )
874
891
{
875
892
_singleton . _currentHistoryIndex -= 1 ;
@@ -882,7 +899,8 @@ public static void PreviousHistory(ConsoleKeyInfo? key = null, object arg = null
882
899
/// </summary>
883
900
public static void NextHistory ( ConsoleKeyInfo ? key = null , object arg = null )
884
901
{
885
- if ( _singleton . _currentHistoryIndex < ( _singleton . _history . Count - 1 ) )
902
+ _singleton . SaveCurrentLine ( ) ;
903
+ if ( _singleton . _currentHistoryIndex < _singleton . _history . Count )
886
904
{
887
905
_singleton . _currentHistoryIndex += 1 ;
888
906
_singleton . UpdateFromHistory ( moveCursor : true ) ;
@@ -915,6 +933,7 @@ private void HistorySearch(bool backward)
915
933
/// </summary>
916
934
public static void HistorySearchBackward ( ConsoleKeyInfo ? key = null , object arg = null )
917
935
{
936
+ _singleton . SaveCurrentLine ( ) ;
918
937
_singleton . HistorySearch ( backward : true ) ;
919
938
}
920
939
@@ -924,6 +943,7 @@ public static void HistorySearchBackward(ConsoleKeyInfo? key = null, object arg
924
943
/// </summary>
925
944
public static void HistorySearchForward ( ConsoleKeyInfo ? key = null , object arg = null )
926
945
{
946
+ _singleton . SaveCurrentLine ( ) ;
927
947
_singleton . HistorySearch ( backward : false ) ;
928
948
}
929
949
0 commit comments