File size: 16,103 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 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// this file contains the data structures for the in memory database
// containing display and formatting information
using System.Collections.Generic;
using Microsoft.PowerShell.Commands;
using Microsoft.PowerShell.Commands.Internal.Format;
namespace Microsoft.PowerShell.Commands.Internal.Format
{
#region Complex View Definitions
/// <summary>
/// In line definition of a complex control.
/// </summary>
internal sealed class ComplexControlBody : ControlBody
{
/// <summary>
/// Default list entry definition
/// It's mandatory.
/// </summary>
internal ComplexControlEntryDefinition defaultEntry;
/// <summary>
/// Optional list of list entry definition overrides. It can be empty if there are no overrides.
/// </summary>
internal List<ComplexControlEntryDefinition> optionalEntryList = new List<ComplexControlEntryDefinition>();
}
internal sealed class ComplexControlEntryDefinition
{
/// <summary>
/// Applicability clause
/// Only valid if not the default definition.
/// </summary>
internal AppliesTo appliesTo = null;
/// <summary>
/// Item associated with this entry definition.
/// </summary>
internal ComplexControlItemDefinition itemDefinition = new ComplexControlItemDefinition();
}
internal sealed class ComplexControlItemDefinition
{
/// <summary>
/// List of tokens the item can contain.
/// </summary>
internal List<FormatToken> formatTokenList = new List<FormatToken>();
}
#endregion
}
namespace System.Management.Automation
{
/// <summary/>
public sealed class CustomControl : PSControl
{
/// <summary/>
public List<CustomControlEntry> Entries { get; set; }
internal ComplexControlBody _cachedBody;
internal CustomControl()
{
Entries = new List<CustomControlEntry>();
}
internal CustomControl(ComplexControlBody body, ViewDefinition viewDefinition)
{
// viewDefinition can be null for nested controls
if (viewDefinition != null)
{
OutOfBand = viewDefinition.outOfBand;
GroupBy = PSControlGroupBy.Get(viewDefinition.groupBy);
}
Entries = new List<CustomControlEntry>();
// Default entry
var cce = new CustomControlEntry(body.defaultEntry);
Entries.Add(cce);
foreach (var entry in body.optionalEntryList)
{
cce = new CustomControlEntry(entry);
Entries.Add(cce);
}
}
/// <summary/>
public static CustomControlBuilder Create(bool outOfBand = false)
{
var customControl = new CustomControl { OutOfBand = outOfBand };
return new CustomControlBuilder(customControl);
}
internal override void WriteToXml(FormatXmlWriter writer)
{
writer.WriteCustomControl(this);
}
internal override bool SafeForExport()
{
if (!base.SafeForExport())
return false;
foreach (var entry in Entries)
{
if (!entry.SafeForExport())
return false;
}
return true;
}
internal override bool CompatibleWithOldPowerShell()
{
// Old versions of PowerShell know nothing about CustomControl.
return false;
}
}
/// <summary/>
public sealed class CustomControlEntry
{
internal CustomControlEntry()
{
CustomItems = new List<CustomItemBase>();
}
internal CustomControlEntry(ComplexControlEntryDefinition entry)
{
if (entry.appliesTo != null)
{
SelectedBy = EntrySelectedBy.Get(entry.appliesTo.referenceList);
}
CustomItems = new List<CustomItemBase>();
foreach (var tok in entry.itemDefinition.formatTokenList)
{
CustomItems.Add(CustomItemBase.Create(tok));
}
}
/// <summary/>
public EntrySelectedBy SelectedBy { get; set; }
/// <summary/>
public List<CustomItemBase> CustomItems { get; set; }
internal bool SafeForExport()
{
foreach (var item in CustomItems)
{
if (!item.SafeForExport())
return false;
}
return SelectedBy == null || SelectedBy.SafeForExport();
}
}
/// <summary/>
public abstract class CustomItemBase
{
internal virtual bool SafeForExport()
{
return true;
}
internal static CustomItemBase Create(FormatToken token)
{
if (token is NewLineToken)
{
return new CustomItemNewline();
}
if (token is TextToken textToken)
{
return new CustomItemText { Text = textToken.text };
}
if (token is FrameToken frameToken)
{
var frame = new CustomItemFrame
{
RightIndent = (uint)frameToken.frameInfoDefinition.rightIndentation,
LeftIndent = (uint)frameToken.frameInfoDefinition.leftIndentation
};
var firstLine = frameToken.frameInfoDefinition.firstLine;
if (firstLine > 0)
{
frame.FirstLineIndent = (uint)firstLine;
}
else if (firstLine < 0)
{
frame.FirstLineHanging = (uint)-firstLine;
}
foreach (var frameItemToken in frameToken.itemDefinition.formatTokenList)
{
frame.CustomItems.Add(CustomItemBase.Create(frameItemToken));
}
return frame;
}
if (token is CompoundPropertyToken cpt)
{
var cie = new CustomItemExpression { EnumerateCollection = cpt.enumerateCollection };
if (cpt.conditionToken != null)
{
cie.ItemSelectionCondition = new DisplayEntry(cpt.conditionToken);
}
if (cpt.expression.expressionValue != null)
{
cie.Expression = new DisplayEntry(cpt.expression);
}
if (cpt.control is ComplexControlBody complexControlBody)
{
cie.CustomControl = new CustomControl(complexControlBody, null);
}
return cie;
}
Diagnostics.Assert(false, "Unexpected formatting token kind");
return null;
}
}
/// <summary/>
public sealed class CustomItemExpression : CustomItemBase
{
internal CustomItemExpression() { }
/// <summary/>
public DisplayEntry ItemSelectionCondition { get; set; }
/// <summary/>
public DisplayEntry Expression { get; set; }
/// <summary/>
public bool EnumerateCollection { get; set; }
/// <summary/>
public CustomControl CustomControl { get; set; }
internal override bool SafeForExport()
{
return (ItemSelectionCondition == null || ItemSelectionCondition.SafeForExport()) &&
(Expression == null || Expression.SafeForExport()) &&
(CustomControl == null || CustomControl.SafeForExport());
}
}
/// <summary/>
public sealed class CustomItemFrame : CustomItemBase
{
/// <summary/>
public uint LeftIndent { get; set; }
/// <summary/>
public uint RightIndent { get; set; }
/// <summary/>
public uint FirstLineHanging { get; set; }
/// <summary/>
public uint FirstLineIndent { get; set; }
internal CustomItemFrame()
{
CustomItems = new List<CustomItemBase>();
}
/// <summary/>
public List<CustomItemBase> CustomItems { get; set; }
internal override bool SafeForExport()
{
foreach (var frameItem in CustomItems)
{
if (!frameItem.SafeForExport())
return false;
}
return true;
}
}
/// <summary/>
public sealed class CustomItemNewline : CustomItemBase
{
/// <summary/>
public CustomItemNewline()
{
this.Count = 1;
}
/// <summary/>
public int Count { get; set; }
}
/// <summary/>
public sealed class CustomItemText : CustomItemBase
{
/// <summary/>
public string Text { get; set; }
}
/// <summary/>
public sealed class CustomEntryBuilder
{
private readonly Stack<List<CustomItemBase>> _entryStack;
private readonly CustomControlBuilder _controlBuilder;
internal CustomEntryBuilder(CustomControlBuilder controlBuilder, CustomControlEntry entry)
{
_entryStack = new Stack<List<CustomItemBase>>();
_entryStack.Push(entry.CustomItems);
_controlBuilder = controlBuilder;
}
/// <summary/>
public CustomEntryBuilder AddNewline(int count = 1)
{
_entryStack.Peek().Add(new CustomItemNewline { Count = count });
return this;
}
/// <summary/>
public CustomEntryBuilder AddText(string text)
{
_entryStack.Peek().Add(new CustomItemText { Text = text });
return this;
}
private void AddDisplayExpressionBinding(
string value,
DisplayEntryValueType valueType,
bool enumerateCollection = false,
string selectedByType = null,
string selectedByScript = null,
CustomControl customControl = null)
{
_entryStack.Peek().Add(new CustomItemExpression()
{
ItemSelectionCondition = selectedByScript != null
? new DisplayEntry(selectedByScript, DisplayEntryValueType.ScriptBlock)
: selectedByType != null
? new DisplayEntry(selectedByType, DisplayEntryValueType.Property)
: null,
EnumerateCollection = enumerateCollection,
Expression = new DisplayEntry(value, valueType),
CustomControl = customControl
});
}
/// <summary/>
public CustomEntryBuilder AddPropertyExpressionBinding(
string property,
bool enumerateCollection = false,
string selectedByType = null,
string selectedByScript = null,
CustomControl customControl = null)
{
AddDisplayExpressionBinding(property, DisplayEntryValueType.Property, enumerateCollection, selectedByType, selectedByScript, customControl);
return this;
}
/// <summary/>
public CustomEntryBuilder AddScriptBlockExpressionBinding(
string scriptBlock,
bool enumerateCollection = false,
string selectedByType = null,
string selectedByScript = null,
CustomControl customControl = null)
{
AddDisplayExpressionBinding(scriptBlock, DisplayEntryValueType.ScriptBlock, enumerateCollection, selectedByType, selectedByScript, customControl);
return this;
}
/// <summary/>
public CustomEntryBuilder AddCustomControlExpressionBinding(
CustomControl customControl,
bool enumerateCollection = false,
string selectedByType = null,
string selectedByScript = null)
{
_entryStack.Peek().Add(new CustomItemExpression()
{
ItemSelectionCondition = selectedByScript != null
? new DisplayEntry(selectedByScript, DisplayEntryValueType.ScriptBlock)
: selectedByType != null
? new DisplayEntry(selectedByType, DisplayEntryValueType.Property)
: null,
EnumerateCollection = enumerateCollection,
CustomControl = customControl
});
return this;
}
/// <summary/>
public CustomEntryBuilder StartFrame(uint leftIndent = 0, uint rightIndent = 0, uint firstLineHanging = 0, uint firstLineIndent = 0)
{
// Mutually exclusive
if (leftIndent != 0 && rightIndent != 0)
{
throw PSTraceSource.NewArgumentException(nameof(leftIndent));
}
// Mutually exclusive
if (firstLineHanging != 0 && firstLineIndent != 0)
{
throw PSTraceSource.NewArgumentException(nameof(firstLineHanging));
}
var frame = new CustomItemFrame
{
LeftIndent = leftIndent,
RightIndent = rightIndent,
FirstLineHanging = firstLineHanging,
FirstLineIndent = firstLineIndent
};
_entryStack.Peek().Add(frame);
_entryStack.Push(frame.CustomItems);
return this;
}
/// <summary/>
public CustomEntryBuilder EndFrame()
{
if (_entryStack.Count < 2)
{
throw PSTraceSource.NewInvalidOperationException();
}
_entryStack.Pop();
return this;
}
/// <summary/>
public CustomControlBuilder EndEntry()
{
if (_entryStack.Count != 1)
{
throw PSTraceSource.NewInvalidOperationException();
}
_entryStack.Pop();
return _controlBuilder;
}
}
/// <summary/>
public sealed class CustomControlBuilder
{
internal CustomControl _control;
internal CustomControlBuilder(CustomControl control)
{
_control = control;
}
/// <summary>Group instances by the property name with an optional label.</summary>
public CustomControlBuilder GroupByProperty(string property, CustomControl customControl = null, string label = null)
{
_control.GroupBy = new PSControlGroupBy
{
Expression = new DisplayEntry(property, DisplayEntryValueType.Property),
CustomControl = customControl,
Label = label
};
return this;
}
/// <summary>Group instances by the script block expression with an optional label.</summary>
public CustomControlBuilder GroupByScriptBlock(string scriptBlock, CustomControl customControl = null, string label = null)
{
_control.GroupBy = new PSControlGroupBy
{
Expression = new DisplayEntry(scriptBlock, DisplayEntryValueType.ScriptBlock),
CustomControl = customControl,
Label = label
};
return this;
}
/// <summary/>
public CustomEntryBuilder StartEntry(IEnumerable<string> entrySelectedByType = null, IEnumerable<DisplayEntry> entrySelectedByCondition = null)
{
var entry = new CustomControlEntry
{
SelectedBy = EntrySelectedBy.Get(entrySelectedByType, entrySelectedByCondition)
};
_control.Entries.Add(entry);
return new CustomEntryBuilder(this, entry);
}
/// <summary/>
public CustomControl EndControl()
{
return _control;
}
}
}
|