I Just Want One Character!

Posted about 1 year ago in HighLine and Ruby Tutorials.

Every so often a person asks the question on Ruby Talk, "How can I get just one character from the keyboard (without needing the user to hit return)?" Everyone is always quick to post solutions, but sadly there are some issues with almost every one of them.

The general consensus is that this is a tough problem to solve correctly. I say that's the exact reason to let HighLine handle this for you:

#!/usr/bin/env ruby -w

require "highline/system_extensions"
include HighLine::SystemExtensions

print "Enter one character:  "
char = get_character
puts char.chr

That doesn't look too tough, does it?

What's terrific about this solution is that under-the-hood HighLine will check your platform and libraries and then try to use the solution that makes the most sense for your environment. The code is really pretty robust too, because people a lot smarter than me have been sending in patches for over a year, slowly eliminating all of those tricky edge cases.

As you can see, I've split this functionality of HighLine into a separate module so you don't even need to load the full HighLine system. This was done just because this is such a real and common problem. This section of HighLine is one pure Ruby file, so feel free to vendor it if the external dependancy is an issue.

Trust me, reading individual characters from the keyboard doesn't have to be that tough. You just need the right tool for the job.

Peter added 5 days later:

Maybe i did something wrong but the above example didn't work for me. I had to change the line:

#!/usr/bin/env ruby -w

to

#!/usr/bin/ruby -w

and also I had to add one more line:

require 'rubygems'

That is maybe beacause of that env didn't worked for me.

James Edward Gray II added 5 days later:

Yeah, that was careless of me. That shebang line only works on some operating systems. It works on my Mac OS X 10.4 for example, but I believe 10.3 chokes on it. Better to point it your local Ruby install, as Peter did.

If you have HighLine installed as a gem, you will need to make gems available, yes. I generally don't like using an explicit require for that in the code, because it makes rubygems a dependancy. Generally I just run the code with:

ruby -rubygems ...
Gregory added 17 days later:

SystemExtensions Rocks.

I vendored it in Ruport and modified it slightly so that I could play around with getting the terminal geometry.

I think i just added a method or to for convenience, but I should really go ahead and stick them back in HighLine....

Jon Egil added 20 days later:

Very nice, works flawlessly on both Linux(ubuntu) and WinXP. Thanks

gm.vlkv@gmail.com added 20 days later:

HighLine::SystemExtensions.get_character does not work when executed on rxvt terminal under cygwin; "# Unix savvy getc(). (Second choice.)" method must be used in this case.

James Edward Gray II added 23 days later:

I've just released HighLine 1.2.3 which should fix the Cygwin issue. Thanks for pointing it out.

Add Your Thoughts

You can use Markdown in the body of your comment to format text and make links.

Note that I reserve the right to edit any content you post here. I typically exercise this right to fix formatting issues. All posts must be approved so spam will never been seen on these pages.

Author:
URL or Email (optional):
Body: