|
| 1 | +// |
| 2 | +// Copyright (c) Microsoft. All rights reserved. |
| 3 | +// Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| 4 | +// |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Runtime.InteropServices; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | + |
| 11 | +namespace Microsoft.PowerShell.EditorServices.Console |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Provides asynchronous implementations of the <see cref="Console" /> API's as well as |
| 15 | + /// synchronous implementations that work around platform specific issues. |
| 16 | + /// </summary> |
| 17 | + internal static class ConsoleProxy |
| 18 | + { |
| 19 | + private static IConsoleOperations s_consoleProxy; |
| 20 | + |
| 21 | + static ConsoleProxy() |
| 22 | + { |
| 23 | + // Maybe we should just include the RuntimeInformation package for FullCLR? |
| 24 | +#if CoreCLR |
| 25 | + if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| 26 | + { |
| 27 | + s_consoleProxy = new WindowsConsoleOperations(); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + s_consoleProxy = new UnixConsoleOperations(); |
| 32 | +#else |
| 33 | + s_consoleProxy = new WindowsConsoleOperations(); |
| 34 | +#endif |
| 35 | + } |
| 36 | + |
| 37 | + public static Task<ConsoleKeyInfo> ReadKeyAsync(CancellationToken cancellationToken) => |
| 38 | + s_consoleProxy.ReadKeyAsync(cancellationToken); |
| 39 | + |
| 40 | + public static int GetCursorLeft() => |
| 41 | + s_consoleProxy.GetCursorLeft(); |
| 42 | + |
| 43 | + public static int GetCursorLeft(CancellationToken cancellationToken) => |
| 44 | + s_consoleProxy.GetCursorLeft(cancellationToken); |
| 45 | + |
| 46 | + public static Task<int> GetCursorLeftAsync() => |
| 47 | + s_consoleProxy.GetCursorLeftAsync(); |
| 48 | + |
| 49 | + public static Task<int> GetCursorLeftAsync(CancellationToken cancellationToken) => |
| 50 | + s_consoleProxy.GetCursorLeftAsync(cancellationToken); |
| 51 | + |
| 52 | + public static int GetCursorTop() => |
| 53 | + s_consoleProxy.GetCursorTop(); |
| 54 | + |
| 55 | + public static int GetCursorTop(CancellationToken cancellationToken) => |
| 56 | + s_consoleProxy.GetCursorTop(cancellationToken); |
| 57 | + |
| 58 | + public static Task<int> GetCursorTopAsync() => |
| 59 | + s_consoleProxy.GetCursorTopAsync(); |
| 60 | + |
| 61 | + public static Task<int> GetCursorTopAsync(CancellationToken cancellationToken) => |
| 62 | + s_consoleProxy.GetCursorTopAsync(cancellationToken); |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// On Unix platforms this method is sent to PSReadLine as a work around for issues |
| 66 | + /// with the System.Console implementation for that platform. Functionally it is the |
| 67 | + /// same as System.Console.ReadKey, with the exception that it will not lock the |
| 68 | + /// standard input stream. |
| 69 | + /// </summary> |
| 70 | + /// <param name="intercept"> |
| 71 | + /// Determines whether to display the pressed key in the console window. |
| 72 | + /// true to not display the pressed key; otherwise, false. |
| 73 | + /// </param> |
| 74 | + /// <param name="cancellationToken"> |
| 75 | + /// The <see cref="CancellationToken" /> that can be used to cancel the request. |
| 76 | + /// </param> |
| 77 | + /// <returns> |
| 78 | + /// An object that describes the ConsoleKey constant and Unicode character, if any, |
| 79 | + /// that correspond to the pressed console key. The ConsoleKeyInfo object also describes, |
| 80 | + /// in a bitwise combination of ConsoleModifiers values, whether one or more Shift, Alt, |
| 81 | + /// or Ctrl modifier keys was pressed simultaneously with the console key. |
| 82 | + /// </returns> |
| 83 | + internal static ConsoleKeyInfo UnixReadKey(bool intercept, CancellationToken cancellationToken) |
| 84 | + { |
| 85 | + try |
| 86 | + { |
| 87 | + return ((UnixConsoleOperations)s_consoleProxy).ReadKey(intercept, cancellationToken); |
| 88 | + } |
| 89 | + catch (OperationCanceledException) |
| 90 | + { |
| 91 | + return default(ConsoleKeyInfo); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments