Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows 'choice' command messing up Ruby 'gets' method

Open up irb and

  1. type gets. It should work fine.
  2. Then try system("choice /c YN") It should work as expected.
  3. Now try gets again, it behaves oddly.

Can someone tell me why this is?

EDIT: For some clarification on the "odd" behavior, it allows me to type for gets, but doesn't show me the characters and I have to press the enter key twice.

like image 638
itdoesntwork Avatar asked Jun 11 '12 10:06

itdoesntwork


1 Answers

Terminal input-output handling is dark and mysterious art. Anyone trying to make colorized output of bash work in windows PowerShell via ssh knows that. (And various shortcutting habits like Ctrl+Backspace only make things worse.)

One of the possible reasons for your problem is special characters handling. Every terminal out there can type characters in number of different modes, and it parses its own output in search for certain character sequences in order to toggle states.

F.e. here one can find ANSI escape code sequences, one of possible supported standards among different kind of terminals.

See there Esc[5;45m? That will make all the following output to blink on magenta background. And there is significantly more stuff like that out there.

So, the answer to your question taken literally is — your choice command messes something with output modes using special escape sequences, and ruby's gets breaks in that quirk special mode of terminal operation.

But more useful will be the link to HighLine gem documentation. Why one might want to implement platform-specific and obtrusive behavior when it is possible to implement the same with about 12 LOC? All the respect for the Gist goes to botimer, I've only stumbled into his code using search.

like image 167
kirushik Avatar answered Nov 18 '22 17:11

kirushik