Class: Clacky::RichUI::ConfigMenuDialog

Inherits:
Object
  • Object
show all
Includes:
Clacky::RichUI::Components::BaseComponent
Defined in:
lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Clacky::RichUI::Components::BaseComponent

#colored, #muted, #status_marker, #theme, #truncate

Constructor Details

#initialize(choices:, selected_index: 0, title: "Model Configuration", width: 86) ⇒ ConfigMenuDialog

Returns a new instance of ConfigMenuDialog.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb', line 13

def initialize(choices:, selected_index: 0, title: "Model Configuration", width: 86)
  @choices = choices
  @selected_index = selected_index
  @width = width
  @height = [choices.length + 7, 12].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: :config_dialog, width: @width, height: @height)
  @layout.update_content(@panel)
  @layout.calculate_dimensions(@width, @height)
end

Instance Attribute Details

#heightObject

Returns the value of attribute height.



11
12
13
# File 'lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb', line 11

def height
  @height
end

#widthObject

Returns the value of attribute width.



11
12
13
# File 'lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb', line 11

def width
  @width
end

Instance Method Details

#finish(value) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb', line 41

def finish(value)
  @mutex.synchronize do
    @result = value
    @finished = true
    @condition.signal
  end
  true
end

#key(event_name, priority = 0, &block) ⇒ Object



55
56
57
58
59
# File 'lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb', line 55

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

#move_downObject



37
38
39
# File 'lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb', line 37

def move_down
  move(1)
end

#move_upObject



33
34
35
# File 'lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb', line 33

def move_up
  move(-1)
end

#notify_listeners(event_data) ⇒ Object



61
62
63
# File 'lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb', line 61

def notify_listeners(event_data)
  Array(@event_listeners[event_data[:name]]).each { |listener| listener[:block].call(event_data, nil) }
end

#render_to_bufferObject



65
66
67
68
69
# File 'lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb', line 65

def render_to_buffer
  @panel.content = render_content
  @layout.calculate_dimensions(@width, @height)
  @layout.render_to_buffer
end

#selected_choiceObject



29
30
31
# File 'lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb', line 29

def selected_choice
  @choices[@selected_index]
end

#waitObject



50
51
52
53
# File 'lib/clacky/rich_ui/components/dialogs/config_menu_dialog.rb', line 50

def wait
  @mutex.synchronize { @condition.wait(@mutex) until @finished }
  @result
end