icr icon indicating copy to clipboard operation
icr copied to clipboard

Feature: Using ctrl-C

Open jwoertink opened this issue 7 years ago • 5 comments

When using IRB, if you're writing out multi-line code, and you make a typo, you can use ctrl-C to sort of reset you back to normal. Take this for example:

[14:07PM] crystal-icr (bugs/alias)$ irb
2.3.1 :001 > class Dog
2.3.1 :002?>   def bark
2.3.1 :003?>     ebd
2.3.1 :004?>     ^C
2.3.1 :004 > 1 + 1
 => 2 
2.3.1 :005 > puts "back to normal"
back to normal
 => nil 

With ICR currently, if you hit ctrl-C, then it will completely exits the session back to your main prompt. If you've been spending time building out an example, then you will have lost setting up variables and such.

Since this needs to work a bit different than how IRB works, my plan would be to create some sort of status that knows if the current state is ok or not. Then catching Signal::INT would basically clear everything back to the last good state.

If you like the idea, then I'll work on sending a PR.

jwoertink avatar Nov 15 '16 22:11 jwoertink

The idea sounds awesome! Is your proposal to work on that still relevant, if so, I (and I believe community) would really appreciate that!

greyblake avatar Jan 31 '17 11:01 greyblake

Yup! I just didn't want to put in a bunch of effort and then get it rejected (has happened before to me) 😅

I'll start working on it and then submit a WIP PR so you can follow along and maybe help with some suggestions along the way. Thanks!

~Jeremy

On Jan 31, 2017, at 3:46 AM, Sergey Potapov [email protected] wrote:

The idea sounds awesome! Is your proposal to work on that still relevant, if so, I (and I believe community) would really appreciate that!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

jwoertink avatar Jan 31 '17 14:01 jwoertink

Sounds good! Thanks again for your support and initiative!

greyblake avatar Jan 31 '17 15:01 greyblake

Maybe something like this would work? https://stackoverflow.com/a/41917863/5655255 (warning C code)

void int_handler(int status)
{
  printf("\n"); // Move to a new line
  rl_on_new_line(); // Regenerate the prompt on a newline
  rl_replace_line("", 0); // Clear the previous text
  rl_redisplay();
}

We'd just need to bind rl_on_new_line, rl_replace_line and rl_redisplay

bew avatar Mar 17 '18 05:03 bew

Possibly. I will need to look back in to it. I had been waiting for this thing to get fixed before I attempted this again, but maybe it's fixed now? Thanks for the suggestion @bew 👍

jwoertink avatar Mar 17 '18 17:03 jwoertink