6
6
using System . Collections . ObjectModel ;
7
7
using System . Linq ;
8
8
using System . Text ;
9
- using System . Runtime . InteropServices ;
10
9
using System . Threading ;
11
10
using System . Threading . Tasks ;
12
11
@@ -20,27 +19,13 @@ namespace Microsoft.PowerShell.EditorServices.Console
20
19
internal class ConsoleReadLine
21
20
{
22
21
#region Private Field
23
- private static IConsoleOperations s_consoleProxy ;
24
-
25
22
private PowerShellContext powerShellContext ;
26
23
27
24
#endregion
28
25
29
26
#region Constructors
30
27
static ConsoleReadLine ( )
31
28
{
32
- // Maybe we should just include the RuntimeInformation package for FullCLR?
33
- #if CoreCLR
34
- if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
35
- {
36
- s_consoleProxy = new WindowsConsoleOperations ( ) ;
37
- return ;
38
- }
39
-
40
- s_consoleProxy = new UnixConsoleOperations ( ) ;
41
- #else
42
- s_consoleProxy = new WindowsConsoleOperations ( ) ;
43
- #endif
44
29
}
45
30
46
31
public ConsoleReadLine ( PowerShellContext powerShellContext )
@@ -66,8 +51,8 @@ public async Task<SecureString> ReadSecureLine(CancellationToken cancellationTok
66
51
{
67
52
SecureString secureString = new SecureString ( ) ;
68
53
69
- int initialPromptRow = Console . CursorTop ;
70
- int initialPromptCol = Console . CursorLeft ;
54
+ int initialPromptRow = await ConsoleProxy . GetCursorTopAsync ( cancellationToken ) ;
55
+ int initialPromptCol = await ConsoleProxy . GetCursorLeftAsync ( cancellationToken ) ;
71
56
int previousInputLength = 0 ;
72
57
73
58
Console . TreatControlCAsInput = true ;
@@ -114,7 +99,8 @@ public async Task<SecureString> ReadSecureLine(CancellationToken cancellationTok
114
99
}
115
100
else if ( previousInputLength > 0 && currentInputLength < previousInputLength )
116
101
{
117
- int row = Console . CursorTop , col = Console . CursorLeft ;
102
+ int row = await ConsoleProxy . GetCursorTopAsync ( cancellationToken ) ;
103
+ int col = await ConsoleProxy . GetCursorLeftAsync ( cancellationToken ) ;
118
104
119
105
// Back up the cursor before clearing the character
120
106
col -- ;
@@ -146,10 +132,30 @@ public async Task<SecureString> ReadSecureLine(CancellationToken cancellationTok
146
132
147
133
private static async Task < ConsoleKeyInfo > ReadKeyAsync ( CancellationToken cancellationToken )
148
134
{
149
- return await s_consoleProxy . ReadKeyAsync ( cancellationToken ) ;
135
+ return await ConsoleProxy . ReadKeyAsync ( cancellationToken ) ;
150
136
}
151
137
152
138
private async Task < string > ReadLine ( bool isCommandLine , CancellationToken cancellationToken )
139
+ {
140
+ return await this . powerShellContext . InvokeReadLine ( isCommandLine , cancellationToken ) ;
141
+ }
142
+
143
+ /// <summary>
144
+ /// Invokes a custom ReadLine method that is similar to but more basic than PSReadLine.
145
+ /// This method should be used when PSReadLine is disabled, either by user settings or
146
+ /// unsupported PowerShell versions.
147
+ /// </summary>
148
+ /// <param name="isCommandLine">
149
+ /// Indicates whether ReadLine should act like a command line.
150
+ /// </param>
151
+ /// <param name="cancellationToken">
152
+ /// The cancellation token that will be checked prior to completing the returned task.
153
+ /// </param>
154
+ /// <returns>
155
+ /// A task object representing the asynchronus operation. The Result property on
156
+ /// the task object returns the user input string.
157
+ /// </returns>
158
+ internal async Task < string > InvokeLegacyReadLine ( bool isCommandLine , CancellationToken cancellationToken )
153
159
{
154
160
string inputBeforeCompletion = null ;
155
161
string inputAfterCompletion = null ;
@@ -160,8 +166,8 @@ private async Task<string> ReadLine(bool isCommandLine, CancellationToken cancel
160
166
161
167
StringBuilder inputLine = new StringBuilder ( ) ;
162
168
163
- int initialCursorCol = Console . CursorLeft ;
164
- int initialCursorRow = Console . CursorTop ;
169
+ int initialCursorCol = await ConsoleProxy . GetCursorLeftAsync ( cancellationToken ) ;
170
+ int initialCursorRow = await ConsoleProxy . GetCursorTopAsync ( cancellationToken ) ;
165
171
166
172
int initialWindowLeft = Console . WindowLeft ;
167
173
int initialWindowTop = Console . WindowTop ;
@@ -492,8 +498,8 @@ private int CalculateIndexFromCursor(
492
498
int consoleWidth )
493
499
{
494
500
return
495
- ( ( Console . CursorTop - promptStartRow ) * consoleWidth ) +
496
- Console . CursorLeft - promptStartCol ;
501
+ ( ( ConsoleProxy . GetCursorTop ( ) - promptStartRow ) * consoleWidth ) +
502
+ ConsoleProxy . GetCursorLeft ( ) - promptStartCol ;
497
503
}
498
504
499
505
private void CalculateCursorFromIndex (
0 commit comments