Skip to content

Commit

Permalink
Add the TerminateOrphanedConsoleApps option on Windows to kill orph…
Browse files Browse the repository at this point in the history
…aned console-attached process that may mess up reading from Console input (#3764)
  • Loading branch information
jazzdelightsme authored Aug 9, 2023
1 parent 33c96af commit 4d78ce1
Show file tree
Hide file tree
Showing 6 changed files with 474 additions and 25 deletions.
10 changes: 10 additions & 0 deletions PSReadLine/Cmdlets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,8 @@ public object ListPredictionTooltipColor
set => _listPredictionTooltipColor = VTColorUtils.AsEscapeSequence(value);
}

public bool TerminateOrphanedConsoleApps { get; set; }

internal string _defaultTokenColor;
internal string _commentColor;
internal string _keywordColor;
Expand Down Expand Up @@ -808,6 +810,14 @@ public PredictionViewStyle PredictionViewStyle
[Parameter]
public Hashtable Colors { get; set; }

[Parameter]
public SwitchParameter TerminateOrphanedConsoleApps
{
get => _terminateOrphanedConsoleApps.GetValueOrDefault();
set => _terminateOrphanedConsoleApps = value;
}
internal SwitchParameter? _terminateOrphanedConsoleApps;

[ExcludeFromCodeCoverage]
protected override void EndProcessing()
{
Expand Down
18 changes: 18 additions & 0 deletions PSReadLine/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Management.Automation;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using Microsoft.PowerShell.PSReadLine;

Expand Down Expand Up @@ -167,6 +169,22 @@ private void SetOptionsInternal(SetPSReadLineOption options)
}
}
}
if (options._terminateOrphanedConsoleApps.HasValue)
{
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
Options.TerminateOrphanedConsoleApps = options.TerminateOrphanedConsoleApps;
PlatformWindows.SetTerminateOrphanedConsoleApps(Options.TerminateOrphanedConsoleApps);
}
else
{
throw new PlatformNotSupportedException(
string.Format(
CultureInfo.CurrentUICulture,
PSReadLineResources.OptionNotSupportedOnNonWindows,
nameof(Options.TerminateOrphanedConsoleApps)));
}
}
}

private void SetKeyHandlerInternal(string[] keys, Action<ConsoleKeyInfo?, object> handler, string briefDescription, string longDescription, ScriptBlock scriptBlock)
Expand Down
3 changes: 3 additions & 0 deletions PSReadLine/PSReadLine.format.ps1xml
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ $d = [Microsoft.PowerShell.KeyHandler]::GetGroupingDescription($_.Group)
<ListItem>
<PropertyName>PredictionViewStyle</PropertyName>
</ListItem>
<ListItem>
<PropertyName>TerminateOrphanedConsoleApps</PropertyName>
</ListItem>
<ListItem>
<Label>CommandColor</Label>
<ScriptBlock>[Microsoft.PowerShell.VTColorUtils]::FormatColor($_.CommandColor)</ScriptBlock>
Expand Down
11 changes: 11 additions & 0 deletions PSReadLine/PSReadLineResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions PSReadLine/PSReadLineResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -873,4 +873,7 @@ Or not saving history with:
<data name="UpcaseWordDescription" xml:space="preserve">
<value>Find the next word starting from the current position and then make it upper case.</value>
</data>
<data name="OptionNotSupportedOnNonWindows" xml:space="preserve">
<value>The '{0}' option is not supported on non-Windows platforms.</value>
</data>
</root>
Loading

0 comments on commit 4d78ce1

Please sign in to comment.