6
6
using System . Diagnostics ;
7
7
using System . Management . Automation ;
8
8
using System . Management . Automation . Internal ;
9
+ using System . Text ;
9
10
10
11
namespace Microsoft . PowerShell . Commands . Internal . Format
11
12
{
@@ -30,6 +31,11 @@ internal class ListWriter
30
31
/// </summary>
31
32
private int _columnWidth = 0 ;
32
33
34
+ /// <summary>
35
+ /// A cached string builder used within this type to reduce creation of temporary strings.
36
+ /// </summary>
37
+ private readonly StringBuilder _cachedBuilder = new ( ) ;
38
+
33
39
/// <summary>
34
40
/// </summary>
35
41
/// <param name="propertyNames">Names of the properties to display.</param>
@@ -207,7 +213,9 @@ private void WriteProperty(int k, string propertyValue, LineOutput lo)
207
213
private void WriteSingleLineHelper ( string prependString , string line , LineOutput lo )
208
214
{
209
215
if ( line == null )
216
+ {
210
217
line = string . Empty ;
218
+ }
211
219
212
220
// compute the width of the field for the value string (in screen cells)
213
221
int fieldCellCount = _columnWidth - _propertyLabelsDisplayLength ;
@@ -221,14 +229,30 @@ private void WriteSingleLineHelper(string prependString, string line, LineOutput
221
229
// display the string collection
222
230
for ( int k = 0 ; k < sc . Count ; k ++ )
223
231
{
232
+ string str = sc [ k ] ;
233
+ _cachedBuilder . Clear ( ) ;
234
+
224
235
if ( k == 0 )
225
236
{
226
- lo . WriteLine ( PSStyle . Instance . Formatting . FormatAccent + prependString + PSStyle . Instance . Reset + sc [ k ] ) ;
237
+ _cachedBuilder
238
+ . Append ( PSStyle . Instance . Formatting . FormatAccent )
239
+ . Append ( prependString )
240
+ . Append ( PSStyle . Instance . Reset )
241
+ . Append ( str ) ;
227
242
}
228
243
else
229
244
{
230
- lo . WriteLine ( padding + PSStyle . Instance . Formatting . FormatAccent + PSStyle . Instance . Reset + sc [ k ] ) ;
245
+ _cachedBuilder
246
+ . Append ( padding )
247
+ . Append ( str ) ;
231
248
}
249
+
250
+ if ( str . Contains ( ValueStringDecorated . ESC ) && ! str . EndsWith ( PSStyle . Instance . Reset ) )
251
+ {
252
+ _cachedBuilder . Append ( PSStyle . Instance . Reset ) ;
253
+ }
254
+
255
+ lo . WriteLine ( _cachedBuilder . ToString ( ) ) ;
232
256
}
233
257
}
234
258
0 commit comments