-
Notifications
You must be signed in to change notification settings - Fork 140
Support copy command on windows and wsl #1153
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
base: master
Are you sure you want to change the base?
Conversation
lib/irb/command/copy.rb
Outdated
| def copy_to_clipboard(text) | ||
| IO.popen(clipboard_program, 'w') do |io| | ||
| io.write(text) | ||
| if Gem.win_platform? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a flag --disable-gems
$ ruby --disable-gems -e binding.irb
irb(main):001> Gem
uninitialized constant Gem (NameError)
I think it's better to add a guard like defined?(Gem) &&
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had not considered running IRB in an environment where Gem is not loaded. But I think a simple Windows check is sufficient here. How about this diff?
d97375e
lib/irb/command/copy.rb
Outdated
| IO.popen(clipboard_program, 'w') do |io| | ||
| io.write(text) | ||
| if Gem.win_platform? | ||
| Kernel.system("powershell.exe", "-NoProfile", "-Command", "Set-Clipboard", "-Value", text) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On Windows, passing text via stdin does not work reliably in IRB
This might be fixed in ruby/reline#875
Although I also think adding this workaround for a while is worth. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had not noticed ruby/reline#875. With this fix, the workaround seems unnecessary, and it looks like we can remove the unnatural branching, so I deleted it e90574a
Enable the
copycommand on Windows and WSL environments.The
copycommand now works both on Windows Ruby and on WSL, copying the evaluated result to the system clipboard.Examples
Windows (mingw-ucrt)
WSL
Windows workaround
On Windows, passing text via stdin does not work reliably in IRB. After calling
IO.popen, IRB’s input handling becomes unstable, but I have not fully identified the root cause yet.To work around this, the Windows implementation invokes PowerShell’s
Set-Clipboardand passes the text via command-line arguments instead of stdin:This approach works reliably on native Windows Ruby.
This issue is likely not limited to the
copycommand, and there may be other cases where IRB does not behave correctly. If this problem can be resolved, it should be possible to simply callclip.exeeven on Windows. I plan to investigate this separately.