@@ -109,6 +109,9 @@ Prints this text.
109
109
}
110
110
// TODO: check for input on stdin
111
111
112
+ ConsoleColor InitialForegroundColor = Console . ForegroundColor ;
113
+ ConsoleColor InitialBackgroundColor = Console . BackgroundColor ;
114
+
112
115
// Create the listener and run it
113
116
Listener l
10000
istener = new Listener ( initialScript , loadProfiles ) ;
114
117
@@ -118,6 +121,9 @@ Prints this text.
118
121
listener . Run ( ) ;
119
122
}
120
123
124
+ Console . ForegroundColor = InitialForegroundColor ;
125
+ Console . BackgroundColor = InitialBackgroundColor ;
126
+
121
127
// Exit with the desired exit code that was set by the exit command.
122
128
// The exit code is set in the host by the MyHost.SetShouldExit() method.
123
129
System . Environment . Exit ( listener . ExitCode ) ;
@@ -129,7 +135,7 @@ internal class Listener
129
135
/// <summary>
130
136
/// Used to read user input.
131
137
/// </summary>
132
- internal ConsoleReadLine consoleReadLine = new ConsoleReadLine ( ) ;
138
+ internal ConsoleReadLine consoleReadLine ;
133
139
134
140
/// <summary>
135
141
/// Holds a reference to the runspace for this interpeter.
@@ -213,6 +219,7 @@ public Listener(string initialScript, bool loadProfiles)
213
219
InitialSessionState iss = InitialSessionState . CreateDefault2 ( ) ;
214
220
this . myRunSpace = RunspaceFactory . CreateRunspace ( this . myHost , iss ) ;
215
221
this . myRunSpace . Open ( ) ;
222
+ this . consoleReadLine = new ConsoleReadLine ( this . myHost . Runspace , this . myHost . UI ) ;
216
223
217
224
if ( this . myRunSpace . Debugger != null )
218
225
{
@@ -222,7 +229,6 @@ public Listener(string initialScript, bool loadProfiles)
222
229
// feature. In order to debug Workflow script functions the debugger
223
230
// DebugMode must include the DebugModes.LocalScript flag.
224
231
this . myRunSpace . Debugger . SetDebugMode ( DebugModes . LocalScript ) ;
225
-
226
232
}
227
233
228
234
if ( loadProfiles )
@@ -360,14 +366,8 @@ private void ExecuteHelper(string cmd, object input)
360
366
{
361
367
this . currentPowerShell . Runspace = this . myRunSpace ;
362
368
363
- if ( incompleteLine )
364
- {
365
- this . currentPowerShell . AddScript ( partialLine + cmd ) ;
366
- }
367
- else
368
- {
369
- this . currentPowerShell . AddScript ( cmd ) ;
370
- }
369
+ string fullCommand = incompleteLine ? ( partialLine + cmd ) : cmd ;
370
+ this . currentPowerShell . AddScript ( fullCommand ) ;
371
371
incompleteLine = false ;
372
372
373
373
// Add the default outputter to the end of the pipe and then call the
@@ -394,11 +394,7 @@ private void ExecuteHelper(string cmd, object input)
394
394
catch ( IncompleteParseException )
395
395
{
396
396
incompleteLine = true ;
397
- cmd = cmd . Trim ( ) ;
398
- partialLine += cmd ;
399
-
400
- partialLine += System . Environment . NewLine ;
401
-
397
+ partialLine = $ "{ partialLine } { cmd } { System . Environment . NewLine } ";
402
398
}
403
399
404
400
finally
@@ -526,7 +522,6 @@ private void HandleControlC(object sender, ConsoleCancelEventArgs e)
526
522
this . currentPowerShell . Stop ( ) ;
527
523
}
528
524
}
529
-
530
525
e . Cancel = true ;
531
526
}
532
527
catch ( Exception exception )
@@ -544,7 +539,8 @@ internal void Run()
544
539
{
545
540
// Set up the control-C handler.
546
541
Console . CancelKeyPress += new ConsoleCancelEventHandler ( this . HandleControlC ) ;
547
- //Console.TreatControlCAsInput = false;
542
+
543
+ string initialCommand = String . Empty ;
548
544
549
545
// Read commands and run them until the ShouldExit flag is set by
550
546
// the user calling "exit".
@@ -564,9 +560,26 @@ internal void Run()
564
560
prompt = "PS> " ;
565
561
}
566
562
567
- this . myHost . UI . Write ( ConsoleColor . White , Console . BackgroundColor , prompt ) ;
568
- string cmd = consoleReadLine . Read ( this . myHost . Runspace , false ) ;
569
- this . Execute ( cmd ) ;
563
+ this . myHost . UI . Write ( prompt ) ;
564
+
565
+ ConsoleReadLine . ReadResult result = consoleReadLine . Read ( false , initialCommand ) ;
566
+
567
+ switch ( result . state )
568
+ {
569
+ case ConsoleReadLine . ReadResult . State . Abort :
570
+ incompleteLine = false ;
571
+ partialLine = string . Empty ;
572
+ initialCommand = String . Empty ;
573
+ break ;
574
+ case ConsoleReadLine . ReadResult . State . Redraw :
575
+ initialCommand = result . command ;
576
+ break ;
577
+ case ConsoleReadLine . ReadResult . State . Complete :
578
+ default :
579
+ this . Execute ( result . command ) ;
580
+ initialCommand = String . Empty ;
581
+ break ;
582
+ }
570
583
}
571
584
}
572
585
@@ -582,20 +595,39 @@ private void HandleDebuggerStopEvent(object sender, DebuggerStopEventArgs args)
582
595
583
596
WriteDebuggerStopMessages ( args ) ;
584
597
598
+ string initialCommand = String . Empty ;
599
+
585
600
// loop to process Debugger commands.
586
601
while ( resumeAction == null )
587
602
{
588
- Console . Write ( "[DBG] PS >> " ) ;
589
- string command = consoleReadLine . Read ( this . myHost . Runspace , true ) ;
590
- Console . WriteLine ( ) ;
603
+ string prompt = incompleteLine ? ">> " : "[DBG] PS >> " ;
604
+ this . myHost . UI . Write ( prompt ) ;
605
+
606
+ ConsoleReadLine . ReadResult result = consoleReadLine . Read ( true , initialCommand ) ;
607
+
608
+ switch ( result . state )
609
+ {
610
+ case ConsoleReadLine . ReadResult . State . Abort :
611
+ incompleteLine = false ;
612
+ partialLine = string . Empty ;
613
+ initialCommand = String . Empty ;
614
+ continue ;
615
+ case ConsoleReadLine . ReadResult . State . Redraw :
616
+ initialCommand = result . command ;
617
+ continue ;
618
+ case ConsoleReadLine . ReadResult . State . Complete :
619
+ default :
620
+ initialCommand = String . Empty ;
621
+ break ;
622
+ }
591
623
592
624
// Stream output from command processing to console.
593
625
var output = new PSDataCollection < PSObject > ( ) ;
594
626
output . DataAdded += ( dSender , dArgs ) =>
595
627
{
596
628
foreach ( var item in output . ReadAll ( ) )
597
629
{
598
- Console . WriteLine ( item ) ;
630
+ this . myHost . UI . WriteLine ( item . ToString ( ) ) ;
599
631
}
600
632
} ;
601
633
@@ -606,10 +638,30 @@ private void HandleDebuggerStopEvent(object sender, DebuggerStopEventArgs args)
606
638
// command or script. The returned DebuggerCommandResults object will indicate
607
639
// whether the command was evaluated by the debugger and if the debugger should
608
640
// be released with a specific resume action.
641
+
609
642
PSCommand psCommand = new PSCommand ( ) ;
610
- psCommand . AddScript ( command ) . AddCommand ( "Out-String" ) . AddParameter ( "Stream" , true ) ;
611
- DebuggerCommandResults results = debugger . ProcessCommand ( psCommand , output ) ;
612
- if ( results . ResumeAction != null )
643
+
644
+ string fullCommand = incompleteLine ? ( partialLine + result . command ) : result . command ;
645
+ psCommand . AddScript ( fullCommand ) . AddCommand ( "Out-String" ) . AddParameter ( "Stream" , true ) ;
646
+ incompleteLine = false ;
647
+
648
+ DebuggerCommandResults results = null ;
649
+ try
650
+ {
651
+ results = debugger . ProcessCommand ( psCommand , output ) ;
652
+ }
653
+ catch ( IncompleteParseException )
654
+ {
655
+ incompleteLine = true ;
656
+ partialLine = $ "{ partialLine } { result . command } { System . Environment . NewLine } ";
657
+ }
658
+
659
+ if ( ! incompleteLine )
660
+ {
661
+ partialLine = string . Empty ;
662
+ }
663
+
664
+ if ( ! incompleteLine && results . ResumeAction != null )
613
665
{
614
666
resumeAction = results . ResumeAction ;
615
667
}
@@ -632,30 +684,30 @@ private void WriteDebuggerStopMessages(DebuggerStopEventArgs args)
632
684
// Show help message only once.
633
685
if ( ! _showHelpMessage )
634
686
{
635
- Console . WriteLine ( "Entering debug mode. Type 'h' to get help." ) ;
636
- Console . WriteLine ( ) ;
687
+ this . myHost . UI . WriteLine ( "Entering debug mode. Type 'h' to get help." ) ;
688
+ this . myHost . UI . WriteLine ( ) ;
637
689
_showHelpMessage = true ;
638
690
}
639
691
640
692
// Breakpoint stop information. Writes all breakpoints that
641
693
// pertain to this debugger execution stop point.
642
694
if ( args . Breakpoints . Count > 0 )
643
695
{
644
- Console . WriteLine ( "Debugger hit breakpoint on:" ) ;
696
+ this . myHost . UI . WriteLine ( "Debugger hit breakpoint on:" ) ;
645
697
foreach ( var breakPoint in args . Breakpoints )
646
698
{
647
- Console . WriteLine ( breakPoint . ToString ( ) ) ;
699
+ this . myHost . UI . WriteLine ( breakPoint . ToString ( ) ) ;
648
700
}
649
- Console . WriteLine ( ) ;
701
+ this . myHost . UI . WriteLine ( ) ;
650
702
}
651
703
652
704
// Script position stop information.
653
705
// This writes the InvocationInfo position message if
654
706
// there is one.
655
707
if ( args . InvocationInfo != null )
656
708
{
657
- Console . WriteLine ( args . InvocationInfo . PositionMessage ) ;
658
- Console . WriteLine ( ) ;
709
+ this . myHost . UI . WriteLine ( args . InvocationInfo . PositionMessage ) ;
710
+ this . myHost . UI . WriteLine ( ) ;
659
711
}
660
712
661
713
Console . ForegroundColor = saveFGColor ;
0 commit comments