Fix powershell -Command - handling of backspace

PowerShell can be started with input redirected in different scenarios.

`powershell -Command -` is one scenario where commands are sent to
PowerShell, but this is not a interactive shell scenario, it's really
a server to run commands from some other process.

`powershell -File -` or just regular redirected input means PowerShell
is an interactive shell, but command line editing must be done via
the redirected standard input handle instead of interacting directly
with a console handle.

In this scenario, we want to provide a good editing experience. Today,
we provide the bare minimum, supporting backspace but no cursor movement.

The bug here is that backspace was treated as a backspace in the server mode
but should not have been.
This commit is contained in:
Jason Shirk (POWERSHELL)
2016-09-21 15:01:33 -07:00
parent f095320787
commit 1913244fad
2 changed files with 13 additions and 1 deletions
@@ -1590,7 +1590,9 @@ namespace Microsoft.PowerShell
break;
}
if (c == '\b')
// If NoPrompt is true, we are in a sort of server mode where we shouldn't
// do anything like edit the command line - every character is part of the input.
if (c == '\b' && !NoPrompt)
{
sb.Remove(sb.Length - 1, 1);
}
@@ -227,6 +227,12 @@ Describe "ConsoleHost unit tests" -tags "Feature" {
$process.StandardInput.Write("1+2`n")
$process.StandardOutput.ReadLine() | Should Be "PS> 1+2"
$process.StandardOutput.ReadLine() | Should Be "3"
# Backspace should work as expected
$process.StandardInput.Write("1+2`b3`n")
# A real console should render 2`b3 as just 3, but we're just capturing exactly what is written
$process.StandardOutput.ReadLine() | Should Be "PS> 1+2`b3"
$process.StandardOutput.ReadLine() | Should Be "4"
$process.StandardInput.Close()
$process.StandardOutput.ReadToEnd() | Should Be "PS> "
EnsureChildHasExited $process
@@ -279,6 +285,10 @@ foo
"@)
$process.StandardOutput.ReadLine() | Should Be "in foo"
# Backspace sent through stdin should be in the final string
$process.StandardInput.Write("`"a`bc`".Length`n")
$process.StandardOutput.ReadLine() | Should Be "3"
# Last command with no newline - should be accepted and
# produce output after closing stdin.
$process.StandardInput.Write('22 + 22')