Skip to content

Provide better x-plat Console/Terminal features #15711

Closed

Description

@tmat

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.

Related Roslyn issue: dotnet/roslyn#6751

Activity

  1. tmat commented on Nov 14, 2015

    @tmat
    MemberAuthor
  2. changed the title [-]Provide a better x-plat Console/Terminal features[/-] [+]Provide better x-plat Console/Terminal features[/+] on Nov 14, 2015
  3. stephentoub commented on Nov 14, 2015

    @stephentoub
    Member

    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.

    using System;
    using System.Runtime.InteropServices;
    
    class Test
    {
        static void Main()
        {
            while (true) Console.WriteLine(SpecialReadLine());
        }
    
        private static string SpecialReadLine()
        {
            try
            {
                string s = readline(null);
                add_history(s);
                return s;
            }
            catch { return Console.ReadLine(); }
        }
    
        [DllImport("libreadline.so.6")]
        private static extern string readline(string prompt);
    
        [DllImport("libreadline.so.6")]
        private static extern void add_history(string line);
    }

    Assuming we didn't include such functionality in ReadLine, an app can of course do something like what I've done above.

  4. ellismg commented on Nov 16, 2015

    @ellismg
    Contributor

    +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.

  5. pallavit commented on Nov 16, 2015

    @pallavit
    Contributor

    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 :).

  6. mellinoe commented on Nov 16, 2015

    @mellinoe
    Contributor

    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.

  7. stephentoub commented on Nov 17, 2015

    @stephentoub
    Member

    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.

  8. khellang commented on Nov 17, 2015

    @khellang
    Member

    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 👍

  9. mellinoe commented on Nov 17, 2015

    @mellinoe
    Contributor

    In particular, we don't provide any control over setting cursor position, clearing text, etc.

    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.

  10. stephentoub commented on Nov 17, 2015

    @stephentoub
    Member

    we both thought that we need to bring at least SetCursorPosition back

    Yes, I think we should add back all members that we can reasonably implement (one of which is SetCursorPosition).

  11. pallavit commented on Nov 17, 2015

    @pallavit
    Contributor

    In particular, we don't provide any control over setting cursor position, clearing text, etc. that you'd need to do this right.

    +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 :)

  12. cherrydev commented on Nov 19, 2015

    @cherrydev

    @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!

  13. mellinoe commented on Nov 19, 2015

    @mellinoe
    Contributor

    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.

  14. pallavit commented on Nov 19, 2015

    @pallavit
    Contributor

    ConsoleKey.Zoom (What key is that anyways? 😄)

    My understanding is some earlier keyboards used to have ZOOM key similar to Play and Esc keys.

  15. 5 remaining items

  16. ejsmith commented on Jun 6, 2016

    @ejsmith

    Just tried a very simple test on OSX and setting console position and clearing the console doesn't seem to work.

    using System;
    
    namespace ConsoleApplication
    {
        public class Program
        {
            public static void Main(string[] args)
            {
                Console.Clear();
                Console.WriteLine($"{Console.CursorLeft} - {Console.CursorTop}");
    
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("Hello World!");
                Console.SetCursorPosition(0, 0);
                Console.WriteLine("!");
            }
        }
    }
    
  17. stephentoub commented on Jun 6, 2016

    @stephentoub
    Member

    How are you running this? "dotnet run"?

  18. ejsmith commented on Jun 6, 2016

    @ejsmith

    Yes

  19. stephentoub commented on Jun 6, 2016

    @stephentoub
    Member

    And what happens if you instead use "dotnet publish" and then invoke the published app?

  20. ejsmith commented on Jun 6, 2016

    @ejsmith

    Yep, that seems to work.

  21. ejsmith commented on Jun 6, 2016

    @ejsmith

    So I guess there is some sort of bug with the way dotnet run is executing the app, eh?

  22. stephentoub commented on Jun 6, 2016

    @stephentoub
    Member

    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.

  23. brthor commented on Jun 6, 2016

    @brthor
    Contributor

    @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.

  24. niemyjski commented on Jun 7, 2016

    @niemyjski
    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@:~$ 
    
  25. eerhardt commented on Jun 7, 2016

    @eerhardt
    Member

    Unfortunately the change to dotnet run to not use Stdout/Stderr redirection didn't make it into preview1, but it is available in the latest preview2 versions.

  26. niemyjski commented on Jun 7, 2016

    @niemyjski

    Awesome, any idea when that will be available? @dotnetConf???

  27. eerhardt commented on Jun 7, 2016

    @eerhardt
    Member

    Awesome, any idea when that will be available? @dotnetConf???

    preview1 hasn'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 the preview2 version of CLI uses pre-release versions of the .NET Core run time that are post-RC2 (the packages are currently labeled RC3.

    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.

  28. transferred this issue fromdotnet/corefxon Jan 31, 2020
  29. added this to the 1.0.0-rc2 milestone on Jan 31, 2020
  30. ghost locked as resolved and limited conversation to collaborators on Jan 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Consoleenhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions