Module: FlycalCli::Clipboard

Defined in:
lib/flycal_cli/clipboard.rb

Overview

Cross-platform clipboard helper. Prefers: pbcopy (macOS) → wl-copy (Wayland) → xclip (X11) → clip (Windows).

Constant Summary collapse

TOOLS =
[
  { name: :pbcopy, command: %w[pbcopy] },
  { name: :wl_copy, command: %w[wl-copy] },
  { name: :xclip, command: %w[xclip -selection clipboard] },
  { name: :clip, command: %w[clip] }
].freeze

Class Method Summary collapse

Class Method Details

.available?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/flycal_cli/clipboard.rb', line 28

def available?
  !available_tool.nil?
end

.available_toolObject



32
33
34
# File 'lib/flycal_cli/clipboard.rb', line 32

def available_tool
  TOOLS.find { |tool| command_exists?(tool[:command].first) }
end

.command_exists?(name) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/flycal_cli/clipboard.rb', line 36

def command_exists?(name)
  exts =
    if Gem.win_platform?
      ENV.fetch("PATHEXT", ".EXE;.BAT;.CMD").split(";").reject(&:empty?)
    else
      [""]
    end

  ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).any? do |dir|
    next false if dir.to_s.empty?

    exts.any? do |ext|
      path = File.join(dir, "#{name}#{ext}")
      File.file?(path) && File.executable?(path)
    end
  end
end

.copy(text) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/flycal_cli/clipboard.rb', line 16

def copy(text)
  tool = available_tool
  return nil unless tool

  IO.popen(tool[:command], "w") { |io| io.write(text.to_s) }
  return nil unless $?.success?

  tool[:name].to_s
rescue StandardError
  nil
end