-
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
6 changed files
with
86 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# 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}" | ||
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 = "pbcopy" if executable?("pbcopy") | ||
@clipboard_program = "xclip -selection clipboard" if executable?("xclip") | ||
@clipboard_program | ||
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