7

I'm considering writing a console application in C# and I want to incorporate history, completion and command line editing features something like GNU readline (but not necessarily as extensive as that!)

Is there an existing library for .net which provides this type of functionality? I guess one option would be to use interop services to call GNU readline. But is there a native option?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Paul Moore
  • 6,569
  • 6
  • 40
  • 47
  • Have you considered a PowerShell snap-in? That's the most 'native' option in the .NET world I can think of right now. – Dirk Vollmar Jan 07 '10 at 22:32
  • Maybe its just me, but when you start thinking about enhancing the user experience with a UI this way, you really ought to start looking at Windows Forms. – Hans Passant Jan 07 '10 at 22:50
  • Not sure I follow? Are you suggesting I write my app as a powershell snap-in? Won't that just give me PowerShell's standard command line editing (ie, basic Windows console features plus the fancy tab completion)? I'm after more than that (history search, word deletion, things like that). – Paul Moore Jan 07 '10 at 22:52
  • nobugz - A GUI app is fine, but I still want to allow the user command-line style interaction and command line editing. Consider PowerShell vs PowerShell ISE. Personally I hate the ISE command line editing facilities, I prefer the console version but I'd like a little more (which I can't add because the console IO isn't customisable). – Paul Moore Jan 07 '10 at 22:55

4 Answers4

10

You may want to checkout Miguel de Icaza's getline.cs (the link in the blog post is broken, the code can now be found here). Depending on what features of readline you actually need, it might be enough for your purposes.

The nice thing is, that it is all contained in a single (hence getline.cs) file and MIT X11 licensed.

Using it is pretty easy.

If you want to give it try, just download the file and compile it:

C:\> csc.exe /d:DEMO getline.cs 
C:\> getline.exe
shell>

The #ifdef DEMO part also shows the basic REPL:

var le = new LineEditor("whatever");
string s;

while ((s = le.Edit("my prompt> ", "")) != null)
{
    // User input from command line / prompt now in "s".
}
Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
Christian.K
  • 47,778
  • 10
  • 99
  • 143
  • I've tried the getline library from the CSharpRepl project, but has a limited support for the tab-completion. Also, the completion feature itself is quite difficult to implement. Maybe just my limits... – Antonello May 03 '10 at 21:23
  • But the link isn't broken o.o – Hi-Angel Mar 03 '15 at 14:36
2

The only thing I know of is Mono-Readline.

It provides a .NET interface to the GNU Readline library - it's a bit raw though, only version 0.0.1, and I've only ever seen it run on the Mono runtime.

You should be careful with licensing too ... AFAIK anything that links the GNU Readline libraries is required to be released under the GPL.

Duncan Bayne
  • 3,870
  • 4
  • 39
  • 64
2

a project that aims to emulate most of the functions of RedLine is Deveel ReadLine, but I have to say that it isn't maintained. Last time I used it it worked very well on both .NET and Mono, although there's a small bug when canceling a line that was automatically folded.

Antonello
  • 1,326
  • 2
  • 16
  • 23
2

Old question, but still relevant since it appears in Google search results. It makes sense to mention the following:

https://github.com/tonerdo/readline is a good option nowadays. Quoting its README:

ReadLine is a GNU Readline like library built in pure C#. It can serve as a drop in replacement for the inbuilt Console.ReadLine() and brings along with it some of the terminal goodness you get from unix shells, like command history navigation and tab auto completion.

It is cross platform and runs anywhere .NET is supported, targeting netstandard1.3 means that it can be used with .NET Core as well as the full .NET Framework.

License

The project is licensed under the MIT license

Per Lundberg
  • 3,837
  • 1
  • 36
  • 46
  • Thanks a lot! Your answer is even more relevant than the accepted one, IMO. – Evgeniy Jun 23 '23 at 06:50
  • Sure, you're welcome. I ended up using Miguel's library mentioned in https://stackoverflow.com/a/2756850/227779 myself. – Per Lundberg Jun 23 '23 at 18:51
  • Interesting , good to know! It would be great to know why tonerodo readline didn't work well for you? From my shallow understanding, the one you mentioned in your answer looks more advanced. – Evgeniy Jun 24 '23 at 20:40
  • @Evgeniy, I think I got the feeling that it was unmaintained. Miguel's library was also nice in that it was "self-contained"; I could just copy it into my app and use it without external dependencies. More details in this thread in my app's repo: https://github.com/perlang-org/perlang/issues/182#issuecomment-1039498915 – Per Lundberg Jul 04 '23 at 19:00