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

Surprising behavior for character = true resp. character = :getc with stty #53

Open
stomar opened this issue Dec 5, 2012 · 10 comments
Open
Labels

Comments

@stomar
Copy link
Contributor

stomar commented Dec 5, 2012

I am trying to get a single character from the user in a "cleaner" way than with SystemExtensions.get_character, which is not well documented and feels rather like a hack to me (see also issue #50).

While playing around with the echo and character settings, I stumbled over two issues(?):

  1. The documentation is not very clear (at least to me) about the difference between character = true and character = :getc. When to choose which? Are they supposed to behave differently or is this only an implementation detail?
  2. I do not understand the behavior of character = true in the example code below (newline and indentation; examples one and three behave as expected).

Output:

Your password?
password: 12345
Answer [ynaq]?
               key: q (character = true)
Answer [ynaq]? key: q (character = :getc)

Code:

require 'rubygems'
require 'highline/import'

answer = ask("Your password? ") do |q|
           q.echo = false
         end
puts "password: #{answer}"

key = ask("Answer [ynaq]? ") do |q|
        q.echo = false
        q.character = true
      end
puts "key: #{key} (character = true)"

key = ask("Answer [ynaq]? ") do |q|
        q.character = :getc
      end
puts "key: #{key} (character = :getc)"
@JEG2
Copy link
Owner

JEG2 commented Dec 5, 2012

Can you share the output of the following?

$ ruby -r highline -ve 'p [HighLine::VERSION, HighLine::SystemExtensions::CHARACTER_MODE]'

@stomar
Copy link
Contributor Author

stomar commented Dec 5, 2012

ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]
["1.6.15", "stty"]

Same behavior with 1.9.3p327, 1.8.7p371 (using rvm) and system ruby (1.9.3p0)

@JEG2
Copy link
Owner

JEG2 commented Dec 5, 2012

Thanks. I'll try to look into this when I get a chance.

@stomar
Copy link
Contributor Author

stomar commented Dec 6, 2012

Small update: I finally got termios installed (I used the wrong gem and gave up too early), and as you probably already know, with termios it works, the only difference between character = true and character = :getc is the newline in the former case, but there is no indentation.

@JEG2
Copy link
Owner

JEG2 commented Dec 6, 2012

Yeah, termios is preferred. Still, stty should work better.

@alexch
Copy link

alexch commented Jan 25, 2013

fyi I was having trouble with gem install termios and needed to do gem install ruby-termios instead... see this SO answer for more details

@carlosefonseca
Copy link

Hi! I'm hitting what I think is the same problem… with character = true, I get indentation on the next puts

Code

require 'highline/import'

ask("question: ") { |q| q.character = true}
puts "text"

Output

❯ ./test.rb
question: y
           text
❯ ruby -r highline -ve 'p [HighLine::VERSION, HighLine::SystemExtensions::CHARACTER_MODE]'
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]
["1.7.2", "stty"]

I tried q.character = :getc but then I needed to add a newline the the next print… is there a way to work around this?

@abinoam
Copy link
Collaborator

abinoam commented Jul 16, 2015

A quick (and dirty) way to work around the issue is to prepend "\r" to the next string.

require 'highline/import'

ask("question: ") { |q| q.character = true }
puts "\rtext"

I'll be looking forward to fix it to the "reasonable expected behaviour".
Thanks for reporting!

@abinoam
Copy link
Collaborator

abinoam commented Jul 16, 2015

@carlosefonseca I've just merged the workaround into 1-7-stable and into master
Please tell me if it worked for you.
When @JEG2 come back from vacation he'll make a gem release of them.
But you are already able to install them manually if you like.
I'll keep this issue open, as I'll review it later.

@carlosefonseca
Copy link

The test above now works with the manual installation of highline! Thanks!

@abinoam abinoam added the bug label May 31, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants