Class: Clacky::RichUI::FormDialog
Instance Attribute Summary collapse
Instance Method Summary
collapse
#colored, #muted, #status_marker, #theme, #truncate
Constructor Details
#initialize(title:, fields:, width: 92) ⇒ FormDialog
Returns a new instance of FormDialog.
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'lib/clacky/rich_ui/components/dialogs/form_dialog.rb', line 13
def initialize(title:, fields:, width: 92)
@title = title
@fields = fields
@field_index = 0
@editors = fields.map do |field|
RubyRich::LineEditor.new.tap { |editor| editor.value = field[:default].to_s }
end
@width = width
@height = [fields.length * 3 + 8, 16].max
@event_listeners = {}
@mutex = Mutex.new
@condition = ConditionVariable.new
@finished = false
@result = nil
@panel = RubyRich::Panel.new("", title: title, border_style: :cyan, title_align: :center)
@layout = RubyRich::Layout.new(name: :form_dialog, width: @width, height: @height)
@layout.update_content(@panel)
@layout.calculate_dimensions(@width, @height)
wire_default_keys
end
|
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
11
12
13
|
# File 'lib/clacky/rich_ui/components/dialogs/form_dialog.rb', line 11
def height
@height
end
|
#width ⇒ Object
Returns the value of attribute width.
11
12
13
|
# File 'lib/clacky/rich_ui/components/dialogs/form_dialog.rb', line 11
def width
@width
end
|
Instance Method Details
#finish(value) ⇒ Object
34
35
36
37
38
39
40
41
|
# File 'lib/clacky/rich_ui/components/dialogs/form_dialog.rb', line 34
def finish(value)
@mutex.synchronize do
@result = value
@finished = true
@condition.signal
end
true
end
|
#key(event_name, priority = 0, &block) ⇒ Object
48
49
50
51
52
|
# File 'lib/clacky/rich_ui/components/dialogs/form_dialog.rb', line 48
def key(event_name, priority = 0, &block)
@event_listeners[event_name] ||= []
@event_listeners[event_name] << { priority: priority, block: block }
@event_listeners[event_name].sort_by! { |listener| -listener[:priority] }
end
|
#notify_listeners(event_data) ⇒ Object
54
55
56
57
|
# File 'lib/clacky/rich_ui/components/dialogs/form_dialog.rb', line 54
def notify_listeners(event_data)
listeners = Array(@event_listeners[event_data[:name]])
listeners.each { |listener| listener[:block].call(event_data, nil) }
end
|
#render_to_buffer ⇒ Object
59
60
61
62
63
|
# File 'lib/clacky/rich_ui/components/dialogs/form_dialog.rb', line 59
def render_to_buffer
@panel.content = render_content
@layout.calculate_dimensions(@width, @height)
@layout.render_to_buffer
end
|
#wait ⇒ Object
43
44
45
46
|
# File 'lib/clacky/rich_ui/components/dialogs/form_dialog.rb', line 43
def wait
@mutex.synchronize { @condition.wait(@mutex) until @finished }
@result
end
|