Skip to content

Commit e2311b0

Browse files
committed
Support Cancel/Abort DigitArgument
There was an exception with input like: a Alt+1 Ctrl+C CancelLine was clearing the buffer, but didn't reset _current. That fixes the exception, but it still clears the input line when called to terminate DigitArgument, so instead we do something similar to how interactive history search is terminated. Fixes #184
1 parent 2edaf0c commit e2311b0

File tree

3 files changed

+31
-20
lines changed

3 files changed

+31
-20
lines changed

PSReadLine/BasicEditing.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public static void CancelLine(ConsoleKeyInfo? key = null, object arg = null)
9999
var y = coordinates.Y + 1;
100100
_singleton.PlaceCursor(0, ref y);
101101
_singleton._buffer.Clear(); // Clear so we don't actually run the input
102+
_singleton._current = 0; // If Render is called, _current must be correct.
102103
_singleton._currentHistoryIndex = _singleton._history.Count;
103104
_singleton._inputAccepted = true;
104105
}

PSReadLine/Changes.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Bug fixes:
1616
* ClearScreen now just clears the visible screen, not the entire buffer.
1717
* Fix exception on error input during rendering after certain keywords like process, begin, or end.
1818
* Fix exception after undo from menu completion
19+
* Support CancelLine (Ctrl+C) and Abort (Ctrl+G in emacs) to cancel DigitArgument
1920

2021
New functions:
2122
* ValidateAndAcceptLine

PSReadLine/ReadLine.cs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -677,34 +677,43 @@ public static void DigitArgument(ConsoleKeyInfo? key = null, object arg = null)
677677
{
678678
var nextKey = ReadKey();
679679
KeyHandler handler;
680-
if (_singleton._dispatchTable.TryGetValue(nextKey, out handler) && handler.Action == DigitArgument)
680+
if (_singleton._dispatchTable.TryGetValue(nextKey, out handler))
681681
{
682-
if (nextKey.KeyChar == '-')
682+
if (handler.Action == DigitArgument)
683683
{
684-
if (argBuffer[0] == '-')
684+
if (nextKey.KeyChar == '-')
685685
{
686-
argBuffer.Remove(0, 1);
686+
if (argBuffer[0] == '-')
687+
{
688+
argBuffer.Remove(0, 1);
689+
}
690+
else
691+
{
692+
argBuffer.Insert(0, '-');
693+
}
694+
_singleton.Render(); // Render prompt
695+
continue;
687696
}
688-
else
697+
698+
if (nextKey.KeyChar >= '0' && nextKey.KeyChar <= '9')
689699
{
690-
argBuffer.Insert(0, '-');
700+
if (!sawDigit && argBuffer.Length > 0)
701+
{
702+
// Buffer is either '-1' or '1' from one or more Alt+- keys
703+
// but no digits yet. Remove the '1'.
704+
argBuffer.Length -= 1;
705+
}
706+
sawDigit = true;
707+
argBuffer.Append(nextKey.KeyChar);
708+
_singleton.Render(); // Render prompt
709+
continue;
691710
}
692-
_singleton.Render(); // Render prompt
693-
continue;
694711
}
695-
696-
if (nextKey.KeyChar >= '0' && nextKey.KeyChar <= '9')
712+
else if (handler.Action == Abort ||
713+
handler.Action == CancelLine ||
714+
handler.Action == CopyOrCancelLine)
697715
{
698-
if (!sawDigit && argBuffer.Length > 0)
699-
{
700-
// Buffer is either '-1' or '1' from one or more Alt+- keys
701-
// but no digits yet. Remove the '1'.
702-
argBuffer.Length -= 1;
703-
}
704-
sawDigit = true;
705-
argBuffer.Append(nextKey.KeyChar);
706-
_singleton.Render(); // Render prompt
707-
continue;
716+
break;
708717
}
709718
}
710719

0 commit comments

Comments
 (0)