Don't add trailing spaces to last column when using Format-Table (#5568)

* don't add trailing space to last column in table

* fix out-file tests to new behavior of no padding trailing spaces
This commit is contained in:
Steve Lee
2018-02-22 11:47:24 -08:00
committed by Aditya Patwardhan
parent 03ac50ade1
commit bdd6008846
3 changed files with 58 additions and 49 deletions
@@ -244,11 +244,18 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
return null;
StringCollection[] scArray = new StringCollection[validColumnCount];
bool addPadding = true;
for (int k = 0; k < scArray.Length; k++)
{
// for the last column, don't pad it with trailing spaces
if (k == scArray.Length-1)
{
addPadding = false;
}
// obtain a set of tokens for each field
scArray[k] = GenerateMultiLineRowField(values[validColumnArray[k]], validColumnArray[k],
alignment[validColumnArray[k]], ds);
alignment[validColumnArray[k]], ds, addPadding);
// NOTE: the following padding operations assume that we
// pad with a blank (or any character that ALWAYS maps to a single screen cell
@@ -283,7 +290,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
}
// add padding for the columns that are shorter
for (int col = 0; col < scArray.Length; col++)
for (int col = 0; col < scArray.Length-1; col++)
{
int paddingBlanks = _si.columnInfo[validColumnArray[col]].width;
if (col > 0)
@@ -317,15 +324,18 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
return rows;
}
private StringCollection GenerateMultiLineRowField(string val, int k, int alignment, DisplayCells dc)
private StringCollection GenerateMultiLineRowField(string val, int k, int alignment, DisplayCells dc, bool addPadding)
{
StringCollection sc = StringManipulationHelper.GenerateLines(dc, val,
_si.columnInfo[k].width, _si.columnInfo[k].width);
// if length is shorter, do some padding
for (int col = 0; col < sc.Count; col++)
if (addPadding)
{
if (dc.Length(sc[col]) < _si.columnInfo[k].width)
sc[col] = GenerateRowField(sc[col], _si.columnInfo[k].width, alignment, dc);
// if length is shorter, do some padding
for (int col = 0; col < sc.Count; col++)
{
if (dc.Length(sc[col]) < _si.columnInfo[k].width)
sc[col] = GenerateRowField(sc[col], _si.columnInfo[k].width, alignment, dc, addPadding);
}
}
return sc;
}
@@ -334,8 +344,15 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
{
StringBuilder sb = new StringBuilder();
bool addPadding = true;
for (int k = 0; k < _si.columnInfo.Length; k++)
{
// don't pad the last column
if (k == _si.columnInfo.Length -1)
{
addPadding = false;
}
if (_si.columnInfo[k].width <= 0)
{
// skip columns that are not at least a single character wide
@@ -357,12 +374,12 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
sb.Append(StringUtil.Padding(_startColumn));
}
}
sb.Append(GenerateRowField(values[k], _si.columnInfo[k].width, alignment[k], dc));
sb.Append(GenerateRowField(values[k], _si.columnInfo[k].width, alignment[k], dc, addPadding));
}
return sb.ToString();
}
private static string GenerateRowField(string val, int width, int alignment, DisplayCells dc)
private static string GenerateRowField(string val, int width, int alignment, DisplayCells dc, bool addPadding)
{
// make sure the string does not have any embedded <CR> in it
string s = StringManipulationHelper.TruncateAtNewLine(val) ?? "";
@@ -370,7 +387,7 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
string currentValue = s;
int currentValueDisplayLength = dc.Length(currentValue);
if (currentValueDisplayLength < width)
if (addPadding && currentValueDisplayLength < width)
{
// the string is shorter than the width of the column
// need to pad with with blanks to reach the desired width
@@ -499,7 +516,10 @@ namespace Microsoft.PowerShell.Commands.Internal.Format
default:
{
// left align is the default
s += " ";
if (addPadding)
{
s += " ";
}
}
break;
}
@@ -137,44 +137,23 @@ Describe "Format-Table DRT Unit Tests" -Tags "CI" {
$result | Should Match "Jim\s+5678\s+False"
}
It "Format-Table with No Objects for End-To-End should work"{
$p = @{}
$result = $p|Format-Table -Property "foo","bar"|Out-String
It "Format-Table with '<testName>' should return `$null" -TestCases @(
@{ testName = "empty array"; testObject = @{} },
@{ testName = "null" ; testObject = $null }
) {
param ($testObject)
$result = $testObject | Format-Table -Property "foo","bar" | Out-String
$result | Should BeNullOrEmpty
}
It "Format-Table with Null Objects for End-To-End should work"{
$p = $null
$result = $p|Format-Table -Property "foo","bar"|Out-String
$result | Should BeNullOrEmpty
}
#pending on issue#900
It "Format-Table with single line string for End-To-End should work" -pending{
$p = "single line string"
$result = $p|Format-Table -Property "foo","bar"|Out-String
$result | Should BeNullOrEmpty
}
#pending on issue#900
It "Format-Table with multiple line string for End-To-End should work" -pending{
$p = "Line1\nLine2"
$result = $p|Format-Table -Property "foo","bar"|Out-String
$result | Should BeNullOrEmpty
}
#pending on issue#900
It "Format-Table with string sequence for End-To-End should work" -pending{
$p = "Line1","Line2"
$result = $p|Format-Table -Property "foo","bar"|Out-String
$result | Should BeNullOrEmpty
}
#pending on issue#900
It "Format-Table with string sequence for End-To-End should work" -pending{
$p = "Line1","Line2"
$result = $p|Format-Table -Property "foo","bar"|Out-String
$result | Should BeNullOrEmpty
It "Format-Table with '<testName>' string and -Force should output table with requested properties" -TestCases @(
@{ testName = "single line"; testString = "single line string" },
@{ testName = "multi line" ; testString = "line1`nline2" },
@{ testName = "array" ; testString = "line1","line2" }
) {
param ($testString)
$result = $testString | Format-Table -Property "foo","bar" -Force | Out-String
$result.Replace(" ","").Replace([Environment]::NewLine,"") | Should BeExactly "foobar------"
}
It "Format-Table with complex object for End-To-End should work" {
@@ -211,4 +190,14 @@ Describe "Format-Table DRT Unit Tests" -Tags "CI" {
$result3 | Should Match "name\s+sub"
$result3 | Should Match "this is name\s+{x 0 y 0, x 1 y 1}"
}
It "Format-Table should not add trailing whitespace to the header" {
$out = "hello" | Format-Table -Property foo -Force | Out-String
$out.Replace([System.Environment]::NewLine, "") | Should BeExactly "foo---"
}
It "Format-Table should not add trailing whitespace to rows" {
$out = [pscustomobject]@{a=1;b=2} | Format-Table -HideTableHeaders | Out-String
$out.Replace([System.Environment]::NewLine, "") | Should BeExactly "1 2"
}
}
@@ -98,8 +98,8 @@ Describe "Out-File" -Tags "CI" {
$actual = Get-Content $testfile
$actual[0] | Should Be ""
$actual[1] | Should Be "text "
$actual[2] | Should Be "---- "
$actual[1] | Should Be "text"
$actual[2] | Should Be "----"
$actual[3] | Should Be "some te..."
}
@@ -129,7 +129,7 @@ Describe "Out-File" -Tags "CI" {
# reset to not read only so it can be deleted
Set-ItemProperty -Path $testfile -Name IsReadOnly -Value $false
}
It "Should be able to use the 'Path' alias for the 'FilePath' parameter" {
{ Out-File -Path $testfile } | Should Not Throw
}