clipboard 1.2.1 → 1.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5cb0707321680a4dbb4b9ab0bf24ff6fc1162e9f2770d6424065ee3c63fd6b1b
4
- data.tar.gz: 4a92d7a1546f178e5acfef108307a27eb1c230d127bedde527f1d45c8f1a1fd2
3
+ metadata.gz: 0db9702c7b8ca48e95e26e612cf294b78d4eb188a4e74a72a002fb064e6c61e3
4
+ data.tar.gz: 6abd7baac11e392fa41997d771504c6b104350f8049693735f233dee6bba8915
5
5
  SHA512:
6
- metadata.gz: f6839e8dc8d63e1fe38a2c66ead80cf90abf22ced2539fa0ab8024f0be8d8fdecf93f3e007a29b039988d9b3162ef29abb9f001db13e8a1cd903242f9c199e22
7
- data.tar.gz: 9c5554455692d8a41322763abbfbc939a8ddd6344b183042b56a4aa6a1d72014449a912fc5cabcc3800b3930022ec831012b6a1f28a2f4b6b943f1cbbc5b91b0
6
+ metadata.gz: a4b5ff54912337f98f1918375e8839b6eae786b9aa464bfa1b1aacf4bff3e30967f9104fa520f8fe6f343e7273c5982db1c109705884f4426b23166f02a8ec37
7
+ data.tar.gz: cbe9164425d0f9f1ab7f0efd270a8adcf9e65ba472a644afab3301e100e9fab9d27a400c0e8964200898edec99a2fa91259d877dc0219549778f35625d594295
@@ -1,5 +1,16 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.3.0
4
+ ### Bug Fixes
5
+ * Conditionally read or don't read the output stream of external commands, fixes #32
6
+ * Special thanks to @orange-kao for the bug report + PR
7
+
8
+ ### New Features
9
+ * Add a GTK based clipboard
10
+
11
+ ### Internal changes
12
+ * Use frozen string literals
13
+
3
14
  ## 1.2.1
4
15
  * Add WSL to autoloaded constants
5
16
 
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010-2016 Jan Lelis
1
+ Copyright (c) 2010-2018 Jan Lelis
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.md CHANGED
@@ -1,66 +1,87 @@
1
- # Ruby Clipboard [![version](https://badge.fury.io/rb/clipboard.svg)](https://badge.fury.io/rb/clipboard) [<img src="https://travis-ci.org/janlelis/clipboard.svg" />](https://travis-ci.org/janlelis/clipboard)
1
+ # Clipboard Ruby Gem [![version](https://badge.fury.io/rb/clipboard.svg)](https://badge.fury.io/rb/clipboard) [<img src="https://travis-ci.org/janlelis/clipboard.svg" />](https://travis-ci.org/janlelis/clipboard)
2
2
 
3
- Lets you access the clipboard on Linux, MacOS, Windows and Cygwin.
3
+ Lets you access the clipboard from everywhere. Currently supported platforms:
4
+
5
+ - Linux
6
+ - MacOS
7
+ - Windows
8
+ - Cygwin (POSIX environment for Windows)
9
+ - WSL (Windows Subsystem for Linux)
10
+ - Gtk+ (Cross Platform Widget Toolkit)
11
+ - Java (on JRuby)
4
12
 
5
13
  ## Usage
6
14
 
7
- * `Clipboard.copy`
8
- * `Clipboard.paste`
9
- * `Clipboard.clear`
15
+ * `Clipboard.copy` - Copies a string to system clipboard
16
+ * `Clipboard.paste` - Paste contents from system clipboard as string
17
+ * `Clipboard.clear` - Empties the system clipboard
10
18
 
11
19
  ## Setup
12
20
 
13
- Add to `Gemfile`:
21
+ Add the following lines to your `Gemfile`:
14
22
 
15
23
  ```ruby
16
24
  gem 'clipboard'
17
25
  gem 'ffi', :platforms => [:mswin, :mingw]
18
26
  ```
19
27
 
20
- - **Linux**: You will need the `xclip` or `xsel` program. On debian/ubuntu
21
- this is: `sudo apt-get install xclip`
28
+ - Important note for **Linux** users: The clipboard requires the *xclip* or the *xsel* command-line program. On debian and ubuntu, xclip can be installed with: `sudo apt-get install xclip`
22
29
 
23
- ## Remarks
24
- ### Multiple Clipboards
30
+ ## Clipboard Implementations
25
31
 
26
- On Linux, you can choose from which clipboard you want to `paste` from by
27
- passing it as an argument. The default is CLIPBOARD.
32
+ In most environments, the appropriate clipboard implementation can be detected automatically. If none is found, the gem will fallback to a file based one, which will just write to/read from `~/.clipboard` instead of the system clipboard.
28
33
 
29
- `copy` copies to all clipboards in `Clipboard::Linux::CLIPBOARDS`.
34
+ You can check the implementation used with: `Clipboard.implementation`
30
35
 
31
- ### Windows Encoding
36
+ ### Alternative Clipboard Providers
32
37
 
33
- Windows uses [UTF-16LE](https://en.wikipedia.org/wiki/UTF-16) as its default
34
- encoding, so pasted strings will always come in UTF-16LE. You can manually
35
- convert them to your desired encoding (e.g. UTF-8) using the
36
- [String#encode](ruby-doc.org/core-2.3.0/String.html#method-i-encode) method:
38
+ There are two implementations included in this gem, which are not used by default. You can opt-in to use them if you think they are a better fit for your application environment:
37
39
 
38
- ```ruby
39
- Clipboard.paste.encode('UTF-8')
40
- ```
40
+ #### Java
41
+
42
+ Activate with: `Clipboard.implementation = Clipboard::Java`
43
+
44
+ This is an option for [JRuby users](https://www.jruby.org/) which will use the clipboard functionality from the Java standard library.
45
+
46
+ #### GTK+
47
+
48
+ Activate with: `Clipboard.implementation = Clipboard::Gtk`
49
+
50
+ This utilizes the **GTK+** library. See [Ruby-GNOME2](https://github.com/ruby-gnome2/ruby-gnome2#ruby-gnome2) for more info.
51
+
52
+ Requires the `gtk3` or `gtk2` gem to be installed.
41
53
 
42
- ### SSH
54
+ ## Tips & Tricks
43
55
 
44
- To use the clipboard through ssh, you need to install `xauth` on your server
45
- and connect via `ssh -X` or `ssh -Y`. Please note that some server settings
46
- restrict this feature.
56
+ ### Linux: Using Clipboard via SSH
47
57
 
48
- ### Java
58
+ To be able to use the clipboard through SSH, you need to install `xauth` on your server and connect via `ssh -X` or `ssh -Y`. Please note that some server settings restrict this feature.
49
59
 
50
- There is a Java implementation included (`Clipboard::Java`) as an option for
51
- JRuby. On Linux, it always operates only on the CLIPBOARD clipboard.
60
+ ### Linux: Paste From Specific X11 Selection
52
61
 
53
- ### TODO
62
+ The clipboard on Linux is divided into multiple clipboard selections. You can choose from which clipboard you want to `paste` from by
63
+ passing it as an argument. The default is *:clipboard*, other options are *:primary* and *:secondary*.
54
64
 
55
- * Native support on X (don't depend on `xclip`/`xsel`) would be great
65
+ `Clipboard.copy` always copies to all three clipboards.
56
66
 
57
- ### blip
67
+ ### Windows: Encoding Info
68
+
69
+ Windows uses [UTF-16LE](https://en.wikipedia.org/wiki/UTF-16) as its default encoding, so pasted strings will always come in UTF-16. You can then manually convert them to your desired encoding, for example, UTF-8, using the [String#encode](ruby-doc.org/core-2.3.0/String.html#method-i-encode) method:
70
+
71
+ ```ruby
72
+ Clipboard.paste.encode('UTF-8')
73
+ ```
74
+
75
+ ### CLI Utility: blip
76
+
77
+ The [blip gem]((https://gist.github.com/janlelis/781835)) is a handy command-line wrapper for the clipboard gem. It lets you quickly copy file content to your clipboard:
78
+
79
+ ```
80
+ $ blip FILE_NAME
81
+ ```
58
82
 
59
- **blip** is a handy commandline wrapper that lets you quickly copy file
60
- content to your clipboard: [blip](https://gist.github.com/janlelis/781835)!
83
+ Without any arguments, it will just paste the contents of the clipboard.
61
84
 
62
- ### Copyright
85
+ ### MIT
63
86
 
64
- Copyright (c) 2010-2016 Jan Lelis <http://janlelis.com> released under the MIT
65
- license. Contributions by and thanks to Michael Grosser and [all the other
66
- contributors!](https://github.com/janlelis/clipboard/graphs/contributors)
87
+ Copyright (c) 2010-2018 Jan Lelis <http://janlelis.com> released under the MIT license. Contributions by and thanks to Michael Grosser and [all the other contributors!](https://github.com/janlelis/clipboard/graphs/contributors)
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.summary = "Access to the clipboard on Linux, MacOS, Windows, and Cygwin."
10
10
  s.description = "Access to the clipboard on Linux, MacOS, Windows, and Cygwin: Clipboard.copy, Clipboard.paste, Clipboard.clear"
11
11
  s.email = "mail@janlelis.de"
12
- s.homepage = "http://github.com/janlelis/clipboard"
12
+ s.homepage = "https://github.com/janlelis/clipboard"
13
13
  s.license = "MIT"
14
14
  s.requirements = [
15
15
  "On Linux (or other X), you will need xclip. On debian/ubuntu this is: sudo apt-get install xclip",
@@ -20,4 +20,6 @@ Gem::Specification.new do |s|
20
20
  s.required_ruby_version = '>= 1.9.3'
21
21
  s.add_development_dependency 'rake', '~> 11'
22
22
  s.add_development_dependency 'rspec', '~> 3'
23
+ # s.add_development_dependency 'ffi', '~> 1.9'
24
+ # s.add_development_dependency 'gtk3', '~> 3'
23
25
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rbconfig'
2
4
  require File.dirname(__FILE__) + '/clipboard/version'
3
5
 
@@ -13,6 +15,7 @@ module Clipboard
13
15
  autoload :Java, 'clipboard/java'
14
16
  autoload :Cygwin, 'clipboard/cygwin'
15
17
  autoload :Wsl, 'clipboard/wsl'
18
+ autoload :Gtk, 'clipboard/gtk'
16
19
  end
17
20
  autoload :Windows, 'clipboard/windows'
18
21
  autoload :File, 'clipboard/file'
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Clipboard
2
4
  module Cygwin
3
5
  extend self
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Clipboard
2
4
  module File
3
5
  extend self
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Ruby-Gnome2 based implementation
4
+ # Requires either the gtk3 or the gtk2 gem
5
+
6
+ module Clipboard
7
+ module Gtk
8
+ extend self
9
+
10
+ CLIPBOARDS = %w[CLIPBOARD PRIMARY SECONDARY].freeze
11
+
12
+ unless defined? ::Gtk
13
+ begin
14
+ require 'gtk3'
15
+ rescue LoadError
16
+ begin
17
+ require 'gtk2'
18
+ rescue LoadError
19
+ raise LoadError, 'Could not load the required gtk3 or gtk2 gem, please install it with: gem install gtk3'
20
+ end
21
+ end
22
+ end
23
+
24
+ def copy(text)
25
+ CLIPBOARDS.each{ |which|
26
+ ::Gtk::Clipboard.get(Gdk::Selection.const_get(which)).set_text(text).store
27
+ }
28
+ paste
29
+ end
30
+
31
+ def paste(which = nil)
32
+ if !which || !CLIPBOARDS.include?(which_normalized = which.to_s.upcase)
33
+ which_normalized = CLIPBOARDS.first
34
+ end
35
+
36
+ ::Gtk::Clipboard.get(
37
+ Gdk::Selection.const_get(which_normalized)
38
+ ).wait_for_text || ""
39
+ end
40
+
41
+ def clear
42
+ CLIPBOARDS.each{ |which|
43
+ ::Gtk::Clipboard.get(Gdk::Selection.const_get(which)).clear
44
+ }
45
+ end
46
+ end
47
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Clipboard
2
4
  # Basic java clipboard access (jruby). No fun to use on X.
3
5
  module Java
@@ -1,6 +1,6 @@
1
- require 'open3'
1
+ # frozen_string_literal: true
2
2
 
3
- require_relative 'utils'
3
+ require_relative "utils"
4
4
 
5
5
  module Clipboard
6
6
  module Linux
@@ -10,23 +10,31 @@ module Clipboard
10
10
 
11
11
  # check which backend to use
12
12
  if Utils.executable_installed?('xclip')
13
- WriteCommand = 'xclip'.freeze
14
- ReadCommand = 'xclip -o'.freeze
15
- Selection = proc{ |x| "-selection #{x}" }.freeze
13
+ WriteCommand = 'xclip'
14
+ ReadCommand = 'xclip -o'
15
+ ReadOutputStream = false
16
+ Selection = proc{ |x|
17
+ "-selection #{x}"
18
+ }.freeze
16
19
  elsif Utils.executable_installed?('xsel')
17
- WriteCommand = 'xsel -i'.freeze
18
- ReadCommand = 'xsel -o'.freeze
19
- Selection = { 'clipboard' => '-b', 'primary' => '-p', 'secondary' => '-s' }.freeze
20
+ WriteCommand = 'xsel -i'
21
+ ReadCommand = 'xsel -o'
22
+ ReadOutputStream = true
23
+ Selection = {
24
+ 'clipboard' => '-b',
25
+ 'primary' => '-p',
26
+ 'secondary' => '-s'
27
+ }.freeze
20
28
  else
21
29
  raise Clipboard::ClipboardLoadError, "clipboard: Could not find required program xclip or xsel\n" \
22
30
  "On debian/ubuntu, you can install it with: sudo apt-get install xclip"
23
31
  end
24
32
 
25
33
  def paste(which = nil)
26
- if !which || !CLIPBOARDS.include?(which.to_s.downcase)
27
- which = CLIPBOARDS.first
34
+ if !which || !CLIPBOARDS.include?(which_normalized = which.to_s.downcase)
35
+ which_normalized = CLIPBOARDS.first
28
36
  end
29
- `#{ReadCommand} #{Selection[which.to_s.downcase]} 2> /dev/null`
37
+ `#{ReadCommand} #{Selection[which_normalized]} 2> /dev/null`
30
38
  end
31
39
 
32
40
  def clear
@@ -35,7 +43,7 @@ module Clipboard
35
43
 
36
44
  def copy(data)
37
45
  CLIPBOARDS.each{ |which|
38
- Open3.popen3( "#{WriteCommand} #{Selection[which.to_s.downcase]}" ){ |input, _, _| input << data }
46
+ Utils.popen "#{WriteCommand} #{Selection[which]}", data, ReadOutputStream
39
47
  }
40
48
  paste
41
49
  end
@@ -1,4 +1,6 @@
1
- require 'open3'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "utils"
2
4
 
3
5
  module Clipboard
4
6
  module Mac
@@ -9,7 +11,7 @@ module Clipboard
9
11
  end
10
12
 
11
13
  def copy(data)
12
- Open3.popen3( 'pbcopy' ){ |input, _, _| input << data }
14
+ Utils.popen "pbcopy", data
13
15
  paste
14
16
  end
15
17
 
@@ -1,3 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "open3"
4
+
1
5
  module Clipboard
2
6
  module Utils
3
7
  extend self
@@ -7,5 +11,23 @@ module Clipboard
7
11
  ::File.executable?(::File.join(path, cmd))
8
12
  end
9
13
  end
14
+
15
+ # Utility to call external command
16
+ # - pure .popen2 becomes messy with xsel when not reading the output stream
17
+ # - xclip doesn't like to have output stream read
18
+ def popen(cmd, data, read_output_stream = false)
19
+ Open3.popen2(cmd) { |input, output, waiter_thread|
20
+ output_thread = Thread.new { output.read } if read_output_stream
21
+
22
+ begin
23
+ input.write data
24
+ rescue Errno::EPIPE
25
+ end
26
+
27
+ input.close
28
+ output_thread.value if read_output_stream
29
+ waiter_thread.value
30
+ }
31
+ end
10
32
  end
11
33
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Clipboard
2
- VERSION = "1.2.1".freeze
4
+ VERSION = "1.3.0"
3
5
  end
@@ -1,4 +1,6 @@
1
- require 'open3'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "utils"
2
4
 
3
5
  module Clipboard
4
6
  module Windows
@@ -76,7 +78,7 @@ module Clipboard
76
78
  User32.set( CF_UNICODETEXT, handler )
77
79
  User32.close( )
78
80
  else # don't touch anything
79
- Open3.popen3( 'clip' ){ |input, _, _| input << data_to_copy } # depends on clip (available by default since Vista)
81
+ Utils.popen "clip", data_to_copy # depends on clip (available by default since Vista)
80
82
  end
81
83
  paste
82
84
  end
@@ -1,4 +1,6 @@
1
- require 'open3'
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "utils"
2
4
 
3
5
  module Clipboard
4
6
  module Wsl
@@ -9,7 +11,7 @@ module Clipboard
9
11
  end
10
12
 
11
13
  def copy(data)
12
- Open3.popen3( 'clip.exe' ){ |input, _, _| input << data }
14
+ Utils.popen "clip.exe", data
13
15
  paste
14
16
  end
15
17
 
@@ -0,0 +1,20 @@
1
+ require_relative 'spec_helper'
2
+
3
+ require "clipboard/linux"
4
+
5
+ describe Clipboard::Linux do
6
+ # See https://github.com/janlelis/clipboard/issues/32 by @orange-kao
7
+ it "can copy more than 8192 bytes" do
8
+ # first batch
9
+ data1 = Random.new.bytes(2**14).unpack("H*").first
10
+ data2 = Clipboard.copy(data1)
11
+
12
+ expect(data2).to eq data1
13
+
14
+ # second batch
15
+ data1 = Random.new.bytes(2**14).unpack("H*").first
16
+ data2 = Clipboard.copy(data1)
17
+
18
+ expect(data2).to eq data1
19
+ end
20
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clipboard
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.1
4
+ version: 1.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jan Lelis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-12-10 00:00:00.000000000 Z
11
+ date: 2018-12-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -53,6 +53,7 @@ files:
53
53
  - lib/clipboard.rb
54
54
  - lib/clipboard/cygwin.rb
55
55
  - lib/clipboard/file.rb
56
+ - lib/clipboard/gtk.rb
56
57
  - lib/clipboard/java.rb
57
58
  - lib/clipboard/linux.rb
58
59
  - lib/clipboard/mac.rb
@@ -60,9 +61,10 @@ files:
60
61
  - lib/clipboard/version.rb
61
62
  - lib/clipboard/windows.rb
62
63
  - lib/clipboard/wsl.rb
64
+ - spec/clipboard_linux_spec.rb
63
65
  - spec/clipboard_spec.rb
64
66
  - spec/spec_helper.rb
65
- homepage: http://github.com/janlelis/clipboard
67
+ homepage: https://github.com/janlelis/clipboard
66
68
  licenses:
67
69
  - MIT
68
70
  metadata: {}