forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgressNode.cs
More file actions
589 lines (488 loc) · 18.9 KB
/
Copy pathProgressNode.cs
File metadata and controls
589 lines (488 loc) · 18.9 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Collections;
using System.Management.Automation;
using System.Management.Automation.Host;
using System.Management.Automation.Internal;
using Dbg = System.Management.Automation.Diagnostics;
namespace Microsoft.PowerShell
{
/// <summary>
///
/// ProgressNode is an augmentation of the ProgressRecord type that adds extra fields for the purposes of tracking
/// outstanding activities received by the host, and rendering them in the console.
///
/// </summary>
internal
class
ProgressNode : ProgressRecord
{
/// <summary>
///
/// Indicates the various layouts for rendering a particular node. Each style is progressively less terse.
///
/// </summary>
internal
enum
RenderStyle
{
Invisible = 0,
Minimal = 1,
Compact = 2,
/// <summary>
/// Allocate only one line for displaying the StatusDescription or the CurrentOperation,
/// truncate the rest if the StatusDescription or CurrentOperation doesn't fit in one line
/// </summary>
Full = 3,
/// <summary>
/// The node will be displayed the same as Full, plus, the whole StatusDescription and CurrentOperation will be displayed (in multiple lines if needed).
/// </summary>
FullPlus = 4,
};
/// <summary>
///
/// Constructs an instance from a ProgressRecord.
///
/// </summary>
internal
ProgressNode(Int64 sourceId, ProgressRecord record)
:
base(record.ActivityId, record.Activity, record.StatusDescription)
{
Dbg.Assert(record.RecordType == ProgressRecordType.Processing, "should only create node for Processing records");
this.ParentActivityId = record.ParentActivityId;
this.CurrentOperation = record.CurrentOperation;
this.PercentComplete = Math.Min(record.PercentComplete, 100);
this.SecondsRemaining = record.SecondsRemaining;
this.RecordType = record.RecordType;
this.Style = RenderStyle.FullPlus;
this.SourceId = sourceId;
}
/// <summary>
///
/// Renders a single progress node as strings of text according to that node's style. The text is appended to the
/// supplied list of strings.
///
/// </summary>
/// <param name="strCollection">
///
/// List of strings to which the node's rendering will be appended.
///
/// </param>
/// <param name="indentation">
///
/// The indentation level (in BufferCells) at which the node should be rendered.
///
/// </param>
/// <param name="maxWidth">
///
/// The maximum number of BufferCells that the rendering is allowed to consume.
///
/// </param>
/// <param name="rawUI">
///
/// The PSHostRawUserInterface used to gauge string widths in the rendering.
///
/// </param>
internal
void
Render(ArrayList strCollection, int indentation, int maxWidth, PSHostRawUserInterface rawUI)
{
Dbg.Assert(strCollection != null, "strCollection should not be null");
Dbg.Assert(indentation >= 0, "indentation is negative");
Dbg.Assert(this.RecordType != ProgressRecordType.Completed, "should never render completed records");
switch (Style)
{
case RenderStyle.FullPlus:
RenderFull(strCollection, indentation, maxWidth, rawUI, isFullPlus: true);
break;
case RenderStyle.Full:
RenderFull(strCollection, indentation, maxWidth, rawUI, isFullPlus: false);
break;
case RenderStyle.Compact:
RenderCompact(strCollection, indentation, maxWidth, rawUI);
break;
case RenderStyle.Minimal:
RenderMinimal(strCollection, indentation, maxWidth, rawUI);
break;
case RenderStyle.Invisible:
// do nothing
break;
default:
Dbg.Assert(false, "unrecognized RenderStyle value");
break;
}
}
/// <summary>
///
/// Renders a node in the "Full" style.
///
/// </summary>
/// <param name="strCollection">
///
/// List of strings to which the node's rendering will be appended.
///
/// </param>
/// <param name="indentation">
///
/// The indentation level (in BufferCells) at which the node should be rendered.
///
/// </param>
/// <param name="maxWidth">
///
/// The maximum number of BufferCells that the rendering is allowed to consume.
///
/// </param>
/// <param name="rawUI">
///
/// The PSHostRawUserInterface used to gauge string widths in the rendering.
///
/// </param>
/// <param name="isFullPlus">
///
/// Indicate if the full StatusDescription and CurrentOperation should be displayed.
///
/// </param>
private
void
RenderFull(ArrayList strCollection, int indentation, int maxWidth, PSHostRawUserInterface rawUI, bool isFullPlus)
{
string indent = StringUtil.Padding(indentation);
// First line: the activity
strCollection.Add(
StringUtil.TruncateToBufferCellWidth(
rawUI, StringUtil.Format(" {0}{1} ", indent, this.Activity), maxWidth));
indentation += 3;
indent = StringUtil.Padding(indentation);
// Second line: the status description
RenderFullDescription(this.StatusDescription, indent, maxWidth, rawUI, strCollection, isFullPlus);
// Third line: the percentage thermometer. The size of this is proportional to the width we're allowed
// to consume. -2 for the whitespace, -2 again for the brackets around thermo, -5 to not be too big
if (PercentComplete >= 0)
{
int thermoWidth = Math.Max(3, maxWidth - indentation - 2 - 2 - 5);
int mercuryWidth = 0;
mercuryWidth = PercentComplete * thermoWidth / 100;
if (PercentComplete < 100 && mercuryWidth == thermoWidth)
{
// back off a tad unless we're totally complete to prevent the appearance of completion before
// the fact.
--mercuryWidth;
}
strCollection.Add(
StringUtil.TruncateToBufferCellWidth(
rawUI,
StringUtil.Format(
" {0}[{1}{2}] ",
indent,
new string('o', mercuryWidth),
StringUtil.Padding(thermoWidth - mercuryWidth)),
maxWidth));
}
// Fourth line: the seconds remaining
if (SecondsRemaining >= 0)
{
TimeSpan span = new TimeSpan(0, 0, this.SecondsRemaining);
strCollection.Add(
StringUtil.TruncateToBufferCellWidth(
rawUI,
" "
+ StringUtil.Format(
ProgressNodeStrings.SecondsRemaining,
indent,
span)
+ " ",
maxWidth));
}
// Fifth and Sixth lines: The current operation
if (!String.IsNullOrEmpty(CurrentOperation))
{
strCollection.Add(" ");
RenderFullDescription(this.CurrentOperation, indent, maxWidth, rawUI, strCollection, isFullPlus);
}
}
private static void RenderFullDescription(string description, string indent, int maxWidth, PSHostRawUserInterface rawUi, ArrayList strCollection, bool isFullPlus)
{
string oldDescription = StringUtil.Format(" {0}{1} ", indent, description);
string newDescription;
do
{
newDescription = StringUtil.TruncateToBufferCellWidth(rawUi, oldDescription, maxWidth);
strCollection.Add(newDescription);
if (oldDescription.Length == newDescription.Length)
{
break;
}
else
{
oldDescription = StringUtil.Format(" {0}{1}", indent, oldDescription.Substring(newDescription.Length));
}
} while (isFullPlus);
}
/// <summary>
///
/// Renders a node in the "Compact" style.
///
/// </summary>
/// <param name="strCollection">
///
/// List of strings to which the node's rendering will be appended.
///
/// </param>
/// <param name="indentation">
///
/// The indentation level (in BufferCells) at which the node should be rendered.
///
/// </param>
/// <param name="maxWidth">
///
/// The maximum number of BufferCells that the rendering is allowed to consume.
///
/// </param>
/// <param name="rawUI">
///
/// The PSHostRawUserInterface used to gauge string widths in the rendering.
///
/// </param>
private
void
RenderCompact(ArrayList strCollection, int indentation, int maxWidth, PSHostRawUserInterface rawUI)
{
string indent = StringUtil.Padding(indentation);
// First line: the activity
strCollection.Add(
StringUtil.TruncateToBufferCellWidth(
rawUI,
StringUtil.Format(" {0}{1} ", indent, this.Activity), maxWidth));
indentation += 3;
indent = StringUtil.Padding(indentation);
// Second line: the status description with percentage and time remaining, if applicable.
string percent = "";
if (PercentComplete >= 0)
{
percent = StringUtil.Format("{0}% ", PercentComplete);
}
string secRemain = "";
if (SecondsRemaining >= 0)
{
TimeSpan span = new TimeSpan(0, 0, SecondsRemaining);
secRemain = span.ToString() + " ";
}
strCollection.Add(
StringUtil.TruncateToBufferCellWidth(
rawUI,
StringUtil.Format(
" {0}{1}{2}{3} ",
indent,
percent,
secRemain,
StatusDescription),
maxWidth));
// Third line: The current operation
if (!String.IsNullOrEmpty(CurrentOperation))
{
strCollection.Add(
StringUtil.TruncateToBufferCellWidth(
rawUI,
StringUtil.Format(" {0}{1} ", indent, this.CurrentOperation), maxWidth));
}
}
/// <summary>
///
/// Renders a node in the "Minimal" style.
///
/// </summary>
/// <param name="strCollection">
///
/// List of strings to which the node's rendering will be appended.
///
/// </param>
/// <param name="indentation">
///
/// The indentation level (in BufferCells) at which the node should be rendered.
///
/// </param>
/// <param name="maxWidth">
///
/// The maximum number of BufferCells that the rendering is allowed to consume.
///
/// </param>
/// <param name="rawUI">
///
/// The PSHostRawUserInterface used to gauge string widths in the rendering.
///
/// </param>
private
void
RenderMinimal(ArrayList strCollection, int indentation, int maxWidth, PSHostRawUserInterface rawUI)
{
string indent = StringUtil.Padding(indentation);
// First line: Everything mushed into one line
string percent = "";
if (PercentComplete >= 0)
{
percent = StringUtil.Format("{0}% ", PercentComplete);
}
string secRemain = "";
if (SecondsRemaining >= 0)
{
TimeSpan span = new TimeSpan(0, 0, SecondsRemaining);
secRemain = span.ToString() + " ";
}
strCollection.Add(
StringUtil.TruncateToBufferCellWidth(
rawUI,
StringUtil.Format(
" {0}{1} {2}{3}{4} ",
indent,
Activity,
percent,
secRemain,
StatusDescription),
maxWidth));
}
/// <summary>
///
/// The nodes that have this node as their parent.
///
/// </summary>
internal
ArrayList
Children;
/// <summary>
///
/// The "age" of the node. A node's age is incremented by PendingProgress.Update each time a new ProgressRecord is
/// received by the host. A node's age is reset when a corresponding ProgressRecord is received. Thus, the age of
/// a node reflects the number of ProgressRecord that have been received since the node was last updated.
///
/// The age is used by PendingProgress.Render to determine which nodes should be rendered on the display, and how. As the
/// display has finite size, it may be possible to have many more outstanding progress activities than will fit in that
/// space. The rendering of nodes can be progressively "compressed" into a more terse format, or not rendered at all in
/// order to fit as many nodes as possible in the available space. The oldest nodes are compressed or skipped first.
///
/// </summary>
internal
int
Age;
/// <summary>
///
/// The style in which this node should be rendered.
///
/// </summary>
internal
RenderStyle
Style = RenderStyle.FullPlus;
/// <summary>
///
/// Identifies the source of the progress record.
///
/// </summary>
internal
Int64
SourceId;
/// <summary>
///
/// The number of vertical BufferCells that are required to render the node in its current style.
///
/// </summary>
/// <value></value>
internal int LinesRequiredMethod(PSHostRawUserInterface rawUi, int maxWidth)
{
Dbg.Assert(this.RecordType != ProgressRecordType.Completed, "should never render completed records");
switch (Style)
{
case RenderStyle.FullPlus:
return LinesRequiredInFullStyleMethod(rawUi, maxWidth, isFullPlus: true);
case RenderStyle.Full:
return LinesRequiredInFullStyleMethod(rawUi, maxWidth, isFullPlus: false);
case RenderStyle.Compact:
return LinesRequiredInCompactStyle;
case RenderStyle.Minimal:
return 1;
case RenderStyle.Invisible:
return 0;
default:
Dbg.Assert(false, "Unknown RenderStyle value");
break;
}
return 0;
}
/// <summary>
///
/// The number of vertical BufferCells that are required to render the node in the Full style.
///
/// </summary>
/// <value></value>
private int LinesRequiredInFullStyleMethod(PSHostRawUserInterface rawUi, int maxWidth, bool isFullPlus)
{
// Since the fields of this instance could have been changed, we compute this on-the-fly.
// NTRAID#Windows OS Bugs-1062104-2004/12/15-sburns we assume 1 line for each field. If we ever need to
// word-wrap text fields, then this calculation will need updating.
// Start with 1 for the Activity
int lines = 1;
// Use 5 spaces as the heuristic indent. 5 spaces stand for the indent for the CurrentOperation of the first-level child node
var indent = StringUtil.Padding(5);
var temp = new ArrayList();
if (isFullPlus)
{
temp.Clear();
RenderFullDescription(StatusDescription, indent, maxWidth, rawUi, temp, isFullPlus: true);
lines += temp.Count;
}
else
{
// 1 for the Status
lines++;
}
if (PercentComplete >= 0)
{
++lines;
}
if (SecondsRemaining >= 0)
{
++lines;
}
if (!String.IsNullOrEmpty(CurrentOperation))
{
if (isFullPlus)
{
lines += 1;
temp.Clear();
RenderFullDescription(CurrentOperation, indent, maxWidth, rawUi, temp, isFullPlus: true);
lines += temp.Count;
}
else
{
lines += 2;
}
}
return lines;
}
/// <summary>
///
/// The number of vertical BufferCells that are required to render the node in the Compact style.
///
/// </summary>
/// <value></value>
private
int
LinesRequiredInCompactStyle
{
get
{
// Since the fields of this instance could have been changed, we compute this on-the-fly.
// NTRAID#Windows OS Bugs-1062104-2004/12/15-sburns we assume 1 line for each field. If we ever need to
// word-wrap text fields, then this calculation will need updating.
// Start with 1 for the Activity, and 1 for the Status.
int lines = 2;
if (!String.IsNullOrEmpty(CurrentOperation))
{
++lines;
}
return lines;
}
}
}
} // namespace