-
Notifications
You must be signed in to change notification settings - Fork 236
PSReadLine integration #672
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
Changes from all commits
5a6eba6
1930afe
7e26e4e
ac44055
d2e1ceb
a507705
a870ee2
190cc0c
49db2ba
379eee4
e16c823
cc62dab
7f2b5b8
e19afe6
afdfb43
3575c79
6a3f7c9
cc10b91
86ab115
b51cc75
1682410
d68fb70
2968d1f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Console | ||
{ | ||
/// <summary> | ||
/// Provides asynchronous implementations of the <see cref="Console" /> API's as well as | ||
/// synchronous implementations that work around platform specific issues. | ||
/// </summary> | ||
internal static class ConsoleProxy | ||
{ | ||
private static IConsoleOperations s_consoleProxy; | ||
|
||
static ConsoleProxy() | ||
{ | ||
// Maybe we should just include the RuntimeInformation package for FullCLR? | ||
#if CoreCLR | ||
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) | ||
{ | ||
s_consoleProxy = new WindowsConsoleOperations(); | ||
return; | ||
} | ||
|
||
s_consoleProxy = new UnixConsoleOperations(); | ||
#else | ||
s_consoleProxy = new WindowsConsoleOperations(); | ||
#endif | ||
} | ||
|
||
public static Task<ConsoleKeyInfo> ReadKeyAsync(CancellationToken cancellationToken) => | ||
s_consoleProxy.ReadKeyAsync(cancellationToken); | ||
|
||
public static int GetCursorLeft() => | ||
s_consoleProxy.GetCursorLeft(); | ||
|
||
public static int GetCursorLeft(CancellationToken cancellationToken) => | ||
s_consoleProxy.GetCursorLeft(cancellationToken); | ||
|
||
public static Task<int> GetCursorLeftAsync() => | ||
s_consoleProxy.GetCursorLeftAsync(); | ||
|
||
public static Task<int> GetCursorLeftAsync(CancellationToken cancellationToken) => | ||
s_consoleProxy.GetCursorLeftAsync(cancellationToken); | ||
|
||
public static int GetCursorTop() => | ||
s_consoleProxy.GetCursorTop(); | ||
|
||
public static int GetCursorTop(CancellationToken cancellationToken) => | ||
s_consoleProxy.GetCursorTop(cancellationToken); | ||
|
||
public static Task<int> GetCursorTopAsync() => | ||
s_consoleProxy.GetCursorTopAsync(); | ||
|
||
public static Task<int> GetCursorTopAsync(CancellationToken cancellationToken) => | ||
s_consoleProxy.GetCursorTopAsync(cancellationToken); | ||
|
||
/// <summary> | ||
/// On Unix platforms this method is sent to PSReadLine as a work around for issues | ||
/// with the System.Console implementation for that platform. Functionally it is the | ||
/// same as System.Console.ReadKey, with the exception that it will not lock the | ||
/// standard input stream. | ||
/// </summary> | ||
/// <param name="intercept"> | ||
/// Determines whether to display the pressed key in the console window. | ||
/// true to not display the pressed key; otherwise, false. | ||
/// </param> | ||
/// <param name="cancellationToken"> | ||
/// The <see cref="CancellationToken" /> that can be used to cancel the request. | ||
/// </param> | ||
/// <returns> | ||
/// An object that describes the ConsoleKey constant and Unicode character, if any, | ||
/// that correspond to the pressed console key. The ConsoleKeyInfo object also describes, | ||
/// in a bitwise combination of ConsoleModifiers values, whether one or more Shift, Alt, | ||
/// or Ctrl modifier keys was pressed simultaneously with the console key. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: keys were pressed There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can change it, but it's from the docs for Console.ReadKey. |
||
/// </returns> | ||
internal static ConsoleKeyInfo UnixReadKey(bool intercept, CancellationToken cancellationToken) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is really nice! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Admitably most of that comes from System.Console.ReadKey, so I can't take much credit there 😉 |
||
{ | ||
try | ||
{ | ||
return ((UnixConsoleOperations)s_consoleProxy).ReadKey(intercept, cancellationToken); | ||
} | ||
catch (OperationCanceledException) | ||
{ | ||
return default(ConsoleKeyInfo); | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest using string interpolation and use platform independent newlines:
$"Exception occurred while awaiting debug launch task.{Environment.Newline}{Environment.Newline}{e.ToString()}"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency I'd like to keep this the same for now. Ultimately we shouldn't be making our own strings while logging at all, that would better be handled by Serilog.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's fine, I just wanted to bring it up as some people are not aware of some of the new C# features
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should be able to use
Logger.WriteException
here and it will handle the newlines/indentation.