Class: Kward::Clipboard

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/clipboard.rb

Overview

Best-effort local clipboard writer used by explicit user copy commands.

Defined Under Namespace

Classes: Result

Instance Method Summary collapse

Constructor Details

#initialize(output: $stdout, env: ENV, command_runner: nil) ⇒ Clipboard

Returns a new instance of Clipboard.



10
11
12
13
14
# File 'lib/kward/clipboard.rb', line 10

def initialize(output: $stdout, env: ENV, command_runner: nil)
  @output = output
  @env = env
  @command_runner = command_runner || method(:run_command)
end

Instance Method Details

#copy(text) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/kward/clipboard.rb', line 16

def copy(text)
  content = text.to_s
  return Result.new(success?: false, message: "nothing to copy") if content.empty?

  if osc52_available?
    write_osc52(content)
    return Result.new(success?: true, method: "osc52", message: "copied")
  end

  clipboard_commands.each do |name, command|
    next unless executable?(name)

    return Result.new(success?: true, method: name, message: "copied") if @command_runner.call(command, content)
  end

  Result.new(success?: false, message: "no supported clipboard mechanism found")
end