Repository navigation
"Get-ChildItem | Format-Wide -AutoSize | Out-String" can't work on Powershell 6.0.1 & 6.0.2 #6471
Description
Activity
Hi, I found an another workaround.
Workaround
Specify
Out-String -Widthparameter explicitly and set the appropriate value.e.g.
Get-ChildItem | Format-Wide -AutoSize | Out-String -Width $Host.UI.RawUI.WindowSize.Width
Cause
When specify
Format-Wide -AutoSizeparameter, display items per row is decided from output width.
In this case, output width is depend onOut-String -Widthvalue.When
-Widthparameter is not specified,int.MaxValueis adopted as output width.
(This behavior is from PowerShell Core 6.0. #5193)When initializing
WideOutputContext,itemsPerRowvalue(=items per row) reachesint.MaxValue.
Therefore, creation of the_bufferbuffer fails, resulting in an error.PowerShell/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs
Lines 1135 to 1170 in 36b71ba
/// <summary> /// initialize column widths /// </summary> internal override void Initialize() { // set the hard wider default, to be used if no other info is available int itemsPerRow = 2; // get the header info and the view hint WideFormattingHint hint = this.InnerCommand.RetrieveFormattingHint() as WideFormattingHint; // give a preference to the hint, if there if (hint != null && hint.maxWidth > 0) { itemsPerRow = TableWriter.ComputeWideViewBestItemsPerRowFit(hint.maxWidth, this.InnerCommand._lo.ColumnNumber); } else if (this.CurrentWideHeaderInfo.columns > 0) { itemsPerRow = this.CurrentWideHeaderInfo.columns; } // create a buffer object to hold partial rows _buffer = new StringValuesBuffer(itemsPerRow); // initialize the writer int[] columnWidths = new int[itemsPerRow]; int[] alignment = new int[itemsPerRow]; for (int k = 0; k < itemsPerRow; k++) { columnWidths[k] = 0; // autosize alignment[k] = TextAlignment.Left; } this.Writer.Initialize(0, this.InnerCommand._lo.ColumnNumber, columnWidths, alignment, false); } Reacted by Ilya and Michael KlementThank you! Now on with Powershell 6.x, my lolcat Module can works well.
- addedIssue-BugIssue has been identified as a bug in the productIssue has been identified as a bug in the productWG-Enginecore PowerShell engine, interpreter, and runtimecore PowerShell engine, interpreter, and runtime
on Mar 24, 2018 Could we use
int.MaxValue-1as init value?Ilya (@iSazonov) No, but we can use
Console.WindowWidthsame asTableOutputContext.PowerShell/src/System.Management.Automation/FormatAndOutput/common/BaseOutputtingCommand.cs
Lines 923 to 939 in 36b71ba
int columnsOnTheScreen = this.InnerCommand._lo.ColumnNumber; // Tables need to use spaces for padding to maintain table look even if console window is resized. // For all other output, we use int.MaxValue if the user didn't explicitly specify a width. // If we detect that int.MaxValue is used, first we try to get the current console window width. // However, if we can't read that (for example, implicit remoting has no console window), we default // to something reasonable: 120 columns. if (columnsOnTheScreen == int.MaxValue) { try { columnsOnTheScreen = Console.WindowWidth; } catch { columnsOnTheScreen = 120; } } Reacted by Michael Klement
Steps to reproduce
or
Expected behavior
Actual behavior
Environment data
If no
-AutoSizeparam, the result will be fine. And it works well in WindowsPowerShell 5.1 with or without-AutoSizeparam.