10000 Merge pull request #730 from PowerShell/rawui2 · PowerShell/PowerShell@06ab38e · GitHub
[go: up one dir, main page]

Skip to content

Commit 06ab38e

Browse files
committed
Merge pull request #730 from PowerShell/rawui2
MegaPatch that includes all of #717, plus RawUI fixes
2 parents 00c2f53 + 5e9c311 commit 06ab38e

File tree

4 files changed

+575
-173
lines changed

4 files changed

+575
-173
lines changed

src/Microsoft.PowerShell.Linux.Host/main.cs

Lines changed: 86 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ Prints this text.
109109
}
110110
// TODO: check for input on stdin
111111

112+
ConsoleColor InitialForegroundColor = Console.ForegroundColor;
113+
ConsoleColor InitialBackgroundColor = Console.BackgroundColor;
114+
112115
// Create the listener and run it
113116
Listener l 10000 istener = new Listener(initialScript, loadProfiles);
114117

@@ -118,6 +121,9 @@ Prints this text.
118121
listener.Run();
119122
}
120123

124+
Console.ForegroundColor = InitialForegroundColor;
125+
Console.BackgroundColor = InitialBackgroundColor;
126+
121127
// Exit with the desired exit code that was set by the exit command.
122128
// The exit code is set in the host by the MyHost.SetShouldExit() method.
123129
System.Environment.Exit(listener.ExitCode);
@@ -129,7 +135,7 @@ internal class Listener
129135
/// <summary>
130136
/// Used to read user input.
131137
/// </summary>
132-
internal ConsoleReadLine consoleReadLine = new ConsoleReadLine();
138+
internal ConsoleReadLine consoleReadLine;
133139

134140
/// <summary>
135141
/// Holds a reference to the runspace for this interpeter.
@@ -213,6 +219,7 @@ public Listener(string initialScript, bool loadProfiles)
213219
InitialSessionState iss = InitialSessionState.CreateDefault2();
214220
this.myRunSpace = RunspaceFactory.CreateRunspace(this.myHost, iss);
215221
this.myRunSpace.Open();
222+
this.consoleReadLine = new ConsoleReadLine(this.myHost.Runspace, this.myHost.UI);
216223

217224
if (this.myRunSpace.Debugger != null)
218225
{
@@ -222,7 +229,6 @@ public Listener(string initialScript, bool loadProfiles)
222229
// feature. In order to debug Workflow script functions the debugger
223230
// DebugMode must include the DebugModes.LocalScript flag.
224231
this.myRunSpace.Debugger.SetDebugMode(DebugModes.LocalScript);
225-
226232
}
227233

228234
if (loadProfiles)
@@ -360,14 +366,8 @@ private void ExecuteHelper(string cmd, object input)
360366
{
361367
this.currentPowerShell.Runspace = this.myRunSpace;
362368

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);
371371
incompleteLine = false;
372372

373373
// 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)
394394
catch (IncompleteParseException)
395395
{
396396
incompleteLine = true;
397-
cmd = cmd.Trim();
398-
partialLine += cmd;
399-
400-
partialLine += System.Environment.NewLine;
401-
397+
partialLine = $"{partialLine}{cmd}{System.Environment.NewLine}";
402398
}
403399

404400
finally
@@ -526,7 +522,6 @@ private void HandleControlC(object sender, ConsoleCancelEventArgs e)
526522
this.currentPowerShell.Stop();
527523
}
528524
}
529-
530525
e.Cancel = true;
531526
}
532527
catch (Exception exception)
@@ -544,7 +539,8 @@ internal void Run()
544539
{
545540
// Set up the control-C handler.
546541
Console.CancelKeyPress += new ConsoleCancelEventHandler(this.HandleControlC);
547-
//Console.TreatControlCAsInput = false;
542+
543+
string initialCommand = String.Empty;
548544

549545
// Read commands and run them until the ShouldExit flag is set by
550546
// the user calling "exit".
@@ -564,9 +560,26 @@ internal void Run()
564560
prompt = "PS> ";
565561
}
566562

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+
}
570583
}
571584
}
572585

@@ -582,20 +595,39 @@ private void HandleDebuggerStopEvent(object sender, DebuggerStopEventArgs args)
582595

583596
WriteDebuggerStopMessages(args);
584597

598+
string initialCommand = String.Empty;
599+
585600
// loop to process Debugger commands.
586601
while (resumeAction == null)
587602
{
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+
}
591623

592624
// Stream output from command processing to console.
593625
var output = new PSDataCollection<PSObject>();
594626
output.DataAdded += (dSender, dArgs) =>
595627
{
596628
foreach (var item in output.ReadAll())
597629
{
598-
Console.WriteLine(item);
630+
this.myHost.UI.WriteLine(item.ToString());
599631
}
600632
};
601633

@@ -606,10 +638,30 @@ private void HandleDebuggerStopEvent(object sender, DebuggerStopEventArgs args)
606638
// command or script. The returned DebuggerCommandResults object will indicate
607639
// whether the command was evaluated by the debugger and if the debugger should
608640
// be released with a specific resume action.
641+
609642
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)
613665
{
614666
resumeAction = results.ResumeAction;
615667
}
@@ -632,30 +684,30 @@ private void WriteDebuggerStopMessages(DebuggerStopEventArgs args)
632684
// Show help message only once.
633685
if (!_showHelpMessage)
634686
{
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();
637689
_showHelpMessage = true;
638690
}
639691

640692
// Breakpoint stop information. Writes all breakpoints that
641693
// pertain to this debugger execution stop point.
642694
if (args.Breakpoints.Count > 0)
643695
{
644-
Console.WriteLine("Debugger hit breakpoint on:");
696+
this.myHost.UI.WriteLine("Debugger hit breakpoint on:");
645697
foreach (var breakPoint in args.Breakpoints)
646698
{
647-
Console.WriteLine(breakPoint.ToString());
699+
this.myHost.UI.WriteLine(breakPoint.ToString());
648700
}
649-
Console.WriteLine();
701+
this.myHost.UI.WriteLine();
650702
}
651703

652704
// Script position stop information.
653705
// This writes the InvocationInfo position message if
654706
// there is one.
655707
if (args.InvocationInfo != null)
656708
{
657-
Console.WriteLine(args.InvocationInfo.PositionMessage);
658-
Console.WriteLine();
709+
this.myHost.UI.WriteLine(args.InvocationInfo.PositionMessage);
710+
this.myHost.UI.WriteLine();
659711
}
660712

661713
Console.ForegroundColor = saveFGColor;

src/Microsoft.PowerShell.Linux.Host/rawui.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,40 @@ public override Size WindowSize
122122
set { Console.SetWindowSize(value.Width, value.Height); }
123123
}
124124

125+
/// <summary>
126+
/// Cached Window Title, for systems that needs it
127+
/// </summary>
128+
private string title = String.Empty;
129+
125130
/// <summary>
126131
/// Gets or sets the title of the displayed window. The example
127132
/// maps the Console.Title property to the value of this property.
128133
/// </summary>
129134
public override string WindowTitle
130135
{
131-
get { return Console.Title; }
132-
set { Console.Title = value; }
136+
get
137+
{
138+
// In Unix/Linux systems, Console.Title current results in a not-implemented
139+
// exception. In that case, we return a cached copy of title that was set earlier.
140+
// Obviously, this will not work if: 1) Title was never set in PowerShell, or 2)
141+
// one sets the windows's title outside of PowerShell.
142+
string result;
143+
try
144+
{
145+
result = Console.Title;
146+
}
147+
catch (PlatformNotSupportedException)
148+
{
149+
return title;
150+
}
151+
return result;
152+
}
153+
154+
set
155+
{
156+
Console.Title = value;
157+
title = value;
158+
}
133159
}
134160

135161
/// <summary>

0 commit comments

Comments
 (0)
0