-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathPromptHandlers.cs
185 lines (163 loc) · 5.99 KB
/
PromptHandlers.cs
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
using System;
using Microsoft.PowerShell.EditorServices.Console;
using Microsoft.PowerShell.EditorServices.Protocol.Messages;
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol;
using Microsoft.PowerShell.EditorServices.Utility;
using System.Threading.Tasks;
using System.Threading;
using System.Security;
namespace Microsoft.PowerShell.EditorServices.Host
{
internal class ProtocolChoicePromptHandler : ConsoleChoicePromptHandler
{
private IHostInput hostInput;
private IMessageSender messageSender;
private TaskCompletionSource<string> readLineTask;
public ProtocolChoicePromptHandler(
IMessageSender messageSender,
IHostInput hostInput,
IHostOutput hostOutput,
ILogger logger)
: base(hostOutput, logger)
{
this.hostInput = hostInput;
this.hostOutput = hostOutput;
this.messageSender = messageSender;
}
protected override void ShowPrompt(PromptStyle promptStyle)
{
base.ShowPrompt(promptStyle);
messageSender
.SendRequestAsync(
ShowChoicePromptRequest.Type,
new ShowChoicePromptRequest
{
IsMultiChoice = this.IsMultiChoice,
Caption = this.Caption,
Message = this.Message,
Choices = this.Choices,
DefaultChoices = this.DefaultChoices
}, true)
.ContinueWith(HandlePromptResponse)
.ConfigureAwait(false);
}
protected override Task<string> ReadInputStringAsync(CancellationToken cancellationToken)
{
this.readLineTask = new TaskCompletionSource<string>();
return this.readLineTask.Task;
}
private void HandlePromptResponse(
Task<ShowChoicePromptResponse> responseTask)
{
if (responseTask.IsCompleted)
{
ShowChoicePromptResponse response = responseTask.Result;
if (!response.PromptCancelled)
{
this.hostOutput.WriteOutput(
response.ResponseText,
OutputType.Normal);
this.readLineTask.TrySetResult(response.ResponseText);
}
else
{
// Cancel the current prompt
this.hostInput.SendControlC();
}
}
else
{
if (responseTask.IsFaulted)
{
// Log the error
Logger.Write(
LogLevel.Error,
"ShowChoicePrompt request failed with error:\r\n{0}",
responseTask.Exception.ToString());
}
// Cancel the current prompt
this.hostInput.SendControlC();
}
this.readLineTask = null;
}
}
internal class ProtocolInputPromptHandler : ConsoleInputPromptHandler
{
private IHostInput hostInput;
private IMessageSender messageSender;
private TaskCompletionSource<string> readLineTask;
public ProtocolInputPromptHandler(
IMessageSender messageSender,
IHostInput hostInput,
IHostOutput hostOutput,
ILogger logger)
: base(hostOutput, logger)
{
this.hostInput = hostInput;
this.hostOutput = hostOutput;
this.messageSender = messageSender;
}
protected override void ShowFieldPrompt(FieldDetails fieldDetails)
{
base.ShowFieldPrompt(fieldDetails);
messageSender
.SendRequestAsync(
ShowInputPromptRequest.Type,
new ShowInputPromptRequest
{
Name = fieldDetails.Name,
Label = fieldDetails.Label
}, true)
.ContinueWith(HandlePromptResponse)
.ConfigureAwait(false);
}
protected override Task<string> ReadInputStringAsync(CancellationToken cancellationToken)
{
this.readLineTask = new TaskCompletionSource<string>();
return this.readLineTask.Task;
}
private void HandlePromptResponse(
Task<ShowInputPromptResponse> responseTask)
{
if (responseTask.IsCompleted)
{
ShowInputPromptResponse response = responseTask.Result;
if (!response.PromptCancelled)
{
this.hostOutput.WriteOutput(
response.ResponseText,
OutputType.Normal);
this.readLineTask.TrySetResult(response.ResponseText);
}
else
{
// Cancel the current prompt
this.hostInput.SendControlC();
}
}
else
{
if (responseTask.IsFaulted)
{
// Log the error
Logger.Write(
LogLevel.Error,
"ShowInputPrompt request failed with error:\r\n{0}",
responseTask.Exception.ToString());
}
// Cancel the current prompt
this.hostInput.SendControlC();
}
this.readLineTask = null;
}
protected override Task<SecureString> ReadSecureStringAsync(CancellationToken cancellationToken)
{
// TODO: Write a message to the console
throw new NotImplementedException();
}
}
}