File size: 11,053 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 | // 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 Wide View Definitions
/// <summary>
/// In line definition of a wide control.
/// </summary>
internal sealed class WideControlBody : ControlBody
{
/// <summary>
/// Number of columns to use for wide display.
/// </summary>
internal int columns = 0;
/// <summary>
/// Default wide entry definition
/// It's mandatory.
/// </summary>
internal WideControlEntryDefinition defaultEntryDefinition = null;
/// <summary>
/// Optional list of list entry definition overrides. It can be empty if there are no overrides.
/// </summary>
internal List<WideControlEntryDefinition> optionalEntryList = new List<WideControlEntryDefinition>();
}
/// <summary>
/// Definition of the data to be displayed in a list entry.
/// </summary>
internal sealed class WideControlEntryDefinition
{
/// <summary>
/// Applicability clause
/// Only valid if not the default definition.
/// </summary>
internal AppliesTo appliesTo = null;
/// <summary>
/// Format directive body telling how to format the cell
/// RULE: the body can only contain
/// * TextToken
/// * PropertyToken
/// * NOTHING (provide an empty cell)
/// </summary>
internal List<FormatToken> formatTokenList = new List<FormatToken>();
}
#endregion
}
namespace System.Management.Automation
{
/// <summary>
/// Defines a list control.
/// </summary>
public sealed class WideControl : PSControl
{
/// <summary>Entries in this wide control</summary>
public List<WideControlEntryItem> Entries { get; internal set; }
/// <summary>When true, widths are calculated based on more than the first object.</summary>
public bool AutoSize { get; set; }
/// <summary>Number of columns in the control</summary>
public uint Columns { get; internal set; }
/// <summary>Create a default WideControl</summary>
public static WideControlBuilder Create(bool outOfBand = false, bool autoSize = false, uint columns = 0)
{
var control = new WideControl { OutOfBand = false, AutoSize = autoSize, Columns = columns };
return new WideControlBuilder(control);
}
internal override void WriteToXml(FormatXmlWriter writer)
{
writer.WriteWideControl(this);
}
/// <summary>
/// Indicates if this control does not have
/// any script blocks and is safe to export.
/// </summary>
/// <returns>True if exportable, false otherwise.</returns>
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()
{
if (!base.CompatibleWithOldPowerShell())
return false;
foreach (var entry in Entries)
{
if (!entry.CompatibleWithOldPowerShell())
return false;
}
return true;
}
/// <summary>Default constructor for WideControl</summary>
public WideControl()
{
Entries = new List<WideControlEntryItem>();
}
internal WideControl(WideControlBody widecontrolbody, ViewDefinition viewDefinition) : this()
{
OutOfBand = viewDefinition.outOfBand;
GroupBy = PSControlGroupBy.Get(viewDefinition.groupBy);
AutoSize = widecontrolbody.autosize.GetValueOrDefault();
Columns = (uint)widecontrolbody.columns;
Entries.Add(new WideControlEntryItem(widecontrolbody.defaultEntryDefinition));
foreach (WideControlEntryDefinition definition in widecontrolbody.optionalEntryList)
{
Entries.Add(new WideControlEntryItem(definition));
}
}
/// <summary>Public constructor for WideControl</summary>
public WideControl(IEnumerable<WideControlEntryItem> wideEntries) : this()
{
if (wideEntries == null)
throw PSTraceSource.NewArgumentNullException(nameof(wideEntries));
foreach (WideControlEntryItem entryItem in wideEntries)
{
this.Entries.Add(entryItem);
}
}
/// <summary>Public constructor for WideControl</summary>
public WideControl(IEnumerable<WideControlEntryItem> wideEntries, uint columns) : this()
{
if (wideEntries == null)
throw PSTraceSource.NewArgumentNullException(nameof(wideEntries));
foreach (WideControlEntryItem entryItem in wideEntries)
{
this.Entries.Add(entryItem);
}
this.Columns = columns;
}
/// <summary>Construct an instance with columns</summary>
public WideControl(uint columns) : this()
{
this.Columns = columns;
}
}
/// <summary>
/// Defines one item in a wide control entry.
/// </summary>
public sealed class WideControlEntryItem
{
/// <summary>Display entry</summary>
public DisplayEntry DisplayEntry { get; internal set; }
/// <summary>List of typenames which select this entry, deprecated, use EntrySelectedBy</summary>
public List<string> SelectedBy
{
get
{
EntrySelectedBy ??= new EntrySelectedBy { TypeNames = new List<string>() };
return EntrySelectedBy.TypeNames;
}
}
/// <summary>List of typenames and/or a script block which select this entry.</summary>
public EntrySelectedBy EntrySelectedBy { get; internal set; }
/// <summary>Format string to apply</summary>
public string FormatString { get; internal set; }
internal WideControlEntryItem()
{
}
internal WideControlEntryItem(WideControlEntryDefinition definition) : this()
{
if (definition.formatTokenList[0] is FieldPropertyToken fpt)
{
DisplayEntry = new DisplayEntry(fpt.expression);
FormatString = fpt.fieldFormattingDirective.formatString;
}
if (definition.appliesTo != null)
{
EntrySelectedBy = EntrySelectedBy.Get(definition.appliesTo.referenceList);
}
}
/// <summary>
/// Public constructor for WideControlEntryItem.
/// </summary>
public WideControlEntryItem(DisplayEntry entry) : this()
{
if (entry == null)
throw PSTraceSource.NewArgumentNullException(nameof(entry));
this.DisplayEntry = entry;
}
/// <summary>
/// Public constructor for WideControlEntryItem.
/// </summary>
public WideControlEntryItem(DisplayEntry entry, IEnumerable<string> selectedBy) : this()
{
if (entry == null)
throw PSTraceSource.NewArgumentNullException(nameof(entry));
if (selectedBy == null)
throw PSTraceSource.NewArgumentNullException(nameof(selectedBy));
this.DisplayEntry = entry;
this.EntrySelectedBy = EntrySelectedBy.Get(selectedBy, null);
}
internal bool SafeForExport()
{
return DisplayEntry.SafeForExport() && (EntrySelectedBy == null || EntrySelectedBy.SafeForExport());
}
internal bool CompatibleWithOldPowerShell()
{
// Old versions of PowerShell don't know anything about FormatString or conditions in EntrySelectedBy.
return FormatString == null &&
(EntrySelectedBy == null || EntrySelectedBy.CompatibleWithOldPowerShell());
}
}
/// <summary/>
public sealed class WideControlBuilder
{
private readonly WideControl _control;
internal WideControlBuilder(WideControl control)
{
_control = control;
}
/// <summary>Group instances by the property name with an optional label.</summary>
public WideControlBuilder 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 WideControlBuilder 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 WideControlBuilder AddScriptBlockEntry(string scriptBlock, string format = null, IEnumerable<string> entrySelectedByType = null, IEnumerable<DisplayEntry> entrySelectedByCondition = null)
{
var entry = new WideControlEntryItem(new DisplayEntry(scriptBlock, DisplayEntryValueType.ScriptBlock))
{
EntrySelectedBy = EntrySelectedBy.Get(entrySelectedByType, entrySelectedByCondition)
};
_control.Entries.Add(entry);
return this;
}
/// <summary/>
public WideControlBuilder AddPropertyEntry(string propertyName, string format = null, IEnumerable<string> entrySelectedByType = null, IEnumerable<DisplayEntry> entrySelectedByCondition = null)
{
var entry = new WideControlEntryItem(new DisplayEntry(propertyName, DisplayEntryValueType.Property))
{
EntrySelectedBy = EntrySelectedBy.Get(entrySelectedByType, entrySelectedByCondition)
};
_control.Entries.Add(entry);
return this;
}
/// <summary/>
public WideControl EndWideControl()
{
return _control;
}
}
}
|