Skip to content
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

Ctrl-C should not interrupt the REPL #6870

Open
ageron opened this issue Jul 5, 2024 · 5 comments
Open

Ctrl-C should not interrupt the REPL #6870

ageron opened this issue Jul 5, 2024 · 5 comments
Labels
enhancement New feature or request repl Relates to `roc repl`

Comments

@ageron
Copy link
Contributor

ageron commented Jul 5, 2024

I'm new to ROC, really enjoying it so far. 👍

However, in ROC's REPL, pressing Ctrl-C shuts down the REPL immediately (at least on MacOS). I lose whatever I was doing.

In pretty much every other REPL I've ever used, Ctrl-C just cancels the command I'm typing (or only interrupts the currently running evaluation). I've tested the following REPLs:

  • Node: standard (meaning Ctrl-C just interrupts the current command and goes to the next line)
  • Julia: standard
  • Haskell: standard
  • Java: standard
  • Bash, Zsh, sh: standard
  • Ruby: standard (but adds ^C at the end of the command)
  • Swift: standard (but also adds ^C)
  • Python: standard (but adds a line with KeyboardInterrupt)
  • iPython: clears the current command without going to the next line. There's less visual clutter, but you lose whatever you were typing. I'm not a fan. Still better than shutting down.

ROC's REPL should not shutdown when Ctrl-C is pressed. It should have the "standard" behavior instead, which prevents losing a session by mistake, and makes it more convenient to cancel the current command (in ROC's REPL, you can type Ctrl-A followed by Ctrl-K, but it's neither obvious nor convenient).

If you're okay with that, I'm happy to contribute a fix (but it might take me a while, I just started learning ROC).

Edit: actually, it might be a really simple fix: removing line 96 (return 1) in crates/repl_cli/src/lib.rs

@rtfeldman
Copy link
Contributor

Thanks for the thorough investigation of what other repls do here!

So it's actually bothered me that in other repls Ctrl-C doesn't exit, especially because if I end up in the repl by mistake, Ctrl-C is my go-to "I don't know how to exit this program but this is the key combination that exits programs" button. It's both standard and frustrating to me, and the standardization of that frustration makes it even worse. 😆

What if we made Ctrl-C cancel the current command if you have something entered, but if you're at a blank prompt (e.g. you press Ctrl-C twice in a row), then it exits?

I've definitely used other programs where pressing Ctrl-C twice is necessary to exit the program, which feels both okay here and also something I would naturally try if I were trying to exit the program (namely, pressing Ctrl-C more than once).

@ageron
Copy link
Contributor Author

ageron commented Jul 5, 2024

Hi Richard, thanks for your reply.
Your suggestion would definitely be a great improvement over the current situation. 👍
And I like the spirit of questioning the status quo when it's a bad status quo.
But in this case I think it's a good status quo. There are good reasons why it has become the de facto standard:

  • A shell (and Language REPLs are no exception) is not a regular program, since it can launch any other program. If you start a program within a shell, and you press Ctrl-C, you expect the program to stop (unless it traps the interrupt signal), but you really don't expect to be kicked out of the shell.
  • Suppose you've started a program, and it looks like it's not responding. You press Ctrl-C, but your system is a bit too busy so the program doesn't stop right away, so you press Ctrl-C again (this is quite common), and unfortunately you get kicked out of the shell.
  • Or suppose you want to interrupt a program and you press Ctrl-C, but the program finishes just before you press Ctrl-C, so you get kicked out of the shell.

I believe this is why shells all adopted a different keystroke to exit (Ctrl-D = EOF).
I sometimes have valuable data in a shell, and exiting by mistake is a really painful experience.

That said, I remember being frustrated the first few times I used shells (a few decades ago), because I didn't know how to exit (not the same level of frustration as trying to exit vi, but still not negligible). A simple solution would be to print "Please press Ctrl-D to exit the REPL" if you press Ctrl-C on an empty command.

Also note that bash (and other shells) let you change the default behavior if you want: you can tell it to exit on Ctrl-C if you really want to (trap "exit" INT). We could imagine offering the same option in ROC's REPL (but you might be one of the only users of that option, haha 😜).

@ageron
Copy link
Contributor Author

ageron commented Jul 6, 2024

Mmh, it looks like rustyline just returns an Err(ReadlineError::Interrupted) when you press Ctrl-C, there's no way to know whether or not the line was empty or not. So if we display a message, it would appear every time, whether the command is empty or not.
I for one vote for no message at all. As I mentioned earlier, the REPL starts with a message that explains how to exit, so no need to repeat it, IMHO.

@lishaduck
Copy link

lishaduck commented Jul 8, 2024

but you might be one of the only users of that option, haha

I'd use it :)
I'm just like @rtfeldman, I always get annoyed at REPLs that don't ⌃C. I guess the logic for ⌃D makes sense, but I'm still with @rtfeldman. I guess the message at the top is fine, as I usually want to exit when I accidentally enter. (i.e., if the plain roc command entered a repl instead of help, which is honestly what I expect from a language given all of the above REPLs' behavior, though I prefer a help message).
Anyway, I'd go with "fine, change it." You're right, but I'm still saddened to lose ⌃C.
Have a nice day.

@lukewilliamboswell
Copy link
Collaborator

I also like the idea proposed by Richard and think we should aim for that behaviour if possible.

I don't have a lot of experience with other REPLs, so I like that it is easy to exit the program using ^C, but I guess I didn't know about ^D.

@skyqrose skyqrose added enhancement New feature or request repl Relates to `roc repl` labels Nov 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request repl Relates to `roc repl`
Projects
None yet
Development

No branches or pull requests

5 participants