-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Add readline for the compiler #11340
Comments
Just as an idea maybe: https://github.com/Papierkorb/fancyline |
Nice! Running
|
@docelic Fancyline could indeed be a feasible option. Thanks for brining it up. It is obviously not as mature and battle-tested as other implementations. This kind of software is really hard because it needs to adapt to many quirks of terminal emulators and that's hard to formalize and test. |
Could E.g. this line
Does it mean making the whole proprietary source available or just the MPL part? |
I think this library should be internal to the compiler and only have the features needed for the interpreter. Then it doesn't matter if it's battle tested for different scenarios. We only need it for the interpreter. The Windows version could ship without readline. I added readline very late to the interpreter, so it's absolutely not needed for it. |
With that in mind we could use libreadline or libedit for Unix, and nothing or something else for Windows. |
The magnitude of scenarios doesn't necessarily come from using specific (enhanced) features. Of course, reducing that is a factor which can keep things simple. But the most tricky thing is ensured compatibility across all weird kinds of terminal emulators and their quirks. The problem is that they show a lot of different behaviour and a readline-like library needs to work with all the flavours in order to provide a single, portable API. That's where it matters to be battle tested. |
If we can treat readline functionality as optional, it's probably fine to ignore Windows support for now. What exactly is it used for in the interpreter? |
Right now it's just for moving the cursor. I think also history. The Idris repl doesn't have readline. There's a program that will wrap any other program with readline, I can't remember the name. Readline is totally optional and I would suggest spending zero time in this if we want to move forward with the interpreter. Removing readline for now and adding it much later could be a good step forward. |
rlwrap is a program that wraps any other program with readline: https://github.com/hanslub42/rlwrap |
That's the one! |
+1 to removing |
To be clear: I don't think
|
https://github.com/ruby/reline full ruby |
@oprypin what's wrong with reline? |
@n-rodriguez It's written in a programming language that's not viable to integrate into Crystal distribution. So I'm not sure why you mention it. |
|
Crystal could ship its own implementation based on reline (just an idea). |
https://stackoverflow.com/questions/17982633/lightweight-gnu-readline-alternative may be useful (basically: libedit, replxx). Or maybe you could put a fancy UI on it like crystal play? :) Not sure what that would look like... :) |
Probably not good 😛 , better keep with the terminal :-) |
I assume this would be the fix for what currently happens in Windows Terminal and some other terminals (like Hyper)? Currently backspace doesn't work (the characters being deleted are printed instead in reverse) and I'm sure there are other issues too. This is also a problem with ICR though. |
@watzon That sounds more like an issue with the terminal emulator than what's received by the acting program. I presume basic editing capabilities such as writing forward and deleting backward should be supported by any terminal. |
Apparently it was an stty issue. Running the command |
While I recently realized that the compiler accidentally links That is, on my system |
on ArchLinux
|
I don't know if we can learn something from the Interactive Crystal Project from I3oris, on github.com/I3oris/ic, which seems to have already resolved these issues (?) |
Just for confirm, @straight-shoota , if this issue can solve following issue? When start
Instead should moving cursor, it print a controller char. |
@paulocoghi, so sorry i never see your messages on this thread (I discovered some month ago all my notifications was disabled!) Work I've done for IC could be suitable for the compiler, or not..
I would be very glad to work one these points and integrate that to @zw963, yes replacing |
@I3oris does your shard implement readline functionality and auticompletion without relying on a C library? If so, to me that sounds more desire able that bonding to a C library (in my opinion) |
Yes it does! |
To be more precise, it relies only on ANSI escape (https://github.com/I3oris/ic/blob/master/src/term_cursor.cr) and a ioctl call |
Sorry, wanted to talk about https://github.com/naqvis/uni_char_width not its dependency It gives the size displayed on screen rather than char size or bytesize e.g. This is required because the cursor you see should correspond to cursor on internal string. |
When starting use debugger in Crystal, really hope we can support at history feature(C-p, C-n), typing same expression again and again, quite boring. |
Using a native-Crystal implementation similar to libreadline seems to be a good path forward. https://github.com/Papierkorb/fancyline has already been mentioned. I haven't looked closely at it and do not know if it is already sufficient for the REPL use case or might use some enhancement. But it might be a good base to avoid starting from scratch. @I3oris I don't think there's much value in calculating the width of unicode characters. It depends on Unicode version and font support in the terminal app, which is impossible to take into account. |
I tested fancyline but is doesn't seam to support unicode either |
Like all things, I would do this by steps. First I wouldn't worry about unicode. Unicode is real, but in my debugging sessions I rarely need to deal with unicode. Like, I won't be typing unicode chars. It's usually English because the programming language uses English. Once we got a pure Crystal readline working, we can start improving things like adding good unicode support. |
You're right, let handle unicode later. I'm working of extracting work from my repo to create a shard similar to fancyline. |
Then it's here! see https://forum.crystal-lang.org/t/new-shard-reply-a-reader-for-your-repl/5005. |
Thank you, @I3oris ! |
Resolved by #12738 |
In interpreted mode (#11159), the compiler includes a REPL interface. For this we need a readline-like interface to provide support for typical console interaction such as cursor control.
There had previously been a
Readline
module in stdlib but it was removed in #8364 in order to keep the standard library slim (#5215). It has subsequently been moved to a shard at https://github.com/crystal-lang/crystal-readline.We could consider reintroducing the module to stdlib. But since it's only used for the compiler, it would be a good candidate for pulling in the shard as a lib dependency. We already do this with
markd
for Markdown rendering (#11040) and I think it should work great withreadline
as well.The shard could definitely use some polishing, but in the long run I wouldn't expect frequent changes to the code base.
There is however a problem with portability. As far as I am aware,
libreadline
is only compatible with POSIX platforms and it doesn't work on native Windows. There is a port of the GnuWin32 project, but it doesn't appear to be actively maintained (http://gnuwin32.sourceforge.net/packages/readline.htm last release is 5.0 from 2008).Even though we don't have full Windows support yet, we have to keep compatibility in mind.
Of course, we could just consider using a different library on windows. But it would certainly be helpful if we could use the same interface on any platform. And this is not just for the compiler, but any Crystal project using readline-like functionality would benefit from portability.
A popular alternative to
libreadline
islibedit
which provides a very similar interface. On MacOS,libreadline
is actually just a wrapper aroundlibedit
(thus there can be slightly different behaviour on MacOS and Linux). A Windows port is available at http://mingweditline.sourceforge.net/Alternatively, we could consider running our own implementation. This would eliminate all portability and dependency issues. Some programming languages such as Python and Java (those I'm aware of) ship their own implementations.
A Ruby implementation is available at https://github.com/ConnorAtherton/rb-readline. It seems to be used as readline implementation by Rubinius. It could serve as a basis for a native Crystal implementation.
By a quick count, rb-readline has about 9K LOC, libedit 20K LOC, libreadline 30K LOC.
The text was updated successfully, but these errors were encountered: