-
Notifications
You must be signed in to change notification settings - Fork 5.1k
[Unix] Console.ReadKey rewrite #72193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
d929235
move WellKnownStrings to a separate file
adamsitnik c8887ec
move Database to separate file
adamsitnik cf77107
move factory logic to DatabaseFactory so the test project does not ne…
adamsitnik c6f3a60
move WellKnownNumbers to separate file
adamsitnik acd4f24
include Database in the test project
adamsitnik f5e3641
move TerminalFormatStrings to a separate file
adamsitnik d3cdb93
include TerminalFormatStrings in test project
adamsitnik 7ea9f50
fix existing tests
adamsitnik dc0af6b
move the key mapping logic to a separate class, do NOT change it
adamsitnik c927091
simple tests for ASCII characters
adamsitnik f30f02e
test cases for xterm and xterm-256color
adamsitnik 5ffdd67
add PuTTY and UXTern
adamsitnik 9230c81
add Windows Terminal data
adamsitnik 41a92bf
add more PuTTY test cases
adamsitnik af92ab3
handle empty encodings
adamsitnik 9a2ed71
make it possible to run the tests on Windows so I can use my favourit…
adamsitnik 69f12c9
add mappings for single characters
adamsitnik 5bbac1d
add mappings for 3 characters
adamsitnik a9db7db
fix test cases where pressing Ctrl/Alt/Shift + other key produces two…
adamsitnik c2e02d8
handle more complex sequences (with modifiers)
adamsitnik 620d1a0
handle edge cases properly
adamsitnik 20593ef
add tmux test data
adamsitnik c612f4f
add rxvt-unicode test data and implementation for rxvt modifiers
adamsitnik 3e62636
polishing
adamsitnik aa07430
switch to the new implementation and allow the users to differentiate…
adamsitnik 254c762
add way more test cases and fix identified issues
adamsitnik 4023725
polishing: reorder the tests
adamsitnik d65c6e7
fix compiler error (it's strange as I was not getting it when I was b…
adamsitnik 8cd9e3d
add more SCO mappings
adamsitnik 7fea037
Numeric Keypad
adamsitnik 16e983f
Merge remote-tracking branch 'upstream/main' into consoleReadKey
adamsitnik dd07418
add Tmux 256 color test cases (TERM=screen-256color)
adamsitnik 632267c
add two Numeric Keypad test cases: period & Enter
adamsitnik 69f6434
fix issue discovered by adding more tmux test cases: ^[OM should be m…
adamsitnik 9c352b4
address code review feedback: move TerminalFormatStrings classs out i…
adamsitnik f974992
address code review feedback: handle all variants of tmux when gettin…
adamsitnik b3366a8
address code review feedback: reduce number of out parameters
adamsitnik dc82fb5
fix TermInfo tests
adamsitnik 214da94
add .NET 6 compat switch
adamsitnik 7766ed3
Apply suggestions from code review
adamsitnik c8010c5
address code review feedback
adamsitnik c28d600
Merge branch 'main' into consoleReadKey
adamsitnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
326 changes: 21 additions & 305 deletions
326
src/libraries/System.Console/src/System/ConsolePal.Unix.cs
Large diffs are not rendered by default.
Oops, something went wrong.
392 changes: 392 additions & 0 deletions
392
src/libraries/System.Console/src/System/IO/KeyParser.cs
Large diffs are not rendered by default.
Oops, something went wrong.
180 changes: 180 additions & 0 deletions
180
src/libraries/System.Console/src/System/IO/Net6KeyParser.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace System.IO; | ||
|
||
internal static class Net6KeyParser | ||
adamsitnik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
internal static ConsoleKeyInfo Parse(char[] buffer, TerminalFormatStrings terminalFormatStrings, byte posixDisableValue, byte veraseCharacter, ref int startIndex, int endIndex) | ||
{ | ||
MapBufferToConsoleKey(buffer, terminalFormatStrings, posixDisableValue, veraseCharacter, out ConsoleKey key, | ||
out char ch, out bool isShift, out bool isAlt, out bool isCtrl, ref startIndex, endIndex); | ||
|
||
// Replace the '\n' char for Enter by '\r' to match Windows behavior. | ||
if (key == ConsoleKey.Enter && ch == '\n') | ||
{ | ||
ch = '\r'; | ||
} | ||
|
||
return new ConsoleKeyInfo(ch, key, isShift, isAlt, isCtrl); | ||
} | ||
|
||
private static bool MapBufferToConsoleKey(char[] buffer, TerminalFormatStrings terminalFormatStrings, byte posixDisableValue, byte veraseCharacter, | ||
out ConsoleKey key, out char ch, out bool isShift, out bool isAlt, out bool isCtrl, ref int startIndex, int endIndex) | ||
{ | ||
// Try to get the special key match from the TermInfo static information. | ||
if (TryGetSpecialConsoleKey(buffer, startIndex, endIndex, terminalFormatStrings, posixDisableValue, veraseCharacter, out ConsoleKeyInfo keyInfo, out int keyLength)) | ||
{ | ||
key = keyInfo.Key; | ||
isShift = (keyInfo.Modifiers & ConsoleModifiers.Shift) != 0; | ||
isAlt = (keyInfo.Modifiers & ConsoleModifiers.Alt) != 0; | ||
isCtrl = (keyInfo.Modifiers & ConsoleModifiers.Control) != 0; | ||
|
||
ch = ((keyLength == 1) ? buffer[startIndex] : '\0'); // ignore keyInfo.KeyChar | ||
startIndex += keyLength; | ||
return true; | ||
} | ||
|
||
// Check if we can match Esc + combination and guess if alt was pressed. | ||
if (buffer[startIndex] == (char)0x1B && // Alt is send as an escape character | ||
endIndex - startIndex >= 2) // We have at least two characters to read | ||
{ | ||
startIndex++; | ||
if (MapBufferToConsoleKey(buffer, terminalFormatStrings, posixDisableValue, veraseCharacter, out key, out ch, out isShift, out _, out isCtrl, ref startIndex, endIndex)) | ||
{ | ||
isAlt = true; | ||
return true; | ||
} | ||
else | ||
{ | ||
// We could not find a matching key here so, Alt+ combination assumption is in-correct. | ||
// The current key needs to be marked as Esc key. | ||
// Also, we do not increment _startIndex as we already did it. | ||
key = ConsoleKey.Escape; | ||
ch = (char)0x1B; | ||
isAlt = false; | ||
return true; | ||
} | ||
} | ||
|
||
// Try reading the first char in the buffer and interpret it as a key. | ||
ch = buffer[startIndex++]; | ||
key = GetKeyFromCharValue(ch, out isShift, out isCtrl); | ||
isAlt = false; | ||
return key != default(ConsoleKey); | ||
} | ||
|
||
private static bool TryGetSpecialConsoleKey(char[] givenChars, int startIndex, int endIndex, | ||
TerminalFormatStrings terminalFormatStrings, byte posixDisableValue, byte veraseCharacter, out ConsoleKeyInfo key, out int keyLength) | ||
{ | ||
int unprocessedCharCount = endIndex - startIndex; | ||
|
||
// First process special control character codes. These override anything from terminfo. | ||
if (unprocessedCharCount > 0) | ||
{ | ||
// Is this an erase / backspace? | ||
char c = givenChars[startIndex]; | ||
if (c != posixDisableValue && c == veraseCharacter) | ||
{ | ||
key = new ConsoleKeyInfo(c, ConsoleKey.Backspace, shift: false, alt: false, control: false); | ||
keyLength = 1; | ||
return true; | ||
} | ||
} | ||
|
||
// Then process terminfo mappings. | ||
int minRange = terminalFormatStrings.MinKeyFormatLength; | ||
if (unprocessedCharCount >= minRange) | ||
{ | ||
int maxRange = Math.Min(unprocessedCharCount, terminalFormatStrings.MaxKeyFormatLength); | ||
|
||
for (int i = maxRange; i >= minRange; i--) | ||
{ | ||
var currentString = new ReadOnlyMemory<char>(givenChars, startIndex, i); | ||
|
||
// Check if the string prefix matches. | ||
if (terminalFormatStrings.KeyFormatToConsoleKey.TryGetValue(currentString, out key)) | ||
{ | ||
keyLength = currentString.Length; | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
// Otherwise, not a known special console key. | ||
key = default(ConsoleKeyInfo); | ||
keyLength = 0; | ||
return false; | ||
} | ||
|
||
private static ConsoleKey GetKeyFromCharValue(char x, out bool isShift, out bool isCtrl) | ||
{ | ||
isShift = false; | ||
isCtrl = false; | ||
|
||
switch (x) | ||
{ | ||
case '\b': | ||
return ConsoleKey.Backspace; | ||
|
||
case '\t': | ||
return ConsoleKey.Tab; | ||
|
||
case '\n': | ||
case '\r': | ||
return ConsoleKey.Enter; | ||
|
||
case (char)(0x1B): | ||
return ConsoleKey.Escape; | ||
|
||
case '*': | ||
return ConsoleKey.Multiply; | ||
|
||
case '+': | ||
return ConsoleKey.Add; | ||
|
||
case '-': | ||
return ConsoleKey.Subtract; | ||
|
||
case '/': | ||
return ConsoleKey.Divide; | ||
|
||
case (char)(0x7F): | ||
return ConsoleKey.Delete; | ||
|
||
case ' ': | ||
return ConsoleKey.Spacebar; | ||
|
||
default: | ||
// 1. Ctrl A to Ctrl Z. | ||
if (char.IsBetween(x, (char)1, (char)26)) | ||
{ | ||
isCtrl = true; | ||
return ConsoleKey.A + x - 1; | ||
} | ||
|
||
// 2. Numbers from 0 to 9. | ||
if (char.IsAsciiDigit(x)) | ||
{ | ||
return ConsoleKey.D0 + x - '0'; | ||
} | ||
|
||
//3. A to Z | ||
if (char.IsAsciiLetterUpper(x)) | ||
{ | ||
isShift = true; | ||
return ConsoleKey.A + (x - 'A'); | ||
} | ||
|
||
// 4. a to z. | ||
if (char.IsAsciiLetterLower(x)) | ||
{ | ||
return ConsoleKey.A + (x - 'a'); | ||
} | ||
|
||
break; | ||
} | ||
|
||
return default(ConsoleKey); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.