-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Console.Unix: for echoing, write back to the terminal instead of writing to Console.Out. #94414
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
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ff2f2c2
Console.Unix: for echoing, write back to the terminal instead of writ…
tmds 6a66e2d
Use console encoding for echoing characters.
tmds 822df76
Fix compilation issue.
tmds 624f51d
StdInReader is used only when stdin is a terminal.
tmds fc5fa66
Prefer stdout as terminal handle and enable cursor position caching f…
tmds ad727c4
PR feedback.
tmds 271a37f
Clean up.
tmds 71789a7
Fix C errors for unused vars.
tmds dc2adb0
Fix ConsolePal.Wasi.
tmds 30400c4
Clean up.
tmds a76ff28
Don't call TryGetCursorPosition when stdin isn't a terminal.
tmds File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,24 +18,20 @@ namespace System | |
| // to also change the test class. | ||
| internal static partial class ConsolePal | ||
| { | ||
| // StdInReader is only used when input isn't redirected and we're working | ||
| // with an interactive terminal. In that case, performance isn't critical | ||
| // and we can use a smaller buffer to minimize working set. | ||
| // there is no dup on WASI | ||
| public static Stream OpenStandardInput() | ||
| { | ||
| return new UnixConsoleStream(Interop.CheckIo(Interop.Sys.FileDescriptors.STDIN_FILENO), FileAccess.Read, | ||
| return new UnixConsoleStream(Interop.Sys.FileDescriptors.STDIN_FILENO, FileAccess.Read, | ||
| useReadLine: !Console.IsInputRedirected); | ||
| } | ||
|
|
||
| public static Stream OpenStandardOutput() | ||
| { | ||
| return new UnixConsoleStream(Interop.CheckIo(Interop.Sys.FileDescriptors.STDOUT_FILENO), FileAccess.Write); | ||
| return new UnixConsoleStream(Interop.Sys.FileDescriptors.STDOUT_FILENO, FileAccess.Write); | ||
| } | ||
|
|
||
| public static Stream OpenStandardError() | ||
| { | ||
| return new UnixConsoleStream(Interop.CheckIo(Interop.Sys.FileDescriptors.STDERR_FILENO), FileAccess.Write); | ||
| return new UnixConsoleStream(Interop.Sys.FileDescriptors.STDERR_FILENO, FileAccess.Write); | ||
| } | ||
|
|
||
| public static Encoding InputEncoding | ||
|
|
@@ -254,17 +250,21 @@ private static unsafe int Read(SafeFileHandle fd, Span<byte> buffer) | |
| } | ||
| } | ||
|
|
||
| internal static unsafe void WriteFromConsoleStream(SafeFileHandle fd, ReadOnlySpan<byte> buffer) | ||
|
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. These other changes sync with the |
||
| { | ||
| EnsureConsoleInitialized(); | ||
|
|
||
| lock (Console.Out) // synchronize with other writers | ||
| { | ||
| Write(fd, buffer); | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Writes data from the buffer into the file descriptor.</summary> | ||
| /// <param name="fd">The file descriptor.</param> | ||
| /// <param name="buffer">The buffer from which to write data.</param> | ||
| /// <param name="mayChangeCursorPosition">Writing this buffer may change the cursor position.</param> | ||
| internal static unsafe void Write(SafeFileHandle fd, ReadOnlySpan<byte> buffer, bool mayChangeCursorPosition = true) | ||
| private static unsafe void Write(SafeFileHandle fd, ReadOnlySpan<byte> buffer) | ||
| { | ||
| // Console initialization might emit data to stdout. | ||
| // In order to avoid splitting user data we need to | ||
| // complete it before any writes are performed. | ||
| EnsureConsoleInitialized(); | ||
|
|
||
| fixed (byte* p = buffer) | ||
| { | ||
| byte* bufPtr = p; | ||
|
|
@@ -298,36 +298,11 @@ internal static unsafe void Write(SafeFileHandle fd, ReadOnlySpan<byte> buffer, | |
| throw Interop.GetExceptionForIoErrno(errorInfo); | ||
| } | ||
| } | ||
|
|
||
| count -= bytesWritten; | ||
| bufPtr += bytesWritten; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// <summary>Writes a terminfo-based ANSI escape string to stdout.</summary> | ||
| /// <param name="value">The string to write.</param> | ||
| /// <param name="mayChangeCursorPosition">Writing this value may change the cursor position.</param> | ||
| internal static void WriteStdoutAnsiString(string? value, bool mayChangeCursorPosition = true) | ||
| { | ||
| if (string.IsNullOrEmpty(value)) | ||
| return; | ||
|
|
||
| scoped Span<byte> data; | ||
| if (value.Length <= 256) // except for extremely rare cases, ANSI escape strings are very short | ||
| { | ||
| data = stackalloc byte[Encoding.UTF8.GetMaxByteCount(value.Length)]; | ||
| int bytesToWrite = Encoding.UTF8.GetBytes(value, data); | ||
| data = data.Slice(0, bytesToWrite); | ||
| } | ||
| else | ||
| { | ||
| data = Encoding.UTF8.GetBytes(value); | ||
| } | ||
|
|
||
| lock (Console.Out) // synchronize with other writers | ||
| { | ||
| Write(Interop.Sys.FileDescriptors.STDOUT_FILENO, data, mayChangeCursorPosition); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'm not sure what the
Interop.CheckIois about, so I removed it.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 am not familiar with this particular overload, let me share it here think loud about that:
runtime/src/libraries/Common/src/Interop/Unix/Interop.IOErrors.cs
Lines 86 to 105 in 487d7f0
I don't think that we need such check here, as we have not performed any IO yet (the handle is created by providing a constant number: 0, 1 or 2, it's not a result of a sys-call), so there is no last error that makes sense. It can become invalid once we try to use it in a sys-call, but other layers are ready for that.
So I am fine with removing these checks (and the method itself if it's not used elsewhere) 👍