Skip to content

Add paging support to Get-Help #13374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add paging support to Get-Help
  • Loading branch information
Aditya Patwardhan authored and Aditya Patwardhan committed Aug 7, 2020
commit 766d1c19d43f878ce5f202d177ed31ff3732ec38
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<!-- the following package(s) are from the powershell org -->
<PackageReference Include="Microsoft.Management.Infrastructure" Version="2.0.0" />
<PackageReference Include="Microsoft.PowerShell.Native" Version="7.0.0" />
<PackageReference Include="Microsoft.PowerShell.Pager" Version="1.0.0-preview.2" />
</ItemGroup>

<PropertyGroup>
Expand Down
140 changes: 135 additions & 5 deletions src/System.Management.Automation/help/HelpCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
using System.Management.Automation.Internal;
using System.Management.Automation.Runspaces;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.PowerShell;
#if !UNIX
using Microsoft.Win32;
#endif
Expand Down Expand Up @@ -115,15 +117,21 @@ public SwitchParameter Detailed
[Parameter(ParameterSetName = "AllUsersView")]
public SwitchParameter Full
{
private get => _full;

set
{
if (value.ToBool())
_full = value.ToBool();

if (_full)
{
_viewTokenToAdd = HelpView.FullView;
}
}
}

private bool _full;

/// <summary>
/// Changes the view of HelpObject returned.
/// </summary>
Expand All @@ -141,15 +149,24 @@ public SwitchParameter Full
[Parameter(ParameterSetName = "Examples", Mandatory = true)]
public SwitchParameter Examples
{
private get
{
return _examples;
}

set
{
if (value.ToBool())
_examples = value.ToBool();

if (_examples)
{
_viewTokenToAdd = HelpView.ExamplesView;
}
}
}

private bool _examples;

/// <summary>
/// Parameter name.
/// </summary>
Expand Down Expand Up @@ -202,6 +219,20 @@ public SwitchParameter Online

private bool _showOnlineHelp;

/// <summary>
/// This parameter,if true, will direct get-help cmdlet to
/// display the help content using Microsoft.PowerShell.Pager on alternate screen buffer.
/// </summary>
[Parameter]
public SwitchParameter Paged
{
get;
set;
}

private Pager pager;
private string multiItemPagerBuffer;

#if !UNIX
private GraphicalHostReflectionWrapper graphicalHostReflectionWrapper;
private bool showWindow;
Expand Down Expand Up @@ -336,6 +367,16 @@ protected override void ProcessRecord()
{
throw PSTraceSource.NewInvalidOperationException(HelpErrors.MultipleOnlineTopicsNotSupported, "Online");
}
else if (Paged && countOfHelpInfos > 1)
{
if (pager is null)
{
pager = new Pager();
}

pager.Write(multiItemPagerBuffer);

}

// show errors only if there is no wildcard search or VerboseHelpErrors is true.
if (((countOfHelpInfos == 0) && (!WildcardPattern.ContainsWildcardCharacters(helpRequest.Target)))
Expand Down Expand Up @@ -496,7 +537,7 @@ private void GetAndWriteParameterInfo(HelpInfo helpInfo)
{
foreach (PSObject pInfo in pInfos)
{
WriteObject(pInfo);
WriteObjectOrWritePaged(pInfo, bufferOutputWhenPaged: false);
}
}
}
Expand Down Expand Up @@ -597,7 +638,7 @@ private void WriteObjectsOrShowOnlineHelp(HelpInfo helpInfo, bool showFullHelp)
{
PSObject objectToReturn = TransformView(helpInfo.FullHelp);
objectToReturn.IsHelpObject = true;
WriteObject(objectToReturn);
WriteObjectOrWritePaged(objectToReturn, bufferOutputWhenPaged: false);
}
}
else
Expand All @@ -612,12 +653,101 @@ private void WriteObjectsOrShowOnlineHelp(HelpInfo helpInfo, bool showFullHelp)
}
}

WriteObject(helpInfo.ShortHelp);
WriteObjectOrWritePaged(helpInfo.ShortHelp, bufferOutputWhenPaged: true);
}
}
}
}

private void WriteObjectOrWritePaged(object sendToPipeline, bool bufferOutputWhenPaged)
{
if (Paged)
{
if (pager is null)
{
pager = new Pager();
}

var helpText = GetHelpOutput();

if (bufferOutputWhenPaged)
{
multiItemPagerBuffer = helpText;
}
else
{
pager.Write(helpText);
}
}
else
{
WriteObject(sendToPipeline);
}
}

private string GetHelpOutput()
{
string helpContentAsString = null;

using (var ps = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace))
{
ps.AddCommand(@"Microsoft.PowerShell.Core\Get-Help");

StringBuilder sb = new StringBuilder();

if (!string.IsNullOrEmpty(this.Name))
{
ps.AddParameter(nameof(this.Name), this.Name);
}

if (this.Category is not null)
{
ps.AddParameter(nameof(this.Category), this.Category);
}

if (this.Component is not null)
{
ps.AddParameter(nameof(this.Component), this.Component);
}

if (this.Functionality is not null)
{
ps.AddParameter(nameof(this.Functionality), this.Functionality);
}

if (!string.IsNullOrEmpty(this.Path))
{
ps.AddParameter(nameof(this.Path), this.Path);
}

if (this.Role is not null)
{
ps.AddParameter(nameof(this.Role), this.Role);
}

if (this.Examples)
{
ps.AddParameter(nameof(this.Examples), this.Examples);
}

if (this.Full)
{
ps.AddParameter(nameof(this.Full), this.Full);
}

if (this.Parameter is not null)
{
ps.AddParameter(nameof(this.Parameter), this.Parameter);
}

ps.AddCommand(@"Microsoft.PowerShell.Utility\Out-String");

helpContentAsString = ps.Invoke<string>().FirstOrDefault();
}

return helpContentAsString;
}

/// <summary>
/// Opens the Uri. System's default application will be used
/// to show the uri.
Expand Down