Sha256: e99fe033f00714540fc2d05bb75ff89d1d25e8c4771293ecab90005412099f0e

Contents?: true

Size: 1.51 KB

Versions: 1

Compression:

Stored size: 1.51 KB

Contents

# frozen_string_literal: true

require_relative "implementation"
require_relative "utils"

module Clipboard
  module Linux
    include Implementation
    extend self

    CLIPBOARDS = %w[clipboard primary secondary].freeze

    # First check for xsel, fallback to xclip
    if Utils.executable_installed? "xsel"
      WRITE_COMMAND = 'xsel -i'
      READ_COMMAND = 'xsel -o'
      READ_OUTPUT_STREAM = true
      SELECTION = {
        'clipboard' => '--clipboard',
        'primary' => '--primary',
        'secondary' => '--secondary'
      }.freeze

    elsif Utils.executable_installed? "xclip"
      WRITE_COMMAND = 'xclip'
      READ_COMMAND = 'xclip -o'
      READ_OUTPUT_STREAM = false
      SELECTION = proc{ |x|
        "-selection #{x}"
      }.freeze

    else
      raise Clipboard::ClipboardLoadError,
            "clipboard: Could not find required program xsl or xclip (X11) or wl-clipboard (Wayland)"
    end

    def paste(which = nil, clipboard: "clipboard")
      selection = which || clipboard
      raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

      `#{READ_COMMAND} #{SELECTION[selection]} 2> /dev/null`
    end

    def copy(data, clipboard: "all")
      selections = clipboard.to_s == "all" ? CLIPBOARDS : [clipboard]
      selections.each{ |selection|
        raise ArgumentError, "unknown clipboard selection" unless CLIPBOARDS.include?(selection)

        Utils.popen "#{WRITE_COMMAND} #{SELECTION[selection]}", data, READ_OUTPUT_STREAM
      }

      true
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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