This repository was archived by the owner on Jun 24, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathShowObjectView.cs
More file actions
462 lines (387 loc) · 13.6 KB
/
Copy pathShowObjectView.cs
File metadata and controls
462 lines (387 loc) · 13.6 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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management.Automation;
using System.Reflection;
using System.Text.RegularExpressions;
using OutGridView.Models;
using Terminal.Gui;
using Terminal.Gui.Trees;
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 CachedMemberResult 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 CachedMemberResult 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 CachedMemberResult p)
{
if (p.IsCollection)
{
return p.Elements;
}
return GetChildren(p.Value);
}
if (forObject is CachedMemberResultElement e)
{
return GetChildren(e.Value);
}
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;
}
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(p => p.BaseObject).ToList(), applicationData);
Application.Top.Add(window);
Application.Run();
}
finally
{
Application.Shutdown();
window?.Dispose();
}
}
sealed class CachedMemberResultElement
{
public int Index;
public object Value;
private string representation;
public CachedMemberResultElement(object value, int index)
{
Index = index;
Value = value;
try
{
representation = Value?.ToString() ?? "Null";
}
catch (Exception)
{
Value = representation = "Unavailable";
}
}
public override string ToString()
{
return $"[{Index}]: {representation}]";
}
}
sealed class CachedMemberResult
{
public MemberInfo Member;
public object Value;
public object Parent;
private string representation;
private List<CachedMemberResultElement> valueAsList;
public bool IsCollection => valueAsList != null;
public IReadOnlyCollection<CachedMemberResultElement> Elements => valueAsList?.AsReadOnly();
public CachedMemberResult(object parent, MemberInfo mem)
{
Parent = parent;
Member = mem;
try
{
if (mem is PropertyInfo p)
{
Value = p.GetValue(parent);
}
else if (mem is FieldInfo f)
{
Value = f.GetValue(parent);
}
else
{
throw new NotSupportedException($"Unknown {nameof(MemberInfo)} Type");
}
representation = ValueToString();
}
catch (Exception)
{
Value = representation = "Unavailable";
}
}
private string ValueToString()
{
if (Value == null)
{
return "Null";
}
try
{
if (IsCollectionOfKnownTypeAndSize(out Type elementType, out int size))
{
return $"{elementType.Name}[{size}]";
}
}
catch (Exception)
{
return Value?.ToString();
}
return Value?.ToString();
}
private bool IsCollectionOfKnownTypeAndSize(out Type elementType, out int size)
{
elementType = null;
size = 0;
if (Value == null || Value is string)
{
return false;
}
if (Value is IEnumerable ienumerable)
{
var list = ienumerable.Cast<object>().ToList();
var types = list.Where(v => v != null).Select(v => v.GetType()).Distinct().ToArray();
if (types.Length == 1)
{
elementType = types[0];
size = list.Count;
valueAsList = list.Select((e, i) => new CachedMemberResultElement(e, i)).ToList();
return true;
}
}
return false;
}
public override string ToString()
{
return Member.Name + ": " + representation;
}
}
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;
}
}
}
}
}