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 mappings for 3 characters
  • Loading branch information
adamsitnik committed Jul 8, 2022
commit 5bbac1d7998b2ae5c732f44bf0ebdaba6b4608e3
80 changes: 73 additions & 7 deletions src/libraries/System.Console/src/System/IO/KeyMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,90 @@ internal static bool MapBufferToConsoleKey(char[] buffer, ConsolePal.TerminalFor
internal static void MapNew(char[] buffer, ConsolePal.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)
{
if (endIndex - startIndex == 1)
int length = endIndex - startIndex;

if (length >= 3 && TryDecodeTerminalInputSequence(buffer, terminalFormatStrings, out key, out isShift, out isCtrl, out isAlt, ref startIndex, endIndex))
{
DecodeFromSingleChar(buffer[startIndex], out key, out ch, out isShift, out isCtrl);
startIndex++;
isAlt = false;
// these special keys never produce any char (Home, Arrow, F1 etc)
ch = default;
return;
}
else if (endIndex - startIndex == 2 && buffer[startIndex] == Escape)
else if (length == 2 && buffer[startIndex] == Escape)
{
DecodeFromSingleChar(buffer[++startIndex], out key, out ch, out isShift, out isCtrl);
startIndex++;
isAlt = key != default; // two char sequences starting with Escape are Alt+$Key
}
else
{
DecodeFromSingleChar(buffer[startIndex], out key, out ch, out isShift, out isCtrl);
startIndex++;
isAlt = false;
}
}

private static bool TryDecodeTerminalInputSequence(char[] buffer, ConsolePal.TerminalFormatStrings terminalFormatStrings,
out ConsoleKey key, out bool isShift, out bool isAlt, out bool isCtrl, ref int startIndex, int endIndex)
{
ReadOnlySpan<char> input = buffer.AsSpan(startIndex, endIndex - startIndex);
isShift = isAlt = isCtrl = false;
key = default;

// xterm and VT sequences start with "^[[", some xterm start with "^[O" ("^[" stands for Escape (27))
if (input.Length < 3 || input[0] != Escape || (input[1] != '[' && input[1] != 'O'))
{
key = default;
ch = default;
isShift = isAlt = isCtrl = false;
return false;
}

if (input[1] == 'O' || char.IsAsciiLetterUpper(input[2])) // xterm "^[[O" or xterm "^[["
{
if (!TryMapUsingDatabase(buffer.AsMemory(startIndex, 3), terminalFormatStrings, ref key, ref isShift, ref isAlt, ref isCtrl))
{
key = Map(input[2]);
Debug.Assert(key != default, $"Missing '{input.Slice(0, 3)}' mapping");
}
startIndex += 3;
return true;
}

if (char.IsAsciiDigit(input[2]))
{

}

return false;

static bool TryMapUsingDatabase(ReadOnlyMemory<char> inputSequence, ConsolePal.TerminalFormatStrings terminalFormatStrings,
ref ConsoleKey key, ref bool isShift, ref bool isAlt, ref bool isCtrl)
{
// Check if the string prefix matches.
if (terminalFormatStrings.KeyFormatToConsoleKey.TryGetValue(inputSequence, out ConsoleKeyInfo consoleKeyInfo))
{
key = consoleKeyInfo.Key;
isShift = (consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0;
isAlt = (consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0;
isCtrl = (consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0;
return true;
}
return false;
}

static ConsoleKey Map(char single)
=> single switch
{
'A' => ConsoleKey.UpArrow,
'B' => ConsoleKey.DownArrow,
'C' => ConsoleKey.RightArrow,
'D' => ConsoleKey.LeftArrow,
'F' => ConsoleKey.End,
'H' => ConsoleKey.Home,
'P' => ConsoleKey.F1,
'Q' => ConsoleKey.F2,
'R' => ConsoleKey.F3,
'S' => ConsoleKey.F4,
_ => default
};
}

private static void DecodeFromSingleChar(char single, out ConsoleKey key, out char ch, out bool isShift, out bool isCtrl)
Expand Down
51 changes: 38 additions & 13 deletions src/libraries/System.Console/tests/KeyMapperTests.Unix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Xunit;

Expand Down Expand Up @@ -61,7 +62,7 @@ private static ConsoleKeyInfo Map(char[] chars, ConsolePal.TerminalFormatStrings
return new ConsoleKeyInfo(ch, consoleKey, isShift, isAlt, isCtrl);
}

private static IEnumerable<(char, ConsoleKey)> AsciiKeys
private static IEnumerable<(char ch, ConsoleKey key)> AsciiKeys
{
get
{
Expand Down Expand Up @@ -96,18 +97,7 @@ private static ConsoleKeyInfo Map(char[] chars, ConsolePal.TerminalFormatStrings
}

public static IEnumerable<object[]> AsciiCharactersArguments
{
get
{
foreach (TerminalData terminalData in Terminals)
{
foreach ((char ch, ConsoleKey key) in AsciiKeys)
{
yield return new object[] { terminalData, ch, key };
}
}
}
}
=> Terminals.SelectMany(terminal => AsciiKeys.Select(tuple => new object[] { terminal, tuple.ch, tuple.key }));

[Theory]
[MemberData(nameof(AsciiCharactersArguments))]
Expand All @@ -119,6 +109,41 @@ public void AsciiCharacters(TerminalData terminalData, char input, ConsoleKey ex
Assert.Equal(expectedKey, consoleKeyInfo.Key);
Assert.Equal(char.IsAsciiLetterUpper(input) ? ConsoleModifiers.Shift : 0, consoleKeyInfo.Modifiers);
}

private static IEnumerable<(string chars, ConsoleKey key)> ThreeCharactersKeys
{
get
{
// "^[["
yield return ("\u001B[H", ConsoleKey.Home);
yield return ("\u001B[F", ConsoleKey.End);
yield return ("\u001B[A", ConsoleKey.UpArrow);
yield return ("\u001B[B", ConsoleKey.DownArrow);
yield return ("\u001B[C", ConsoleKey.RightArrow);
yield return ("\u001B[D", ConsoleKey.LeftArrow);
// "^[O"
yield return ("\u001BOP", ConsoleKey.F1);
yield return ("\u001BOQ", ConsoleKey.F2);
yield return ("\u001BOR", ConsoleKey.F3);
yield return ("\u001BOS", ConsoleKey.F4);
}
}

public static IEnumerable<object[]> ThreeCharactersKeysArguments
=> Terminals
.Where(t => t is not PuTTYData_putty) // different mappings (handled by KeysAreProperlyMapped test)
.SelectMany(terminal => ThreeCharactersKeys.Select(tuple => new object[] { terminal, tuple.chars, tuple.key }));

[Theory]
[MemberData(nameof(ThreeCharactersKeysArguments))]
public void ThreeCharactersKey(TerminalData terminalData, string input, ConsoleKey expectedKey)
{
ConsoleKeyInfo consoleKeyInfo = Map(input.ToCharArray(), terminalData.TerminalDb, terminalData.Verase);

Assert.Equal(expectedKey, consoleKeyInfo.Key);
Assert.Equal(default, consoleKeyInfo.KeyChar);
Assert.Equal(default, consoleKeyInfo.Modifiers);
}
}

public abstract class TerminalData
Expand Down