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

Use IO.console for readline over STD{IN,OUT,ERR}. #211

Merged
merged 1 commit into from
Feb 19, 2016

Conversation

sethk
Copy link
Contributor

@sethk sethk commented Feb 10, 2016

I've been testing this for a few weeks to debug programs that read from stdin and write to stdout. This is the way I'm accustomed to other debuggers working out of the box.

@deivid-rodriguez
Copy link
Owner

@sethk Thanks. Could you teach me which problem this solves? I've never used IO.console before...

@sethk
Copy link
Contributor Author

sethk commented Feb 10, 2016

IO.console is a wrapper for /dev/tty, which is the controlling terminal for the current session (usually the shell). This means you can interact with the user even if stdin/stdout are being redirected.

Let's say you have a small script like rev.rb:

#!/usr/bin/env ruby -w

ARGF.each { |line| print line.reverse }

Run it against the system dictionary:

ruby rev.rb < /usr/share/dict/words

But if you try to debug with byebug, it tries to interpret the input as debugger commands:

byebug rev.rb < /usr/share/dict/words

But with my patch it works correctly, and there is no change unless STDIN.tty? || STDOUT.tty? If you want, I can special case it to only use IO.console in that case.

@deivid-rodriguez
Copy link
Owner

Sweet! This is great!! :)

Just 2 comments:

  • Can you think of a way to test this?
  • Can you move the require under the magic comment? Otherwise it won't be picked up and frozen string literals won't be enforced.

@sethk
Copy link
Contributor Author

sethk commented Feb 19, 2016

As far as testing goes, it's possible to use a PTY (pseudo terminal) to simulate input for Readline. If you spawned a Byebug session connected to a PTY, with the PTY's FD used for debugger I/O, and STD{IN,OUT} used by the Ruby script itself, it might prevent you from having to use Byebug::TestInterface as much and make your tests closer to reality. On the other hand, it may not be worth the trouble, as the many different Readline versions should have their own tests for interacting directly with the terminal.

@sethk
Copy link
Contributor Author

sethk commented Feb 19, 2016

Fixed the magic string and squashed the commits.

deivid-rodriguez pushed a commit that referenced this pull request Feb 19, 2016
Use IO.console for readline over STD{IN,OUT,ERR}.
@deivid-rodriguez deivid-rodriguez merged commit bb98cd6 into deivid-rodriguez:master Feb 19, 2016
@deivid-rodriguez
Copy link
Owner

Thank you @sethk !

deivid-rodriguez pushed a commit that referenced this pull request Apr 8, 2016
With that patch, I get an unusable debugger. It needs more work.
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 this pull request may close these issues.

2 participants