Fix formatting and output when stdout is redirected

This commit is contained in:
Andrew Schwartzmeyer 2016-05-13 11:29:21 -07:00
parent fa86c89925
commit 44028f0da3
2 changed files with 12 additions and 11 deletions

View file

@ -1545,6 +1545,9 @@ namespace Microsoft.PowerShell
set { Console.BackgroundColor = value; }
}
// TODO: Make wrap width user-customizable.
private static Size WrapSize = new Size(80, 40);
/// <summary>
/// Gets or sets the size of the host buffer. In this example the
/// buffer size is adapted from the Console buffer size members.
@ -1553,19 +1556,17 @@ namespace Microsoft.PowerShell
{
get
{
try
// When stdout is redirected, the buffer size is (0, 0);
// however, this is still queried for use in formatting, so we
// provide the WrapSize.
if (Console.IsOutputRedirected)
{
return WrapSize;
}
else
{
return new Size(Console.BufferWidth, Console.BufferHeight);
}
catch (System.IO.IOException)
{
// When running without a proper terminal (like on AppVeyor)
// the above will throw because there is no available buffer
// information, so we make a good guess here (since this
// size is mostly used for formatting, this lets wrapping
// work well).
return new Size(80, 80);
}
}
set { Console.SetBufferSize(value.Width, value.Height); }
}

View file

@ -1148,7 +1148,7 @@ namespace Microsoft.PowerShell
// we leave a 1-cell margin on the end because if the very last character butts up against the
// edge of the screen buffer, then the console will wrap the line.
List<string> lines = WrapText(text, RawUI.WindowSize.Width - 1);
List<string> lines = WrapText(text, RawUI.BufferSize.Width - 1);
int count = 0;
foreach (string s in lines)
{