palaver 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012, Peter Allin <peter@peca.dk>
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted
5
+ provided that the following conditions are met:
6
+
7
+ - Redistributions of source code must retain the above copyright notice, this list of conditions and
8
+ the following disclaimer.
9
+ - Redistributions in binary form must reproduce the above copyright notice, this list of conditions
10
+ and the following disclaimer in the documentation and/or other materials provided with the
11
+ distribution.
12
+
13
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
14
+ IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
15
+ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
16
+ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
18
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
19
+ IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
20
+ THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,16 @@
1
+ # Palaver -- A 'dialog' API for Ruby
2
+
3
+ Palaver is a ruby library for making text user interfaces based on the program "dialog"
4
+ (http://invisible-island.net/dialog/). It aims at making all the features of the "dialog" program
5
+ available for Ruby programs.
6
+
7
+ The project was started because the RDialog (http://rdialog.rubyforge.org/) project with similar
8
+ aims seemed idle and missed some features that I needed. I also felt that RDialogs API was not a
9
+ very good match for some of the more complex dialogs supported by the "dialog" program.
10
+
11
+ In order to scratch these itches I have implemented, what I call the "blocks based API", in
12
+ Palaver. This API works by passing a Ruby block to a factory method, the function calls in this
13
+ block then defines the dialog to be created. I believe this kind of API is well suited for things
14
+ like the "menu" and "form" dialogs from the "dialog" program. For the simpler dialogs a more
15
+ conventional API where the factory methods are given hashes containing parameter to value mappings
16
+ is implemented. The examples in the "examples" directory show how both kind of APIs can be used.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rake'
2
+ require 'rubygems/package_task'
3
+
4
+ gemspec = eval(File.read("palaver.gemspec"))
5
+
6
+ desc "Validate the gemspec"
7
+ task :gemspec do
8
+ gemspec.validate
9
+ end
10
+
11
+ desc "Build gem locally"
12
+ task :build => :gemspec do
13
+ system "gem build #{gemspec.name}.gemspec"
14
+ FileUtils.mkdir "pkg" unless File.exists? "pkg"
15
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", "pkg"
16
+ end
17
+
18
+ desc "Clean automatically generated files"
19
+ task :clean do
20
+ FileUtils.rm_rf "pkg"
21
+ end
@@ -0,0 +1,14 @@
1
+ require 'palaver'
2
+ require 'date'
3
+
4
+ dialog_factory = Palaver::DialogFactory.new
5
+
6
+ # Using the hash based API with the calendar dialog
7
+ start_date = dialog_factory.calendar(:text => "Choose the start date", :date => Date.new(1939,9,1)).show
8
+
9
+ # Using the block based API with the calendar dialog. This probably doesn'palaver'
10
+ # simple widget like the calendar.
11
+ end_date = dialog_factory.calendar { text "Choose the end date"; date Date.new(1945,9,2); }.show
12
+
13
+ puts start_date
14
+ puts end_date
@@ -0,0 +1,28 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ chosen1 = dialog_factory.checklist(:text => "Which do you want, hash style?",
6
+ :options => [
7
+ [ "fisk", "A confusing choice for a metasyntactic variable name", :off ],
8
+ [ "foo", "The classic first metasyntactic variable name", :on ],
9
+ [ "bar", "The classic second metasyntactic variable name", :off ],
10
+ [ "baz", "The classic third metasyntactic variable name", :off ],
11
+ [ "quux", "A quite unusual metasyntactic variable name", :off ]
12
+ ]).show
13
+
14
+ chosen2 = dialog_factory.checklist {
15
+ text "Which do you want, block style?"
16
+ option "fisk", "A confusing choice for a metasyntactic variable name"
17
+ option "foo", "The classic first metasyntactic variable name", :on
18
+ option "bar", "The classic second metasyntactic variable name", :on
19
+ option "baz", "The classic third metasyntactic variable name", :on
20
+ option "quux", "A quite unusual metasyntactic variable name"
21
+ }.show
22
+
23
+ puts "Chosen from the dialog created with the hash style:"
24
+ puts chosen1
25
+ puts
26
+ puts "Chosen from the dialog created with the block style"
27
+ puts chosen2
28
+
@@ -0,0 +1,13 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ # Using the hash based API for requesting the user to choose a directory
6
+ dir_hash = dialog_factory.dselect(:path => "/").show
7
+
8
+ # Using the block based API for requesting the user to choose a directory
9
+ dir_block = dialog_factory.dselect { path "/" }.show
10
+
11
+ puts "Dir chosen with hash method #{dir_hash}"
12
+ puts "Dir chosen with block method #{dir_block}"
13
+
data/examples/form.rb ADDED
@@ -0,0 +1,16 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ answers = dialog_factory.form {
6
+ text "Foos and their texts"
7
+ height 10
8
+ form_height 5
9
+ entry :first, "First foo", 1, 0, "Default text for first foo", 1, 15, 20, 200
10
+ entry :second, "Second foo", 1, 40, "Default text for second foo", 1, 55, 20, 200
11
+ entry :third, "Third foo", 3, 0, "Default text for third foo", 3, 15, 20, 200
12
+ }.show
13
+
14
+ answers.keys.each do |k|
15
+ puts "#{k}: #{answers[k]}"
16
+ end
@@ -0,0 +1,18 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ answers = dialog_factory.form {
6
+ text "Simple-entry foos and their texts"
7
+ height 10
8
+ form_height 5
9
+ simple_display_width 30
10
+ simple_text_length 200
11
+ simple_entry :first, "First foo", "Default text for first foo"
12
+ simple_entry :second, "Second foo", "Default text for second foo"
13
+ simple_entry :third, "Third foo", "Default text for third foo"
14
+ }.show
15
+
16
+ answers.keys.each do |k|
17
+ puts "#{k}: #{answers[k]}"
18
+ end
@@ -0,0 +1,13 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ # Using the hash based API for requesting the user to choose a fileectory
6
+ file_hash = dialog_factory.fselect(:path => "/", :width => 60, :height => 10).show
7
+
8
+ # Using the block based API for requesting the user to choose a fileectory
9
+ file_block = dialog_factory.fselect { path "/"; width 60; height 10; }.show
10
+
11
+ puts "File chosen with hash method #{file_hash}"
12
+ puts "File chosen with block method #{file_block}"
13
+
@@ -0,0 +1,14 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ dialog_factory.infobox(:text => "This is information from the hash based api").show
6
+ sleep 2
7
+ dialog_factory.infobox(:text => "This is wide information from the hash based api", :width => 60, :height => 10).show
8
+ sleep 2
9
+
10
+ dialog_factory.infobox{ text "This is information from the block based api" }.show
11
+ sleep 2
12
+ dialog_factory.infobox{ text "This is wide information from the block based api"; width 60; height 10 }.show
13
+ sleep 2
14
+
@@ -0,0 +1,16 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ # Using the hash based API for asking a question in an input box
6
+ answer_hash = dialog_factory.inputbox(:text => "What do you think about the hash based API?", :initial => "I like it very much" ).show
7
+
8
+ # Using the block based API for asking a question in an input box
9
+ answer_block = dialog_factory.inputbox{ text "What do you think about the block based API?"; initial "I love it"; }.show
10
+
11
+ # Using the block based API, with no initial value
12
+ answer_ruby = dialog_factory.inputbox { text "What do you think about Ruby? (be creative)" }.show
13
+
14
+ puts "Answer to the hash question: #{answer_hash}"
15
+ puts "Answer to the block question: #{answer_block}"
16
+ puts "Answer to the Ruby question: #{answer_ruby}"
data/examples/menu.rb ADDED
@@ -0,0 +1,28 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ chosen1 = dialog_factory.menu(:text => "Choose from the menu, hash style",
6
+ :options => [
7
+ [ "fisk", "A confusing choice for a metasyntactic variable name", :off ],
8
+ [ "foo", "The classic first metasyntactic variable name", :on ],
9
+ [ "bar", "The classic second metasyntactic variable name", :off ],
10
+ [ "baz", "The classic third metasyntactic variable name", :off ],
11
+ [ "quux", "A quite unusual metasyntactic variable name", :off ]
12
+ ]).show
13
+
14
+ chosen2 = dialog_factory.menu {
15
+ text "Choose from the menu, block style"
16
+ option "fisk", "A confusing choice for a metasyntactic variable name"
17
+ option "foo", "The classic first metasyntactic variable name", :on
18
+ option "bar", "The classic second metasyntactic variable name", :on
19
+ option "baz", "The classic third metasyntactic variable name", :on
20
+ option "quux", "A quite unusual metasyntactic variable name"
21
+ }.show
22
+
23
+ puts "Chosen from the dialog created with the hash style:"
24
+ puts chosen1
25
+ puts
26
+ puts "Chosen from the dialog created with the block style"
27
+ puts chosen2
28
+
@@ -0,0 +1,9 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ # Using the hash based API for showing a msgbox
6
+ dialog_factory.msgbox(:text => "This is a message from the hash based API").show
7
+
8
+ # Using the block based API for showing a msgbox
9
+ dialog_factory.msgbox { text "This is a message from the block based API" }.show
@@ -0,0 +1,16 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ # Using the hash based API to ask for a password
6
+ hash_pw = dialog_factory.passwordbox(:text => "Please enter password, hash style").show
7
+ hash_pw_insec = dialog_factory.passwordbox(:text => "Please enter password, more usable hash style", :insecure => true).show
8
+ # Using the block based API to ask for a password
9
+ block_pw = dialog_factory.passwordbox{ text "Please enter passwrod, block style" }.show
10
+ block_pw_insec = dialog_factory.passwordbox{ insecure; text "Please enter passwrod, more usable block style" }.show
11
+
12
+ puts "Hash password: #{hash_pw}"
13
+ puts "Hash password (entered in less secure way): #{hash_pw_insec}"
14
+ puts "Block password: #{block_pw}"
15
+ puts "Block parssword (entered ien less secure way): #{block_pw_insec}"
16
+
data/examples/pause.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ accepted_hash_style = dialog_factory.pause(:text => "Pause, hash style", :seconds => 15, :width => 40, :height => 8).show
6
+ accepted_block_style = dialog_factory.pause{ text "Pause, block style"; seconds 20; width 30; height 8; }.show
7
+
8
+ if accepted_hash_style then
9
+ puts "You accepted the hash style pause"
10
+ end
11
+
12
+ if accepted_block_style then
13
+ puts "You accepted the block style pause"
14
+ end
@@ -0,0 +1,28 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ chosen1 = dialog_factory.radiolist(:text => "Choose one, hash style",
6
+ :options => [
7
+ [ "fisk", "A confusing choice for a metasyntactic variable name", :off ],
8
+ [ "foo", "The classic first metasyntactic variable name", :on ],
9
+ [ "bar", "The classic second metasyntactic variable name", :off ],
10
+ [ "baz", "The classic third metasyntactic variable name", :off ],
11
+ [ "quux", "A quite unusual metasyntactic variable name", :off ]
12
+ ]).show
13
+
14
+ chosen2 = dialog_factory.radiolist {
15
+ text "Choose one, block style"
16
+ option "fisk", "A confusing choice for a metasyntactic variable name"
17
+ option "foo", "The classic first metasyntactic variable name", :on
18
+ option "bar", "The classic second metasyntactic variable name"
19
+ option "baz", "The classic third metasyntactic variable name"
20
+ option "quux", "A quite unusual metasyntactic variable name"
21
+ }.show
22
+
23
+ puts "Chosen from the dialog created with the hash style:"
24
+ puts chosen1
25
+ puts
26
+ puts "Chosen from the dialog created with the block style"
27
+ puts chosen2
28
+
@@ -0,0 +1,9 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ # How to view a file in a dialog using the hash API
6
+ dialog_factory.textbox(:filename => ARGV[0]).show
7
+
8
+ # How to view a file in a dialog using the block API, does not make much sense
9
+ dialog_factory.textbox { filename ARGV[1] }.show
data/examples/yesno.rb ADDED
@@ -0,0 +1,15 @@
1
+ require 'palaver'
2
+
3
+ dialog_factory = Palaver::DialogFactory.new
4
+
5
+ # Using the hash based API, which makes most sense for this kind of dialog
6
+ likes_hash_based_api = dialog_factory.yesno(:text => "Do you like the hash based API?").show
7
+
8
+ # Using the block based API
9
+ likes_block_based_api = dialog_factory.yesno { text "Do you like the block based API?" }.show
10
+
11
+ if likes_block_based_api or likes_hash_based_api then
12
+ puts "Great"
13
+ else
14
+ puts "What a pity. Try RDialog instead, it's also very nice"
15
+ end
@@ -0,0 +1,40 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+ class Base
6
+ def initialize(options)
7
+ @text = nil
8
+ @width = 0
9
+ @height = 0
10
+
11
+ options.each do |option,value|
12
+ case option
13
+ when :width then self.width(value)
14
+ when :height then self.height(value)
15
+ when :text then self.text(value)
16
+ end
17
+ end
18
+ end
19
+
20
+ def text(str)
21
+ @text = str
22
+ end
23
+
24
+ def width(w)
25
+ @width = w
26
+ end
27
+
28
+ def height(h)
29
+ @height = h
30
+ end
31
+
32
+ def with_tempfile
33
+ tf = Tempfile.new "palaver"
34
+ tfpath = tf.path
35
+ tf.close
36
+ yield tfpath
37
+ File.delete tfpath
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,43 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ require 'tempfile'
5
+ require 'date'
6
+ require 'palaver/base'
7
+
8
+ module Palaver
9
+ class Calendar < Palaver::Base
10
+ def initialize(options={})
11
+ super(options)
12
+ @default_date = nil
13
+
14
+ options.each do |option,value|
15
+ case option
16
+ when :date then self.date(value)
17
+ end
18
+ end
19
+
20
+ end
21
+
22
+ def date(d)
23
+ @default_date = d
24
+ end
25
+
26
+ def show
27
+ day, month, year =
28
+ if @default_date then [ @default_date.day, @default_date.month, @default_date.year]
29
+ else [ 0, 0, 0 ]
30
+ end
31
+ chosen_date = nil
32
+ with_tempfile do |tfpath|
33
+ cmd = 'dialog --calendar "%s" %d %d %d %d %d 2> %s' % [ @text,
34
+ @height, @width,
35
+ day, month, year,
36
+ tfpath ]
37
+ success = system cmd
38
+ chosen_date = Date.strptime(File.read(tfpath), "%d/%m/%Y") if success
39
+ end
40
+ return chosen_date
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,24 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ require 'palaver/list_with_options'
5
+
6
+ module Palaver
7
+ class Checklist < Palaver::ListWithOptions
8
+ def initialize(options)
9
+ super(options)
10
+ end
11
+
12
+ def show
13
+ choices = nil
14
+ with_tempfile do |fname|
15
+ cmd = "dialog --checklist '#@text' #@height #@width #@list_height #{options_string_with_status} 2> #{fname}"
16
+ success = system cmd
17
+ if success then
18
+ choices = File.read(fname).split(" ").map { |s| s[1..-2] }
19
+ end
20
+ end
21
+ return choices
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,42 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+ class DialogFactory
6
+ @@dialog_types = {
7
+ :calendar => Calendar,
8
+ :checklist => Checklist,
9
+ :radiolist => Radiolist,
10
+ :menu => Menu,
11
+ :yesno => YesNo,
12
+ :textbox => TextBox,
13
+ :msgbox => MsgBox,
14
+ :inputbox => InputBox,
15
+ :infobox => InfoBox,
16
+ :pause => Pause,
17
+ :passwordbox => PasswordBox,
18
+ :dselect => DSelect,
19
+ :fselect => FSelect,
20
+ :form => Form
21
+ }
22
+
23
+ private
24
+
25
+ def method_missing(methodsym, *args, &block)
26
+ if @@dialog_types.has_key? methodsym then
27
+ c = @@dialog_types[methodsym]
28
+ options = args.size == 1 ? args[0] : {}
29
+ return make_dialog(c, options, &block)
30
+ else
31
+ raise NoMethodError.new("Undefined method: #{methodsym} for #{self}", methodsym)
32
+ end
33
+ end
34
+
35
+ def make_dialog(cls, options, &spec)
36
+ d = cls.new(options)
37
+ d.instance_eval &spec if spec
38
+ return d
39
+ end
40
+
41
+ end
42
+ end
@@ -0,0 +1,18 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ require 'palaver/pathselect'
5
+
6
+ module Palaver
7
+ class DSelect < Palaver::PathSelect
8
+ def initialize(options={})
9
+ super(options)
10
+ end
11
+
12
+ def dialog_name
13
+ "dselect"
14
+ end
15
+
16
+ end
17
+ end
18
+
@@ -0,0 +1,96 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+
6
+ class Form < Palaver::Base
7
+ def initialize(options)
8
+ super(options)
9
+ @entries = []
10
+ @form_height = nil
11
+ @simple_display_width = nil
12
+ @simple_text_length = nil
13
+ end
14
+
15
+ def show
16
+ text_width = @entries.map { |e| e.text_width }.max
17
+ @entries.each { |e| e.update_value_x text_width }
18
+
19
+ cmd_intro = "dialog --form '#@text' #@height #@width #@form_height "
20
+ cmd_entries = @entries.map { |e| e.to_cmd_s }.join(' ')
21
+
22
+ result = nil
23
+ with_tempfile do |tfpath|
24
+ cmd = "#{cmd_intro} #{cmd_entries} 2> #{tfpath}"
25
+ success = system cmd
26
+ result = read_dialog_output tfpath
27
+ end
28
+
29
+ return result
30
+ end
31
+
32
+ private
33
+
34
+ class Entry
35
+ attr_reader :field_id, :text
36
+
37
+ def initialize(id, text, text_y, text_x, default_value, value_y, value_x, field_length, input_length)
38
+ @field_id = id
39
+ @text = text
40
+ @text_y = text_y
41
+ @text_x = text_x
42
+ @default_value = default_value
43
+ @value_y = value_y
44
+ @value_x = value_x
45
+ @field_length = field_length
46
+ @input_length = input_length
47
+ end
48
+
49
+ def text_width
50
+ @text.length + 2
51
+ end
52
+
53
+ def update_value_x(min)
54
+ @value_x = [ @value_x ? @value_x : 0, min ].max
55
+ end
56
+
57
+ def to_cmd_s
58
+ "'#@text' #@text_y #@text_x '#@default_value' #@value_y #@value_x #@field_length #@input_length"
59
+ end
60
+ end
61
+
62
+ def read_dialog_output(fname)
63
+ lines = File.read(fname).split("\n")
64
+ result = {}
65
+ lines.each_with_index { |l,i| result[@entries[i].field_id] = l }
66
+ return result
67
+ end
68
+
69
+ def form_height(val)
70
+ @form_height = val
71
+ end
72
+
73
+ def entry(id, title, text_y, text_x, default_value, value_y, value_x, field_length, input_length)
74
+ newentry = Entry.new(id, title, text_y, text_x, default_value, value_y, value_x, field_length, input_length)
75
+ @entries.push newentry
76
+ end
77
+
78
+ def simple_display_width(width)
79
+ @simple_display_width = width
80
+ end
81
+
82
+ def simple_text_length(length)
83
+ @simple_text_length = length
84
+ end
85
+
86
+ def simple_entry(id, title, default_value)
87
+ @simple_next_y = 1 if not @simple_next_y
88
+ newentry = Entry.new(id, title, @simple_next_y, 0, default_value, @simple_next_y, nil, @simple_display_width, @simple_text_length)
89
+ @entries.push newentry
90
+ @simple_next_y = @simple_next_y + 1
91
+ end
92
+
93
+ end
94
+
95
+ end
96
+
@@ -0,0 +1,17 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ require 'palaver/pathselect'
5
+
6
+ module Palaver
7
+ class FSelect < Palaver::PathSelect
8
+ def initialize(options={})
9
+ super(options)
10
+ end
11
+
12
+ def dialog_name
13
+ "fselect"
14
+ end
15
+ end
16
+ end
17
+
@@ -0,0 +1,15 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+ class InfoBox < Palaver::Base
6
+ def initialize(options)
7
+ super(options)
8
+ end
9
+
10
+ def show
11
+ cmd = "dialog --infobox '#@text' #@height #@width"
12
+ system cmd
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,34 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+ class InputBox < Palaver::Base
6
+ def initialize(options)
7
+ super(options)
8
+ @initial = nil
9
+
10
+ options.each do |option,value|
11
+ case option
12
+ when :initial then self.initial(value)
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ def initial(text)
19
+ @initial = text
20
+ end
21
+
22
+ def show
23
+ answer = nil
24
+ with_tempfile do |fname|
25
+ cmd = "dialog --inputbox '#@text' #@height #@width '#@initial' 2> #{fname}"
26
+ success = system cmd
27
+ if success then
28
+ answer = File.read(fname)
29
+ end
30
+ end
31
+ return answer
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,46 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+
6
+ class ListWithOptions < Palaver::Base
7
+
8
+ class Option
9
+ attr_reader :tag, :item, :status
10
+ def initialize(tag,item,status=nil)
11
+ @tag = tag
12
+ @item = item
13
+ @status = status
14
+ end
15
+ end
16
+
17
+ def initialize(options)
18
+ super(options)
19
+ @dialog_options = []
20
+ @list_height = 0
21
+
22
+ options.each do |option,value|
23
+ case option
24
+ when :options then @dialog_options = value.map { |o| Option.new(o[0],o[1],o[2]) }
25
+ end
26
+ end
27
+ end
28
+
29
+ def option(tag, desc, status=nil)
30
+ @dialog_options.push Option.new(tag,desc,status)
31
+ end
32
+
33
+ def list_height(h)
34
+ @list_height = h
35
+ end
36
+
37
+ def options_string_with_status
38
+ @dialog_options.map { |o| "'#{o.tag}' '#{o.item}' '#{o.status == :on ? 'on' : 'off'}'" }.join ' '
39
+ end
40
+
41
+ def options_string_no_status
42
+ @dialog_options.map { |o| "'#{o.tag}' '#{o.item}'" }.join ' '
43
+ end
44
+
45
+ end
46
+ end
@@ -0,0 +1,25 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ require 'palaver/list_with_options'
5
+
6
+ module Palaver
7
+ class Menu < Palaver::ListWithOptions
8
+ def initialize(options)
9
+ super options
10
+ end
11
+
12
+ def show
13
+ chosen = nil
14
+ with_tempfile do |fname|
15
+ cmd = "dialog --menu '#@text' #@height #@width #@list_height #{options_string_no_status} 2> #{fname}"
16
+ sucess = system cmd
17
+ if sucess then
18
+ chosen = File.read(fname)
19
+ end
20
+ end
21
+ return chosen
22
+ end
23
+
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+ class MsgBox < Palaver::Base
6
+ def initialize(options)
7
+ super options
8
+ end
9
+
10
+ def show
11
+ cmd = "dialog --msgbox '#@text' #@height #@width"
12
+ system cmd
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+ class PasswordBox < Palaver::Base
6
+ def initialize(options)
7
+ super(options)
8
+ @insecure = false
9
+
10
+ options.each do |option,value|
11
+ case option
12
+ when :insecure then self.insecure(value)
13
+ end
14
+ end
15
+ end
16
+
17
+ def insecure(should_be_insecure=true)
18
+ @insecure = should_be_insecure
19
+ end
20
+
21
+
22
+ def show
23
+ answer = nil
24
+ with_tempfile do |fname|
25
+ insecure = @insecure ? "--insecure" : ""
26
+ cmd = "dialog #{insecure} --passwordbox '#@text' #@height #@width 2> #{fname}"
27
+ #puts cmd
28
+ success = system cmd
29
+ if success then
30
+ answer = File.read(fname)
31
+ end
32
+ end
33
+ return answer
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,35 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+ class PathSelect < Palaver::Base
6
+ def initialize(options={})
7
+ super(options)
8
+ @path = nil
9
+
10
+ options.each do |option,value|
11
+ case option
12
+ when :path then self.path(value)
13
+ end
14
+ end
15
+
16
+ end
17
+
18
+ def path(str)
19
+ @path = str
20
+ end
21
+
22
+ def show
23
+ raise "CAn't open a #{dialog_name} dialog with a path" if not @path
24
+ chosen_file = nil
25
+ with_tempfile do |tfpath|
26
+ cmd = "dialog --#{dialog_name} '#@path' #@height #@width 2> #{tfpath}"
27
+ puts cmd
28
+ success = system cmd
29
+ chosen_file = File.read(tfpath) if success
30
+ end
31
+ return chosen_file
32
+ end
33
+ end
34
+ end
35
+
@@ -0,0 +1,26 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+ class Pause < Palaver::Base
6
+ def initialize(options)
7
+ super(options)
8
+ @seconds = 0
9
+ options.each do |option,value|
10
+ case option
11
+ when :seconds then @seconds = value
12
+ end
13
+ end
14
+ end
15
+
16
+ def seconds(val)
17
+ @seconds = val
18
+ end
19
+
20
+ def show
21
+ cmd = "dialog --pause '#@text' #@height #@width #@seconds"
22
+ rc = system cmd
23
+ return rc
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,24 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ require 'palaver/list_with_options'
5
+
6
+ module Palaver
7
+ class Radiolist < Palaver::ListWithOptions
8
+ def initialize(options)
9
+ super(options)
10
+ end
11
+
12
+ def show
13
+ chosen = nil
14
+ with_tempfile do |fname|
15
+ cmd = "dialog --radiolist '#@text' #@height #@width #@list_height #{options_string_with_status} 2> #{fname}"
16
+ sucess = system cmd
17
+ if sucess then
18
+ chosen = File.read(fname)
19
+ end
20
+ end
21
+ return chosen
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+ class TextBox < Base
6
+ def initialize(options)
7
+ super options
8
+ options.each do |option,value|
9
+ case option
10
+ when :filename then filename(value)
11
+ end
12
+ end
13
+ end
14
+
15
+ def show
16
+ cmd = "dialog --textbox '#@filename' #@height #@width"
17
+ system cmd
18
+ end
19
+
20
+ def filename(name)
21
+ @filename = name
22
+ end
23
+ end
24
+ end
25
+
@@ -0,0 +1,16 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ module Palaver
5
+ class YesNo < Palaver::Base
6
+ def initialize(optons)
7
+ super optons
8
+ end
9
+
10
+ def show
11
+ cmd = "dialog --yesno '#@text' #@height #@width"
12
+ rc = system cmd
13
+ return rc
14
+ end
15
+ end
16
+ end
data/lib/palaver.rb ADDED
@@ -0,0 +1,18 @@
1
+ # Copyright (c) 2012, Peter Allin <peter@peca.dk> All rights reserved.
2
+ # See LICENSE file for licensing information.
3
+
4
+ require 'palaver/calendar.rb'
5
+ require 'palaver/checklist.rb'
6
+ require 'palaver/radiolist.rb'
7
+ require 'palaver/menu.rb'
8
+ require 'palaver/yesno.rb'
9
+ require 'palaver/textbox.rb'
10
+ require 'palaver/msgbox.rb'
11
+ require 'palaver/inputbox.rb'
12
+ require 'palaver/infobox.rb'
13
+ require 'palaver/pause.rb'
14
+ require 'palaver/passwordbox.rb'
15
+ require 'palaver/dselect.rb'
16
+ require 'palaver/fselect.rb'
17
+ require 'palaver/form.rb'
18
+ require 'palaver/dialog_factory.rb'
data/palaver.gemspec ADDED
@@ -0,0 +1,14 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = "palaver"
3
+ s.version = "0.2.0"
4
+ s.platform = Gem::Platform::RUBY
5
+ s.authors = [ "Peter Allin" ]
6
+ s.email = [ "peter@peca.dk" ]
7
+ s.summary = "Ruby interface for the 'dialog' progam"
8
+ s.description = "Uses the 'dialog' program to create Ruby applications with text based user interfaces"
9
+ s.rubyforge_project = s.name
10
+ s.required_rubygems_version = ">= 1.3.6"
11
+ s.files = `git ls-files`.split "\n"
12
+ s.require_path = "lib"
13
+ s.homepage = "https://github.com/peterallin/palaver"
14
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: palaver
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
+ platform: ruby
12
+ authors:
13
+ - Peter Allin
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-07-10 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Uses the 'dialog' program to create Ruby applications with text based user interfaces
22
+ email:
23
+ - peter@peca.dk
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - .gitignore
32
+ - LICENSE
33
+ - README.md
34
+ - Rakefile
35
+ - examples/calendar.rb
36
+ - examples/checklist.rb
37
+ - examples/dselect.rb
38
+ - examples/form.rb
39
+ - examples/form_simple.rb
40
+ - examples/fselect.rb
41
+ - examples/infobox.rb
42
+ - examples/inputbox.rb
43
+ - examples/menu.rb
44
+ - examples/msgbox.rb
45
+ - examples/passwordbox.rb
46
+ - examples/pause.rb
47
+ - examples/radiolist.rb
48
+ - examples/textbox.rb
49
+ - examples/yesno.rb
50
+ - lib/palaver.rb
51
+ - lib/palaver/base.rb
52
+ - lib/palaver/calendar.rb
53
+ - lib/palaver/checklist.rb
54
+ - lib/palaver/dialog_factory.rb
55
+ - lib/palaver/dselect.rb
56
+ - lib/palaver/form.rb
57
+ - lib/palaver/fselect.rb
58
+ - lib/palaver/infobox.rb
59
+ - lib/palaver/inputbox.rb
60
+ - lib/palaver/list_with_options.rb
61
+ - lib/palaver/menu.rb
62
+ - lib/palaver/msgbox.rb
63
+ - lib/palaver/passwordbox.rb
64
+ - lib/palaver/pathselect.rb
65
+ - lib/palaver/pause.rb
66
+ - lib/palaver/radiolist.rb
67
+ - lib/palaver/textbox.rb
68
+ - lib/palaver/yesno.rb
69
+ - palaver.gemspec
70
+ homepage: https://github.com/peterallin/palaver
71
+ licenses: []
72
+
73
+ post_install_message:
74
+ rdoc_options: []
75
+
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ hash: 3
84
+ segments:
85
+ - 0
86
+ version: "0"
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ hash: 23
93
+ segments:
94
+ - 1
95
+ - 3
96
+ - 6
97
+ version: 1.3.6
98
+ requirements: []
99
+
100
+ rubyforge_project: palaver
101
+ rubygems_version: 1.8.16
102
+ signing_key:
103
+ specification_version: 3
104
+ summary: Ruby interface for the 'dialog' progam
105
+ test_files: []
106
+