Skip to content

Commit f14ce81

Browse files
committed
WIP: Added -AllProperties option
Add AllProperties toggle to Out-ConsoleGridView cmdlet Introduced a new `AllProperties` parameter to the `Out-ConsoleGridView` cmdlet, allowing users to display all object properties instead of default display properties. Key changes: - Added `AllProperties` as a `SwitchParameter` in `OutConsoleGridViewCmdletCommand`. - Updated `ApplicationData` to store the `AllProperties` state. - Enhanced `OutGridViewWindow` with a `CheckBox` for toggling `AllProperties` and dynamic updates to the grid view and status bar. - Added `ReloadDataWithAllProperties` and `UpdateStatusBar` methods to handle UI updates. - Modified `TypeGetter` to support `AllProperties`, prioritizing format view definitions, default display property sets, and fallback to all properties. - Updated `CastObjectsToTableView` to include an `allProperties` parameter. - Improved compatibility with CIM instances by using the current runspace for format data retrieval. These changes enhance the cmdlet's flexibility and usability, particularly for inspecting all object properties.
1 parent aac40e6 commit f14ce81

File tree

4 files changed

+249
-49
lines changed

4 files changed

+249
-49
lines changed

src/Microsoft.PowerShell.ConsoleGuiTools/OutConsoleGridviewCmdletCommand.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,13 @@ public class OutConsoleGridViewCmdletCommand : PSCmdlet, IDisposable
7575
"If specified the Terminal.Gui System.Net.Console-based ConsoleDriver (NetDriver) will be used.")]
7676
public SwitchParameter UseNetDriver { set; get; }
7777

78+
/// <summary>
79+
/// Gets or sets a value indicating whether all properties should be displayed instead of just the default display properties.
80+
/// </summary>
81+
[Parameter(HelpMessage =
82+
"If specified, all properties of the objects will be displayed instead of just the default display properties.")]
83+
public SwitchParameter AllProperties { set; get; }
84+
7885
/// <summary>
7986
/// Gets a value indicating whether the Verbose switch is present.
8087
/// </summary>
@@ -165,6 +172,7 @@ protected override void EndProcessing()
165172
Filter = Filter,
166173
MinUI = MinUI,
167174
UseNetDriver = UseNetDriver,
175+
AllProperties = AllProperties,
168176
Verbose = Verbose,
169177
Debug = Debug,
170178
ModuleVersion = MyInvocation.MyCommand.Version.ToString()

src/Microsoft.PowerShell.ConsoleGuiTools/OutGridViewWindow.cs

Lines changed: 95 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ internal sealed class OutGridViewWindow : Window
3737
private readonly GridViewDetails _gridViewDetails;
3838
private readonly DataTable _dataTable;
3939
private int[]? _naturalColumnWidths;
40+
private StatusBar? _statusBar;
4041

4142
/// <summary>
4243
/// Initializes a new instance of the <see cref="OutGridViewWindow" /> class with the specified application data.
@@ -61,7 +62,7 @@ public OutGridViewWindow(ApplicationData applicationData)
6162
if (_applicationData.PSObjects is { Count: > 0 })
6263
{
6364
var psObjects = _applicationData.PSObjects.Cast<PSObject>().ToList();
64-
_dataTable = TypeGetter.CastObjectsToTableView(psObjects);
65+
_dataTable = TypeGetter.CastObjectsToTableView(psObjects, _applicationData.AllProperties);
6566
}
6667
else
6768
{
@@ -291,6 +292,68 @@ private void ListViewSource_MarkChanged(object? s, GridViewDataSource.RowMarkedE
291292

292293
#region User Actions
293294

295+
/// <summary>
296+
/// Reloads the data with the specified AllProperties setting.
297+
/// </summary>
298+
private void ReloadDataWithAllProperties(bool allProperties)
299+
{
300+
_applicationData.AllProperties = allProperties;
301+
302+
// Recreate the data table with the new property settings
303+
DataTable newDataTable;
304+
if (_applicationData.PSObjects is { Count: > 0 })
305+
{
306+
var psObjects = _applicationData.PSObjects.Cast<PSObject>().ToList();
307+
newDataTable = TypeGetter.CastObjectsToTableView(psObjects, allProperties);
308+
}
309+
else
310+
{
311+
newDataTable = new DataTable([], []);
312+
}
313+
314+
// Update the data table reference
315+
typeof(OutGridViewWindow)
316+
.GetField("_dataTable", BindingFlags.NonPublic | BindingFlags.Instance)!
317+
.SetValue(this, newDataTable);
318+
319+
// Recalculate column widths
320+
var gridHeaders = newDataTable.DataColumns.Select(c => c.Label).ToList();
321+
_naturalColumnWidths = CalculateNaturalColumnWidths(gridHeaders);
322+
_gridViewDetails.ListViewColumnWidths = _naturalColumnWidths;
323+
_gridViewDetails.UsableWidth = _naturalColumnWidths.Sum();
324+
325+
// Update header
326+
if (_header is { })
327+
_header.Text = GridViewHelpers.GetPaddedString(gridHeaders, _gridViewDetails.ListViewOffset,
328+
_gridViewDetails.ListViewColumnWidths);
329+
330+
// Reload and reapply filter
331+
_inputSource = LoadData();
332+
ApplyFilter();
333+
334+
// Update content size
335+
_listView?.SetContentSize(new Size(_naturalColumnWidths.Sum(), _listView.GetContentSize().Height));
336+
337+
// Update status bar to show current state
338+
UpdateStatusBar();
339+
340+
// Force redraw
341+
SetNeedsLayout();
342+
SetNeedsDraw();
343+
}
344+
345+
/// <summary>
346+
/// Updates the status bar to reflect the current AllProperties state.
347+
/// </summary>
348+
private void UpdateStatusBar()
349+
{
350+
if (_statusBar == null) return;
351+
352+
// Remove and recreate status bar to update the checkbox text
353+
Remove(_statusBar);
354+
AddStatusBar();
355+
}
356+
294357
/// <summary>
295358
/// Accepts the current selection and closes the window.
296359
/// </summary>
@@ -477,6 +540,35 @@ private void AddStatusBar()
477540
}));
478541

479542
shortcuts.Add(new Shortcut(Key.Esc, "Close", Close));
543+
544+
CheckBox allPropertiesCheckBox = new CheckBox()
545+
{
546+
Title = "A_ll Properties",
547+
CheckedState = _applicationData.AllProperties ? CheckState.Checked : CheckState.UnChecked,
548+
CanFocus = false,
549+
};
550+
allPropertiesCheckBox.CheckedStateChanging += AllPropertiesCheckBoxOnCheckedStateChanging;
551+
552+
void AllPropertiesCheckBoxOnCheckedStateChanging(object? sender, ResultEventArgs<CheckState> e)
553+
{
554+
555+
}
556+
557+
allPropertiesCheckBox.CheckedStateChanged += AllPropertiesCheckBoxOnCheckedStateChanged;
558+
559+
void AllPropertiesCheckBoxOnCheckedStateChanged(object? sender, EventArgs<CheckState> e)
560+
{
561+
ReloadDataWithAllProperties(!_applicationData.AllProperties);
562+
}
563+
564+
Shortcut allPropertiesShortcut = new Shortcut()
565+
{
566+
CommandView = allPropertiesCheckBox,
567+
CanFocus = false,
568+
BindKeyToApplication = true,
569+
};
570+
shortcuts.Add(allPropertiesShortcut);
571+
480572
if (_applicationData.Verbose || _applicationData.Debug)
481573
{
482574
shortcuts.Add(new Shortcut(Key.Empty, $" v{_applicationData.ModuleVersion}", null));
@@ -485,7 +577,8 @@ private void AddStatusBar()
485577
null));
486578
}
487579

488-
Add(new StatusBar(shortcuts));
580+
_statusBar = new StatusBar(shortcuts);
581+
Add(_statusBar);
489582
}
490583

491584
#endregion

0 commit comments

Comments
 (0)