-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPowerShellIntegration.cs
More file actions
139 lines (118 loc) · 4.6 KB
/
PowerShellIntegration.cs
File metadata and controls
139 lines (118 loc) · 4.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
using System;
using System.IO;
using System.Linq;
using System.Text;
using NStack;
using System.Management.Automation;
namespace Terminal.Gui.Designer
{
public class PowerShellIntegration
{
private DesignerState _state;
public PowerShellIntegration(DesignerState state)
{
_state = state;
}
public void Save()
{
var stringBuilder = new StringBuilder();
stringBuilder.AppendLine("# This file was generated at " + DateTime.Now);
stringBuilder.AppendLine("# Manually editing this file may result in issues with the designer");
var id = _state.Window.Id;
stringBuilder.AppendLine($"${id} = [{_state.Window.GetType()}]::new()");
stringBuilder.AppendLine($"${id}.Id = '{id}'");
var window = (FrameView)_state.Window;
stringBuilder.AppendLine($"${id}.Title = '{window.Title}'");
stringBuilder.AppendLine($"${id}.X = 0");
stringBuilder.AppendLine($"${id}.Y = 0");
stringBuilder.AppendLine($"${id}.Width = [Terminal.Gui.Dim]::Fill()");
stringBuilder.AppendLine($"${id}.Height = [Terminal.Gui.Dim]::Fill()");
WriteSubViews(_state.Window, stringBuilder);
stringBuilder.AppendLine($"${_state.Window.Id}");
File.WriteAllText(_state.FileName, stringBuilder.ToString());
}
private void WriteView(View view, StringBuilder stringBuilder)
{
stringBuilder.AppendLine($"${view.Id} = [{view.GetType()}]::new()");
WriteProperties(view, stringBuilder);
WriteSubViews(view, stringBuilder);
}
private void WriteSubViews(View view, StringBuilder stringBuilder)
{
if (view is FrameView)
{
// Skip nested content view
WriteSubViews(view.Subviews.First(), stringBuilder);
}
else if (view is ComboBox)
{
return;
}
else
{
var id = view.Id;
if (id.IsEmpty)
{
id = view.SuperView.Id;
}
foreach (var subView in view.Subviews)
{
WriteView(subView, stringBuilder);
stringBuilder.AppendLine($"${id}.Add(${subView.Id})");
}
}
}
private void WriteProperties(View view, StringBuilder stringBuilder)
{
var left = $"${view.Id}";
foreach (var property in view.GetType().GetProperties().Where(m => m.CanWrite))
{
if (Constants.SkippedProperties.Contains(property.Name)) continue;
var value = property.GetValue(view);
if (value == null) continue;
if (value is string || value is ustring)
{
stringBuilder.AppendLine($"{left}.{property.Name} = '{value}'");
}
else if (value is ustring[] arr)
{
stringBuilder.AppendLine($"{left}.{property.Name} = @({arr.Aggregate((x, y) => x + "," + y)})");
}
else if (value is bool)
{
stringBuilder.AppendLine($"{left}.{property.Name} = ${value}");
}
else if (value is Pos pos)
{
var posInfo = new PosInfo(pos);
stringBuilder.AppendLine($"{left}.{property.Name} = {posInfo}");
}
else if (value is Dim dim)
{
var dimInfo = new DimInfo(dim);
stringBuilder.AppendLine($"{left}.{property.Name} = {dimInfo}");
}
else if (property.PropertyType.IsEnum)
{
stringBuilder.AppendLine($"{left}.{property.Name} = '{value}'");
}
else
{
stringBuilder.AppendLine($"{left}.{property.Name} = {value}");
}
}
}
public void Load()
{
if (string.IsNullOrEmpty(_state.FileName)) return;
using (var powerShell = PowerShell.Create())
{
var contents = File.ReadAllText(_state.FileName);
powerShell.AddScript(contents);
var objects = powerShell.Invoke();
var window = objects.Select(m => m.BaseObject).OfType<View>().First().SuperView;
_state.LoadWindow(window);
}
}
}
}