Module: Rvim::SystemClipboard
- Defined in:
- lib/rvim/system_clipboard.rb
Class Method Summary collapse
- .available? ⇒ Boolean
- .detect_tool ⇒ Object
- .read ⇒ Object
- .which(cmd) ⇒ Object
- .write(text) ⇒ Object
Class Method Details
.available? ⇒ Boolean
7 8 9 |
# File 'lib/rvim/system_clipboard.rb', line 7 def available? detect_tool != nil end |
.detect_tool ⇒ Object
36 37 38 39 40 41 42 43 44 |
# File 'lib/rvim/system_clipboard.rb', line 36 def detect_tool return @tool if defined?(@tool) @tool = if RUBY_PLATFORM =~ /darwin/ && which('pbpaste') :pbpaste elsif which('xclip') :xclip end end |
.read ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/rvim/system_clipboard.rb', line 11 def read raw = case detect_tool when :pbpaste then `pbpaste` when :xclip then `xclip -selection clipboard -o 2>/dev/null` else '' end # Backticks return a string in Encoding.default_external; force-label as # UTF-8 and scrub any invalid byte sequences so downstream String#split # / regex calls don't raise on (e.g.) a mid-paste binary blob. raw = raw.dup.force_encoding(Encoding::UTF_8) raw.valid_encoding? ? raw : raw.scrub('?') end |
.which(cmd) ⇒ Object
46 47 48 |
# File 'lib/rvim/system_clipboard.rb', line 46 def which(cmd) ENV.fetch('PATH', '').split(File::PATH_SEPARATOR).any? { |d| File.executable?(File.join(d, cmd)) } end |
.write(text) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/rvim/system_clipboard.rb', line 24 def write(text) tool = detect_tool return false unless tool cmd = case tool when :pbpaste then 'pbcopy' when :xclip then 'xclip -selection clipboard -i' end IO.popen(cmd, 'w') { |io| io.write(text) } $?.success? end |