@@ -17,6 +17,7 @@ class HistoryItem
17
17
public List < EditItem > _edits ;
18
18
public int _undoEditIndex ;
19
19
public bool _saved ;
20
+ public bool _fromDifferentLiveSession ;
20
21
}
21
22
22
23
// History state
@@ -39,7 +40,7 @@ class HistoryItem
39
40
private const string _failedForwardISearchPrompt = "failed-fwd-i-search: " ;
40
41
private const string _failedBackwardISearchPrompt = "failed-bck-i-search: " ;
41
42
42
- private string MaybeAddToHistory ( string result , List < EditItem > edits , int undoEditIndex , bool readingHistoryFile )
43
+ private string MaybeAddToHistory ( string result , List < EditItem > edits , int undoEditIndex , bool readingHistoryFile , bool fromDifferentSession )
43
44
{
44
45
bool addToHistory = ! string . IsNullOrWhiteSpace ( result ) && ( ( Options . AddToHistoryHandler == null ) || Options . AddToHistoryHandler ( result ) ) ;
45
46
if ( addToHistory )
@@ -50,6 +51,7 @@ private string MaybeAddToHistory(string result, List<EditItem> edits, int undoEd
50
51
_edits = edits ,
51
52
_undoEditIndex = undoEditIndex ,
52
53
_saved = readingHistoryFile ,
54
+ _fromDifferentLiveSession = fromDifferentSession ,
53
55
} ) ;
54
56
_currentHistoryIndex = _history . Count ;
55
57
@@ -168,7 +170,7 @@ private void MaybeReadHistoryFile()
168
170
historyLines . Add ( sr . ReadLine ( ) ) ;
169
171
}
170
172
}
171
- UpdateHistoryFromFile ( historyLines ) ;
173
+ UpdateHistoryFromFile ( historyLines , fromDifferentSession : true ) ;
172
174
173
175
_historyFileLastSavedSize = fileInfo . Length ;
174
176
}
@@ -197,7 +199,7 @@ private void ReadHistoryFile()
197
199
}
198
200
199
201
var historyLines = File . ReadAllLines ( Options . HistorySavePath ) ;
200
- UpdateHistoryFromFile ( historyLines ) ;
202
+ UpdateHistoryFromFile ( historyLines , fromDifferentSession : false ) ;
201
203
var fileInfo = new FileInfo ( Options . HistorySavePath ) ;
202
204
_historyFileLastSavedSize = fileInfo . Length ;
203
205
}
@@ -208,7 +210,7 @@ private void ReadHistoryFile()
208
210
}
209
211
}
210
212
211
- void UpdateHistoryFromFile ( IEnumerable < string > historyLines )
213
+ void UpdateHistoryFromFile ( IEnumerable < string > historyLines , bool fromDifferentSession )
212
214
{
213
215
var sb = new StringBuilder ( ) ;
214
216
foreach ( var line in historyLines )
@@ -223,13 +225,13 @@ void UpdateHistoryFromFile(IEnumerable<string> historyLines)
223
225
sb . Append ( line ) ;
224
226
var l = sb . ToString ( ) ;
225
227
var editItems = new List < EditItem > { EditItemInsertString . Create ( l , 0 ) } ;
226
- MaybeAddToHistory ( l , editItems , 1 , readingHistoryFile : true ) ;
228
+ MaybeAddToHistory ( l , editItems , 1 , /* readingHistoryFile*/ true , fromDifferentSession ) ;
227
229
sb . Clear ( ) ;
228
230
}
229
231
else
230
232
{
231
233
var editItems = new List < EditItem > { EditItemInsertString . Create ( line , 0 ) } ;
232
- MaybeAddToHistory ( line , editItems , 1 , readingHistoryFile : true ) ;
234
+ MaybeAddToHistory ( line , editItems , 1 , /* readingHistoryFile*/ true , fromDifferentSession ) ;
233
235
}
234
236
}
235
237
}
@@ -242,7 +244,7 @@ public static void AddToHistory(string command)
242
244
{
243
245
command = command . Replace ( "\r \n " , "\n " ) ;
244
246
var editItems = new List < EditItem > { EditItemInsertString . Create ( command , 0 ) } ;
245
- _singleton . MaybeAddToHistory ( command , editItems , 1 , readingHistoryFile : false ) ;
247
+ _singleton . MaybeAddToHistory ( command , editItems , 1 , readingHistoryFile : false , fromDifferentSession : false ) ;
246
248
}
247
249
248
250
/// <summary>
@@ -307,38 +309,45 @@ private void HistoryRecall(int direction)
307
309
return ;
308
310
}
309
311
310
- int newHistoryIndex ;
311
- if ( Options . HistoryNoDuplicates )
312
+ if ( Options . HistoryNoDuplicates && _recallHistoryCommandCount == 0 )
313
+ {
314
+ _hashedHistory = new Dictionary < string , int > ( ) ;
315
+ }
316
+
317
+ int count = Math . Abs ( direction ) ;
318
+ direction = direction < 0 ? - 1 : + 1 ;
319
+ int newHistoryIndex = _currentHistoryIndex ;
320
+ while ( count > 0 )
312
321
{
313
- if ( _recallHistoryCommandCount == 0 )
322
+ newHistoryIndex += direction ;
323
+ if ( newHistoryIndex < 0 || newHistoryIndex >= _history . Count )
314
324
{
315
- _hashedHistory = new Dictionary < string , int > ( ) ;
325
+ break ;
316
326
}
317
327
318
- newHistoryIndex = _currentHistoryIndex ;
319
- while ( true )
328
+ if ( _history [ newHistoryIndex ] . _fromDifferentLiveSession )
329
+ {
330
+ continue ;
331
+ }
332
+
333
+ if ( Options . HistoryNoDuplicates )
320
334
{
321
- newHistoryIndex = newHistoryIndex + direction ;
322
- if ( newHistoryIndex < 0 || newHistoryIndex >= _history . Count )
323
- {
324
- break ;
325
- }
326
335
var line = _history [ newHistoryIndex ] . _line ;
327
336
int index ;
328
337
if ( ! _hashedHistory . TryGetValue ( line , out index ) )
329
338
{
330
339
_hashedHistory . Add ( line , newHistoryIndex ) ;
331
- break ;
340
+ -- count ;
332
341
}
333
- if ( index == newHistoryIndex )
342
+ else if ( newHistoryIndex == index )
334
343
{
335
- break ;
344
+ -- count ;
336
345
}
337
346
}
338
- }
339
- else
340
- {
341
- newHistoryIndex = _currentHistoryIndex + direction ;
347
+ else
348
+ {
349
+ -- count ;
350
+ }
342
351
}
343
352
_recallHistoryCommandCount += 1 ;
344
353
if ( newHistoryIndex >= 0 && newHistoryIndex <= _history . Count )
@@ -378,6 +387,14 @@ public static void NextHistory(ConsoleKeyInfo? key = null, object arg = null)
378
387
379
388
private void HistorySearch ( int direction )
380
389
{
390
+ if ( _current == 0 )
391
+ {
392
+ // If we aren't actually searching, use HistoryRecall
393
+ // because it ignores command lines from other running sessions.
394
+ HistoryRecall ( direction ) ;
395
+ return ;
396
+ }
397
+
381
398
if ( _searchHistoryCommandCount == 0 )
382
399
{
383
400
if ( LineIsMultiLine ( ) )
@@ -396,28 +413,45 @@ private void HistorySearch(int direction)
396
413
}
397
414
_searchHistoryCommandCount += 1 ;
398
415
399
- for ( int i = _currentHistoryIndex + direction ; i >= 0 && i <= _history . Count ; i += direction )
416
+ int count = Math . Abs ( direction ) ;
417
+ direction = direction < 0 ? - 1 : + 1 ;
418
+ int newHistoryIndex = _currentHistoryIndex ;
419
+ while ( count > 0 )
400
420
{
401
- var line = i == _history . Count ? _savedCurrentLine . _line : _history [ i ] . _line ;
421
+ newHistoryIndex += direction ;
422
+ if ( newHistoryIndex < 0 || newHistoryIndex >= _history . Count )
423
+ {
424
+ break ;
425
+ }
426
+
427
+ var line = newHistoryIndex == _history . Count ? _savedCurrentLine . _line : _history [ newHistoryIndex ] . _line ;
402
428
if ( line . StartsWith ( _searchHistoryPrefix , Options . HistoryStringComparison ) )
403
429
{
404
430
if ( Options . HistoryNoDuplicates )
405
431
{
406
432
int index ;
407
433
if ( ! _hashedHistory . TryGetValue ( line , out index ) )
408
434
{
409
- _hashedHistory . Add ( line , i ) ;
435
+ _hashedHistory . Add ( line , newHistoryIndex ) ;
436
+ -- count ;
410
437
}
411
- else if ( index != i )
438
+ else if ( index == newHistoryIndex )
412
439
{
413
- continue ;
440
+ -- count ;
414
441
}
415
442
}
416
- _currentHistoryIndex = i ;
417
- UpdateFromHistory ( moveCursor : true ) ;
418
- break ;
443
+ else
444
+ {
445
+ -- count ;
446
+ }
419
447
}
420
448
}
449
+
450
+ if ( newHistoryIndex >= 0 && newHistoryIndex <= _history . Count )
451
+ {
452
+ _currentHistoryIndex = newHistoryIndex ;
453
+ UpdateFromHistory ( moveCursor : true ) ;
454
+ }
421
455
}
422
456
423
457
/// <summary>
0 commit comments