Class ConsoleTTY
Implements a pseudo TTY that links the Console for the current application to a remote process started via a command line.
Namespace: Neon.WinTTY
Assembly: Neon.WinTTY.dll
Syntax
public sealed class ConsoleTTY
Remarks
This is very easy to use. Just instantiate an instance in your console application and then call Run(string, IDictionary<ConsoleKeyInfo, string>), passing the command line to be executed with a TTY.
using System;
using Neon.WinTTY;
namespace MyConsoleApp
{
public class Program
{
public static void Main(string[] args)
{
new ConsoleTTY().Run(@"docker exec -it alpine /bin/sh");
}
}
}
Run(string, IDictionary<ConsoleKeyInfo, string>) receives user keystrokes and then forwards them to the remote process, optionally translating the keystroke into an ECMA-48 control sequence. By default, this methods uses the DefaultKeyMap dictionary to translate keystrokes but users may override this by passing a custom dictionary.
The ConsoleKeyInfo values received as the user types include flag bits indicating the current state of the ALT, CONTROL, and SHIFT keys, the ConsoleKey code identifying the specific key, and the key character. The key character is either the Unicode value for the keystroke or 0 when the keystroke doesn't map to a character (e.g. for an ARROW key).
Here's how keypress handling work:
- A new ConsoleKeyInfo is received by Run(string, IDictionary<ConsoleKeyInfo, string>).
- The key map is searched for a control sequence string for the ConsoleKeyInfo.
-
If a control sequence is found then it will be sent to the remote process. Note that the control
sequence string is
null
then nothing will be sent and the keypress will essentially be ignored. - If there's no matching control sequence in the key map and the key character is not zero, then the key character will be sent to the remote process. Zero key characters are never transmitted.
Constructors
ConsoleTTY()
Constructor.
Declaration
public ConsoleTTY()
Properties
DefaultKeyMap
Returns the default mapping used to translate a keyboard keypress into the ECMA-48 (or other) control sequence to be sent to the remote process.
Declaration
public static IDictionary<ConsoleKeyInfo, string> DefaultKeyMap { get; }
Property Value
Type | Description |
---|---|
IDictionary<ConsoleKeyInfo, string> |
Methods
Run(string, IDictionary<ConsoleKeyInfo, string>)
Starts a remote process by executing a command line and then wiring up a pseudo TTY that forwards keystrokes to the remote process and also receives VTx formatted output from the process and handle rendering on the local Console.
Declaration
public void Run(string command, IDictionary<ConsoleKeyInfo, string> keyMap = null)
Parameters
Type | Name | Description |
---|---|---|
string | command | Specifies the local command to execute as the remote process. noteYou must take care to quote the command executable path or any arguments that include spaces. |
IDictionary<ConsoleKeyInfo, string> | keyMap | Optionally specifies the map to be used for translating keystrokes into ECMA-48 (or other) control sequences. This defaults to DefaultKeyMap but you may pass a custom map when required. |
Remarks
If the command path specifies an absolute or relative directory then the command will be execute from there, otherwise the method will first attempt executing the command from the current directory before searching the PATH for the command.
You may omit the command file extension and the method will try .exe, .cmd, and .bat extensions in that order.