123456789_123456789_123456789_123456789_123456789_

Class: IRB::Command::Copy

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Base
Instance Chain:
self, Base
Inherits: IRB::Command::Base
Defined in: lib/irb/command/copy.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

This class inherits a constructor from IRB::Command::Base

Instance Attribute Details

#clipboard_available?Boolean (readonly, private)

[ GitHub ]

  
# File 'lib/irb/command/copy.rb', line 68

def clipboard_available?
  !!clipboard_program
end

Instance Method Details

#clipboard_program (private)

[ GitHub ]

  
# File 'lib/irb/command/copy.rb', line 54

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

#copy_to_clipboard(text) (private)

[ GitHub ]

  
# File 'lib/irb/command/copy.rb', line 41

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

#executable?(command) ⇒ Boolean (private)

[ GitHub ]

  
# File 'lib/irb/command/copy.rb', line 64

def executable?(command)
  system("which #{command} > /dev/null 2>&1")
end

#execute(arg)

[ GitHub ]

  
# File 'lib/irb/command/copy.rb', line 23

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