@@ -21,7 +21,8 @@ public static PowerShellConsoleService CreateAndStart(
21
21
executionService ,
22
22
executionService . EngineIntrinsics ,
23
23
executionService . EditorServicesHost ,
24
- executionService . ReadLine ) ;
24
+ executionService . ReadLine ,
25
+ executionService . PSReadLineProxy ) ;
25
26
26
27
return consoleService ;
27
28
}
@@ -36,6 +37,8 @@ public static PowerShellConsoleService CreateAndStart(
36
37
37
38
private readonly ConsoleReadLine _readLine ;
38
39
40
+ private readonly PSReadLineProxy _psrlProxy ;
41
+
39
42
private Task _consoleLoopThread ;
40
43
41
44
private CancellationTokenSource _replLoopCancellationSource ;
@@ -47,13 +50,15 @@ private PowerShellConsoleService(
47
50
PowerShellExecutionService executionService ,
48
51
EngineIntrinsics engineIntrinsics ,
49
52
EditorServicesConsolePSHost editorServicesHost ,
50
- ConsoleReadLine readLine )
53
+ ConsoleReadLine readLine ,
54
+ PSReadLineProxy psrlProxy )
51
55
{
52
56
_logger = loggerFactory . CreateLogger < PowerShellConsoleService > ( ) ;
53
57
_executionService = executionService ;
54
58
_engineIntrinsics = engineIntrinsics ;
55
59
_editorServicesHost = editorServicesHost ;
56
60
_readLine = readLine ;
61
+ _psrlProxy = psrlProxy ;
57
62
}
58
63
59
64
public void Dispose ( )
@@ -66,7 +71,14 @@ public void StartRepl()
66
71
_replLoopCancellationSource = new CancellationTokenSource ( ) ;
67
72
System . Console . CancelKeyPress += HandleConsoleCancellation ;
68
73
System . Console . OutputEncoding = Encoding . UTF8 ;
74
+ _psrlProxy . OverrideReadKey ( ReadKey ) ;
69
75
_consoleLoopThread = Task . Run ( RunReplLoopAsync , _replLoopCancellationSource . Token ) ;
76
+ _executionService . RegisterConsoleService ( this ) ;
77
+ }
78
+
79
+ public void CancelCurrentPrompt ( )
80
+ {
81
+ _currentCommandCancellationSource ? . Cancel ( ) ;
70
82
}
71
83
72
84
private async Task RunReplLoopAsync ( )
@@ -78,29 +90,29 @@ private async Task RunReplLoopAsync()
78
90
try
79
91
{
80
92
await InvokePromptFunctionAsync ( ) . ConfigureAwait ( false ) ;
81
- }
82
- catch ( OperationCanceledException )
83
- {
84
- break ;
85
- }
86
93
87
- try
88
- {
89
- // Poll for user input here so that the prompt does not block
90
- string userInput = await InvokeReadLineAsync ( ) ;
94
+ string userInput = await InvokeReadLineAsync ( ) . ConfigureAwait ( false ) ;
91
95
92
96
await InvokeInputAsync ( userInput ) . ConfigureAwait ( false ) ;
93
97
}
94
98
catch ( OperationCanceledException )
95
99
{
96
100
continue ;
97
101
}
102
+ catch ( Exception e )
103
+ {
104
+
105
+ }
98
106
}
99
107
}
100
108
101
109
private Task InvokePromptFunctionAsync ( )
102
110
{
103
- var promptCommand = new PSCommand ( ) . AddCommand ( "prompt" ) ;
111
+ var promptCommand = new PSCommand ( )
112
+ . AddCommand ( "prompt" )
113
+ . AddCommand ( "Write-Host" )
114
+ . AddParameter ( "NoNewline" ) ;
115
+
104
116
var executionOptions = new PowerShellExecutionOptions
105
117
{
106
118
WriteOutputToHost = true ,
@@ -131,6 +143,12 @@ private Task InvokeInputAsync(string input)
131
143
132
144
private void HandleConsoleCancellation ( object sender , ConsoleCancelEventArgs args )
133
145
{
146
+ _currentCommandCancellationSource . Cancel ( ) ;
147
+ }
148
+
149
+ private ConsoleKeyInfo ReadKey ( bool intercept )
150
+ {
151
+ return ConsoleProxy . SafeReadKey ( intercept , _currentCommandCancellationSource ? . Token ?? CancellationToken . None ) ;
134
152
}
135
153
}
136
154
}
0 commit comments