Skip to content

Commit 4e44a11

Browse files
committed
Implement shared Ctrl-C test implementation
1 parent 7ab8639 commit 4e44a11

File tree

4 files changed

+33
-24
lines changed

4 files changed

+33
-24
lines changed

src/PowerShellEditorServices/Services/PowerShell/Console/LegacyReadLine.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
using System.Collections.Generic;
5-
using System.Management.Automation;
6-
using System.Security;
7-
using System.Text;
8-
using System.Threading;
94
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Debugging;
105
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Execution;
116
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Host;
7+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;
8+
using System.Collections.Generic;
129
using System.Linq;
10+
using System.Management.Automation;
1311
using System.Management.Automation.Language;
12+
using System.Text;
13+
using System.Threading;
1414

1515
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console
1616
{
@@ -345,7 +345,7 @@ public override string ReadLine(CancellationToken cancellationToken)
345345
return completedInput;
346346

347347
default:
348-
if (IsCtrlC(keyInfo))
348+
if (keyInfo.IsCtrlC())
349349
{
350350
throw new PipelineStoppedException();
351351
}

src/PowerShellEditorServices/Services/PowerShell/Console/TerminalReadLine.cs

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
using Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility;
5+
using System.Management.Automation;
46
using System.Security;
57
using System.Threading;
68

79
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Console
810
{
911
using System;
10-
using System.Management.Automation;
1112

1213
internal abstract class TerminalReadLine : IReadLine
1314
{
@@ -31,7 +32,7 @@ public SecureString ReadSecureLine(CancellationToken cancellationToken)
3132
{
3233
ConsoleKeyInfo keyInfo = ReadKey(cancellationToken);
3334

34-
if (IsCtrlC(keyInfo))
35+
if (keyInfo.IsCtrlC())
3536
{
3637
throw new PipelineStoppedException();
3738
}
@@ -97,18 +98,5 @@ public SecureString ReadSecureLine(CancellationToken cancellationToken)
9798

9899
return secureString;
99100
}
100-
101-
protected static bool IsCtrlC(ConsoleKeyInfo keyInfo)
102-
{
103-
if ((int)keyInfo.Key == 3)
104-
{
105-
return true;
106-
}
107-
108-
return keyInfo.Key == ConsoleKey.C
109-
&& (keyInfo.Modifiers & ConsoleModifiers.Control) != 0
110-
&& (keyInfo.Modifiers & ConsoleModifiers.Alt) == 0;
111-
}
112-
113101
}
114102
}

src/PowerShellEditorServices/Services/PowerShell/Host/PsesInternalHost.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,7 @@ private ConsoleKeyInfo ReadKey(bool intercept)
747747
private bool LastKeyWasCtrlC()
748748
{
749749
return _lastKey.HasValue
750-
&& _lastKey.Value.Key == ConsoleKey.C
751-
&& (_lastKey.Value.Modifiers & ConsoleModifiers.Control) != 0
752-
&& (_lastKey.Value.Modifiers & ConsoleModifiers.Alt) == 0;
750+
&& _lastKey.Value.IsCtrlC();
753751
}
754752

755753
private void OnDebuggerStopped(object sender, DebuggerStopEventArgs debuggerStopEventArgs)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
6+
namespace Microsoft.PowerShell.EditorServices.Services.PowerShell.Utility
7+
{
8+
internal static class ConsoleKeyInfoExtensions
9+
{
10+
public static bool IsCtrlC(this ConsoleKeyInfo keyInfo)
11+
{
12+
if ((int)keyInfo.Key == 3)
13+
{
14+
return true;
15+
}
16+
17+
return keyInfo.Key == ConsoleKey.C
18+
&& (keyInfo.Modifiers & ConsoleModifiers.Control) != 0
19+
&& (keyInfo.Modifiers & ConsoleModifiers.Shift) == 0
20+
&& (keyInfo.Modifiers & ConsoleModifiers.Alt) == 0;
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)