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 improperly handled when q.echo = false #236

Closed
aspyct opened this issue Aug 29, 2018 · 6 comments · Fixed by #259
Closed

Ctrl-C improperly handled when q.echo = false #236

aspyct opened this issue Aug 29, 2018 · 6 comments · Fixed by #259

Comments

@aspyct
Copy link

aspyct commented Aug 29, 2018

Hi,

Just noticed something a little odd with the handling of a ctrl-c while in an ask with echo set to false.

#!/usr/bin/env ruby

require 'highline'

cli = HighLine.new
cli.ask("Password?") {|q| q.echo = false}
cli.ask("Anything else?")
$ ./highline_test.rb 
Password?
<hit ctrl-c>
Anything else?
<hit enter>

Two things happened here:

  1. the ctrl-c did not interrupt the script (it does if q.echo is not set to false)
  2. after this, and every time I run a new command (ls, ps etc), an empty line will be printed before the output of the command (see example below)
$ ls

file1
file2

I'm using highline-2.0.0.gem, and zsh in the standard terminal app in OSX.

@abinoam
Copy link
Collaborator

abinoam commented Sep 3, 2018

I'll be having a look at it as soon as possible.
HighLine dates from a time where all the hard work should be done by it.
This is probably an issue related to the fact that HighLine is handling all cases related to special characters like backspace and others.
If you come up with a PR, feel free to open it, I would be thankful!
If not possible, just wait some time and I will surely try to fix it.

Thanks for reporting the issue! 👍

@aspyct
Copy link
Author

aspyct commented Sep 14, 2018

Hi,

No worries, this isn't in any way a blocking/problematic bug, so don't feel pressed to fix this :) I might look into it on the train sometime, but no promises here :)

@Thalagyrt
Copy link

I've just run into this as well - we can work around it for now, and if I have time for a PR I'll raise one, but also subscribing for notifications if you happen to fix it before I do. Cheers!

@abinoam
Copy link
Collaborator

abinoam commented Jan 23, 2019

io/console #getpass handles ctrl-C correctly

@Faheetah
Copy link
Contributor

Just a thought, you could always throw the exit in with the character checks in HighLine#get_line_raw_no_echo_mode, this would "catch" (I say that because trap doesn't seem to catch anything in the method, or anywhere for that matter). This worked for me. I am not sure off hand if \u0003 is going to be cross platform but it seems to work in POSIX environments, tested on macOS:

diff --git a/lib/highline.rb b/lib/highline.rb
index 3e60cd3..e33e13a 100755
--- a/lib/highline.rb
+++ b/lib/highline.rb
@@ -538,6 +538,7 @@ class HighLine
     terminal.raw_no_echo_mode_exec do
       loop do
         character = terminal.get_character
+        exit 130 if character == "\u0003"
         break unless character
         break if ["\n", "\r"].include? character

@abinoam
Copy link
Collaborator

abinoam commented Jan 4, 2023

Thanks for your suggestion @Faheetah
Sorry for the long wait.
I've just opened a PR for it #259

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants