Skip to content

Commit 06c70c8

Browse files
committed
Add ScreenReader support CLI option
That should default to enabled when one is detected on startup, but also allows the support to be forcibly enabled.
1 parent ceaad10 commit 06c70c8

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

PSReadLine/Accessibility.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ internal static bool IsScreenReaderActive()
1414

1515
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
1616
{
17+
// NOTE: This API can detect if a third-party screen reader is active, such as NVDA, but not the in-box Windows Narrator.
1718
PlatformWindows.SystemParametersInfo(PlatformWindows.SPI_GETSCREENREADER, 0, ref returnValue, 0);
18-
}
19+
} // TODO: Support other platforms per https://code.visualstudio.com/docs/configure/accessibility/accessibility
1920

2021
return returnValue;
2122
}

PSReadLine/Cmdlets.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
using System.Runtime.InteropServices;
1616
using System.Threading;
1717
using Microsoft.PowerShell.PSReadLine;
18+
using Microsoft.PowerShell.Internal;
1819
using AllowNull = System.Management.Automation.AllowNullAttribute;
1920

2021
namespace Microsoft.PowerShell
@@ -163,6 +164,11 @@ public class PSConsoleReadLineOptions
163164
/// </summary>
164165
public const int DefaultAnsiEscapeTimeout = 100;
165166

167+
/// <summary>
168+
/// If screen reader support is enabled, which enables safe rendering using ANSI control codes.
169+
/// </summary>
170+
public static readonly bool DefaultScreenReader = Accessibility.IsScreenReaderActive();
171+
166172
static PSConsoleReadLineOptions()
167173
{
168174
// For inline-view suggestion text, we use the new FG color 'dim white italic' when possible, because it provides
@@ -224,6 +230,7 @@ public PSConsoleReadLineOptions(string hostName, bool usingLegacyConsole)
224230
: PredictionSource.HistoryAndPlugin;
225231
PredictionViewStyle = DefaultPredictionViewStyle;
226232
MaximumHistoryCount = 0;
233+
ScreenReader = DefaultScreenReader;
227234

228235
var historyFileName = hostName + "_history.txt";
229236
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
@@ -533,6 +540,8 @@ public object ListPredictionTooltipColor
533540

534541
public bool TerminateOrphanedConsoleApps { get; set; }
535542

543+
public bool ScreenReader { get; set; }
544+
536545
internal string _defaultTokenColor;
537546
internal string _commentColor;
538547
internal string _keywordColor;
@@ -847,6 +856,14 @@ public SwitchParameter TerminateOrphanedConsoleApps
847856
}
848857
internal SwitchParameter? _terminateOrphanedConsoleApps;
849858

859+
[Parameter]
860+
public SwitchParameter ScreenReader
861+
{
862+
get => _screenReader.GetValueOrDefault();
863+
set => _screenReader = value;
864+
}
865+
internal SwitchParameter? _screenReader;
866+
850867
[ExcludeFromCodeCoverage]
851868
protected override void EndProcessing()
852869
{

PSReadLine/Options.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ private void SetOptionsInternal(SetPSReadLineOption options)
185185
nameof(Options.TerminateOrphanedConsoleApps)));
186186
}
187187
}
188+
if (options._screenReader.HasValue)
189+
{
190+
Options.ScreenReader = options.ScreenReader;
191+
}
188192
}
189193

190194
private void SetKeyHandlerInternal(string[] keys, Action<ConsoleKeyInfo?, object> handler, string briefDescription, string longDescription, ScriptBlock scriptBlock)

0 commit comments

Comments
 (0)