menu 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require 'rubygems' unless Object.const_defined?(:Gem)
3
+ require File.dirname(__FILE__) + "/lib/menu/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "menu"
7
+ s.version = Menu::VERSION
8
+ s.authors = ["Gabriel Horner"]
9
+ s.email = "gabriel.horner@gmail.com"
10
+ s.homepage = "http://github.com/cldwalker/menu"
11
+ s.summary = "More choices with less typing"
12
+ s.description = "A simple *nix command which takes arguments or stdin and provides a menu to print chosen lines."
13
+ s.required_rubygems_version = ">= 1.3.6"
14
+ s.rubyforge_project = 'tagaholic'
15
+ s.add_development_dependency 'bacon', '>= 1.1.0'
16
+ s.add_development_dependency 'rr', '>= 1.0'
17
+ s.add_development_dependency 'bacon-bits'
18
+ s.add_development_dependency 'bacon-rr'
19
+ s.files = Dir.glob(%w[{lib,test}/**/*.rb bin/* [A-Z]*.{txt,rdoc} ext/**/*.{rb,c} **/deps.rip]) + %w{Rakefile .gemspec}
20
+ s.extra_rdoc_files = ["README.rdoc", "LICENSE.txt"]
21
+ s.license = 'MIT'
22
+ end
@@ -0,0 +1,2 @@
1
+ == 0.1.0
2
+ * Initial release!
@@ -0,0 +1,22 @@
1
+ The MIT LICENSE
2
+
3
+ Copyright (c) 2010 Gabriel Horner
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.
@@ -0,0 +1,12 @@
1
+ == Description
2
+
3
+ A simple *nix command which takes arguments or stdin and provides a menu to print chosen lines.
4
+
5
+ == Todo
6
+
7
+ * handle multiple pipes
8
+ * add help and other basic options
9
+ * manpage
10
+ * noninteractive mode
11
+ * action menu - hirb style
12
+ * negation choosing
@@ -0,0 +1,35 @@
1
+ require 'rake'
2
+ require 'fileutils'
3
+
4
+ def gemspec
5
+ @gemspec ||= eval(File.read('.gemspec'), binding, '.gemspec')
6
+ end
7
+
8
+ desc "Build the gem"
9
+ task :gem=>:gemspec do
10
+ sh "gem build .gemspec"
11
+ FileUtils.mkdir_p 'pkg'
12
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", 'pkg'
13
+ end
14
+
15
+ desc "Install the gem locally"
16
+ task :install => :gem do
17
+ sh %{gem install pkg/#{gemspec.name}-#{gemspec.version}}
18
+ end
19
+
20
+ desc "Generate the gemspec"
21
+ task :generate do
22
+ puts gemspec.to_ruby
23
+ end
24
+
25
+ desc "Validate the gemspec"
26
+ task :gemspec do
27
+ gemspec.validate
28
+ end
29
+
30
+ desc 'Run tests'
31
+ task :test do |t|
32
+ sh 'bacon -q -Ilib -I. test/*_test.rb'
33
+ end
34
+
35
+ task :default => :test
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'menu'
4
+ Menu.run
@@ -0,0 +1,44 @@
1
+ require 'menu/version'
2
+
3
+ module Menu
4
+ extend self
5
+
6
+ def run(lines=ARGV)
7
+ lines = $stdin.read.split("\n") if !$stdin.tty?
8
+ prompt(lines)
9
+ answer = ask
10
+ puts parse_answer(lines, answer)
11
+ end
12
+
13
+ def prompt(lines)
14
+ ljust_size = lines.size.to_s.size + 1
15
+ lines.each_with_index {|obj,i|
16
+ puts "#{i+1}.".ljust(ljust_size) + " " +obj
17
+ }
18
+ print "\nSpecify individual choices (4,7), range of choices (1-3)" +
19
+ " or all choices (*).\nChoose: "
20
+ end
21
+
22
+ def ask
23
+ $stdin.reopen '/dev/tty'
24
+ $stdin.gets.chomp
25
+ end
26
+
27
+ def parse_answer(array, input)
28
+ return array if input.strip == '*'
29
+ result = []
30
+ input.split(/\s*,\s*/).each do |e|
31
+ if e[/(\d+)-(\d+)/]
32
+ min, max = $1, $2
33
+ slice_min = min.to_i - 1
34
+ result.push(*array.slice(slice_min, max.to_i - min.to_i + 1))
35
+ elsif e[/^(\d+)$/]
36
+ index = $1.to_i - 1
37
+ result.push(array[index]) if array[index]
38
+ else
39
+ abort("`#{e}' is an invalid choice.")
40
+ end
41
+ end
42
+ result
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module Menu
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,4 @@
1
+ bacon >=1.1.0
2
+ rr >=1.0
3
+ bacon-bits >=0
4
+ bacon-rr >=0
@@ -0,0 +1,83 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ describe "Menu.run" do
4
+ def menu(args)
5
+ Menu.run(args)
6
+ end
7
+
8
+ shared "parses choices" do
9
+ it 'parses empty choice' do
10
+ mock($stdin).gets { '' }
11
+ mock(Menu).puts []
12
+ menu @args
13
+ end
14
+
15
+ it "parses single choice" do
16
+ mock($stdin).gets { '1' }
17
+ mock(Menu).puts ['uno']
18
+ menu @args
19
+ end
20
+
21
+ it "parses hyphenated choice" do
22
+ mock($stdin).gets { '1-2' }
23
+ mock(Menu).puts ['uno','dos']
24
+ menu @args
25
+ end
26
+
27
+ it "parses comma separated choice" do
28
+ mock($stdin).gets { '1,3' }
29
+ mock(Menu).puts ['uno','tres']
30
+ menu @args
31
+ end
32
+
33
+ it "parses asterisk as all choices" do
34
+ mock($stdin).gets { '*' }
35
+ mock(Menu).puts ['uno','dos', 'tres']
36
+ menu @args
37
+ end
38
+
39
+ it "aborts with invalid choice" do
40
+ mock($stdin).gets { 'a' }
41
+ mock(Menu).abort(/a' is an invalid choice/) { raise RuntimeError }
42
+ menu(@args) rescue nil
43
+ end
44
+
45
+ it "aborts a negative number choice" do
46
+ mock($stdin).gets { '-4' }
47
+ mock(Menu).abort(/-4' is an invalid choice/) { raise RuntimeError }
48
+ menu(@args) rescue nil
49
+ end
50
+ end
51
+
52
+ describe "with commandline arguments" do
53
+ before do
54
+ mock($stdin).tty? { true }
55
+ ['1. uno', '2. dos', '3. tres'].each {|e| mock(Menu).puts e }
56
+ mock(Menu).print(anything)
57
+ @args = %w{uno dos tres}
58
+ end
59
+
60
+ behaves_like "parses choices"
61
+ end
62
+
63
+ it "displays choices on same column" do
64
+ mock($stdin).tty? { true }
65
+ mock(Menu).print anything
66
+ ['1. 1', '2. 2', '3. 3', '4. 4', '5. 5', '6. 6', '7. 7', '8. 8',
67
+ '9. 9', '10. 10'].each {|e| mock(Menu).puts e }
68
+ mock($stdin).gets {'' }
69
+ mock(Menu).puts []
70
+ menu (1..10).to_a.map {|e| e.to_s }
71
+ end
72
+
73
+ describe "with stdin" do
74
+ before do
75
+ mock($stdin).tty? { false }
76
+ mock($stdin).read { "uno\ndos\ntres" }
77
+ ['1. uno', '2. dos', '3. tres'].each {|e| mock(Menu).puts e }
78
+ mock(Menu).print(anything)
79
+ end
80
+
81
+ behaves_like "parses choices"
82
+ end
83
+ end
@@ -0,0 +1,5 @@
1
+ require 'bacon'
2
+ require 'bacon/bits'
3
+ require 'rr'
4
+ require 'bacon/rr'
5
+ require 'menu'
metadata ADDED
@@ -0,0 +1,138 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: menu
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Gabriel Horner
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-09-16 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bacon
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 19
30
+ segments:
31
+ - 1
32
+ - 1
33
+ - 0
34
+ version: 1.1.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rr
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 15
46
+ segments:
47
+ - 1
48
+ - 0
49
+ version: "1.0"
50
+ type: :development
51
+ version_requirements: *id002
52
+ - !ruby/object:Gem::Dependency
53
+ name: bacon-bits
54
+ prerelease: false
55
+ requirement: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
63
+ version: "0"
64
+ type: :development
65
+ version_requirements: *id003
66
+ - !ruby/object:Gem::Dependency
67
+ name: bacon-rr
68
+ prerelease: false
69
+ requirement: &id004 !ruby/object:Gem::Requirement
70
+ none: false
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ hash: 3
75
+ segments:
76
+ - 0
77
+ version: "0"
78
+ type: :development
79
+ version_requirements: *id004
80
+ description: A simple *nix command which takes arguments or stdin and provides a menu to print chosen lines.
81
+ email: gabriel.horner@gmail.com
82
+ executables: []
83
+
84
+ extensions: []
85
+
86
+ extra_rdoc_files:
87
+ - README.rdoc
88
+ - LICENSE.txt
89
+ files:
90
+ - lib/menu/version.rb
91
+ - lib/menu.rb
92
+ - test/menu_test.rb
93
+ - test/test_helper.rb
94
+ - bin/menu
95
+ - LICENSE.txt
96
+ - CHANGELOG.rdoc
97
+ - README.rdoc
98
+ - test/deps.rip
99
+ - Rakefile
100
+ - .gemspec
101
+ has_rdoc: true
102
+ homepage: http://github.com/cldwalker/menu
103
+ licenses:
104
+ - MIT
105
+ post_install_message:
106
+ rdoc_options: []
107
+
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ none: false
112
+ requirements:
113
+ - - ">="
114
+ - !ruby/object:Gem::Version
115
+ hash: 3
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ required_rubygems_version: !ruby/object:Gem::Requirement
120
+ none: false
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ hash: 23
125
+ segments:
126
+ - 1
127
+ - 3
128
+ - 6
129
+ version: 1.3.6
130
+ requirements: []
131
+
132
+ rubyforge_project: tagaholic
133
+ rubygems_version: 1.3.7
134
+ signing_key:
135
+ specification_version: 3
136
+ summary: More choices with less typing
137
+ test_files: []
138
+