blob: b6aee0c3381b4e10d8c370bf46cb5642e610df89 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
# 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)
# Copy last value if no expression was supplied
arg = '_' if arg.to_s.strip.empty?
value = irb_context.workspace.binding.eval(arg)
output = irb_context.inspect_method.inspect_value(value, +'', colorize: false).chomp
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"
rescue Errno::ENOENT => e
warn e.message
warn "Is IRB.conf[:COPY_COMMAND] set to a bad value?"
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
|