rdialogy 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.
Files changed (48) hide show
  1. data/History.txt +6 -0
  2. data/Manifest.txt +47 -0
  3. data/README.txt +60 -0
  4. data/Rakefile +19 -0
  5. data/examples/calendar.rb +5 -0
  6. data/examples/checklist.rb +8 -0
  7. data/examples/dselect.rb +5 -0
  8. data/examples/form.rb +11 -0
  9. data/examples/gauge.rb +17 -0
  10. data/examples/inputmenu.rb +9 -0
  11. data/examples/menu.rb +9 -0
  12. data/examples/message_box.rb +5 -0
  13. data/examples/mixedgauge.rb +21 -0
  14. data/examples/passwordbox.rb +0 -0
  15. data/lib/rdialogy/base.rb +67 -0
  16. data/lib/rdialogy/calendar.rb +46 -0
  17. data/lib/rdialogy/checklist.rb +56 -0
  18. data/lib/rdialogy/dselect.rb +39 -0
  19. data/lib/rdialogy/editbox.rb +31 -0
  20. data/lib/rdialogy/filepath_widget.rb +30 -0
  21. data/lib/rdialogy/form.rb +67 -0
  22. data/lib/rdialogy/form_field.rb +8 -0
  23. data/lib/rdialogy/fselect.rb +35 -0
  24. data/lib/rdialogy/gauge.rb +56 -0
  25. data/lib/rdialogy/infobox.rb +10 -0
  26. data/lib/rdialogy/inputbox.rb +16 -0
  27. data/lib/rdialogy/inputmenu.rb +41 -0
  28. data/lib/rdialogy/menu.rb +22 -0
  29. data/lib/rdialogy/menu_item.rb +2 -0
  30. data/lib/rdialogy/message_box.rb +10 -0
  31. data/lib/rdialogy/mixedgauge.rb +32 -0
  32. data/lib/rdialogy/passwordbox.rb +15 -0
  33. data/lib/rdialogy.rb +21 -0
  34. data/spec/base_spec.rb +30 -0
  35. data/spec/calendar_spec.rb +41 -0
  36. data/spec/checklist_spec.rb +41 -0
  37. data/spec/dselect_spec.rb +38 -0
  38. data/spec/form_spec.rb +47 -0
  39. data/spec/fselect_spec.rb +11 -0
  40. data/spec/infobox_spec.rb +18 -0
  41. data/spec/inputbox_spec.rb +30 -0
  42. data/spec/inputmenu_spec.rb +56 -0
  43. data/spec/menu_spec.rb +41 -0
  44. data/spec/message_box_spec.rb +18 -0
  45. data/spec/mixedgauge_spec.rb +0 -0
  46. data/spec/passwordbox_spec.rb +29 -0
  47. data/spec/spec_helper.rb +6 -0
  48. metadata +139 -0
@@ -0,0 +1,10 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ module RDialogy
4
+ class InfoBox < Base
5
+ private
6
+ def self.command
7
+ 'infobox'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,16 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ module RDialogy
4
+ class InputBox < Base
5
+ private
6
+ def self.command
7
+ 'inputbox'
8
+ end
9
+
10
+ def self.args(options={})
11
+ base_args = super
12
+ base_args << options[:init] unless options[:init].nil?
13
+ base_args
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,41 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+ require File.dirname(__FILE__) + '/menu_item'
3
+
4
+ module RDialogy
5
+ class InputMenu < Base
6
+
7
+ def self.run(options={})
8
+ super options, true do |input|
9
+ if input =~ /^RENAMED/
10
+ input.split(' ')[1..-1]
11
+ else
12
+ input.chomp
13
+ end
14
+ end
15
+ end
16
+
17
+ # Formats the hash into an ordered list, valid options are:
18
+ # * :text - Title of the widget
19
+ # * :width
20
+ # * :height
21
+ # * :list_height - Number of items to display in the list
22
+ # * :items - Array of MenuItem
23
+
24
+ def self.args(options={})
25
+ options[:items] ||= []
26
+ options[:list_height] ||= options[:items].count * 3
27
+
28
+ options[:items].map! do |item|
29
+ [item.tag, item.item]
30
+ end
31
+
32
+ super + [options[:list_height]] + options[:items].flatten
33
+ end
34
+
35
+ # Maps to the appropriate dialog argument
36
+ def self.command
37
+ 'inputmenu'
38
+ end
39
+
40
+ end
41
+ end
@@ -0,0 +1,22 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+ require File.dirname(__FILE__) + '/menu_item'
3
+
4
+ module RDialogy
5
+ class Menu < Base
6
+ def self.run(options={})
7
+ super options, true do |input|
8
+ input.chomp
9
+ end
10
+ end
11
+
12
+ def self.command
13
+ 'menu'
14
+ end
15
+
16
+ def self.args(options={})
17
+ options[:items] ||= []
18
+ options[:list_height] ||= options[:items].count
19
+ super + [options[:list_height]] + options[:items].map{|e| [e.tag, e.item] }.flatten
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,2 @@
1
+ class MenuItem < Struct.new(:tag, :item, :status)
2
+ end
@@ -0,0 +1,10 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ module RDialogy
4
+ class MessageBox < Base
5
+ private
6
+ def self.command
7
+ 'msgbox'
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,32 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+ require File.dirname(__FILE__) + '/menu_item'
3
+
4
+ # A mixedgauge box displays a meter along the bottom of the box. The meter indicates the percentage.
5
+ # It also displays a list of the tag- and item-values at the top of the box.
6
+ # The text is shown as a caption between the list and meter. The :percent value denotes the initial percentage shown in the
7
+ # meter.
8
+ # MixedGauge does not provide any method for updating the percentage in a single instance as Gauge does
9
+ # The widget accepts no input and returns true.
10
+
11
+ module RDialogy
12
+
13
+ class MixedGauge < Base
14
+ # Valid options:
15
+ # * :text,
16
+ # * :width,
17
+ # * :height,
18
+ # * :percent - The percentage progress indicated by the gauge.
19
+ # * :items - An array of MenuItem objects
20
+
21
+ def self.args(options={})
22
+ options[:percent] ||= 0
23
+ options[:items] ||= []
24
+ super + [options[:percent].to_i] + options[:items].map{|e| [e.tag, e.item] }.flatten
25
+ end
26
+
27
+ def self.command
28
+ 'mixedgauge'
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ module RDialogy
4
+ # A password box is similar to an input box, except that the text the user enters is not displayed. This is useful when
5
+ # prompting for passwords or other sensitive information.
6
+ class PasswordBox < Base
7
+ def self.run(options={})
8
+ super options, true
9
+ end
10
+
11
+ def self.command
12
+ 'passwordbox'
13
+ end
14
+ end
15
+ end
data/lib/rdialogy.rb ADDED
@@ -0,0 +1,21 @@
1
+ class Rdialogy
2
+ VERSION = '0.1.0'
3
+ end
4
+
5
+ $LOAD_PATH << File.expand_path(File.dirname(__FILE__))
6
+
7
+ require 'rdialogy/base'
8
+ require 'rdialogy/message_box'
9
+ require 'rdialogy/calendar'
10
+ require 'rdialogy/checklist'
11
+ require 'rdialogy/dselect'
12
+ require 'rdialogy/editbox'
13
+ require 'rdialogy/form'
14
+ require 'rdialogy/gauge'
15
+ require 'rdialogy/inputbox'
16
+ require 'rdialogy/inputmenu'
17
+ require 'rdialogy/infobox'
18
+ require 'rdialogy/fselect'
19
+ require 'rdialogy/menu'
20
+ require 'rdialogy/mixedgauge'
21
+ require 'rdialogy/passwordbox'
data/spec/base_spec.rb ADDED
@@ -0,0 +1,30 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+
3
+ spec_require __FILE__
4
+
5
+ describe RDialogy::Base do
6
+ describe ".command" do
7
+ it "should raise an error" do
8
+ lambda { RDialogy::Base.command }.should raise_error
9
+ end
10
+ end
11
+
12
+ describe ".args" do
13
+ describe "without any options supplied" do
14
+ it "should return an array of defaults" do
15
+ RDialogy::Base.args.should == ['default title', 0, 0]
16
+ end
17
+ end
18
+ end
19
+
20
+ describe ".add_quotes" do
21
+ it "should return quoted strings" do
22
+ RDialogy::Base.add_quotes([1, 2]).should == ["'1'", "'2'"]
23
+ end
24
+
25
+ it "should escape single quotes inside the strings" do
26
+ RDialogy::Base.add_quotes(["Kitten's feet"]).should == ["'Kitten'\\''s feet'"]
27
+ end
28
+ end
29
+ end
30
+
@@ -0,0 +1,41 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+
3
+ spec_require __FILE__
4
+
5
+ describe RDialogy::Calendar do
6
+ describe ".command" do
7
+ it "should return calendar" do
8
+ RDialogy::Calendar.command.should == 'calendar'
9
+ end
10
+ end
11
+
12
+ describe ".run" do
13
+ before :each do
14
+ require 'stringio'
15
+
16
+ data = StringIO.new "01/05/2010\n"
17
+ tmp = mock('tempfile')
18
+ Tempfile.stub!(:new).and_return(tmp)
19
+ tmp.stub!(:path).and_return('/tmp/tempfile')
20
+ tmp.stub!(:readline).and_return { data.readline }
21
+ tmp.stub!(:close)
22
+
23
+ @expectation = "dialog --calendar 'hello world' '0' '0'"
24
+ end
25
+
26
+ describe "with no date arguments" do
27
+ it "should call external dialog" do
28
+ RDialogy::Calendar.should_receive(:system).with(/#{@expectation}/)
29
+ RDialogy::Calendar.run(:text => 'hello world').should == Time.mktime(2010, 5, 1)
30
+ end
31
+ end
32
+
33
+ describe "with date arguments" do
34
+ it "should call external dialog with the date arguments after the normal ones" do
35
+ RDialogy::Calendar.should_receive(:system).with(/#{@expectation} '17' '11' '1985'/)
36
+ args = { :text => 'hello world', :year => 1985, :month => 11, :day => 17 }
37
+ RDialogy::Calendar.run(args)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,41 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+
3
+ spec_require __FILE__
4
+
5
+ describe RDialogy::Checklist do
6
+ describe ".command" do
7
+ it "should return checklist" do
8
+ RDialogy::Checklist.command.should == 'checklist'
9
+ end
10
+ end
11
+
12
+ describe ".run" do
13
+ before :each do
14
+ require 'stringio'
15
+
16
+ data = StringIO.new '"bread" "milk"' + "\n"
17
+
18
+ tmp = mock('tempfile')
19
+ Tempfile.stub!(:new).and_return(tmp)
20
+ tmp.stub!(:path).and_return('/tmp/tempfile')
21
+ tmp.stub!(:readline).and_return { data.readline }
22
+ tmp.stub!(:close)
23
+
24
+ @items = %w(bread milk cheese).map{|e| MenuItem.new(e, e)}
25
+ @expectation = "dialog --checklist 'hello world' '0' '0' '3'"
26
+ end
27
+
28
+ describe "with no :list_height" do
29
+ it "should use the number of _items_ as list height" do
30
+ RDialogy::Checklist.should_receive(:system).with(/#{@expectation}/)
31
+ RDialogy::Checklist.run(:text => 'hello world', :items => @items)
32
+ end
33
+ end
34
+
35
+ it "should return an array of the selected tags" do
36
+ args = { :text => 'hello world', :items => @items }
37
+ RDialogy::Checklist.should_receive(:system).with(/#{@expectation}/)
38
+ RDialogy::Checklist.run(args).should == ['bread', 'milk']
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,38 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+
3
+ spec_require __FILE__
4
+
5
+ describe RDialogy::DSelect do
6
+ describe ".command" do
7
+ it "should return 'dselect'" do
8
+ RDialogy::DSelect.command.should == 'dselect'
9
+ end
10
+ end
11
+
12
+ describe ".run" do
13
+ before :each do
14
+ require 'stringio'
15
+
16
+ data = StringIO.new '/var/www/Eliza_Dushku'
17
+
18
+ tmp = mock('tempfile')
19
+ Tempfile.stub!(:new).and_return(tmp)
20
+ tmp.stub!(:path).and_return('/tmp/tempfile')
21
+ tmp.stub!(:readline).and_return { data.readline }
22
+ tmp.stub!(:close)
23
+
24
+ @expectation = "dialog --dselect '/var/www' '0' '0'"
25
+ end
26
+
27
+ it "should use :path as the first argument" do
28
+ RDialogy::DSelect.should_receive(:system).with(/#{@expectation}/)
29
+ RDialogy::DSelect.run(:path => '/var/www')
30
+ end
31
+
32
+ it "should return a string" do
33
+ args = { :path => '/var/www' }
34
+ RDialogy::DSelect.should_receive(:system).with(/#{@expectation}/)
35
+ RDialogy::DSelect.run(args).should == '/var/www/Eliza_Dushku'
36
+ end
37
+ end
38
+ end
data/spec/form_spec.rb ADDED
@@ -0,0 +1,47 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+
3
+ spec_require __FILE__
4
+
5
+ describe RDialogy::Form do
6
+ describe ".command" do
7
+ it 'should return "form"' do
8
+ RDialogy::Form.command.should == 'form'
9
+ end
10
+ end
11
+
12
+ describe ".run" do
13
+ before :each do
14
+ require 'stringio'
15
+
16
+ data = StringIO.new "Paul\n42\n"
17
+
18
+ tmp = mock('tempfile')
19
+ Tempfile.stub!(:new).and_return(tmp)
20
+ tmp.stub!(:path).and_return('/tmp/tempfile')
21
+ tmp.stub!(:readline).and_return { data.readline }
22
+ tmp.stub!(:close)
23
+
24
+ @items = [
25
+ ['name', 1, 1, '', 1, 5, 5, 10],
26
+ ['age', 2, 1, '', 2, 5, 5, 10]
27
+ ]
28
+ @expectation = "dialog --form 'hello world' '0' '0' '2' " +
29
+ @items.flatten.map{|e| "'#{e}'" }.join(' ')
30
+
31
+ @items.map!{|e| FormField.new(*e)}
32
+ end
33
+
34
+ describe "with no :list_height" do
35
+ it "should use the number of _items_ as list height" do
36
+ RDialogy::Form.should_receive(:system).with(/#{@expectation}/)
37
+ RDialogy::Form.run(:text => 'hello world', :items => @items)
38
+ end
39
+ end
40
+
41
+ it "should return an hash of the user inputs" do
42
+ args = { :text => 'hello world', :items => @items }
43
+ RDialogy::Form.should_receive(:system).with(/#{@expectation}/)
44
+ RDialogy::Form.run(args).should == {'name' => 'Paul', 'age' => '42' }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,11 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+
3
+ spec_require __FILE__
4
+
5
+ describe RDialogy::FSelect do
6
+ describe ".command" do
7
+ it "should return 'fselect'" do
8
+ RDialogy::FSelect.command.should == 'fselect'
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,18 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+
3
+ spec_require __FILE__
4
+
5
+ describe RDialogy::InfoBox do
6
+ describe ".command" do
7
+ it "should return infobox" do
8
+ RDialogy::InfoBox.command.should == 'infobox'
9
+ end
10
+ end
11
+
12
+ describe ".run" do
13
+ it "should return dialog run string" do
14
+ RDialogy::InfoBox.should_receive(:system).with("dialog --infobox 'hello world' '0' '0'")
15
+ RDialogy::InfoBox.run(:text => 'hello world')
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,30 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+
3
+ spec_require __FILE__
4
+
5
+ describe RDialogy::InputBox do
6
+ describe ".command" do
7
+ it "should return inputbox" do
8
+ RDialogy::InputBox.command.should == 'inputbox'
9
+ end
10
+ end
11
+
12
+ describe ".args" do
13
+ before :each do
14
+ @args = { :text => "Hello", :height => 4, :width => 5 }
15
+ end
16
+
17
+ describe "when :init is not an option" do
18
+ it "should call dialog with base args only" do
19
+ RDialogy::InputBox.args(@args).should == ['Hello', 4, 5]
20
+ end
21
+ end
22
+
23
+ describe "when :init is an option" do
24
+ it "should call dialog with base args + the value of init" do
25
+ @args[:init] = "Catfish"
26
+ RDialogy::InputBox.args(@args).should == ['Hello', 4, 5, 'Catfish']
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,56 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+ require 'stringio'
3
+
4
+ spec_require __FILE__
5
+
6
+ describe RDialogy::InputMenu do
7
+ describe ".command" do
8
+ it "should return inputmenu" do
9
+ RDialogy::InputMenu.command.should == 'inputmenu'
10
+ end
11
+ end
12
+
13
+ describe ".run" do
14
+ def stub_tempfile(value)
15
+ data = StringIO.new value
16
+
17
+ tmp = mock('tempfile')
18
+ Tempfile.stub!(:new).and_return(tmp)
19
+ tmp.stub!(:path).and_return('/tmp/tempfile')
20
+ tmp.stub!(:readline).and_return { data.readline }
21
+ tmp.stub!(:close)
22
+ end
23
+
24
+ before :each do
25
+ @items = %w(grapes bread milk cheese).map{|e| MenuItem.new(e, e)}
26
+ RDialogy::InputMenu.stub!(:system)
27
+ end
28
+
29
+ describe "when the user doesn't rename an object" do
30
+ it "should return the tag of the object as a string" do
31
+ stub_tempfile "grapes\n"
32
+ RDialogy::InputMenu.run(:text => 'hello world', :items => @items).should == 'grapes'
33
+ end
34
+ end
35
+
36
+ describe "when the user does rename an object" do
37
+ it "should return an array containing the tag and the new value" do
38
+ stub_tempfile "RENAMED grapes sultanas\n"
39
+ RDialogy::InputMenu.run(:text => 'hi', :items => @items).should == ['grapes', 'sultanas']
40
+ end
41
+ end
42
+ end
43
+
44
+ describe ".args" do
45
+ before :each do
46
+ @items = %w(grapes bread milk cheese).map{|e| MenuItem.new(e, e)}
47
+ @args = {:text => "hello world", :items => @items }
48
+ end
49
+
50
+ describe "list height" do
51
+ it "should be 3 times the number of list elements" do
52
+ RDialogy::InputMenu.args(@args)[3].should == 12
53
+ end
54
+ end
55
+ end
56
+ end
data/spec/menu_spec.rb ADDED
@@ -0,0 +1,41 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+
3
+ spec_require __FILE__
4
+
5
+ describe RDialogy::Menu do
6
+ describe ".command" do
7
+ it "should return menu" do
8
+ RDialogy::Menu.command.should == 'menu'
9
+ end
10
+ end
11
+
12
+ describe ".run" do
13
+ before :each do
14
+ require 'stringio'
15
+
16
+ data = StringIO.new 'bread' + "\n"
17
+
18
+ tmp = mock('tempfile')
19
+ Tempfile.stub!(:new).and_return(tmp)
20
+ tmp.stub!(:path).and_return('/tmp/tempfile')
21
+ tmp.stub!(:readline).and_return { data.readline }
22
+ tmp.stub!(:close)
23
+
24
+ @items = %w(bread milk cheese).map{|e| MenuItem.new(e, e)}
25
+ @expectation = "dialog --menu 'hello world' '0' '0' '3'"
26
+ end
27
+
28
+ describe "with no :list_height" do
29
+ it "should use the number of _items_ as list height" do
30
+ RDialogy::Menu.should_receive(:system).with(/#{@expectation}/)
31
+ RDialogy::Menu.run(:text => 'hello world', :items => @items)
32
+ end
33
+ end
34
+
35
+ it "should return a string that is the tag of the selected item" do
36
+ args = { :text => 'hello world', :items => @items }
37
+ RDialogy::Menu.should_receive(:system).with(/#{@expectation}/)
38
+ RDialogy::Menu.run(args).should == 'bread'
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,18 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+
3
+ spec_require __FILE__
4
+
5
+ describe RDialogy::MessageBox do
6
+ describe ".command" do
7
+ it "should return msgbox" do
8
+ RDialogy::MessageBox.command.should == 'msgbox'
9
+ end
10
+ end
11
+
12
+ describe ".run" do
13
+ it "should return dialog run string" do
14
+ RDialogy::MessageBox.should_receive(:system).with("dialog --msgbox 'hello world' '0' '0'")
15
+ RDialogy::MessageBox.run(:text => 'hello world')
16
+ end
17
+ end
18
+ end
File without changes
@@ -0,0 +1,29 @@
1
+ require [File.dirname(__FILE__), 'spec_helper'].join '/'
2
+
3
+ spec_require __FILE__
4
+
5
+ describe RDialogy::PasswordBox do
6
+ describe ".command" do
7
+ it "should return passwordbox" do
8
+ RDialogy::PasswordBox.command.should == 'passwordbox'
9
+ end
10
+ end
11
+
12
+ describe ".run" do
13
+ it "should return the string that is input by the user" do
14
+ require 'stringio'
15
+
16
+ data = StringIO.new "sexmachine41"
17
+
18
+ tmp = mock('tempfile')
19
+ Tempfile.stub!(:new).and_return(tmp)
20
+ tmp.stub!(:path).and_return('/tmp/tempfile')
21
+ tmp.stub!(:readline).and_return { data.readline }
22
+ tmp.stub!(:close)
23
+
24
+ expectation = /--passwordbox 'Enter password'/
25
+ RDialogy::PasswordBox.should_receive(:system).with(expectation)
26
+ RDialogy::PasswordBox.run(:text => "Enter password").should == "sexmachine41"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,6 @@
1
+ $LOAD_PATH << File.dirname(__FILE__)+'/../lib/rdialogy'
2
+
3
+ def spec_require(file)
4
+ #puts $LOAD_PATH.inspect
5
+ require file.scan(/.*\/(.*)_spec.rb/).flatten.first
6
+ end