File size: 11,218 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Management.Automation;
using System.Management.Automation.Internal;
using System.Text;
namespace Microsoft.PowerShell.Commands.Internal.Format
{
/// <summary>
/// Class to write object properties in list form by using
/// the host screen interfaces.
/// </summary>
internal class ListWriter
{
/// <summary>
/// Labels already padded with blanks, separator characters, etc.
/// </summary>
private string[] _propertyLabels;
/// <summary>
/// Display length of the property labels in the array (all the same length)
/// </summary>
private int _propertyLabelsDisplayLength = 0;
/// <summary>
/// Column width of the screen.
/// </summary>
private int _columnWidth = 0;
/// <summary>
/// A cached string builder used within this type to reduce creation of temporary strings.
/// </summary>
private readonly StringBuilder _cachedBuilder = new();
/// <summary>
/// </summary>
/// <param name="propertyNames">Names of the properties to display.</param>
/// <param name="screenColumnWidth">Column width of the screen.</param>
/// <param name="dc">Instance of the DisplayCells helper object.</param>
internal void Initialize(string[] propertyNames, int screenColumnWidth, DisplayCells dc)
{
_columnWidth = screenColumnWidth;
if (propertyNames == null || propertyNames.Length == 0)
{
// there is nothing to show
_disabled = true;
return;
}
_disabled = false;
Debug.Assert(propertyNames != null, "propertyNames is null");
Debug.Assert(propertyNames.Length > 0, "propertyNames has zero length");
// assess the useful widths
if ((screenColumnWidth - Separator.Length - MinFieldWidth - MinLabelWidth) < 0)
{
// we do not have enough space for any meaningful display
_disabled = true;
return;
}
// check if we have to truncate the labels
int maxAllowableLabelLength = screenColumnWidth - Separator.Length - MinFieldWidth;
if (InternalTestHooks.ForceFormatListFixedLabelWidth)
{
maxAllowableLabelLength = 10;
}
// find out the max display length (cell count) of the property names
_propertyLabelsDisplayLength = 0; // reset max
// cache the cell lengths for each property
Span<int> propertyNameCellCounts = propertyNames.Length <= OutCommandInner.StackAllocThreshold ? stackalloc int[propertyNames.Length] : new int[propertyNames.Length];
for (int k = 0; k < propertyNames.Length; k++)
{
Debug.Assert(propertyNames[k] != null, "propertyNames[k] is null");
propertyNameCellCounts[k] = dc.Length(propertyNames[k]);
if (propertyNameCellCounts[k] > _propertyLabelsDisplayLength)
_propertyLabelsDisplayLength = propertyNameCellCounts[k];
}
if (_propertyLabelsDisplayLength > maxAllowableLabelLength)
{
// need to truncate
_propertyLabelsDisplayLength = maxAllowableLabelLength;
}
_propertyLabels = new string[propertyNames.Length];
for (int k = 0; k < propertyNames.Length; k++)
{
string propertyName = propertyNames[k];
if (propertyNameCellCounts[k] < _propertyLabelsDisplayLength)
{
// shorter than the max, add padding
_propertyLabels[k] = propertyName + StringUtil.Padding(_propertyLabelsDisplayLength - propertyNameCellCounts[k]);
}
else if (propertyNameCellCounts[k] > _propertyLabelsDisplayLength)
{
// longer than the max, clip
_propertyLabels[k] = propertyName.VtSubstring(0, dc.TruncateTail(propertyName, _propertyLabelsDisplayLength));
}
else
{
_propertyLabels[k] = propertyName;
}
_propertyLabels[k] += Separator;
}
_propertyLabelsDisplayLength += Separator.Length;
}
/// <summary>
/// Write the values of the properties of an object.
/// </summary>
/// <param name="values">Array with the values in form of formatted strings.</param>
/// <param name="lo">LineOutput interface to write to.</param>
internal void WriteProperties(string[] values, LineOutput lo)
{
if (_disabled)
return;
string[] valuesToPrint = null;
if (values == null)
{
// we have nothing, but we have to create an empty array
valuesToPrint = new string[_propertyLabels.Length];
for (int k = 0; k < _propertyLabels.Length; k++)
valuesToPrint[k] = string.Empty;
}
else if (values.Length < _propertyLabels.Length)
{
// need to pad to the end of the array
valuesToPrint = new string[_propertyLabels.Length];
for (int k = 0; k < _propertyLabels.Length; k++)
{
if (k < values.Length)
valuesToPrint[k] = values[k];
else
valuesToPrint[k] = string.Empty;
}
}
else if (values.Length > _propertyLabels.Length)
{
// need to trim
valuesToPrint = new string[_propertyLabels.Length];
for (int k = 0; k < _propertyLabels.Length; k++)
valuesToPrint[k] = values[k];
}
else
{
// perfect match
valuesToPrint = values;
}
Debug.Assert(lo != null, "LineOutput is null");
for (int k = 0; k < _propertyLabels.Length; k++)
{
WriteProperty(k, valuesToPrint[k], lo);
}
}
/// <summary>
/// Helper, writing a single property to the screen.
/// It wraps the value of the property if it is tool long to fit.
/// </summary>
/// <param name="k">Index of property to write.</param>
/// <param name="propertyValue">String value of the property to write.</param>
/// <param name="lo">LineOutput interface to write to.</param>
private void WriteProperty(int k, string propertyValue, LineOutput lo)
{
propertyValue ??= string.Empty;
// make sure we honor embedded newlines
List<string> lines = StringManipulationHelper.SplitLines(propertyValue);
// padding to use in the lines after the first
string padding = null;
for (int i = 0; i < lines.Count; i++)
{
string prependString = null;
if (i == 0)
prependString = _propertyLabels[k];
else
{
padding ??= StringUtil.Padding(_propertyLabelsDisplayLength);
prependString = padding;
}
WriteSingleLineHelper(prependString, lines[i], lo);
}
}
/// <summary>
/// Internal helper to split a line that is too long to fit and pad it to the left
/// with a given string.
/// </summary>
/// <param name="prependString">String to add to the left.</param>
/// <param name="line">Line to print.</param>
/// <param name="lo">LineOutput to write to.</param>
private void WriteSingleLineHelper(string prependString, string line, LineOutput lo)
{
line ??= string.Empty;
// compute the width of the field for the value string (in screen cells)
int fieldCellCount = _columnWidth - _propertyLabelsDisplayLength;
// split the lines
StringCollection sc = StringManipulationHelper.GenerateLines(lo.DisplayCells, line, fieldCellCount, fieldCellCount);
// The padding to use in the lines after the first.
string headPadding = null;
// The VT style used for the list label.
string style = PSStyle.Instance.Formatting.FormatAccent;
string reset = PSStyle.Instance.Reset;
// display the string collection
for (int k = 0; k < sc.Count; k++)
{
string str = sc[k];
_cachedBuilder.Clear();
if (k == 0)
{
if (string.IsNullOrWhiteSpace(prependString) || style == string.Empty)
{
// - Sometimes 'prependString' is just padding white spaces, and we don't
// need to add formatting escape sequences in such a case.
// - Otherwise, if the style is an empty string, then the user has chosen
// to not apply a style to the list label.
_cachedBuilder.Append(prependString).Append(str);
}
else
{
// Apply the style to the list label.
_cachedBuilder
.Append(style)
.Append(prependString)
.Append(reset)
.Append(str);
}
}
else
{
// Lazily calculate the padding to use for the subsequent lines as it's quite often that only the first line exists.
headPadding ??= StringUtil.Padding(_propertyLabelsDisplayLength);
_cachedBuilder.Append(headPadding).Append(str);
}
if (str.Contains(ValueStringDecorated.ESC) && !str.AsSpan().TrimEnd().EndsWith(reset, StringComparison.Ordinal))
{
_cachedBuilder.Append(reset);
}
lo.WriteLine(_cachedBuilder.ToString());
}
}
/// <summary>
/// Set to true when the width of the screen is too small to do anything useful.
/// </summary>
private bool _disabled = false;
private const string Separator = " : ";
/// <summary>
/// Minimum width for the property label field.
/// </summary>
private const int MinLabelWidth = 1;
/// <summary>
/// Minimum width for the property value field.
/// </summary>
private const int MinFieldWidth = 1;
}
}
|