// 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 Table View Definitions /// /// Alignment values /// NOTE: we do not use an enum because this will have to be /// serialized and ERS/serialization do not support enumerations. /// internal static class TextAlignment { internal const int Undefined = 0; internal const int Left = 1; internal const int Center = 2; internal const int Right = 3; } /// /// Definition of a table control. /// internal sealed class TableControlBody : ControlBody { /// /// Optional, if not present, use data off the default table row definition. /// internal TableHeaderDefinition header = new TableHeaderDefinition(); /// /// Default row definition /// It's mandatory. /// internal TableRowDefinition defaultDefinition; /// /// Optional list of row definition overrides. It can be empty if there are no overrides. /// internal List optionalDefinitionList = new List(); internal override ControlBase Copy() { TableControlBody result = new TableControlBody { autosize = this.autosize, header = this.header.Copy() }; if (defaultDefinition != null) { result.defaultDefinition = this.defaultDefinition.Copy(); } foreach (TableRowDefinition trd in this.optionalDefinitionList) { result.optionalDefinitionList.Add(trd); } return result; } } /// /// Information about the table header /// NOTE: if an instance of this class is present, the list must not be empty. /// internal sealed class TableHeaderDefinition { /// /// If true, direct the outputter to suppress table header printing. /// internal bool hideHeader; /// /// Mandatory list of column header definitions. /// internal List columnHeaderDefinitionList = new List(); /// /// Returns a Shallow Copy of the current object. /// /// internal TableHeaderDefinition Copy() { TableHeaderDefinition result = new TableHeaderDefinition { hideHeader = this.hideHeader }; foreach (TableColumnHeaderDefinition tchd in this.columnHeaderDefinitionList) { result.columnHeaderDefinitionList.Add(tchd); } return result; } } internal sealed class TableColumnHeaderDefinition { /// /// Optional label /// If not present, use the name of the property from the matching /// mandatory row description. /// internal TextToken label = null; /// /// General alignment for the column /// If not present, either use the one from the row definition /// or the data driven heuristics. /// internal int alignment = TextAlignment.Undefined; /// /// Width of the column. /// internal int width = 0; // undefined } /// /// Definition of the data to be displayed in a table row. /// internal sealed class TableRowDefinition { /// /// Applicability clause /// Only valid if not the default definition. /// internal AppliesTo appliesTo; /// /// If true, the current table row should be allowed /// to wrap to multiple lines, else truncated. /// internal bool multiLine; /// /// Mandatory list of column items. /// It cannot be empty. /// internal List rowItemDefinitionList = new List(); /// /// Returns a Shallow Copy of the current object. /// /// internal TableRowDefinition Copy() { TableRowDefinition result = new TableRowDefinition { appliesTo = this.appliesTo, multiLine = this.multiLine }; foreach (TableRowItemDefinition trid in this.rowItemDefinitionList) { result.rowItemDefinitionList.Add(trid); } return result; } } /// /// Cell definition inside a row. /// internal sealed class TableRowItemDefinition { /// /// Optional alignment to override the default one at the header level. /// internal int alignment = TextAlignment.Undefined; /// /// Format directive body telling how to format the cell /// RULE: the body can only contain /// * TextToken /// * PropertyToken /// * NOTHING (provide an empty cell) /// internal List formatTokenList = new List(); } #endregion } namespace System.Management.Automation { /// /// Defines a table control. /// public sealed class TableControl : PSControl { /// Collection of column header definitions for this table control public List Headers { get; set; } /// Collection of row definitions for this table control public List Rows { get; set; } /// When true, column widths are calculated based on more than the first object. public bool AutoSize { get; set; } /// When true, table headers are not displayed public bool HideTableHeaders { get; set; } /// Create a default TableControl public static TableControlBuilder Create(bool outOfBand = false, bool autoSize = false, bool hideTableHeaders = false) { var table = new TableControl { OutOfBand = outOfBand, AutoSize = autoSize, HideTableHeaders = hideTableHeaders }; return new TableControlBuilder(table); } /// Public default constructor for TableControl public TableControl() { Headers = new List(); Rows = new List(); } internal override void WriteToXml(FormatXmlWriter writer) { writer.WriteTableControl(this); } /// /// Determines if this object is safe to be written. /// /// True if safe, false otherwise. internal override bool SafeForExport() { if (!base.SafeForExport()) return false; foreach (var row in Rows) { if (!row.SafeForExport()) return false; } return true; } internal override bool CompatibleWithOldPowerShell() { if (!base.CompatibleWithOldPowerShell()) return false; foreach (var row in Rows) { if (!row.CompatibleWithOldPowerShell()) return false; } return true; } internal TableControl(TableControlBody tcb, ViewDefinition viewDefinition) : this() { this.OutOfBand = viewDefinition.outOfBand; this.GroupBy = PSControlGroupBy.Get(viewDefinition.groupBy); this.AutoSize = tcb.autosize.GetValueOrDefault(); this.HideTableHeaders = tcb.header.hideHeader; TableControlRow row = new TableControlRow(tcb.defaultDefinition); Rows.Add(row); foreach (TableRowDefinition rd in tcb.optionalDefinitionList) { row = new TableControlRow(rd); Rows.Add(row); } foreach (TableColumnHeaderDefinition hd in tcb.header.columnHeaderDefinitionList) { TableControlColumnHeader header = new TableControlColumnHeader(hd); Headers.Add(header); } } /// /// Public constructor for TableControl that only takes 'tableControlRows'. /// /// public TableControl(TableControlRow tableControlRow) : this() { if (tableControlRow == null) throw PSTraceSource.NewArgumentNullException("tableControlRows"); this.Rows.Add(tableControlRow); } /// /// Public constructor for TableControl that takes both 'tableControlRows' and 'tableControlColumnHeaders'. /// /// /// public TableControl(TableControlRow tableControlRow, IEnumerable tableControlColumnHeaders) : this() { if (tableControlRow == null) throw PSTraceSource.NewArgumentNullException("tableControlRows"); if (tableControlColumnHeaders == null) throw PSTraceSource.NewArgumentNullException(nameof(tableControlColumnHeaders)); this.Rows.Add(tableControlRow); foreach (TableControlColumnHeader header in tableControlColumnHeaders) { this.Headers.Add(header); } } } /// /// Defines the header for a particular column in a table control. /// public sealed class TableControlColumnHeader { /// Label for the column public string Label { get; set; } /// Alignment of the string within the column public Alignment Alignment { get; set; } /// Width of the column - in number of display cells public int Width { get; set; } internal TableControlColumnHeader(TableColumnHeaderDefinition colheaderdefinition) { if (colheaderdefinition.label != null) { Label = colheaderdefinition.label.text; } Alignment = (Alignment)colheaderdefinition.alignment; Width = colheaderdefinition.width; } /// Default constructor public TableControlColumnHeader() { } /// /// Public constructor for TableControlColumnHeader. /// /// Could be null if no label to specify. /// The Value should be non-negative. /// The default value is Alignment.Undefined. public TableControlColumnHeader(string label, int width, Alignment alignment) { if (width < 0) throw PSTraceSource.NewArgumentOutOfRangeException(nameof(width), width); this.Label = label; this.Width = width; this.Alignment = alignment; } } /// /// Defines a particular column within a row /// in a table control. /// public sealed class TableControlColumn { /// Alignment of the particular column public Alignment Alignment { get; set; } /// Display Entry public DisplayEntry DisplayEntry { get; set; } /// Format string to apply public string FormatString { get; internal set; } /// /// Returns the value of the entry. /// /// public override string ToString() { return DisplayEntry.Value; } /// Default constructor public TableControlColumn() { } internal TableControlColumn(string text, int alignment, bool isscriptblock, string formatString) { Alignment = (Alignment)alignment; DisplayEntry = new DisplayEntry(text, isscriptblock ? DisplayEntryValueType.ScriptBlock : DisplayEntryValueType.Property); FormatString = formatString; } /// /// Public constructor for TableControlColumn. /// /// /// public TableControlColumn(Alignment alignment, DisplayEntry entry) { this.Alignment = alignment; this.DisplayEntry = entry; } internal bool SafeForExport() { return DisplayEntry.SafeForExport(); } } /// /// Defines a single row in a table control. /// public sealed class TableControlRow { /// Collection of column definitions for this row public List Columns { get; set; } /// List of typenames which select this entry public EntrySelectedBy SelectedBy { get; internal set; } /// When true, instead of truncating to the column width, use multiple lines. public bool Wrap { get; set; } /// Public constructor for TableControlRow public TableControlRow() { Columns = new List(); } internal TableControlRow(TableRowDefinition rowdefinition) : this() { Wrap = rowdefinition.multiLine; if (rowdefinition.appliesTo != null) { SelectedBy = EntrySelectedBy.Get(rowdefinition.appliesTo.referenceList); } foreach (TableRowItemDefinition itemdef in rowdefinition.rowItemDefinitionList) { TableControlColumn column; if (itemdef.formatTokenList[0] is FieldPropertyToken fpt) { column = new TableControlColumn(fpt.expression.expressionValue, itemdef.alignment, fpt.expression.isScriptBlock, fpt.fieldFormattingDirective.formatString); } else { column = new TableControlColumn(); } Columns.Add(column); } } /// Public constructor for TableControlRow. public TableControlRow(IEnumerable columns) : this() { if (columns == null) throw PSTraceSource.NewArgumentNullException(nameof(columns)); foreach (TableControlColumn column in columns) { Columns.Add(column); } } internal bool SafeForExport() { foreach (var column in Columns) { if (!column.SafeForExport()) return false; } return SelectedBy != null && SelectedBy.SafeForExport(); } internal bool CompatibleWithOldPowerShell() { // Old versions of PowerShell don't support multiple row definitions. return SelectedBy == null; } } /// A helper class for defining table controls public sealed class TableRowDefinitionBuilder { internal readonly TableControlBuilder _tcb; internal readonly TableControlRow _tcr; internal TableRowDefinitionBuilder(TableControlBuilder tcb, TableControlRow tcr) { _tcb = tcb; _tcr = tcr; } private TableRowDefinitionBuilder AddItem(string value, DisplayEntryValueType entryType, Alignment alignment, string format) { if (string.IsNullOrEmpty(value)) throw PSTraceSource.NewArgumentException(nameof(value)); var tableControlColumn = new TableControlColumn(alignment, new DisplayEntry(value, entryType)) { FormatString = format }; _tcr.Columns.Add(tableControlColumn); return this; } /// /// Add a column to the current row definition that calls a script block. /// public TableRowDefinitionBuilder AddScriptBlockColumn(string scriptBlock, Alignment alignment = Alignment.Undefined, string format = null) { return AddItem(scriptBlock, DisplayEntryValueType.ScriptBlock, alignment, format); } /// /// Add a column to the current row definition that references a property. /// public TableRowDefinitionBuilder AddPropertyColumn(string propertyName, Alignment alignment = Alignment.Undefined, string format = null) { return AddItem(propertyName, DisplayEntryValueType.Property, alignment, format); } /// /// Complete a row definition. /// public TableControlBuilder EndRowDefinition() { return _tcb; } } /// A helper class for defining table controls public sealed class TableControlBuilder { internal readonly TableControl _table; internal TableControlBuilder(TableControl table) { _table = table; } /// Group instances by the property name with an optional label. public TableControlBuilder GroupByProperty(string property, CustomControl customControl = null, string label = null) { _table.GroupBy = new PSControlGroupBy { Expression = new DisplayEntry(property, DisplayEntryValueType.Property), CustomControl = customControl, Label = label }; return this; } /// Group instances by the script block expression with an optional label. public TableControlBuilder GroupByScriptBlock(string scriptBlock, CustomControl customControl = null, string label = null) { _table.GroupBy = new PSControlGroupBy { Expression = new DisplayEntry(scriptBlock, DisplayEntryValueType.ScriptBlock), CustomControl = customControl, Label = label }; return this; } /// Add a header public TableControlBuilder AddHeader(Alignment alignment = Alignment.Undefined, int width = 0, string label = null) { _table.Headers.Add(new TableControlColumnHeader(label, width, alignment)); return this; } /// Add a header public TableRowDefinitionBuilder StartRowDefinition(bool wrap = false, IEnumerable entrySelectedByType = null, IEnumerable entrySelectedByCondition = null) { var row = new TableControlRow { Wrap = wrap }; if (entrySelectedByType != null || entrySelectedByCondition != null) { row.SelectedBy = new EntrySelectedBy(); if (entrySelectedByType != null) { row.SelectedBy.TypeNames = new List(entrySelectedByType); } if (entrySelectedByCondition != null) { row.SelectedBy.SelectionCondition = new List(entrySelectedByCondition); } } _table.Rows.Add(row); return new TableRowDefinitionBuilder(this, row); } /// Complete a table definition public TableControl EndTable() { return _table; } } }