srsly 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in continue.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Junegunn Choi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,38 @@
1
+ ```
2
+ ..............................................
3
+ ..####...#####....####...##....##..##...####..
4
+ .##......##..##..##......##.....####...##..##.
5
+ ..####...#####....####...##......##.......##..
6
+ .....##..##..##......##..##......##......##...
7
+ ..####...##..##...####...######..##......##...
8
+ ..............................................
9
+ ```
10
+
11
+ ```ruby
12
+ require 'srsly'
13
+
14
+ r = SRSLY? # Default "Are you sure (Y/N)?" message
15
+ r = SRSLY? nil => :eof # Returns :eof on EOF (nil)
16
+
17
+ r = SRSLY? 'Continue (Y/N)? ' # Custom message
18
+ r = SRSLY? 'Continue (Y/N)? ', nil => :eof # Returns :eof on EOF
19
+
20
+ r = SRSLY? 'Seriously (Y/N/A)? ',
21
+ # Expected responses (Regexp or String)
22
+ /^y/i => true,
23
+ /^n/i => false,
24
+ /^a/i => :all,
25
+ # Other options
26
+ :error => 'Invalid input. Seriously (Y/N/A)? ',
27
+ :tries => 5,
28
+ :in => $stdin,
29
+ :out => $stdout
30
+
31
+ r = SRSLY? "A or B? ",
32
+ 'A' => :a,
33
+ 'B' => :b,
34
+ nil => :quit,
35
+ :tries => 5,
36
+ :error => proc { |got, try, total| print "#{got}? Try again (#{try}/#{total}) " },
37
+ :out => proc { |msg| print msg + '>> ' }
38
+ ```
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rake/testtask'
3
+ Rake::TestTask.new(:test) do |test|
4
+ test.libs << 'lib' << 'test'
5
+ test.pattern = 'test/**/test_*.rb'
6
+ test.verbose = true
7
+ end
data/lib/srsly.rb ADDED
@@ -0,0 +1,103 @@
1
+ # Prompt user for an input.
2
+ # @param [String] message Output message
3
+ # @param [Hash] options Options
4
+ # @option options [String|Proc] :error Message to print, or Proc to call on unexpected input.
5
+ # @option options [Fixnum] :tries Number of tries.
6
+ # @option options [IO|Proc] :in
7
+ # @option options [IO|Proc] :out
8
+ # @return [Object]
9
+ def SRSLY? *args
10
+ message, options =
11
+ case args.length
12
+ when 0
13
+ [nil, {}]
14
+ when 1
15
+ case args.first
16
+ when Hash
17
+ [nil, args.first]
18
+ when String
19
+ [args.first, {}]
20
+ else
21
+ raise ArgumentError, "Invalid parameter"
22
+ end
23
+ when 2
24
+ args
25
+ else
26
+ raise ArgumentError, "wrong number of arguments (#{args.length} for 2)"
27
+ end
28
+
29
+ message ||= 'Are you sure (Y/N)? '
30
+ options = {
31
+ :error => nil,
32
+ :tries => nil,
33
+ :in => $stdin,
34
+ :out => $stdout
35
+ }.merge(options)
36
+
37
+ error, tries, input, output = options.values_at(
38
+ :error, :tries, :in, :out)
39
+
40
+ raise ArgumentError, "Message must be a String" unless message.is_a?(String)
41
+ raise ArgumentError, "Invalid :tries" unless tries.nil? ||
42
+ (tries.is_a?(Fixnum) && tries > 0)
43
+
44
+ read =
45
+ if input.respond_to?(:gets)
46
+ proc { input.gets }
47
+ elsif input.is_a?(Proc)
48
+ input
49
+ else
50
+ raise ArgumentError, "Invalid :in"
51
+ end
52
+
53
+ write =
54
+ if output.respond_to?(:write)
55
+ proc { |s| output.write s }
56
+ elsif output.is_a?(Proc)
57
+ output
58
+ else
59
+ raise ArgumentError, "Invalid :out"
60
+ end
61
+
62
+ alert =
63
+ case error
64
+ when String
65
+ proc { write.call error }
66
+ when Proc
67
+ error
68
+ else
69
+ raise ArgumentError, "Invalid :error"
70
+ end if error
71
+
72
+ resps = options.select { |k, v| k.is_a?(Regexp) || k.is_a?(String) || k.nil? }
73
+ resps = resps.merge(
74
+ # Default responses if nothing given
75
+ /^y/i => true,
76
+ /^n/i => false) if resps.reject { |k, _| k.nil? }.empty?
77
+
78
+ ask = proc { write.call message }
79
+ t = 0
80
+ got = nil
81
+ while tries.nil? || t < tries
82
+ ask.call got, t += 1, tries
83
+
84
+ if got = read.call
85
+ got = got.chomp
86
+ else
87
+ write.call $/
88
+ end
89
+
90
+ resps.each do |k, v|
91
+ case k
92
+ when String, nil
93
+ return v if k == got
94
+ when Regexp
95
+ return v if k.match got
96
+ end
97
+ end
98
+
99
+ ask = alert if alert
100
+ end
101
+
102
+ nil
103
+ end
data/srsly.gemspec ADDED
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |gem|
6
+ gem.name = "srsly"
7
+ gem.version = "1.0.0"
8
+ gem.authors = ["Junegunn Choi"]
9
+ gem.email = ["junegunn.c@gmail.com"]
10
+ gem.description = %q{SRSLY?}
11
+ gem.summary = %q{SRSLY?}
12
+ gem.homepage = "https://github.com/junegunn/srsly"
13
+
14
+ gem.files = `git ls-files`.split($/)
15
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
16
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
17
+ gem.require_paths = ["lib"]
18
+
19
+ gem.add_development_dependency 'minitest'
20
+ gem.add_development_dependency 'mocha'
21
+ end
@@ -0,0 +1,148 @@
1
+ $VERBOSE = true
2
+
3
+ require 'rubygems'
4
+ require 'minitest/autorun'
5
+ require 'mocha/setup'
6
+ require 'srsly'
7
+
8
+ class TestSRSLY < MiniTest::Unit::TestCase
9
+ def setup
10
+ $stdin = STDIN
11
+ $stdout = STDOUT
12
+ end
13
+
14
+ def test_srsly_invalid_opts
15
+ assert_raises(ArgumentError) { SRSLY? nil }
16
+ assert_raises(ArgumentError) { SRSLY? "message0", "m", :tries => 1 }
17
+ assert_raises(ArgumentError) { SRSLY? "message1", :tries => -1 }
18
+ assert_raises(ArgumentError) { SRSLY? "message2", :tries => 0.1 }
19
+ assert_raises(ArgumentError) { SRSLY? "message3", :in => "In" }
20
+ assert_raises(ArgumentError) { SRSLY? "message4", :out => "Out" }
21
+ assert_raises(ArgumentError) { SRSLY? "message5", :error => 1 }
22
+ end
23
+
24
+ def test_srsly_basic
25
+ m = "SRSLY? (Y/N) "
26
+
27
+ {
28
+ :io_procs => [:call, :call],
29
+ :io_streams => [:gets, :write]
30
+ }.each do |io, ms|
31
+ {
32
+ %w[y Y yes Yes] => true,
33
+ %w[n N no No] => false,
34
+ }.each do |resps, exp|
35
+ resps.each do |resp|
36
+ i, o = self.send io
37
+ o.expects(ms[1]).times(3).with(m).returns(m)
38
+ o.expects(ms[1]).times(1).with($/).returns($/)
39
+ o.expects(ms[1]).times(1).with(m).returns(m)
40
+ i.expects(ms[0]).times(4).returns('???', '???', nil, resp + $/)
41
+
42
+ assert_equal exp, SRSLY?(m, :in => i, :out => o)
43
+ end
44
+ end
45
+ end
46
+ end
47
+
48
+ def test_srsly_multiple_patterns
49
+ m = "(a/b/c) "
50
+ m2 = "Hey!!"
51
+
52
+ {
53
+ :io_procs => [:call, :call],
54
+ :io_streams => [:gets, :write]
55
+ }.each do |io, ms|
56
+ {
57
+ %w[a A] => :a,
58
+ %w[b B ab] => :b,
59
+ %w[c C] => :c,
60
+ }.each do |resps, exp|
61
+ resps.each do |resp|
62
+ i, o = self.send io
63
+ o.expects(ms[1]).times(1).with(m).returns(m)
64
+ o.expects(ms[1]).times(2).with(m2).returns(m2)
65
+ o.expects(ms[1]).times(1).with($/).returns($/)
66
+ o.expects(ms[1]).times(1).with(m2).returns(m2)
67
+ i.expects(ms[0]).times(4).returns('???', '???', nil, resp + $/)
68
+
69
+ assert_equal exp, SRSLY?(m, :error => m2,
70
+ 'a' => :a, 'A' => :a,
71
+ /b/i => :b,
72
+ /c/ => :c, 'C' => :c,
73
+ :in => i, :out => o)
74
+ end
75
+ end
76
+ end
77
+ end
78
+
79
+ def test_srsly_fail
80
+ m = "(a/b/c) "
81
+ m2 = "Hey!!"
82
+ f = 'Duh'
83
+
84
+ {
85
+ :io_procs => [:call, :call],
86
+ :io_streams => [:gets, :write]
87
+ }.each do |io, ms|
88
+ i, o = self.send io
89
+ o.expects(ms[1]).times(1).with(m).returns(m)
90
+ o.expects(ms[1]).times(2).with(m2).returns(m2)
91
+ o.expects(ms[1]).times(1).with($/).returns($/)
92
+ o.expects(ms[1]).times(1).with(m2).returns(m2)
93
+
94
+ i.expects(ms[0]).times(4).returns('???', '???', nil, '???')
95
+
96
+ assert_equal nil, SRSLY?(m,
97
+ :error => m2,
98
+ 'a' => :a, 'A' => :a,
99
+ /b/i => :b,
100
+ /c/ => :c, 'C' => :c,
101
+ :tries => 4,
102
+ :in => i, :out => o)
103
+ end
104
+ end
105
+
106
+ def test_srsly_nil
107
+ i, o = io_procs
108
+
109
+ m = "nil?"
110
+ o.expects(:call).times(1).with(m).returns(m)
111
+ o.expects(:call).times(1).with($/).returns($/)
112
+ i.expects(:call).times(1).returns(nil)
113
+ assert_equal :nil, SRSLY?(m, nil => :nil, :in => i, :out => o)
114
+ end
115
+
116
+ def test_srsly_error_proc
117
+ i, o = io_procs
118
+
119
+ m = "Go!"
120
+ o.expects(:call).times(1).with(m).returns(m)
121
+ i.expects(:call).times(4).returns("???", "???", "???", "ok")
122
+
123
+ cnt = 0
124
+ assert_equal :ok,
125
+ SRSLY?(m, :error => proc { |err, t, tot|
126
+ cnt += 1
127
+ assert_equal '???', err
128
+ assert_equal cnt + 1, t
129
+ assert_equal 5, tot
130
+ }, :tries => 5, 'ok' => :ok, :in => i, :out => o)
131
+
132
+ assert_equal 3, cnt
133
+ end
134
+
135
+ private
136
+ def io_procs
137
+ Array.new(2) {
138
+ mock.tap { |m| m.expects(:is_a?).with(Proc).returns(true).once }
139
+ }
140
+ end
141
+
142
+ def io_streams
143
+ [
144
+ mock.tap { |m| m.expects(:respond_to?).with(:gets).returns(true).once },
145
+ mock.tap { |m| m.expects(:respond_to?).with(:write).returns(true).once }
146
+ ]
147
+ end
148
+ end
metadata ADDED
@@ -0,0 +1,86 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: srsly
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Junegunn Choi
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-02-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: minitest
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: mocha
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: SRSLY?
47
+ email:
48
+ - junegunn.c@gmail.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/srsly.rb
59
+ - srsly.gemspec
60
+ - test/test_srsly.rb
61
+ homepage: https://github.com/junegunn/srsly
62
+ licenses: []
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ none: false
69
+ requirements:
70
+ - - ! '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 1.8.24
82
+ signing_key:
83
+ specification_version: 3
84
+ summary: SRSLY?
85
+ test_files:
86
+ - test/test_srsly.rb