Sha256: 295c356aa6e8cc20ae0f1ab3f6f49fd0b665b4534a9e7cd8701883d3b67075ec

Contents?: true

Size: 1.5 KB

Versions: 4

Compression:

Stored size: 1.5 KB

Contents

# original clipboard code: http://project.ioni.st/post/1334#snippet_1334
# turned it into a class to make it flexxy:
# http://gilesbowkett.blogspot.com/2007/09/improved-auto-pastie-irb-code.html
# Extended to handle windows and linux as well
require 'platform'

module UtilityBelt
  class Clipboard
    
    def self.available?
      @@implemented || false
    end
    
    case Platform::IMPL
    when :macosx

      def self.read
        IO.popen('pbpaste') {|clipboard| clipboard.read}
      end

      def self.write(stuff)
        IO.popen('pbcopy', 'w+') {|clipboard| clipboard.write(stuff)}
      end
      @@implemented = true
  
    when :mswin

      begin
        # Try loading the win32-clipboard gem
        require 'win32/clipboard'

        def self.read
          Win32::Clipboard.data
        end

        def self.write(stuff)
          Win32::Clipboard.set_data(stuff)
        end
        @@implemented = true

      rescue LoadError
        raise "You need the win32-clipboard gem for clipboard functionality!"
      end

    when :linux
      #test execute xsel
      `xsel`
      if $?.exitstatus != 0
        raise "You need to install xsel for clipboard functionality!"
      end

      def self.read
        `xsel`
      end
      
      def self.write(stuff)
        `echo '#{stuff}' | xsel -i`
      end
      @@implemented = true

    else
      raise "No suitable clipboard implementation for your platform found!"
    end
    
  end
end

Clipboard = UtilityBelt::Clipboard if Object.const_defined? :IRB

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
akitaonrails-utility_belt-1.0.10 lib/utility_belt/clipboard.rb
akitaonrails-utility_belt-1.0.11 lib/utility_belt/clipboard.rb
akitaonrails-utility_belt-1.0.12 lib/utility_belt/clipboard.rb
akitaonrails-utility_belt-1.0.13 lib/utility_belt/clipboard.rb