Skip to content

Commit 71cca64

Browse files
committed
Better use of v2 tech + added -AllProperties
Refactored `OutConsoleGridViewCmdletCommand` and `OutGridViewWindow` classes for improved readability, maintainability, and performance. Introduced a new `Header` class for dynamic grid column headers with bold and underlined styles. Simplified logic, removed redundant methods, and optimized nullability handling. Enhanced `TypeGetter` to filter out expensive `PS*` metadata properties, improving performance. Updated `ApplicationData` to clarify `AllProperties` behavior and removed unused imports. Added a new launch configuration in `launchSettings.json` for testing the `-AllProperties` parameter. Improved status bar shortcuts and event handling for better user experience. Updated comments and documentation for consistency.
1 parent f14ce81 commit 71cca64

File tree

6 files changed

+167
-233
lines changed

6 files changed

+167
-233
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System.Collections.Generic;
5+
using System.Reflection;
6+
using System.Reflection.PortableExecutable;
7+
using Terminal.Gui.Drawing;
8+
using Terminal.Gui.ViewBase;
9+
using Terminal.Gui.Views;
10+
11+
namespace Microsoft.PowerShell.ConsoleGuiTools;
12+
13+
/// <summary>
14+
/// A specialized view for displaying grid column headers with individual subviews for each column.
15+
/// </summary>
16+
internal sealed class Header : View
17+
{
18+
public Header()
19+
{
20+
Height = 1;
21+
CanFocus = false;
22+
Width = Dim.Fill();
23+
}
24+
25+
public override void EndInit()
26+
{
27+
base.EndInit();
28+
29+
30+
// We are a subview of the ListView.Padding.
31+
if (SuperView is Padding padding)
32+
{
33+
padding.Parent?.ViewportChanged += ListViewOnViewportChanged;
34+
}
35+
36+
void ListViewOnViewportChanged(object? sender, DrawEventArgs e)
37+
{
38+
if (sender is ListView listView)
39+
Viewport = Viewport with { X = listView.Viewport.X };
40+
}
41+
}
42+
43+
protected override void OnSubViewLayout(LayoutEventArgs args)
44+
{
45+
if (SuperView is Padding { Parent: ListView listView })
46+
SetContentSize(GetContentSize() with { Width = listView.GetContentSize().Width });
47+
48+
base.OnSubViewLayout(args);
49+
}
50+
51+
/// <summary>
52+
/// Updates the header with new column strings and widths.
53+
/// </summary>
54+
/// <param name="headers">The list of header strings to display.</param>
55+
/// <param name="columnWidths">The width of each column.</param>
56+
public void SetHeaders(List<string>? headers, int[]? columnWidths)
57+
{
58+
if (headers == null || columnWidths == null)
59+
return;
60+
61+
// Clear existing labels
62+
RemoveAll();
63+
64+
// Create a label for each header
65+
var currentX = 0;
66+
for (var i = 0; i < headers.Count; i++)
67+
{
68+
// Skip columns with zero width
69+
if (columnWidths[i] <= 0)
70+
continue;
71+
72+
var column = new View
73+
{
74+
Text = headers[i],
75+
X = currentX,
76+
Y = 0,
77+
Width = Dim.Auto(DimAutoStyle.Text),
78+
Height = 1,
79+
TextAlignment = Alignment.Start,
80+
VerticalTextAlignment = Alignment.Start
81+
};
82+
column.GettingAttributeForRole += ColumnOnGettingAttributeForRole;
83+
84+
void ColumnOnGettingAttributeForRole(object? sender, VisualRoleEventArgs e)
85+
{
86+
if (e.Role == VisualRole.Normal)
87+
{
88+
e.Result = e.Result!.Value with { Style = TextStyle.Bold | TextStyle.Underline };
89+
e.Handled = true;
90+
}
91+
}
92+
93+
Add(column);
94+
95+
// Move to next column position (width + 1 space separator)
96+
currentX += columnWidths[i] + 1;
97+
}
98+
}
99+
}

src/Microsoft.PowerShell.ConsoleGuiTools/OutConsoleGridviewCmdletCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ public class OutConsoleGridViewCmdletCommand : PSCmdlet, IDisposable
6767
public SwitchParameter MinUI { set; get; }
6868

6969
/// <summary>
70-
/// Gets or sets a value indicating whether the Terminal.Gui System.Net.Console-based ConsoleDriver will be used
70+
/// Gets or sets a value indicating whether the Terminal.Gui System.Net.Console-based Driver will be used
7171
/// instead of the
72-
/// default platform-specific (Windows or Curses) ConsoleDriver.
72+
/// default platform-specific (Windows or Curses) Driver.
7373
/// </summary>
7474
[Parameter(HelpMessage =
75-
"If specified the Terminal.Gui System.Net.Console-based ConsoleDriver (NetDriver) will be used.")]
75+
"If specified the Terminal.Gui System.Net.Console-based Driver (NetDriver) will be used.")]
7676
public SwitchParameter UseNetDriver { set; get; }
7777

7878
/// <summary>

0 commit comments

Comments
 (0)