-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathShowObjectView.cs
More file actions
356 lines (302 loc) · 10.7 KB
/
ShowObjectView.cs
File metadata and controls
356 lines (302 loc) · 10.7 KB
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using OutGridView.Models;
using Terminal.Gui;
using Terminal.Gui.Trees;
using OutGridView.Cmdlet.TreeNodeCaching;
namespace OutGridView.Cmdlet
{
internal sealed class ShowObjectView : Window, ITreeBuilder<object>
{
private readonly TreeView<object> tree;
private readonly RegexTreeViewTextFilter filter;
private readonly Label filterErrorLabel;
public bool SupportsCanExpand => true;
private StatusItem selectedStatusBarItem;
private StatusBar statusBar;
public ShowObjectView(List<object> rootObjects, ApplicationData applicationData)
{
Title = applicationData.Title;
Width = Dim.Fill();
Height = Dim.Fill(1);
Modal = false;
if (applicationData.MinUI)
{
Border.BorderStyle = BorderStyle.None;
Title = string.Empty;
X = -1;
Height = Dim.Fill();
}
tree = new TreeView<object>
{
Y = applicationData.MinUI ? 0 : 2,
Width = Dim.Fill(),
Height = Dim.Fill(),
};
tree.TreeBuilder = this;
tree.AspectGetter = this.AspectGetter;
tree.SelectionChanged += this.SelectionChanged;
tree.ClearKeybinding(Command.ExpandAll);
this.filter = new RegexTreeViewTextFilter(this, tree);
this.filter.Text = applicationData.Filter ?? string.Empty;
tree.Filter = this.filter;
if (rootObjects.Count > 0)
{
tree.AddObjects(rootObjects);
}
else
{
tree.AddObject("No Objects");
}
statusBar = new StatusBar();
string elementDescription = "objects";
var types = rootObjects.Select(o => o.GetType()).Distinct().ToArray();
if (types.Length == 1)
{
elementDescription = types[0].Name;
}
var lblFilter = new Label()
{
Text = "Filter:",
X = 1,
};
var tbFilter = new TextField()
{
X = Pos.Right(lblFilter),
Width = Dim.Fill(1),
Text = applicationData.Filter ?? string.Empty
};
tbFilter.CursorPosition = tbFilter.Text.Length;
tbFilter.TextChanged += (_) =>
{
filter.Text = tbFilter.Text.ToString();
};
filterErrorLabel = new Label(string.Empty)
{
X = Pos.Right(lblFilter) + 1,
Y = Pos.Top(lblFilter) + 1,
ColorScheme = Colors.Base,
Width = Dim.Fill() - lblFilter.Text.Length
};
if (!applicationData.MinUI)
{
Add(lblFilter);
Add(tbFilter);
Add(filterErrorLabel);
}
int pos = 0;
statusBar.AddItemAt(pos++, new StatusItem(Key.Esc, "~ESC~ Close", () => Application.RequestStop()));
var siCount = new StatusItem(Key.Null, $"{rootObjects.Count} {elementDescription}", null);
selectedStatusBarItem = new StatusItem(Key.Null, string.Empty, null);
statusBar.AddItemAt(pos++, siCount);
statusBar.AddItemAt(pos++, selectedStatusBarItem);
if (applicationData.Debug)
{
statusBar.AddItemAt(pos++, new StatusItem(Key.Null, $" v{applicationData.ModuleVersion}", null));
statusBar.AddItemAt(pos++, new StatusItem(Key.Null,
$"{Application.Driver} v{FileVersionInfo.GetVersionInfo(Assembly.GetAssembly(typeof(Application)).Location).ProductVersion}", null));
}
statusBar.Visible = !applicationData.MinUI;
Application.Top.Add(statusBar);
Add(tree);
}
private void SetRegexError(string error)
{
if (string.Equals(error, filterErrorLabel.Text.ToString(), StringComparison.Ordinal))
{
return;
}
filterErrorLabel.Text = error;
filterErrorLabel.ColorScheme = Colors.Error;
filterErrorLabel.Redraw(filterErrorLabel.Bounds);
}
private void SelectionChanged(object sender, SelectionChangedEventArgs<object> e)
{
var selectedValue = e.NewValue;
if (selectedValue is ICachedMemberResult cmr)
{
selectedValue = cmr.Value;
}
if (selectedValue != null && selectedStatusBarItem != null)
{
selectedStatusBarItem.Title = selectedValue.GetType().Name;
}
else
{
selectedStatusBarItem.Title = string.Empty;
}
statusBar.SetNeedsDisplay();
}
private string AspectGetter(object toRender)
{
if (toRender is Process p)
{
return p.ProcessName;
}
if (toRender is null)
{
return "Null";
}
if (toRender is FileSystemInfo fsi && !IsRootObject(fsi))
{
return fsi.Name;
}
return toRender.ToString();
}
private bool IsRootObject(object o)
{
return tree.Objects.Contains(o);
}
public bool CanExpand(object toExpand)
{
if (toExpand is ICachedMemberResult p)
{
return IsBasicType(p?.Value);
}
// Any complex object type can be expanded to reveal properties
return IsBasicType(toExpand);
}
private static bool IsBasicType(object value)
{
return value != null && value is not string && !value.GetType().IsValueType;
}
public IEnumerable<object> GetChildren(object forObject)
{
if (forObject == null || !this.CanExpand(forObject))
{
return Enumerable.Empty<object>();
}
if (forObject is ICachedMemberResult p)
{
if (p.IsCollection)
{
return p.Elements;
}
return GetChildren(p.Value);
}
if (forObject is CachedMemberResultElement e)
{
return GetChildren(e.Value);
}
if(forObject is PSObject pso)
{
return GetPSObjectChildren(pso);
}
List<object> children = new List<object>();
foreach (var member in forObject.GetType().GetMembers(BindingFlags.Instance | BindingFlags.Public).OrderBy(m => m.Name))
{
if (member is PropertyInfo prop)
{
children.Add(new CachedMemberResult(forObject, prop));
}
if (member is FieldInfo field)
{
children.Add(new CachedMemberResult(forObject, field));
}
}
try
{
children.AddRange(GetExtraChildren(forObject));
}
catch (Exception)
{
// Extra children unavailable, possibly security or IO exceptions enumerating children etc
}
return children;
}
/// <summary>
/// We only deal with PSObject when there is no native type (e.g. Process).
/// For example when the PSObject.BaseObject is a PSCustomObject.
/// </summary>
/// <param name="pso"></param>
/// <returns></returns>
public IEnumerable<object> GetPSObjectChildren(PSObject pso)
{
foreach(var m in pso.Members.Where(PsoHelper.IsDisplayableMember))
{
yield return new CachedPSObjectMemberResult(pso, m);
}
}
private static IEnumerable<object> GetExtraChildren(object forObject)
{
if (forObject is DirectoryInfo dir)
{
foreach (var c in dir.EnumerateFileSystemInfos())
{
yield return c;
}
}
}
internal static void Run(List<PSObject> objects, ApplicationData applicationData)
{
// Note, in Terminal.Gui v2, this property is renamed to Application.UseNetDriver, hence
// using that terminology here.
Application.UseSystemConsole = applicationData.UseNetDriver;
Application.Init();
Window window = null;
try
{
window = new ShowObjectView(objects.Select(PsoHelper.MaybeUnwrap).ToList(), applicationData);
Application.Top.Add(window);
Application.Run();
}
finally
{
Application.Shutdown();
window?.Dispose();
}
}
private sealed class RegexTreeViewTextFilter : ITreeViewFilter<object>
{
private readonly ShowObjectView parent;
readonly TreeView<object> _forTree;
public RegexTreeViewTextFilter(ShowObjectView parent, TreeView<object> forTree)
{
this.parent = parent;
_forTree = forTree ?? throw new ArgumentNullException(nameof(forTree));
}
private string text;
public string Text
{
get { return text; }
set
{
text = value;
RefreshTreeView();
}
}
private void RefreshTreeView()
{
_forTree.InvalidateLineMap();
_forTree.SetNeedsDisplay();
}
public bool IsMatch(object model)
{
if (string.IsNullOrWhiteSpace(Text))
{
return true;
}
parent.SetRegexError(string.Empty);
var modelText = _forTree.AspectGetter(model);
try
{
return Regex.IsMatch(modelText, text, RegexOptions.IgnoreCase);
}
catch (RegexParseException e)
{
parent.SetRegexError(e.Message);
return true;
}
}
}
}
}