From a494516c5de90007a169927472f70e04781bf72e Mon Sep 17 00:00:00 2001 From: George Fleming Date: Tue, 23 Feb 2016 12:53:47 -0800 Subject: [PATCH 1/3] Handle multi-line commands --- src/Microsoft.PowerShell.Linux.Host/main.cs | 45 ++++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.PowerShell.Linux.Host/main.cs b/src/Microsoft.PowerShell.Linux.Host/main.cs index e8605c79f5..a214846c69 100644 --- a/src/Microsoft.PowerShell.Linux.Host/main.cs +++ b/src/Microsoft.PowerShell.Linux.Host/main.cs @@ -167,6 +167,16 @@ OPTIONS /// private bool _showHelpMessage; + /// + /// To keep track whether last entered command was complete + /// + private bool incompleteLine = false; + + /// + /// To store incomplete lines + /// + private string partialLine = string.Empty; + /// /// Gets or sets a value indicating whether the host application /// should exit. @@ -270,7 +280,11 @@ OPTIONS return returnVal; } - // TODO: if we are in block mode, sep the prompt to ">> " + if (incompleteLine) + { + return ">> "; + } + Pipeline pipeline = rs.CreatePipeline(); Command promptCommand = new Command("prompt"); @@ -343,7 +357,15 @@ OPTIONS { this.currentPowerShell.Runspace = this.myRunSpace; - this.currentPowerShell.AddScript(cmd); + if (incompleteLine) + { + this.currentPowerShell.AddScript(partialLine + cmd); + } + else + { + this.currentPowerShell.AddScript(cmd); + } + incompleteLine = false; // Add the default outputter to the end of the pipe and then call the // MergeMyResults method to merge the output and error streams from the @@ -366,6 +388,20 @@ OPTIONS this.currentPowerShell.Invoke(null, settings); } } + catch (IncompleteParseException e) + { + incompleteLine = true; + partialLine += cmd; + + while (partialLine[partialLine.Length - 1] == '`') + { + Console.WriteLine("Trimming..."); + partialLine = partialLine.Remove(partialLine.Length - 1); + } + + partialLine += " "; + } + finally { // Dispose the PowerShell object and set currentPowerShell to null. @@ -376,6 +412,11 @@ OPTIONS this.currentPowerShell.Dispose(); this.currentPowerShell = null; } + + if (!incompleteLine) + { + partialLine = string.Empty; + } } } From e92c1e80cc649cfbdcb60a283bf12157890f1d82 Mon Sep 17 00:00:00 2001 From: George Fleming Date: Tue, 23 Feb 2016 15:10:32 -0800 Subject: [PATCH 2/3] Add support for here-string --- src/Microsoft.PowerShell.Linux.Host/main.cs | 29 ++++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.PowerShell.Linux.Host/main.cs b/src/Microsoft.PowerShell.Linux.Host/main.cs index a214846c69..63f8cdd7c7 100644 --- a/src/Microsoft.PowerShell.Linux.Host/main.cs +++ b/src/Microsoft.PowerShell.Linux.Host/main.cs @@ -177,6 +177,11 @@ OPTIONS /// private string partialLine = string.Empty; + /// + /// To keep track of here-strings + /// + private bool hereString = false; + /// /// Gets or sets a value indicating whether the host application /// should exit. @@ -391,15 +396,24 @@ OPTIONS catch (IncompleteParseException e) { incompleteLine = true; + cmd = cmd.Trim(); partialLine += cmd; - - while (partialLine[partialLine.Length - 1] == '`') - { - Console.WriteLine("Trimming..."); - partialLine = partialLine.Remove(partialLine.Length - 1); - } - partialLine += " "; + if (hereString || cmd.EndsWith("@\"") || cmd.EndsWith("@\'")) + { + hereString = true; + + partialLine += System.Environment.NewLine; + } + else + { + while (partialLine[partialLine.Length - 1] == '`') + { + partialLine = partialLine.Remove(partialLine.Length - 1); + } + + partialLine += " "; + } } finally @@ -416,6 +430,7 @@ OPTIONS if (!incompleteLine) { partialLine = string.Empty; + hereString = false; } } } From 6776a909b21a9fb05442689b761b4d9c33e5db9f Mon Sep 17 00:00:00 2001 From: George Fleming Date: Tue, 23 Feb 2016 15:23:12 -0800 Subject: [PATCH 3/3] Simply code --- src/Microsoft.PowerShell.Linux.Host/main.cs | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) diff --git a/src/Microsoft.PowerShell.Linux.Host/main.cs b/src/Microsoft.PowerShell.Linux.Host/main.cs index 63f8cdd7c7..d0f8e0f751 100644 --- a/src/Microsoft.PowerShell.Linux.Host/main.cs +++ b/src/Microsoft.PowerShell.Linux.Host/main.cs @@ -177,11 +177,6 @@ OPTIONS /// private string partialLine = string.Empty; - /// - /// To keep track of here-strings - /// - private bool hereString = false; - /// /// Gets or sets a value indicating whether the host application /// should exit. @@ -399,21 +394,8 @@ OPTIONS cmd = cmd.Trim(); partialLine += cmd; - if (hereString || cmd.EndsWith("@\"") || cmd.EndsWith("@\'")) - { - hereString = true; + partialLine += System.Environment.NewLine; - partialLine += System.Environment.NewLine; - } - else - { - while (partialLine[partialLine.Length - 1] == '`') - { - partialLine = partialLine.Remove(partialLine.Length - 1); - } - - partialLine += " "; - } } finally @@ -430,7 +412,6 @@ OPTIONS if (!incompleteLine) { partialLine = string.Empty; - hereString = false; } } }