|
|
|
SearchCategories
Books by the AuthorOther Ruby Projects |
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:
That doesn't look too tough, does it? What's terrific about this solution is that under-the-hood As you can see, I've split this functionality of Trust me, reading individual characters from the keyboard doesn't have to be that tough. You just need the right tool for the job. |
|
|
|
Maybe i did something wrong but the above example didn't work for me. I had to change the line:
to
and also I had to add one more line:
That is maybe beacause of that env didn't worked for me.
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
HighLineinstalled 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: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....
Very nice, works flawlessly on both Linux(ubuntu) and WinXP. Thanks
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.
I've just released HighLine 1.2.3 which should fix the Cygwin issue. Thanks for pointing it out.