-
Notifications
You must be signed in to change notification settings - Fork 234
/
Copy pathEditorServicesPSHostUserInterface.cs
1074 lines (937 loc) · 38.6 KB
/
EditorServicesPSHostUserInterface.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// 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 System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
using System.Threading;
using System.Globalization;
using Microsoft.Extensions.Logging;
using Microsoft.PowerShell.EditorServices.Logging;
namespace Microsoft.PowerShell.EditorServices.Services.PowerShellContext
{
/// <summary>
/// Provides an implementation of the PSHostUserInterface class
/// for the ConsoleService and routes its calls to an IConsoleHost
/// implementation.
/// </summary>
internal abstract class EditorServicesPSHostUserInterface :
PSHostUserInterface,
IHostInput,
IHostOutput,
IHostUISupportsMultipleChoiceSelection
{
#region Private Fields
private readonly ConcurrentDictionary<ProgressKey, object> currentProgressMessages =
new ConcurrentDictionary<ProgressKey, object>();
private PromptHandler activePromptHandler;
private PSHostRawUserInterface rawUserInterface;
private CancellationTokenSource commandLoopCancellationToken;
/// <summary>
/// The PowerShellContext to use for executing commands.
/// </summary>
protected PowerShellContextService powerShellContext;
#endregion
#region Public Constants
/// <summary>
/// Gets a const string for the console's debug message prefix.
/// </summary>
public const string DebugMessagePrefix = "DEBUG: ";
/// <summary>
/// Gets a const string for the console's warning message prefix.
/// </summary>
public const string WarningMessagePrefix = "WARNING: ";
/// <summary>
/// Gets a const string for the console's verbose message prefix.
/// </summary>
public const string VerboseMessagePrefix = "VERBOSE: ";
#endregion
#region Properties
#if !PowerShellv3 && !PowerShellv4 && !PowerShellv5r1 // Only available in Windows 10 Update 1 or higher
/// <summary>
/// Returns true if the host supports VT100 output codes.
/// </summary>
public override bool SupportsVirtualTerminal => true;
#endif
/// <summary>
/// Returns true if a native application is currently running.
/// </summary>
public bool IsNativeApplicationRunning { get; internal set; }
private bool IsCommandLoopRunning { get; set; }
/// <summary>
/// Gets the ILogger implementation used for this host.
/// </summary>
protected ILogger Logger { get; private set; }
/// <summary>
/// Gets a value indicating whether writing progress is supported.
/// </summary>
internal protected virtual bool SupportsWriteProgress => false;
#endregion
#region Constructors
/// <summary>
/// Creates a new instance of the ConsoleServicePSHostUserInterface
/// class with the given IConsoleHost implementation.
/// </summary>
/// <param name="powerShellContext">The PowerShellContext to use for executing commands.</param>
/// <param name="rawUserInterface">The PSHostRawUserInterface implementation to use for this host.</param>
/// <param name="logger">An ILogger implementation to use for this host.</param>
public EditorServicesPSHostUserInterface(
PowerShellContextService powerShellContext,
PSHostRawUserInterface rawUserInterface,
ILogger logger)
{
this.Logger = logger;
this.powerShellContext = powerShellContext;
this.rawUserInterface = rawUserInterface;
this.powerShellContext.DebuggerStop += PowerShellContext_DebuggerStop;
this.powerShellContext.DebuggerResumed += PowerShellContext_DebuggerResumed;
this.powerShellContext.ExecutionStatusChanged += PowerShellContext_ExecutionStatusChanged;
}
#endregion
#region Public Methods
/// <summary>
/// Starts the host's interactive command loop.
/// </summary>
public void StartCommandLoop()
{
if (!this.IsCommandLoopRunning)
{
this.IsCommandLoopRunning = true;
this.ShowCommandPrompt();
}
}
/// <summary>
/// Stops the host's interactive command loop.
/// </summary>
public void StopCommandLoop()
{
if (this.IsCommandLoopRunning)
{
this.IsCommandLoopRunning = false;
this.CancelCommandPrompt();
}
}
private void ShowCommandPrompt()
{
if (this.commandLoopCancellationToken == null)
{
this.commandLoopCancellationToken = new CancellationTokenSource();
Task.Run(() => this.StartReplLoopAsync(this.commandLoopCancellationToken.Token));
}
else
{
Logger.LogTrace("StartReadLoop called while read loop is already running");
}
}
private void CancelCommandPrompt()
{
if (this.commandLoopCancellationToken != null)
{
// Set this to false so that Ctrl+C isn't trapped by any
// lingering ReadKey
// TOOD: Move this to Terminal impl!
//Console.TreatControlCAsInput = false;
this.commandLoopCancellationToken.Cancel();
this.commandLoopCancellationToken = null;
}
}
/// <summary>
/// Cancels the currently executing command or prompt.
/// </summary>
public void SendControlC()
{
if (this.activePromptHandler != null)
{
this.activePromptHandler.CancelPrompt();
}
else
{
// Cancel the current execution
this.powerShellContext.AbortExecution();
}
}
#endregion
#region Abstract Methods
/// <summary>
/// Requests that the HostUI implementation read a command line
/// from the user to be executed in the integrated console command
/// loop.
/// </summary>
/// <param name="cancellationToken">
/// A CancellationToken used to cancel the command line request.
/// </param>
/// <returns>A Task that can be awaited for the resulting input string.</returns>
protected abstract Task<string> ReadCommandLineAsync(CancellationToken cancellationToken);
/// <summary>
/// Creates an InputPrompt handle to use for displaying input
/// prompts to the user.
/// </summary>
/// <returns>A new InputPromptHandler instance.</returns>
protected abstract InputPromptHandler OnCreateInputPromptHandler();
/// <summary>
/// Creates a ChoicePromptHandler to use for displaying a
/// choice prompt to the user.
/// </summary>
/// <returns>A new ChoicePromptHandler instance.</returns>
protected abstract ChoicePromptHandler OnCreateChoicePromptHandler();
/// <summary>
/// Writes output of the given type to the user interface with
/// the given foreground and background colors. Also includes
/// a newline if requested.
/// </summary>
/// <param name="outputString">
/// The output string to be written.
/// </param>
/// <param name="includeNewLine">
/// If true, a newline should be appended to the output's contents.
/// </param>
/// <param name="outputType">
/// Specifies the type of output to be written.
/// </param>
/// <param name="foregroundColor">
/// Specifies the foreground color of the output to be written.
/// </param>
/// <param name="backgroundColor">
/// Specifies the background color of the output to be written.
/// </param>
public abstract void WriteOutput(
string outputString,
bool includeNewLine,
OutputType outputType,
ConsoleColor foregroundColor,
ConsoleColor backgroundColor);
/// <summary>
/// Sends a progress update event to the user.
/// </summary>
/// <param name="sourceId">The source ID of the progress event.</param>
/// <param name="progressDetails">The details of the activity's current progress.</param>
protected abstract void UpdateProgress(
long sourceId,
ProgressDetails progressDetails);
#endregion
#region IHostInput Implementation
#endregion
#region PSHostUserInterface Implementation
/// <summary>
///
/// </summary>
/// <param name="promptCaption"></param>
/// <param name="promptMessage"></param>
/// <param name="fieldDescriptions"></param>
/// <returns></returns>
public override Dictionary<string, PSObject> Prompt(
string promptCaption,
string promptMessage,
Collection<FieldDescription> fieldDescriptions)
{
FieldDetails[] fields =
fieldDescriptions
.Select(f => { return FieldDetails.Create(f, this.Logger); })
.ToArray();
CancellationTokenSource cancellationToken = new CancellationTokenSource();
Task<Dictionary<string, object>> promptTask =
this.CreateInputPromptHandler()
.PromptForInputAsync(
promptCaption,
promptMessage,
fields,
cancellationToken.Token);
// Run the prompt task and wait for it to return
this.WaitForPromptCompletion(
promptTask,
"Prompt",
cancellationToken);
// Convert all values to PSObjects
var psObjectDict = new Dictionary<string, PSObject>();
// The result will be null if the prompt was cancelled
if (promptTask.Result != null)
{
// Convert all values to PSObjects
foreach (var keyValuePair in promptTask.Result)
{
psObjectDict.Add(
keyValuePair.Key,
PSObject.AsPSObject(keyValuePair.Value ?? string.Empty));
}
}
// Return the result
return psObjectDict;
}
/// <summary>
///
/// </summary>
/// <param name="promptCaption"></param>
/// <param name="promptMessage"></param>
/// <param name="choiceDescriptions"></param>
/// <param name="defaultChoice"></param>
/// <returns></returns>
public override int PromptForChoice(
string promptCaption,
string promptMessage,
Collection<ChoiceDescription> choiceDescriptions,
int defaultChoice)
{
ChoiceDetails[] choices =
choiceDescriptions
.Select(ChoiceDetails.Create)
.ToArray();
CancellationTokenSource cancellationToken = new CancellationTokenSource();
Task<int> promptTask =
this.CreateChoicePromptHandler()
.PromptForChoiceAsync(
promptCaption,
promptMessage,
choices,
defaultChoice,
cancellationToken.Token);
// Run the prompt task and wait for it to return
this.WaitForPromptCompletion(
promptTask,
"PromptForChoice",
cancellationToken);
// Return the result
return promptTask.Result;
}
/// <summary>
///
/// </summary>
/// <param name="promptCaption"></param>
/// <param name="promptMessage"></param>
/// <param name="userName"></param>
/// <param name="targetName"></param>
/// <param name="allowedCredentialTypes"></param>
/// <param name="options"></param>
/// <returns></returns>
public override PSCredential PromptForCredential(
string promptCaption,
string promptMessage,
string userName,
string targetName,
PSCredentialTypes allowedCredentialTypes,
PSCredentialUIOptions options)
{
CancellationTokenSource cancellationToken = new CancellationTokenSource();
Task<Dictionary<string, object>> promptTask =
this.CreateInputPromptHandler()
.PromptForInputAsync(
promptCaption,
promptMessage,
new FieldDetails[] { new CredentialFieldDetails("Credential", "Credential", userName) },
cancellationToken.Token);
Task<PSCredential> unpackTask =
promptTask.ContinueWith(
task =>
{
if (task.IsFaulted)
{
throw task.Exception;
}
else if (task.IsCanceled)
{
throw new TaskCanceledException(task);
}
// Return the value of the sole field
return (PSCredential)task.Result?["Credential"];
});
// Run the prompt task and wait for it to return
this.WaitForPromptCompletion(
unpackTask,
"PromptForCredential",
cancellationToken);
return unpackTask.Result;
}
/// <summary>
///
/// </summary>
/// <param name="caption"></param>
/// <param name="message"></param>
/// <param name="userName"></param>
/// <param name="targetName"></param>
/// <returns></returns>
public override PSCredential PromptForCredential(
string caption,
string message,
string userName,
string targetName)
{
return this.PromptForCredential(
caption,
message,
userName,
targetName,
PSCredentialTypes.Default,
PSCredentialUIOptions.Default);
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override PSHostRawUserInterface RawUI
{
get { return this.rawUserInterface; }
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override string ReadLine()
{
CancellationTokenSource cancellationToken = new CancellationTokenSource();
Task<string> promptTask =
this.CreateInputPromptHandler()
.PromptForInputAsync(cancellationToken.Token);
// Run the prompt task and wait for it to return
this.WaitForPromptCompletion(
promptTask,
"ReadLine",
cancellationToken);
return promptTask.Result;
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public override SecureString ReadLineAsSecureString()
{
CancellationTokenSource cancellationToken = new CancellationTokenSource();
Task<SecureString> promptTask =
this.CreateInputPromptHandler()
.PromptForSecureInputAsync(cancellationToken.Token);
// Run the prompt task and wait for it to return
this.WaitForPromptCompletion(
promptTask,
"ReadLineAsSecureString",
cancellationToken);
return promptTask.Result;
}
/// <summary>
///
/// </summary>
/// <param name="foregroundColor"></param>
/// <param name="backgroundColor"></param>
/// <param name="value"></param>
public override void Write(
ConsoleColor foregroundColor,
ConsoleColor backgroundColor,
string value)
{
this.WriteOutput(
value,
false,
OutputType.Normal,
foregroundColor,
backgroundColor);
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
public override void Write(string value)
{
this.WriteOutput(
value,
false,
OutputType.Normal,
this.rawUserInterface.ForegroundColor,
this.rawUserInterface.BackgroundColor);
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
public override void WriteLine(string value)
{
this.WriteOutput(
value,
true,
OutputType.Normal,
this.rawUserInterface.ForegroundColor,
this.rawUserInterface.BackgroundColor);
}
/// <summary>
///
/// </summary>
/// <param name="message"></param>
public override void WriteDebugLine(string message)
{
this.WriteOutput(
DebugMessagePrefix + message,
true,
OutputType.Debug,
foregroundColor: this.DebugForegroundColor,
backgroundColor: this.DebugBackgroundColor);
}
/// <summary>
///
/// </summary>
/// <param name="message"></param>
public override void WriteVerboseLine(string message)
{
this.WriteOutput(
VerboseMessagePrefix + message,
true,
OutputType.Verbose,
foregroundColor: this.VerboseForegroundColor,
backgroundColor: this.VerboseBackgroundColor);
}
/// <summary>
///
/// </summary>
/// <param name="message"></param>
public override void WriteWarningLine(string message)
{
this.WriteOutput(
WarningMessagePrefix + message,
true,
OutputType.Warning,
foregroundColor: this.WarningForegroundColor,
backgroundColor: this.WarningBackgroundColor);
}
/// <summary>
///
/// </summary>
/// <param name="value"></param>
public override void WriteErrorLine(string value)
{
// PowerShell's ConsoleHost also skips over empty lines:
// https://github.com/PowerShell/PowerShell/blob/8e683972284a5a7f773ea6d027d9aac14d7e7524/src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHostUserInterface.cs#L1334-L1337
if (string.IsNullOrEmpty(value))
{
return;
}
this.WriteOutput(
value,
true,
OutputType.Error,
foregroundColor: this.ErrorForegroundColor,
backgroundColor: this.ErrorBackgroundColor);
}
/// <summary>
/// Invoked by <see cref="Cmdlet.WriteProgress(ProgressRecord)" /> to display a progress record.
/// </summary>
/// <param name="sourceId">
/// Unique identifier of the source of the record. An int64 is used because typically,
/// the 'this' pointer of the command from whence the record is originating is used, and
/// that may be from a remote Runspace on a 64-bit machine.
/// </param>
/// <param name="record">
/// The record being reported to the host.
/// </param>
public sealed override void WriteProgress(
long sourceId,
ProgressRecord record)
{
// Maintain old behavior if this isn't overridden.
if (!this.SupportsWriteProgress)
{
this.UpdateProgress(sourceId, ProgressDetails.Create(record));
return;
}
// Keep a list of progress records we write so we can automatically
// clean them up after the pipeline ends.
if (record.RecordType == ProgressRecordType.Completed)
{
this.currentProgressMessages.TryRemove(new ProgressKey(sourceId, record), out _);
}
else
{
// Adding with a value of null here because we don't actually need a dictionary. We're
// only using ConcurrentDictionary<,> becuase there is no ConcurrentHashSet<>.
this.currentProgressMessages.TryAdd(new ProgressKey(sourceId, record), null);
}
this.WriteProgressImpl(sourceId, record);
}
/// <summary>
/// Invoked by <see cref="Cmdlet.WriteProgress(ProgressRecord)" /> to display a progress record.
/// </summary>
/// <param name="sourceId">
/// Unique identifier of the source of the record. An int64 is used because typically,
/// the 'this' pointer of the command from whence the record is originating is used, and
/// that may be from a remote Runspace on a 64-bit machine.
/// </param>
/// <param name="record">
/// The record being reported to the host.
/// </param>
protected virtual void WriteProgressImpl(long sourceId, ProgressRecord record)
{
}
internal void ClearProgress()
{
const string nonEmptyString = "noop";
if (!this.SupportsWriteProgress)
{
return;
}
foreach (ProgressKey key in this.currentProgressMessages.Keys)
{
// This constructor throws if the activity description is empty even
// with completed records.
var record = new ProgressRecord(
key.ActivityId,
activity: nonEmptyString,
statusDescription: nonEmptyString);
record.RecordType = ProgressRecordType.Completed;
this.WriteProgressImpl(key.SourceId, record);
}
this.currentProgressMessages.Clear();
}
#endregion
#region IHostUISupportsMultipleChoiceSelection Implementation
/// <summary>
///
/// </summary>
/// <param name="promptCaption"></param>
/// <param name="promptMessage"></param>
/// <param name="choiceDescriptions"></param>
/// <param name="defaultChoices"></param>
/// <returns></returns>
public Collection<int> PromptForChoice(
string promptCaption,
string promptMessage,
Collection<ChoiceDescription> choiceDescriptions,
IEnumerable<int> defaultChoices)
{
ChoiceDetails[] choices =
choiceDescriptions
.Select(ChoiceDetails.Create)
.ToArray();
CancellationTokenSource cancellationToken = new CancellationTokenSource();
Task<int[]> promptTask =
this.CreateChoicePromptHandler()
.PromptForChoiceAsync(
promptCaption,
promptMessage,
choices,
defaultChoices.ToArray(),
cancellationToken.Token);
// Run the prompt task and wait for it to return
this.WaitForPromptCompletion(
promptTask,
"PromptForChoice",
cancellationToken);
// Return the result
return new Collection<int>(promptTask.Result.ToList());
}
#endregion
#region Private Methods
private Coordinates lastPromptLocation;
private async Task WritePromptStringToHostAsync(CancellationToken cancellationToken)
{
try
{
if (this.lastPromptLocation != null &&
this.lastPromptLocation.X == await ConsoleProxy.GetCursorLeftAsync(cancellationToken).ConfigureAwait(false) &&
this.lastPromptLocation.Y == await ConsoleProxy.GetCursorTopAsync(cancellationToken).ConfigureAwait(false))
{
return;
}
}
// When output is redirected (like when running tests) attempting to get
// the cursor position will throw.
catch (System.IO.IOException)
{
}
PSCommand promptCommand = new PSCommand().AddScript("prompt");
cancellationToken.ThrowIfCancellationRequested();
string promptString =
(await this.powerShellContext.ExecuteCommandAsync<PSObject>(promptCommand, false, false).ConfigureAwait(false))
.Select(pso => pso.BaseObject)
.OfType<string>()
.FirstOrDefault() ?? "PS> ";
// Add the [DBG] prefix if we're stopped in the debugger and the prompt doesn't already have [DBG] in it
if (this.powerShellContext.IsDebuggerStopped && !promptString.Contains("[DBG]"))
{
promptString =
string.Format(
CultureInfo.InvariantCulture,
"[DBG]: {0}",
promptString);
}
// Update the stored prompt string if the session is remote
if (this.powerShellContext.CurrentRunspace.Location == RunspaceLocation.Remote)
{
promptString =
string.Format(
CultureInfo.InvariantCulture,
"[{0}]: {1}",
this.powerShellContext.CurrentRunspace.Runspace.ConnectionInfo != null
? this.powerShellContext.CurrentRunspace.Runspace.ConnectionInfo.ComputerName
: this.powerShellContext.CurrentRunspace.SessionDetails.ComputerName,
promptString);
}
cancellationToken.ThrowIfCancellationRequested();
// Write the prompt string
this.WriteOutput(promptString, false);
this.lastPromptLocation = new Coordinates(
await ConsoleProxy.GetCursorLeftAsync(cancellationToken).ConfigureAwait(false),
await ConsoleProxy.GetCursorTopAsync(cancellationToken).ConfigureAwait(false));
}
private void WriteDebuggerBanner(DebuggerStopEventArgs eventArgs)
{
// TODO: What do we display when we don't know why we stopped?
if (eventArgs.Breakpoints.Count > 0)
{
// The breakpoint classes have nice ToString output so use that
this.WriteOutput(
Environment.NewLine + $"Hit {eventArgs.Breakpoints[0].ToString()}\n",
true,
OutputType.Normal,
ConsoleColor.Blue);
}
}
internal static ConsoleColor BackgroundColor { get; set; }
internal ConsoleColor FormatAccentColor { get; set; } = ConsoleColor.Green;
internal ConsoleColor ErrorAccentColor { get; set; } = ConsoleColor.Cyan;
internal ConsoleColor ErrorForegroundColor { get; set; } = ConsoleColor.Red;
internal ConsoleColor ErrorBackgroundColor { get; set; } = BackgroundColor;
internal ConsoleColor WarningForegroundColor { get; set; } = ConsoleColor.Yellow;
internal ConsoleColor WarningBackgroundColor { get; set; } = BackgroundColor;
internal ConsoleColor DebugForegroundColor { get; set; } = ConsoleColor.Yellow;
internal ConsoleColor DebugBackgroundColor { get; set; } = BackgroundColor;
internal ConsoleColor VerboseForegroundColor { get; set; } = ConsoleColor.Yellow;
internal ConsoleColor VerboseBackgroundColor { get; set; } = BackgroundColor;
internal ConsoleColor ProgressForegroundColor { get; set; } = ConsoleColor.Yellow;
internal ConsoleColor ProgressBackgroundColor { get; set; } = ConsoleColor.DarkCyan;
private async Task StartReplLoopAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
string commandString = null;
try
{
await this.WritePromptStringToHostAsync(cancellationToken).ConfigureAwait(false);
}
catch (OperationCanceledException)
{
break;
}
try
{
commandString = await this.ReadCommandLineAsync(cancellationToken).ConfigureAwait(false);
}
catch (PipelineStoppedException)
{
this.WriteOutput(
"^C",
true,
OutputType.Normal,
foregroundColor: ConsoleColor.Red);
}
// Do nothing here, the while loop condition will exit.
catch (TaskCanceledException)
{ }
catch (OperationCanceledException)
{ }
catch (Exception e) // Narrow this if possible
{
this.WriteOutput(
$"\n\nAn error occurred while reading input:\n\n{e.ToString()}\n",
true,
OutputType.Error);
Logger.LogException("Caught exception while reading command line", e);
}
finally
{
// This supplies the newline in the Legacy ReadLine when executing code in the terminal via hitting the ENTER key
// or Ctrl+C. Without this, hitting ENTER with a no input looks like it does nothing (no new prompt is written)
// and also the output would show up on the same line as the code you wanted to execute (the prompt line).
// This is AlSO applied to PSReadLine for the Ctrl+C scenario which appears like it does nothing...
// TODO: This still gives an extra newline when you hit ENTER in the PSReadLine experience. We should figure
// out if there's any way to avoid that... but unfortunately, in both scenarios, we only see that empty
// string is returned.
if (!cancellationToken.IsCancellationRequested)
{
this.WriteLine();
}
}
if (!string.IsNullOrWhiteSpace(commandString))
{
var unusedTask =
this.powerShellContext
.ExecuteScriptStringAsync(
commandString,
writeInputToHost: false,
writeOutputToHost: true,
addToHistory: true)
.ConfigureAwait(continueOnCapturedContext: false);
break;
}
}
}
private InputPromptHandler CreateInputPromptHandler()
{
if (this.activePromptHandler != null)
{
Logger.LogError(
"Prompt handler requested while another prompt is already active.");
}
InputPromptHandler inputPromptHandler = this.OnCreateInputPromptHandler();
this.activePromptHandler = inputPromptHandler;
this.activePromptHandler.PromptCancelled += activePromptHandler_PromptCancelled;
return inputPromptHandler;
}
private ChoicePromptHandler CreateChoicePromptHandler()
{
if (this.activePromptHandler != null)
{
Logger.LogError(
"Prompt handler requested while another prompt is already active.");
}
ChoicePromptHandler choicePromptHandler = this.OnCreateChoicePromptHandler();
this.activePromptHandler = choicePromptHandler;
this.activePromptHandler.PromptCancelled += activePromptHandler_PromptCancelled;
return choicePromptHandler;
}
private void activePromptHandler_PromptCancelled(object sender, EventArgs e)
{
// Clean up the existing prompt
this.activePromptHandler.PromptCancelled -= activePromptHandler_PromptCancelled;
this.activePromptHandler = null;
}
private void WaitForPromptCompletion<TResult>(
Task<TResult> promptTask,
string promptFunctionName,
CancellationTokenSource cancellationToken)
{
try
{
// This will synchronously block on the prompt task
// method which gets run on another thread.
promptTask.Wait();
if (promptTask.Status == TaskStatus.WaitingForActivation)
{
// The Wait() call has timed out, cancel the prompt
cancellationToken.Cancel();
this.WriteOutput("\r\nPrompt has been cancelled due to a timeout.\r\n");
throw new PipelineStoppedException();
}
}
catch (AggregateException e)
{
// Find the right InnerException
Exception innerException = e.InnerException;
while (innerException is AggregateException)
{
innerException = innerException.InnerException;
}
// Was the task cancelled?
if (innerException is TaskCanceledException)
{
// Stop the pipeline if the prompt was cancelled
throw new PipelineStoppedException();
}
else if (innerException is PipelineStoppedException)
{
// The prompt is being cancelled, rethrow the exception
throw innerException;
}
else
{
// Rethrow the exception
throw new Exception(
string.Format(
"{0} failed, check inner exception for details",
promptFunctionName),
innerException);
}
}
}
private void PowerShellContext_DebuggerStop(object sender, System.Management.Automation.DebuggerStopEventArgs e)
{
if (!this.IsCommandLoopRunning)
{
StartCommandLoop();
return;
}
// Cancel any existing prompt first
this.CancelCommandPrompt();
this.WriteDebuggerBanner(e);
this.ShowCommandPrompt();
}
private void PowerShellContext_DebuggerResumed(object sender, System.Management.Automation.DebuggerResumeAction e)
{
this.CancelCommandPrompt();
}
private void PowerShellContext_ExecutionStatusChanged(object sender, ExecutionStatusChangedEventArgs eventArgs)
{
// The command loop should only be manipulated if it's already started
if (eventArgs.ExecutionStatus == ExecutionStatus.Aborted)
{
this.ClearProgress();
// When aborted, cancel any lingering prompts
if (this.activePromptHandler != null)
{
this.activePromptHandler.CancelPrompt();