You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Provide better x-plat Console/Terminal features #15711
The current implementation of System.Console doesn't provide good experience on Linux. For example, arrow keys (left/right for caret navigation, up/down for history) don't work.
It is left to the developer to implement this basic functionality in their app if they want to provide consistent good experience across platforms. CoreFX should provide such implementation either as part of System.Console or as a separate library/nuget. On Linux the implementation can use existing native libraries such as curses or readline.
@tmat, thanks for the issue. As you and I discussed offline, I'm not convinced this is something that should be built into Console.ReadLine(). The functionality you're describing on Windows is provided not by Console but by the cmd terminal, and my expectation is that it would be provided either by the terminal elsewhere or by an app that specifically wants the functionality. This functionality to me seems too specialized to include in a core ReadLine() method from the library, which provides the same basic experience as you get on Linux/OSX from other languages and their core libraries.
That said, I'm happy to hear the opinions of others. I don't want us to build this functionality ourselves, but if a 3rd party library is available, I could see us potentially using it as an optional light-up thing. For example, you can play with libreadline today in C# on coreclr, e.g.
I think that this sort of stuff should be outside the normal Console contract. I could imagine the community producing some sort of wrappers around readline or some other platform specific library for handling input.
I agree with both @stephentoub and @ellismg. This behavior should be outside of Console.ReadLine() and outside of Console class . As stephen mentioned Console class does not provide this behavior on windows but cmd. And so, wrappers on top of ReadLine() seem like the best option to me too :).
I'm having a bit trouble following the discussion here, I think maybe there's some context missing. The original post talks about using arrow keys for caret navigation and history completion, but most everyone else is talking about ReadLine(). I haven't really thought about this much, but I would think you would implement something like this by looping over ReadKey(true), right? Maybe there was some offline discussion that isn't captured here.
I'm having a bit trouble following the discussion here
@tmat is asking that Console.ReadLine() allow you to use the left/right arrow keys to move around in the line of text that you're writing, and that the up/down array keys allow you to go back through the history of lines that you've typed. This is the experience for most folks on Windows because cmd intercepts those keys and layers that functionality on top of any console app.
I would think you would implement something like this by looping over ReadKey(true), right?
Yes, however I don't think in CoreCLR that we actually expose enough on Console to make this work (we should fix that). In particular, we don't provide any control over setting cursor position, clearing text, etc. that you'd need to do this right.
You can of course P/Invoke to a library that already implements this functionality, as I did in my example.
FYI, Mono has a LineEditor class that does exactly this. @migueldeicaza recently PR'd it to scriptcs.
It supports editing, history, reverse-history search, pluggable tab-completion and keyboard shortcuts. It probably uses some of the APIs that @stephentoub mentions, so it would be nice to get those exposed 👍
@khellang I was able to get a basic working command line prompt that was integrated with my (existing) CommandLineApplication program in about 30 minutes using LineEditor. There's an example of its usage here. The biggest pain was actually parsing (including quotations) the command string into arguments to pass to CommandLineApplication. Thanks for the fantastic tip!
For anyone's curiosity, the Console methods that getline.cs linked above uses that we don't support in .NET Core right now are:
Console.Clear() Probably just a generally useful thing to support Console.SetCursorPosition(int, int) Console.CursorTop (CursorLeft not used) Console.BufferHeight (BufferWidth not used) ConsoleKey.Zoom (What key is that anyways? 😄)
There's also some other stuff that would have to be changed, like it's use of ThreadAbortException and Thread.ResetAbort, and things like Environment.SpecialFolder.
IIRC, "dotnet run" launches the app as a separate process and redirects input/output, which would cause these to become nops when used with "dotnet run". @eerhardt or @brthor can probably speak best to what the current state of "dotnet run" here is, whether this behavior has been changed, and any plans around it for the future.
@eerhardt Recently changed dotnet-run to no longer use explicit Stdout/Stderr redirection and we rely on the default behavior of the process api to forward it appropriately.
var psi = new ProcessStartInfo
{
FileName = commandSpec.Path,
Arguments = commandSpec.Args,
UseShellExecute = false
};
This is the ProcessStartInfo which is used to execute the command, RedirectStandardOutput and RedirectStandardError are no longer used.
@ejsmith Can you show the output of dotnet --info ? I'm wondering if you have an older version.
Last login: Tue Jun 7 07:25:28 on console
doblake@:~$ dotnet --info
.NET Command Line Tools (1.0.0-preview1-002702)
Product Information:
Version: 1.0.0-preview1-002702
Commit Sha: 6cde21225e
Runtime Environment:
OS Name: Mac OS X
OS Version: 10.11
OS Platform: Darwin
RID: osx.10.11-x64
blake@:~$
Activity
tmat commentedon Nov 14, 2015
FYI @stephentoub @agocke @genlu @amcasey @weshaggard
[-]Provide a better x-plat Console/Terminal features[/-][+]Provide better x-plat Console/Terminal features[/+]stephentoub commentedon Nov 14, 2015
cc: @pallavit, @ellismg
@tmat, thanks for the issue. As you and I discussed offline, I'm not convinced this is something that should be built into Console.ReadLine(). The functionality you're describing on Windows is provided not by Console but by the cmd terminal, and my expectation is that it would be provided either by the terminal elsewhere or by an app that specifically wants the functionality. This functionality to me seems too specialized to include in a core ReadLine() method from the library, which provides the same basic experience as you get on Linux/OSX from other languages and their core libraries.
That said, I'm happy to hear the opinions of others. I don't want us to build this functionality ourselves, but if a 3rd party library is available, I could see us potentially using it as an optional light-up thing. For example, you can play with libreadline today in C# on coreclr, e.g.
Assuming we didn't include such functionality in ReadLine, an app can of course do something like what I've done above.
ellismg commentedon Nov 16, 2015
+1
I think that this sort of stuff should be outside the normal Console contract. I could imagine the community producing some sort of wrappers around readline or some other platform specific library for handling input.
pallavit commentedon Nov 16, 2015
I agree with both @stephentoub and @ellismg. This behavior should be outside of Console.ReadLine() and outside of Console class . As stephen mentioned Console class does not provide this behavior on windows but cmd. And so, wrappers on top of ReadLine() seem like the best option to me too :).
mellinoe commentedon Nov 16, 2015
I'm having a bit trouble following the discussion here, I think maybe there's some context missing. The original post talks about using arrow keys for caret navigation and history completion, but most everyone else is talking about ReadLine(). I haven't really thought about this much, but I would think you would implement something like this by looping over ReadKey(true), right? Maybe there was some offline discussion that isn't captured here.
stephentoub commentedon Nov 17, 2015
@tmat is asking that Console.ReadLine() allow you to use the left/right arrow keys to move around in the line of text that you're writing, and that the up/down array keys allow you to go back through the history of lines that you've typed. This is the experience for most folks on Windows because cmd intercepts those keys and layers that functionality on top of any console app.
Yes, however I don't think in CoreCLR that we actually expose enough on Console to make this work (we should fix that). In particular, we don't provide any control over setting cursor position, clearing text, etc. that you'd need to do this right.
You can of course P/Invoke to a library that already implements this functionality, as I did in my example.
khellang commentedon Nov 17, 2015
FYI, Mono has a
LineEditorclass that does exactly this. @migueldeicaza recently PR'd it to scriptcs.It supports editing, history, reverse-history search, pluggable tab-completion and keyboard shortcuts. It probably uses some of the APIs that @stephentoub mentions, so it would be nice to get those exposed 👍
mellinoe commentedon Nov 17, 2015
Yeah, agreed. I had brought that up with @pallavit a while back and I think we both thought that we need to bring at least SetCursorPosition back.
stephentoub commentedon Nov 17, 2015
Yes, I think we should add back all members that we can reasonably implement (one of which is SetCursorPosition).
pallavit commentedon Nov 17, 2015
+1. I agree, we need to expose the full extend of Console APIs and I am hoping to see it happening in the near future :)
cherrydev commentedon Nov 19, 2015
@khellang I was able to get a basic working command line prompt that was integrated with my (existing) CommandLineApplication program in about 30 minutes using LineEditor. There's an example of its usage here. The biggest pain was actually parsing (including quotations) the command string into arguments to pass to CommandLineApplication. Thanks for the fantastic tip!
mellinoe commentedon Nov 19, 2015
For anyone's curiosity, the Console methods that getline.cs linked above uses that we don't support in .NET Core right now are:
Console.Clear()Probably just a generally useful thing to supportConsole.SetCursorPosition(int, int)Console.CursorTop(CursorLeftnot used)Console.BufferHeight(BufferWidthnot used)ConsoleKey.Zoom(What key is that anyways? 😄)There's also some other stuff that would have to be changed, like it's use of ThreadAbortException and Thread.ResetAbort, and things like Environment.SpecialFolder.
pallavit commentedon Nov 19, 2015
My understanding is some earlier keyboards used to have ZOOM key similar to Play and Esc keys.
5 remaining items
ejsmith commentedon Jun 6, 2016
Just tried a very simple test on OSX and setting console position and clearing the console doesn't seem to work.
stephentoub commentedon Jun 6, 2016
How are you running this? "dotnet run"?
ejsmith commentedon Jun 6, 2016
Yes
stephentoub commentedon Jun 6, 2016
And what happens if you instead use "dotnet publish" and then invoke the published app?
ejsmith commentedon Jun 6, 2016
Yep, that seems to work.
ejsmith commentedon Jun 6, 2016
So I guess there is some sort of bug with the way dotnet run is executing the app, eh?
stephentoub commentedon Jun 6, 2016
Console.Clear(), Console.SetCursorPosition(), etc. on unix work by writing ANSI escape codes to stdout. If stdout has been redirected, these become nops, e.g.
https://github.com/dotnet/corefx/blob/master/src/System.Console/src/System/ConsolePal.Unix.cs#L217
https://github.com/dotnet/corefx/blob/master/src/System.Console/src/System/ConsolePal.Unix.cs#L204
IIRC, "dotnet run" launches the app as a separate process and redirects input/output, which would cause these to become nops when used with "dotnet run". @eerhardt or @brthor can probably speak best to what the current state of "dotnet run" here is, whether this behavior has been changed, and any plans around it for the future.
brthor commentedon Jun 6, 2016
@eerhardt Recently changed
dotnet-runto no longer use explicit Stdout/Stderr redirection and we rely on the default behavior of the process api to forward it appropriately.This is the ProcessStartInfo which is used to execute the command,
RedirectStandardOutputandRedirectStandardErrorare no longer used.@ejsmith Can you show the output of
dotnet --info? I'm wondering if you have an older version.niemyjski commentedon Jun 7, 2016
eerhardt commentedon Jun 7, 2016
Unfortunately the change to
dotnet runto not use Stdout/Stderr redirection didn't make it intopreview1, but it is available in the latestpreview2versions.niemyjski commentedon Jun 7, 2016
Awesome, any idea when that will be available? @dotnetConf???
eerhardt commentedon Jun 7, 2016
preview1hasn't officially released yet. If you are interested in taking pre-release versions, you can get the latest here: https://github.com/dotnet/cli#installers-and-binaries. Note that thepreview2version of CLI uses pre-release versions of the .NET Core run time that are post-RC2(the packages are currently labeledRC3.If you'd like to wait until it is officially released, see https://blogs.msdn.microsoft.com/dotnet/2016/05/06/net-core-rc2-improvements-schedule-and-roadmap/ for the schedule and roadmap.