viking-copier 1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/README.md +61 -0
  2. data/lib/copier.rb +72 -0
  3. data/spec/copier_spec.rb +62 -0
  4. metadata +66 -0
@@ -0,0 +1,61 @@
1
+ # Copier
2
+
3
+ $ gem install copier
4
+
5
+ ## Usage
6
+
7
+ Usage 1: as a command
8
+
9
+ $ copier 'hello'
10
+ $ echo 'hello' | copier
11
+
12
+ Usage 2: as a library
13
+
14
+ require 'copier'
15
+ Copier('text')
16
+
17
+ run the code and then you can paste `text` by Cmd-v on Mac OS X, Ctrl-v on Windows, and as well on other platforms.
18
+
19
+ there's no paster command or method intentionally.
20
+
21
+ ## Platform
22
+
23
+ This library supports the following platforms. I'm always welcome patches for supporting other platforms, including spec.
24
+
25
+ * Mac OS X
26
+ * Windows (cygwin)
27
+
28
+ ## Licence
29
+
30
+ (The MIT License)
31
+
32
+ Copyright (c) 2010 - 2010:
33
+
34
+ * Tatsuhiro Ujihisa <http://ujihisa.blogspot.com>
35
+
36
+ Permission is hereby granted, free of charge, to any person obtaining
37
+ a copy of this software and associated documentation files (the
38
+ 'Software'), to deal in the Software without restriction, including
39
+ without limitation the rights to use, copy, modify, merge, publish,
40
+ distribute, sublicense, and/or sell copies of the Software, and to
41
+ permit persons to whom the Software is furnished to do so, subject to
42
+ the following conditions:
43
+
44
+ The above copyright notice and this permission notice shall be
45
+ included in all copies or substantial portions of the Software.
46
+
47
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
48
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
50
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
51
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
52
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
53
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
54
+
55
+ ## development
56
+
57
+ issues
58
+
59
+ * no GNOME support yet
60
+ * no KDE support yet
61
+ * copier over ssh
@@ -0,0 +1,72 @@
1
+ module Copier
2
+ class NotSupported < StandardError; end
3
+
4
+ class Config < Hash
5
+ def method_missing(name,*args)
6
+ if /=$/ =~ name.to_s
7
+ self[name.to_s.gsub(/=$/,"").to_sym] = args[0]
8
+ else
9
+ self[name]
10
+ end
11
+ end
12
+ end
13
+
14
+ class << self
15
+ def config_prepare
16
+ @config = Config.new()
17
+ end
18
+
19
+ def load_config(f = '~/.copier')
20
+ fn = File.expand_path(f)
21
+ self.config_prepare
22
+ config = @config
23
+ eval(File.read(fn)) rescue nil
24
+ @config = config.dup
25
+ @config_loaded = true
26
+ end
27
+
28
+ def method_missing(name, *args)
29
+ @config.__send__(name, *args)
30
+ end
31
+
32
+ def disable_config_file=(a)
33
+ @disable_config_file = a
34
+ if a
35
+ self.config_prepare
36
+ @config_loaded = false
37
+ end
38
+ end
39
+
40
+ def disable_config_file
41
+ @disable_config_file
42
+ end
43
+
44
+ def config_loaded
45
+ @config_loaded
46
+ end
47
+ end
48
+ end
49
+
50
+ def Copier(text)
51
+ Copier.load_config unless Copier.config_loaded || Copier.disable_config_file
52
+
53
+ if Copier.copy_method
54
+ Copier.copy_method.call(text)
55
+ return
56
+ end
57
+
58
+ if Copier.copy_file
59
+ File.open(Copier.copy_file, 'w') {|io| io.write text }
60
+ end
61
+
62
+ case RUBY_PLATFORM
63
+ when /darwin/
64
+ IO.popen('pbcopy', 'w') {|io| io.write text }
65
+ when /cygwin/
66
+ File.open('/dev/clipboard', 'wb') {|io| io.write text.gsub("\x0A", "\n") }
67
+ when /linux/
68
+ IO.popen('xclip -selection clipboard', 'w') {|io| io.write text}
69
+ else
70
+ raise Copier::NotSupported, RUBY_PLATFORM + " is not supported yet by Copier."
71
+ end
72
+ end
@@ -0,0 +1,62 @@
1
+ unless respond_to? :require_relative
2
+ def require_relative(o)
3
+ require File.dirname(__FILE__) + "/#{o}"
4
+ end
5
+ end
6
+
7
+ require_relative '../lib/copier'
8
+ require 'tempfile'
9
+ Copier.config_prepare
10
+
11
+ describe 'Copier()' do
12
+ describe 'in default case' do
13
+ before do
14
+ Copier.disable_config_file = true
15
+ end
16
+
17
+ it 'copies text on Mac OS X' do
18
+ pending 'This platform is not Mac OS X, so you cannot run this case now' unless
19
+ /darwin/ =~ RUBY_PLATFORM
20
+ a = rand.to_s
21
+ Copier(a)
22
+ `pbpaste`.chomp.should == a
23
+ end
24
+
25
+ it 'copies text on Windows (cygwin)' do
26
+ pending 'This platform is not Windows (cygwin), so you cannot run this case now' unless
27
+ /cygwin/ =~ RUBY_PLATFORM
28
+ a = rand.to_s
29
+ Copier(a)
30
+ #`pbpaste`.chomp.should == a
31
+ pending 'Please fill this case... I dont know how to paste text'
32
+ end
33
+
34
+ it 'uses xclip on linux' do
35
+ pending 'This platform is not Linux, so you cannot run this case now' unless
36
+ /linux/ =~ RUBY_PLATFORM
37
+ a = rand.to_s
38
+ Copier(a)
39
+ `xclip -selection clipboard -o`.chomp.should == a
40
+ end
41
+ end
42
+
43
+ describe 'with config' do
44
+ before do
45
+ Copier.disable_config_file = true
46
+ Copier.config_prepare
47
+ end
48
+
49
+ it '.copy_method can copy by block' do
50
+ Copier.copy_method = lambda do |a|
51
+ a.should == "Copier will be used over ssh"
52
+ end
53
+ Copier("Copier will be used over ssh")
54
+ end
55
+
56
+ it '.copy_file can copy to file' do
57
+ Copier.copy_file = Tempfile.new('copier_spec').path
58
+ Copier("fork it")
59
+ File.read(Copier.copy_file).should == "fork it"
60
+ end
61
+ end
62
+ end
metadata ADDED
@@ -0,0 +1,66 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: viking-copier
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 2
8
+ version: "1.2"
9
+ platform: ruby
10
+ authors:
11
+ - ujihisa
12
+ - viking
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-31 00:00:00 -06:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: A clipboard manager on arbitrary platforms
22
+ email: ujihisa at gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README.md
29
+ files:
30
+ - lib/copier.rb
31
+ - README.md
32
+ - spec/copier_spec.rb
33
+ has_rdoc: true
34
+ homepage: https://github.com/viking/copier
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ segments:
48
+ - 0
49
+ version: "0"
50
+ required_rubygems_version: !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ segments:
56
+ - 0
57
+ version: "0"
58
+ requirements: []
59
+
60
+ rubyforge_project:
61
+ rubygems_version: 1.3.7
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A clipboard manager on arbitrary platforms
65
+ test_files: []
66
+