Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
d929235
move WellKnownStrings to a separate file
adamsitnik Jun 28, 2022
c8887ec
move Database to separate file
adamsitnik Jun 28, 2022
cf77107
move factory logic to DatabaseFactory so the test project does not ne…
adamsitnik Jun 28, 2022
c6f3a60
move WellKnownNumbers to separate file
adamsitnik Jun 28, 2022
acd4f24
include Database in the test project
adamsitnik Jun 28, 2022
f5e3641
move TerminalFormatStrings to a separate file
adamsitnik Jun 28, 2022
d3cdb93
include TerminalFormatStrings in test project
adamsitnik Jun 28, 2022
7ea9f50
fix existing tests
adamsitnik Jun 28, 2022
dc0af6b
move the key mapping logic to a separate class, do NOT change it
adamsitnik Jun 29, 2022
c927091
simple tests for ASCII characters
adamsitnik Jun 29, 2022
f30f02e
test cases for xterm and xterm-256color
adamsitnik Jul 5, 2022
5ffdd67
add PuTTY and UXTern
adamsitnik Jul 5, 2022
9230c81
add Windows Terminal data
adamsitnik Jul 6, 2022
41a92bf
add more PuTTY test cases
adamsitnik Jul 6, 2022
af92ab3
handle empty encodings
adamsitnik Jul 6, 2022
9a2ed71
make it possible to run the tests on Windows so I can use my favourit…
adamsitnik Jul 7, 2022
69f12c9
add mappings for single characters
adamsitnik Jul 7, 2022
5bbac1d
add mappings for 3 characters
adamsitnik Jul 8, 2022
a9db7db
fix test cases where pressing Ctrl/Alt/Shift + other key produces two…
adamsitnik Jul 8, 2022
c2e02d8
handle more complex sequences (with modifiers)
adamsitnik Jul 8, 2022
620d1a0
handle edge cases properly
adamsitnik Jul 9, 2022
20593ef
add tmux test data
adamsitnik Jul 11, 2022
c612f4f
add rxvt-unicode test data and implementation for rxvt modifiers
adamsitnik Jul 11, 2022
3e62636
polishing
adamsitnik Jul 12, 2022
aa07430
switch to the new implementation and allow the users to differentiate…
adamsitnik Jul 12, 2022
254c762
add way more test cases and fix identified issues
adamsitnik Jul 12, 2022
4023725
polishing: reorder the tests
adamsitnik Jul 13, 2022
d65c6e7
fix compiler error (it's strange as I was not getting it when I was b…
adamsitnik Jul 14, 2022
8cd9e3d
add more SCO mappings
adamsitnik Jul 14, 2022
7fea037
Numeric Keypad
adamsitnik Jul 14, 2022
16e983f
Merge remote-tracking branch 'upstream/main' into consoleReadKey
adamsitnik Jul 14, 2022
dd07418
add Tmux 256 color test cases (TERM=screen-256color)
adamsitnik Jul 21, 2022
632267c
add two Numeric Keypad test cases: period & Enter
adamsitnik Jul 21, 2022
69f6434
fix issue discovered by adding more tmux test cases: ^[OM should be m…
adamsitnik Jul 21, 2022
9c352b4
address code review feedback: move TerminalFormatStrings classs out i…
adamsitnik Jul 21, 2022
f974992
address code review feedback: handle all variants of tmux when gettin…
adamsitnik Jul 21, 2022
b3366a8
address code review feedback: reduce number of out parameters
adamsitnik Jul 21, 2022
dc82fb5
fix TermInfo tests
adamsitnik Jul 21, 2022
214da94
add .NET 6 compat switch
adamsitnik Jul 21, 2022
7766ed3
Apply suggestions from code review
adamsitnik Aug 1, 2022
c8010c5
address code review feedback
adamsitnik Aug 1, 2022
c28d600
Merge branch 'main' into consoleReadKey
adamsitnik Aug 1, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add way more test cases and fix identified issues
remove UXTerm as its the same as Term
  • Loading branch information
adamsitnik committed Jul 13, 2022
commit 254c7625f543096a55ad6d90433e3d81cfd9d645
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ internal sealed class TerminalFormatStrings
public readonly int MinKeyFormatLength;
/// <summary>The ANSI string used to enter "application" / "keypad transmit" mode.</summary>
public readonly string? KeypadXmit;
/// <summary>Indicates that it was created out of rxvt TERM</summary>
public readonly bool IsRxvtTerm;

public TerminalFormatStrings(TermInfo.Database? db)
{
Expand All @@ -81,6 +83,7 @@ public TerminalFormatStrings(TermInfo.Database? db)
CursorLeft = db.GetString(TermInfo.WellKnownStrings.CursorLeft);
ClrEol = db.GetString(TermInfo.WellKnownStrings.ClrEol);

IsRxvtTerm = !string.IsNullOrEmpty(db.Term) && db.Term.IndexOf("rxvt", StringComparison.OrdinalIgnoreCase) >= 0;
Title = GetTitle(db);

Debug.WriteLineIf(db.GetString(TermInfo.WellKnownStrings.CursorPositionReport) != CursorPositionReport,
Expand Down
82 changes: 70 additions & 12 deletions src/libraries/System.Console/src/System/IO/KeyParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,40 @@ private static bool TryParseTerminalInputSequence(char[] buffer, ConsolePal.Term
character = default;
key = default;

// xterm and VT sequences start with "^[[", some xterm start with "^[O" ("^[" stands for Escape (27))
// sequences start with either "^[[" or "^[O". "^[" stands for Escape (27).
if (input.Length < MinimalSequenceLength || input[0] != Escape || (input[1] != '[' && input[1] != 'O'))
{
return false;
}

// Is it a three character sequence? (examples: '^[[H' (Home), '^[OP' (F1), '^[Ow' (End))
if (input[1] == 'O' || char.IsAsciiLetter(input[2]))
// Is it a three character sequence? (examples: '^[[H' (Home), '^[OP' (F1))
if (input[1] == 'O' || char.IsAsciiLetter(input[2]) || input.Length == MinimalSequenceLength)
Comment on lines +69 to +70
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Is it a three character sequence? (examples: '^[[H' (Home), '^[OP' (F1))
if (input[1] == 'O' || char.IsAsciiLetter(input[2]) || input.Length == MinimalSequenceLength)
// Is it a three character sequence? (examples: '^[[H' (Home), '^[OP' (F1))
if (char.IsAsciiLetter(input[2]) || input.Length == MinimalSequenceLength)

{
if (!TryMapUsingTerminfoDb(buffer.AsMemory(startIndex, MinimalSequenceLength), terminalFormatStrings, ref key, ref isShift, ref isAlt, ref isCtrl))
{
key = MapKeyId(input[2]); // fallback to well known mappings
// All terminals which use "^[O{letter}" escape sequences don't define conflicting mappings.
// Example: ^[OH either means Home or simply is not used by given terminal.
// But with "^[[{character}" sequences, there are conflicts between rxvt and SCO.
// Example: "^[[a" is Shift+UpArrow for rxvt and Shift+F3 for SCO.
if (input[1] == 'O'|| terminalFormatStrings.IsRxvtTerm)
{
key = MapKeyIdOXterm(input[2]); // fallback to well known mappings

if (key != default)
{
// lowercase characters are used by rxvt to express Shift modifier for the arrow keys
isShift = char.IsBetween(input[2], 'a', 'd');
}
}
else
{
(key, ConsoleModifiers mod) = MapSCO(input[2]); // fallback to well known mappings

if (key != default)
{
Apply(mod, ref isShift, ref isAlt, ref isCtrl);
}
}

if (key == default)
{
Expand All @@ -90,9 +112,15 @@ private static bool TryParseTerminalInputSequence(char[] buffer, ConsolePal.Term
return true;
}

if (input.Length == MinimalSequenceLength)
// Is it a four character sequence used by Linux Console or PuTTy configured to emulate it? (examples: '^[[[A' (F1), '^[[[B' (F2))
if (input[1] == '[' && input[2] == '[' && char.IsBetween(input[3], 'A', 'E'))
{
return false;
if (!TryMapUsingTerminfoDb(buffer.AsMemory(startIndex, 4), terminalFormatStrings, ref key, ref isShift, ref isAlt, ref isCtrl))
{
key = ConsoleKey.F1 + input[3] - 'A';
}
startIndex += 4;
return true;
}

// If sequence does not start with a letter, it must start with one or two digits that represent the Sequence Number
Expand Down Expand Up @@ -142,7 +170,7 @@ private static bool TryParseTerminalInputSequence(char[] buffer, ConsolePal.Term

key = input[SequencePrefixLength + digitCount + 2] is VtSequenceEndTag
? MapEscapeSequenceNumber(byte.Parse(input.Slice(SequencePrefixLength, digitCount)))
: MapKeyId(input[SequencePrefixLength + digitCount + 2]);
: MapKeyIdOXterm(input[SequencePrefixLength + digitCount + 2]);

if (key != default)
{
Expand All @@ -166,24 +194,53 @@ static bool TryMapUsingTerminfoDb(ReadOnlyMemory<char> inputSequence, ConsolePal
return false;
}

static ConsoleKey MapKeyId(char single)
=> single switch
// maps "^[O{character}" for all Terminals and "^[[{character}" for rxvt Terminals
static ConsoleKey MapKeyIdOXterm(char character)
=> character switch
{
// lowercase characters are used by rxvt
'A' or 'a' => ConsoleKey.UpArrow,
'B' or 'b' => ConsoleKey.DownArrow,
'C' or 'c' => ConsoleKey.RightArrow,
'D' or 'd' => ConsoleKey.LeftArrow,
'F' or 'w' => ConsoleKey.End,
'H' => ConsoleKey.Home,
'M' => ConsoleKey.Enter,
'P' => ConsoleKey.F1,
'Q' => ConsoleKey.F2,
'R' => ConsoleKey.F3,
'S' => ConsoleKey.F4,
'T' => ConsoleKey.F5, // VT 100+
'U' => ConsoleKey.F6, // VT 100+
'V' => ConsoleKey.F7, // VT 100+
'W' => ConsoleKey.F8, // VT 100+
'X' => ConsoleKey.F9, // VT 100+
'Y' => ConsoleKey.F10, // VT 100+
'Z' => ConsoleKey.F11, // VT 100+
'[' => ConsoleKey.F12, // VT 100+
_ => default
};

// maps "^[[{character}" for SCO terminals, based on https://vt100.net/docs/vt510-rm/chapter6.html
static (ConsoleKey key, ConsoleModifiers modifiers) MapSCO(char character)
=> character switch
{
'H' => (ConsoleKey.Home, 0),
_ when char.IsBetween(character, 'M', 'X') => (ConsoleKey.F1 + character - 'M', 0),
_ when char.IsBetween(character, 'Y', 'Z') => (ConsoleKey.F1 + character - 'Y', ConsoleModifiers.Shift),
_ when char.IsBetween(character, 'a', 'j') => (ConsoleKey.F3 + character - 'a', ConsoleModifiers.Shift),
_ when char.IsBetween(character, 'k', 'v') => (ConsoleKey.F1 + character - 'k', ConsoleModifiers.Control),
_ when char.IsBetween(character, 'w', 'z') => (ConsoleKey.F1 + character - 'w', ConsoleModifiers.Control | ConsoleModifiers.Shift),
'@' => (ConsoleKey.F5, ConsoleModifiers.Control | ConsoleModifiers.Shift),
'[' => (ConsoleKey.F6, ConsoleModifiers.Control | ConsoleModifiers.Shift),
'<' or '\\' => (ConsoleKey.F7, ConsoleModifiers.Control | ConsoleModifiers.Shift), // the Spec says <, PuTTy uses \.
']' => (ConsoleKey.F8, ConsoleModifiers.Control | ConsoleModifiers.Shift),
'^' => (ConsoleKey.F9, ConsoleModifiers.Control | ConsoleModifiers.Shift),
'_' => (ConsoleKey.F10, ConsoleModifiers.Control | ConsoleModifiers.Shift),
'`' => (ConsoleKey.F11, ConsoleModifiers.Control | ConsoleModifiers.Shift),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'`' => (ConsoleKey.F11, ConsoleModifiers.Control | ConsoleModifiers.Shift),
''' => (ConsoleKey.F11, ConsoleModifiers.Control | ConsoleModifiers.Shift),

This one looks to me as ' on https://vt100.net/docs/vt510-rm/chapter6.html.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, but PuTTy uses '`' for it and it's the only Terminal that supports SCO emulation that I could find and test

https://serverfault.com/questions/429901/how-to-change-the-terminal-to-sco-compliant-in-ubuntu

'{' => (ConsoleKey.F12, ConsoleModifiers.Control | ConsoleModifiers.Shift),
_ => default
};

// based on https://en.wikipedia.org/wiki/ANSI_escape_code#Fe_Escape_sequences
static ConsoleKey MapEscapeSequenceNumber(byte number)
=> number switch
{
Expand Down Expand Up @@ -214,10 +271,11 @@ static ConsoleKey MapEscapeSequenceNumber(byte number)
32 => ConsoleKey.F18,
33 => ConsoleKey.F19,
34 => ConsoleKey.F20,
// 9, 16, 22, 27, 30 and 35 have no mapping (https://en.wikipedia.org/wiki/ANSI_escape_code#Fe_Escape_sequences)
// 9, 16, 22, 27, 30 and 35 have no mapping
_ => default
};

// based on https://www.xfree86.org/current/ctlseqs.html
static ConsoleModifiers MapXtermModifiers(char modifier)
=> modifier switch
{
Expand Down
Loading