-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #753
- Loading branch information
Showing
11 changed files
with
186 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# frozen_string_literal: true | ||
|
||
module IRB | ||
module Command | ||
class Copy < Base | ||
category "Workspace" | ||
description "Copy command output to clipboard" | ||
|
||
help_message(<<~HELP) | ||
Usage: copy (command) | ||
HELP | ||
|
||
def execute(arg) | ||
value = irb_context.workspace.binding.eval(arg) | ||
output = irb_context.inspect_method.inspect_value(value, colorize: false) | ||
|
||
if clipboard_available? | ||
copy_to_clipboard(output) | ||
else | ||
warn "System clipboard not found" | ||
end | ||
rescue StandardError => e | ||
warn "Error: #{e}" | ||
warn "Is IRB.conf[:COPY_COMMAND] set to a bad value?" | ||
end | ||
|
||
private | ||
|
||
def copy_to_clipboard(text) | ||
IO.popen(clipboard_program, 'w') do |io| | ||
io.write(text) | ||
end | ||
|
||
raise IOError.new("Copying to clipboard failed") unless $? == 0 | ||
|
||
puts "Copied to system clipboard" | ||
end | ||
|
||
def clipboard_program | ||
@clipboard_program ||= if IRB.conf[:COPY_COMMAND] | ||
IRB.conf[:COPY_COMMAND] | ||
elsif executable?("pbcopy") | ||
"pbcopy" | ||
elsif executable?("xclip") | ||
"xclip -selection clipboard" | ||
end | ||
end | ||
|
||
def executable?(command) | ||
system("which #{command} > /dev/null 2>&1") | ||
end | ||
|
||
def clipboard_available? | ||
!!clipboard_program | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'irb' | ||
|
||
require_relative "../helper" | ||
|
||
module TestIRB | ||
class CopyTest < IntegrationTestCase | ||
def setup | ||
super | ||
@envs['IRB_COPY_COMMAND'] = "ruby -e \"puts 'foo' + STDIN.read\"" | ||
end | ||
|
||
def test_copy_with_pbcopy | ||
write_ruby <<~'ruby' | ||
class Answer | ||
def initialize(answer) | ||
@answer = answer | ||
end | ||
end | ||
binding.irb | ||
ruby | ||
|
||
output = run_ruby_file do | ||
type "copy Answer.new(42)" | ||
type "exit" | ||
end | ||
|
||
assert_match(/foo#<Answer:0x[0-9a-f]+ @answer=42/, output) | ||
assert_match(/Copied to system clipboard/, output) | ||
end | ||
|
||
# copy puts 5 should: | ||
# - Print value to the console | ||
# - Copy nil to clipboard, since that is what the puts call evaluates to | ||
def test_copy_when_expression_has_side_effects | ||
write_ruby <<~'ruby' | ||
binding.irb | ||
ruby | ||
|
||
output = run_ruby_file do | ||
type "copy puts 42" | ||
type "exit" | ||
end | ||
|
||
assert_match(/^42\r\n/, output) | ||
assert_match(/foonil/, output) | ||
assert_match(/Copied to system clipboard/, output) | ||
refute_match(/foo42/, output) | ||
end | ||
|
||
def test_copy_when_copy_command_is_invalid | ||
@envs['IRB_COPY_COMMAND'] = "lulz" | ||
|
||
write_ruby <<~'ruby' | ||
binding.irb | ||
ruby | ||
|
||
output = run_ruby_file do | ||
type "copy 42" | ||
type "exit" | ||
end | ||
|
||
assert_match(/Error: No such file or directory - lulz/, output) | ||
assert_match(/Is IRB\.conf\[:COPY_COMMAND\] set to a bad value/, output) | ||
refute_match(/Copied to system clipboard/, output) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters