Skip to content
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

[Unix] Console.ReadKey rewrite #72193

Merged
merged 42 commits into from
Aug 1, 2022
Merged
Show file tree
Hide file tree
Changes from 31 commits
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
7 changes: 7 additions & 0 deletions src/libraries/System.Console/src/System.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,18 @@
<!-- Unix -->
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Unix'">
<Compile Include="System\ConsolePal.Unix.cs" />
<Compile Include="System\ConsolePal.TerminalFormatStrings.cs" />
<Compile Include="$(CommonPath)System\Console\ConsoleUtils.cs"
Link="Common\System\Console\ConsoleUtils.cs" />
<Compile Include="System\TermInfo.cs" />
<Compile Include="System\TermInfo.WellKnownStrings.cs" />
<Compile Include="System\TermInfo.Database.cs" />
<Compile Include="System\TermInfo.DatabaseFactory.cs" />
<Compile Include="System\TermInfo.WellKnownNumbers.cs" />
<Compile Include="System\IO\StdInReader.cs" />
<Compile Include="System\IO\SyncTextReader.Unix.cs" />
<Compile Include="System\IO\KeyParser.cs" />
<Compile Include="System\IO\Net6KeyParser.cs" />
<Compile Include="$(CoreLibSharedDir)System\IO\PersistedFiles.Unix.cs"
Link="Common\System\IO\PersistedFiles.Unix.cs" />
<Compile Include="$(CoreLibSharedDir)System\IO\PersistedFiles.Names.Unix.cs"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,255 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Diagnostics;

namespace System;

internal static partial class ConsolePal
{
/// <summary>Provides format strings and related information for use with the current terminal.</summary>
internal sealed class TerminalFormatStrings
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>The format string to use to change the foreground color.</summary>
public readonly string? Foreground;
/// <summary>The format string to use to change the background color.</summary>
public readonly string? Background;
/// <summary>The format string to use to reset the foreground and background colors.</summary>
public readonly string? Reset;
/// <summary>The maximum number of colors supported by the terminal.</summary>
public readonly int MaxColors;
/// <summary>The number of columns in a format.</summary>
public readonly int Columns;
/// <summary>The number of lines in a format.</summary>
public readonly int Lines;
/// <summary>The format string to use to make cursor visible.</summary>
public readonly string? CursorVisible;
/// <summary>The format string to use to make cursor invisible</summary>
public readonly string? CursorInvisible;
/// <summary>The format string to use to set the window title.</summary>
public readonly string? Title;
/// <summary>The format string to use for an audible bell.</summary>
public readonly string? Bell;
/// <summary>The format string to use to clear the terminal.</summary>
public readonly string? Clear;
/// <summary>The format string to use to set the position of the cursor.</summary>
public readonly string? CursorAddress;
/// <summary>The format string to use to move the cursor to the left.</summary>
public readonly string? CursorLeft;
/// <summary>The format string to use to clear to the end of line.</summary>
public readonly string? ClrEol;
/// <summary>The ANSI-compatible string for the Cursor Position report request.</summary>
/// <remarks>
/// This should really be in user string 7 in the terminfo file, but some terminfo databases
/// are missing it. As this is defined to be supported by any ANSI-compatible terminal,
/// we assume it's available; doing so means CursorTop/Left will work even if the terminfo database
/// doesn't contain it (as appears to be the case with e.g. screen and tmux on Ubuntu), at the risk
/// of outputting the sequence on some terminal that's not compatible.
/// </remarks>
public const string CursorPositionReport = "\x1B[6n";
/// <summary>
/// The dictionary of keystring to ConsoleKeyInfo.
/// Only some members of the ConsoleKeyInfo are used; in particular, the actual char is ignored.
/// </summary>
public readonly Dictionary<ReadOnlyMemory<char>, ConsoleKeyInfo> KeyFormatToConsoleKey =
new Dictionary<ReadOnlyMemory<char>, ConsoleKeyInfo>(new ReadOnlyMemoryContentComparer());

/// <summary> Max key length </summary>
public readonly int MaxKeyFormatLength;
/// <summary> Min key length </summary>
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)
{
if (db == null)
return;

KeypadXmit = db.GetString(TermInfo.WellKnownStrings.KeypadXmit);
Foreground = db.GetString(TermInfo.WellKnownStrings.SetAnsiForeground);
Background = db.GetString(TermInfo.WellKnownStrings.SetAnsiBackground);
Reset = db.GetString(TermInfo.WellKnownStrings.OrigPairs) ?? db.GetString(TermInfo.WellKnownStrings.OrigColors);
Bell = db.GetString(TermInfo.WellKnownStrings.Bell);
Clear = db.GetString(TermInfo.WellKnownStrings.Clear);
Columns = db.GetNumber(TermInfo.WellKnownNumbers.Columns);
Lines = db.GetNumber(TermInfo.WellKnownNumbers.Lines);
CursorVisible = db.GetString(TermInfo.WellKnownStrings.CursorVisible);
CursorInvisible = db.GetString(TermInfo.WellKnownStrings.CursorInvisible);
CursorAddress = db.GetString(TermInfo.WellKnownStrings.CursorAddress);
CursorLeft = db.GetString(TermInfo.WellKnownStrings.CursorLeft);
ClrEol = db.GetString(TermInfo.WellKnownStrings.ClrEol);

IsRxvtTerm = !string.IsNullOrEmpty(db.Term) && db.Term.Contains("rxvt", StringComparison.OrdinalIgnoreCase);
Copy link
Member Author

Choose a reason for hiding this comment

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

this is the only change I've made to this file (introducing an initializing IsRxvtTerm property). The rest was just moved from TermInfo.cs

Title = GetTitle(db);

Debug.WriteLineIf(db.GetString(TermInfo.WellKnownStrings.CursorPositionReport) != CursorPositionReport,
"Getting the cursor position will only work if the terminal supports the CPR sequence," +
"but the terminfo database does not contain an entry for it.");

int maxColors = db.GetNumber(TermInfo.WellKnownNumbers.MaxColors);
MaxColors = // normalize to either the full range of all ANSI colors, just the dark ones, or none
maxColors >= 16 ? 16 :
maxColors >= 8 ? 8 :
0;

AddKey(db, TermInfo.WellKnownStrings.KeyF1, ConsoleKey.F1);
AddKey(db, TermInfo.WellKnownStrings.KeyF2, ConsoleKey.F2);
AddKey(db, TermInfo.WellKnownStrings.KeyF3, ConsoleKey.F3);
AddKey(db, TermInfo.WellKnownStrings.KeyF4, ConsoleKey.F4);
AddKey(db, TermInfo.WellKnownStrings.KeyF5, ConsoleKey.F5);
AddKey(db, TermInfo.WellKnownStrings.KeyF6, ConsoleKey.F6);
AddKey(db, TermInfo.WellKnownStrings.KeyF7, ConsoleKey.F7);
AddKey(db, TermInfo.WellKnownStrings.KeyF8, ConsoleKey.F8);
AddKey(db, TermInfo.WellKnownStrings.KeyF9, ConsoleKey.F9);
AddKey(db, TermInfo.WellKnownStrings.KeyF10, ConsoleKey.F10);
AddKey(db, TermInfo.WellKnownStrings.KeyF11, ConsoleKey.F11);
AddKey(db, TermInfo.WellKnownStrings.KeyF12, ConsoleKey.F12);
AddKey(db, TermInfo.WellKnownStrings.KeyF13, ConsoleKey.F13);
AddKey(db, TermInfo.WellKnownStrings.KeyF14, ConsoleKey.F14);
AddKey(db, TermInfo.WellKnownStrings.KeyF15, ConsoleKey.F15);
AddKey(db, TermInfo.WellKnownStrings.KeyF16, ConsoleKey.F16);
AddKey(db, TermInfo.WellKnownStrings.KeyF17, ConsoleKey.F17);
AddKey(db, TermInfo.WellKnownStrings.KeyF18, ConsoleKey.F18);
AddKey(db, TermInfo.WellKnownStrings.KeyF19, ConsoleKey.F19);
AddKey(db, TermInfo.WellKnownStrings.KeyF20, ConsoleKey.F20);
AddKey(db, TermInfo.WellKnownStrings.KeyF21, ConsoleKey.F21);
AddKey(db, TermInfo.WellKnownStrings.KeyF22, ConsoleKey.F22);
AddKey(db, TermInfo.WellKnownStrings.KeyF23, ConsoleKey.F23);
AddKey(db, TermInfo.WellKnownStrings.KeyF24, ConsoleKey.F24);
AddKey(db, TermInfo.WellKnownStrings.KeyBackspace, ConsoleKey.Backspace);
AddKey(db, TermInfo.WellKnownStrings.KeyBackTab, ConsoleKey.Tab, shift: true, alt: false, control: false);
AddKey(db, TermInfo.WellKnownStrings.KeyBegin, ConsoleKey.Home);
AddKey(db, TermInfo.WellKnownStrings.KeyClear, ConsoleKey.Clear);
AddKey(db, TermInfo.WellKnownStrings.KeyDelete, ConsoleKey.Delete);
AddKey(db, TermInfo.WellKnownStrings.KeyDown, ConsoleKey.DownArrow);
AddKey(db, TermInfo.WellKnownStrings.KeyEnd, ConsoleKey.End);
AddKey(db, TermInfo.WellKnownStrings.KeyEnter, ConsoleKey.Enter);
AddKey(db, TermInfo.WellKnownStrings.KeyHelp, ConsoleKey.Help);
AddKey(db, TermInfo.WellKnownStrings.KeyHome, ConsoleKey.Home);
AddKey(db, TermInfo.WellKnownStrings.KeyInsert, ConsoleKey.Insert);
AddKey(db, TermInfo.WellKnownStrings.KeyLeft, ConsoleKey.LeftArrow);
AddKey(db, TermInfo.WellKnownStrings.KeyPageDown, ConsoleKey.PageDown);
AddKey(db, TermInfo.WellKnownStrings.KeyPageUp, ConsoleKey.PageUp);
AddKey(db, TermInfo.WellKnownStrings.KeyPrint, ConsoleKey.Print);
AddKey(db, TermInfo.WellKnownStrings.KeyRight, ConsoleKey.RightArrow);
AddKey(db, TermInfo.WellKnownStrings.KeyScrollForward, ConsoleKey.PageDown, shift: true, alt: false, control: false);
AddKey(db, TermInfo.WellKnownStrings.KeyScrollReverse, ConsoleKey.PageUp, shift: true, alt: false, control: false);
AddKey(db, TermInfo.WellKnownStrings.KeySBegin, ConsoleKey.Home, shift: true, alt: false, control: false);
AddKey(db, TermInfo.WellKnownStrings.KeySDelete, ConsoleKey.Delete, shift: true, alt: false, control: false);
AddKey(db, TermInfo.WellKnownStrings.KeySHome, ConsoleKey.Home, shift: true, alt: false, control: false);
AddKey(db, TermInfo.WellKnownStrings.KeySelect, ConsoleKey.Select);
AddKey(db, TermInfo.WellKnownStrings.KeySLeft, ConsoleKey.LeftArrow, shift: true, alt: false, control: false);
AddKey(db, TermInfo.WellKnownStrings.KeySPrint, ConsoleKey.Print, shift: true, alt: false, control: false);
AddKey(db, TermInfo.WellKnownStrings.KeySRight, ConsoleKey.RightArrow, shift: true, alt: false, control: false);
AddKey(db, TermInfo.WellKnownStrings.KeyUp, ConsoleKey.UpArrow);
AddPrefixKey(db, "kLFT", ConsoleKey.LeftArrow);
AddPrefixKey(db, "kRIT", ConsoleKey.RightArrow);
AddPrefixKey(db, "kUP", ConsoleKey.UpArrow);
AddPrefixKey(db, "kDN", ConsoleKey.DownArrow);
AddPrefixKey(db, "kDC", ConsoleKey.Delete);
AddPrefixKey(db, "kEND", ConsoleKey.End);
AddPrefixKey(db, "kHOM", ConsoleKey.Home);
AddPrefixKey(db, "kNXT", ConsoleKey.PageDown);
AddPrefixKey(db, "kPRV", ConsoleKey.PageUp);

if (KeyFormatToConsoleKey.Count > 0)
{
MaxKeyFormatLength = int.MinValue;
MinKeyFormatLength = int.MaxValue;

foreach (KeyValuePair<ReadOnlyMemory<char>, ConsoleKeyInfo> entry in KeyFormatToConsoleKey)
{
if (entry.Key.Length > MaxKeyFormatLength)
{
MaxKeyFormatLength = entry.Key.Length;
}
if (entry.Key.Length < MinKeyFormatLength)
{
MinKeyFormatLength = entry.Key.Length;
}
}
}
}

private static string GetTitle(TermInfo.Database db)
{
// Try to get the format string from tsl/fsl and use it if they're available
string? tsl = db.GetString(TermInfo.WellKnownStrings.ToStatusLine);
string? fsl = db.GetString(TermInfo.WellKnownStrings.FromStatusLine);
if (tsl != null && fsl != null)
{
return tsl + "%p1%s" + fsl;
}

string term = db.Term;
if (term == null)
{
return string.Empty;
}

if (term.StartsWith("xterm", StringComparison.Ordinal)) // normalize all xterms to enable easier matching
{
term = "xterm";
}

switch (term)
{
case "aixterm":
case "dtterm":
case "linux":
case "rxvt":
case "xterm":
return "\x1B]0;%p1%s\x07";
case "cygwin":
return "\x1B];%p1%s\x07";
case "konsole":
return "\x1B]30;%p1%s\x07";
case "screen":
adamsitnik marked this conversation as resolved.
Show resolved Hide resolved
return "\x1Bk%p1%s\x1B";
default:
return string.Empty;
}
}

private void AddKey(TermInfo.Database db, TermInfo.WellKnownStrings keyId, ConsoleKey key)
{
AddKey(db, keyId, key, shift: false, alt: false, control: false);
}

private void AddKey(TermInfo.Database db, TermInfo.WellKnownStrings keyId, ConsoleKey key, bool shift, bool alt, bool control)
{
ReadOnlyMemory<char> keyFormat = db.GetString(keyId).AsMemory();
if (!keyFormat.IsEmpty)
KeyFormatToConsoleKey[keyFormat] = new ConsoleKeyInfo('\0', key, shift, alt, control);
}

private void AddPrefixKey(TermInfo.Database db, string extendedNamePrefix, ConsoleKey key)
{
AddKey(db, extendedNamePrefix + "3", key, shift: false, alt: true, control: false);
AddKey(db, extendedNamePrefix + "4", key, shift: true, alt: true, control: false);
AddKey(db, extendedNamePrefix + "5", key, shift: false, alt: false, control: true);
AddKey(db, extendedNamePrefix + "6", key, shift: true, alt: false, control: true);
AddKey(db, extendedNamePrefix + "7", key, shift: false, alt: false, control: true);
}

private void AddKey(TermInfo.Database db, string extendedName, ConsoleKey key, bool shift, bool alt, bool control)
{
ReadOnlyMemory<char> keyFormat = db.GetExtendedString(extendedName).AsMemory();
if (!keyFormat.IsEmpty)
KeyFormatToConsoleKey[keyFormat] = new ConsoleKeyInfo('\0', key, shift, alt, control);
}
}

private sealed class ReadOnlyMemoryContentComparer : IEqualityComparer<ReadOnlyMemory<char>>
{
public bool Equals(ReadOnlyMemory<char> x, ReadOnlyMemory<char> y) =>
x.Span.SequenceEqual(y.Span);

public int GetHashCode(ReadOnlyMemory<char> obj) =>
string.GetHashCode(obj.Span);
}
}
Loading