Skip to content

Commit 28c5d8b

Browse files
committed
IRuby::Input (#77)
Adds the IRuby::Input class which provides widgets for getting inputs from the user.
1 parent 0ef1f38 commit 28c5d8b

File tree

21 files changed

+1141
-2
lines changed

21 files changed

+1141
-2
lines changed

iruby.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ Gem::Specification.new do |s|
3030
s.add_runtime_dependency 'bond', '~> 0.5'
3131
s.add_runtime_dependency 'multi_json', '~> 1.11'
3232
s.add_runtime_dependency 'mimemagic', '~> 0.3'
33+
s.add_runtime_dependency 'data_uri', '~> 0.1'
3334
end

lib/iruby.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
require 'iruby/kernel'
99
require 'iruby/backend'
1010
require 'iruby/ostream'
11+
require 'iruby/input'
1112
require 'iruby/formatter'
1213
require 'iruby/utils'
1314
require 'iruby/display'

lib/iruby/input.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
module IRuby
2+
module Input
3+
# autoload so that erector is not a required
4+
# runtime dependency of IRuby
5+
autoload :Builder, 'iruby/input/autoload'
6+
7+
def input prompt='Input'
8+
result = form{input label: prompt}
9+
result[:input] unless result.nil?
10+
end
11+
12+
def password prompt='Password'
13+
result = form{password label: prompt}
14+
result[:password] unless result.nil?
15+
end
16+
17+
def form &block
18+
builder = Builder.new &block
19+
form = InputForm.new(
20+
fields: builder.fields,
21+
buttons: builder.buttons
22+
)
23+
form.widget_display
24+
builder.process_result form.submit
25+
end
26+
27+
def popup title='Input', &block
28+
builder = Builder.new &block
29+
form = InputForm.new fields: builder.fields
30+
popup = Popup.new(
31+
title: title,
32+
form: form,
33+
buttons: builder.buttons
34+
)
35+
popup.widget_display
36+
builder.process_result form.submit
37+
end
38+
end
39+
40+
extend Input
41+
end

0 commit comments

Comments
 (0)