-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPhysicalConsole.cs
More file actions
115 lines (97 loc) · 3.77 KB
/
PhysicalConsole.cs
File metadata and controls
115 lines (97 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.DotNet.Watch;
internal sealed class PhysicalConsole : IConsole
{
public const char CtrlC = '\x03';
public const char CtrlR = '\x12';
public event Action<ConsoleKeyInfo>? KeyPressed;
public PhysicalConsole(TestFlags testFlags)
{
Console.OutputEncoding = Encoding.UTF8;
if (testFlags.HasFlag(TestFlags.ReadKeyFromStdin))
{
_ = ListenToStandardInputAsync();
}
else if (!Console.IsInputRedirected)
{
_ = ListenToConsoleKeyPressAsync();
}
}
private async Task ListenToStandardInputAsync()
{
using var stream = Console.OpenStandardInput();
var buffer = new byte[1];
while (true)
{
var bytesRead = await stream.ReadAsync(buffer, CancellationToken.None);
if (bytesRead != 1)
{
break;
}
var c = (char)buffer[0];
// emulate propagation of Ctrl+C/SIGTERM to child processes
if (c == CtrlC)
{
Console.WriteLine("Received CTRL+C key");
foreach (var processId in ProcessRunner.GetRunningApplicationProcesses())
{
string? error;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Console.WriteLine($"Sending Ctrl+C to {processId}");
error = ProcessUtilities.SendWindowsCtrlCEvent(processId);
}
else
{
Console.WriteLine($"Sending SIGTERM to {processId}");
error = ProcessUtilities.SendPosixSignal(processId, ProcessUtilities.SIGTERM);
}
if (error != null)
{
throw new InvalidOperationException(error);
}
}
}
// handle all input keys that watcher might consume:
var key = c switch
{
CtrlC => new ConsoleKeyInfo('C', ConsoleKey.C, shift: false, alt: false, control: true),
CtrlR => new ConsoleKeyInfo('R', ConsoleKey.R, shift: false, alt: false, control: true),
>= 'a' and <= 'z' => new ConsoleKeyInfo(c, ConsoleKey.A + (c - 'a'), shift: false, alt: false, control: false),
>= 'A' and <= 'Z' => new ConsoleKeyInfo(c, ConsoleKey.A + (c - 'A'), shift: true, alt: false, control: false),
>= '0' and <= '9' => new ConsoleKeyInfo(c, ConsoleKey.NumPad0 + (c - '0'), shift: false, alt: false, control: false),
_ => default
};
if (key.Key != ConsoleKey.None)
{
KeyPressed?.Invoke(key);
}
}
}
private Task ListenToConsoleKeyPressAsync()
{
Console.CancelKeyPress += (s, e) =>
{
e.Cancel = true;
KeyPressed?.Invoke(new ConsoleKeyInfo(CtrlC, ConsoleKey.C, shift: false, alt: false, control: true));
};
return Task.Factory.StartNew(() =>
{
while (true)
{
var key = Console.ReadKey(intercept: true);
KeyPressed?.Invoke(key);
}
}, TaskCreationOptions.LongRunning);
}
public TextWriter Error => Console.Error;
public TextWriter Out => Console.Out;
public ConsoleColor ForegroundColor
{
get => Console.ForegroundColor;
set => Console.ForegroundColor = value;
}
public void ResetColor() => Console.ResetColor();
public void Clear() => Console.Clear();
}