-
Notifications
You must be signed in to change notification settings - Fork 322
Expand file tree
/
Copy pathDynamicHelp.cs
More file actions
306 lines (258 loc) · 10.3 KB
/
DynamicHelp.cs
File metadata and controls
306 lines (258 loc) · 10.3 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
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Language;
using Microsoft.PowerShell.Internal;
using Microsoft.PowerShell.PSReadLine;
namespace Microsoft.PowerShell
{
public partial class PSConsoleReadLine
{
// Stub helper methods so dynamic help can be mocked
[ExcludeFromCodeCoverage]
void IPSConsoleReadLineMockableMethods.RenderFullHelp(string content, string regexPatternToScrollTo)
{
_pager ??= new Pager();
_pager.Write(content, regexPatternToScrollTo);
}
[ExcludeFromCodeCoverage]
object IPSConsoleReadLineMockableMethods.GetDynamicHelpContent(string commandName, string parameterName, bool isFullHelp)
{
if (string.IsNullOrEmpty(commandName))
{
return null;
}
System.Management.Automation.PowerShell ps = null;
try
{
if (!_mockableMethods.RunspaceIsRemote(_runspace))
{
ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace);
}
else
{
ps = System.Management.Automation.PowerShell.Create();
ps.Runspace = _runspace;
}
if (isFullHelp)
{
return ps
.AddCommand($"Microsoft.PowerShell.Core\\Get-Help")
.AddParameter("Name", commandName)
.AddParameter("Full", value: true)
.AddCommand($"Microsoft.PowerShell.Utility\\Out-String")
.Invoke<string>()
.FirstOrDefault();
}
if (string.IsNullOrEmpty(parameterName))
{
return null;
}
return ps
.AddCommand("Microsoft.PowerShell.Core\\Get-Help")
.AddParameter("Name", commandName)
.AddParameter("Parameter", parameterName)
.Invoke<PSObject>()
.FirstOrDefault();
}
catch (Exception)
{
return null;
}
finally
{
ps?.Dispose();
// GetDynamicHelpContent could scroll the screen, e.g. via Write-Progress. For example,
// Get-Help for unknown command under the CloudShell Azure drive will show the progress bar while searching for command.
// We need to update the _initialY in case the current cursor postion has changed.
if (_singleton._initialY > _console.CursorTop)
{
_singleton._initialY = _console.CursorTop;
}
}
}
private Pager _pager;
/// <summary>
/// Attempt to show help content.
/// Show the full help for the command on the alternate screen buffer.
/// </summary>
public static void ShowCommandHelp(ConsoleKeyInfo? key = null, object arg = null)
{
if (_singleton._console is PlatformWindows.LegacyWin32Console)
{
Collection<string> helpBlock = new Collection<string>()
{
string.Empty,
PSReadLineResources.FullHelpNotSupportedInLegacyConsole
};
_singleton.WriteDynamicHelpBlock(helpBlock);
return;
}
_singleton.DynamicHelpImpl(isFullHelp: true);
}
/// <summary>
/// Attempt to show help content.
/// Show the short help of the parameter next to the cursor.
/// </summary>
public static void ShowParameterHelp(ConsoleKeyInfo? key = null, object arg = null)
{
_singleton.DynamicHelpImpl(isFullHelp: false);
}
private void WriteDynamicHelpContent(string commandName, string parameterName, bool isFullHelp)
{
var helpContent = _mockableMethods.GetDynamicHelpContent(commandName, parameterName, isFullHelp);
if (helpContent is string fullHelp && fullHelp.Length > 0)
{
string regexPatternToScrollTo = null;
if (!string.IsNullOrEmpty(parameterName))
{
regexPatternToScrollTo = $"-{parameterName} [<|\\[]";
}
_mockableMethods.RenderFullHelp(fullHelp, regexPatternToScrollTo);
}
else if (helpContent is PSObject paramHelp)
{
WriteParameterHelp(paramHelp);
}
}
private void DynamicHelpImpl(bool isFullHelp)
{
int cursor = _singleton._current;
string commandName = null;
string parameterName = null;
// Simply return if nothing is rendered yet.
if (_singleton._tokens == null) { return; }
foreach(var token in _singleton._tokens)
{
var extent = token.Extent;
if (extent.StartOffset > cursor)
{
break;
}
if (token.TokenFlags == TokenFlags.CommandName)
{
commandName = token.Text;
}
if (extent.StartOffset <= cursor && extent.EndOffset >= cursor)
{
if (token.Kind == TokenKind.Parameter)
{
parameterName = ((ParameterToken)token).ParameterName;
break;
}
}
}
WriteDynamicHelpContent(commandName, parameterName, isFullHelp);
}
private void WriteDynamicHelpBlock(Collection<string> helpBlock)
{
var dynHelp = new MultilineDisplayBlock
{
Singleton = this,
ItemsToDisplay = helpBlock
};
dynHelp.DrawMultilineBlock();
ReadKey();
dynHelp.Clear();
}
private void WriteParameterHelp(dynamic helpContent)
{
System.Diagnostics.Debug.Assert(helpContent is not null);
Collection<string> helpBlock;
if (helpContent.Description is not string descriptionText)
{
descriptionText = helpContent.Description?[0]?.Text;
}
if (descriptionText is null)
{
helpBlock = new Collection<string>()
{
string.Empty,
PSReadLineResources.NeedsUpdateHelp
};
}
else
{
string syntax = $"-{helpContent.name} <{helpContent.type?.name}>";
helpBlock = new Collection<string>
{
string.Empty,
syntax,
string.Empty
};
string text = descriptionText;
if (text.Contains("\n"))
{
string[] lines = text.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < lines.Length; i++)
{
string prefix = i == 0 ? "DESC: " : " ";
string s = prefix + lines[i].Trim('\r');
helpBlock.Add(s);
}
}
else
{
string desc = "DESC: " + text;
// trim new line characters as some help content has it at the end of the first list on the description.
helpBlock.Add(desc.Trim('\r', '\n'));
}
string details = $"Required: {helpContent.required}, Position: {helpContent.position}, Default Value: {helpContent.defaultValue}, Pipeline Input: {helpContent.pipelineInput}, WildCard: {helpContent.globbing}";
helpBlock.Add(details);
}
WriteDynamicHelpBlock(helpBlock);
}
private class MultilineDisplayBlock : DisplayBlockBase
{
internal Collection<string> ItemsToDisplay;
// Keep track of the number of extra physical lines due to multi-line text.
private int extraPhysicalLines = 0;
public void DrawMultilineBlock()
{
IConsole console = Singleton._console;
extraPhysicalLines = 0;
SaveCursor();
MoveCursorToStartDrawingPosition(console);
var bufferWidth = console.BufferWidth;
var items = ItemsToDisplay;
for (var index = 0; index < items.Count; index++)
{
var itemLength = LengthInBufferCells(items[index]);
int extra = 0;
if (itemLength > bufferWidth)
{
extra = itemLength / bufferWidth;
if (itemLength % bufferWidth == 0)
{
extra--;
}
}
if (extra > 0)
{
// Extra physical lines may cause buffer to scroll up.
AdjustForPossibleScroll(extra);
extraPhysicalLines += extra;
}
console.Write(items[index]);
// Explicit newline so consoles see each row as distinct lines, but skip the
// last line so we don't scroll.
if (index != (items.Count - 1))
{
AdjustForPossibleScroll(1);
MoveCursorDown(1);
}
}
RestoreCursor();
}
public void Clear()
{
_singleton.WriteBlankLines(Top, ItemsToDisplay.Count + extraPhysicalLines);
}
}
}
}