Sha256: 4a5c8e1f6c629e3e0a4b24a3cfd958c0256973a02fbe3bfd39545ce694df6dff

Contents?: true

Size: 1.31 KB

Versions: 1

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

require_relative "utils"

module Clipboard
  module Linux
    extend self

    CLIPBOARDS = %w[clipboard primary secondary].freeze

    # check which backend to use
    if Utils.executable_installed? "xsel"
      WriteCommand = 'xsel -i'
      ReadCommand  = 'xsel -o'
      ReadOutputStream = true
      Selection    = {
        'clipboard' => '-b',
        'primary' => '-p',
        'secondary' => '-s'
      }.freeze
    elsif Utils.executable_installed? "xclip"
      WriteCommand = 'xclip'
      ReadCommand  = 'xclip -o'
      ReadOutputStream = false
      Selection    = proc{ |x|
        "-selection #{x}"
      }.freeze
    else
      raise Clipboard::ClipboardLoadError, "clipboard: Could not find required program xclip or xsel\n" \
                                           "On debian/ubuntu, you can install it with: sudo apt-get install xsel"
    end

    def paste(which = nil)
      if !which || !CLIPBOARDS.include?(which_normalized = which.to_s.downcase)
        which_normalized = CLIPBOARDS.first
      end
      `#{ReadCommand} #{Selection[which_normalized]} 2> /dev/null`
    end

    def clear
      copy ''
    end

    def copy(data)
      CLIPBOARDS.each{ |which|
        Utils.popen "#{WriteCommand} #{Selection[which]}", data, ReadOutputStream
      }
      paste
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clipboard-1.3.6 lib/clipboard/linux.rb