Class: RubyRich::Dialog
- Inherits:
-
Object
- Object
- RubyRich::Dialog
- Defined in:
- lib/ruby_rich/dialog.rb
Instance Attribute Summary collapse
-
#buttons ⇒ Object
Returns the value of attribute buttons.
-
#content ⇒ Object
Returns the value of attribute content.
-
#height ⇒ Object
Returns the value of attribute height.
-
#live ⇒ Object
Returns the value of attribute live.
-
#title ⇒ Object
Returns the value of attribute title.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
- #build_button(buttons) ⇒ Object
-
#initialize(title: "", content: "", width: 48, height: 8, buttons: [:ok]) ⇒ Dialog
constructor
A new instance of Dialog.
- #key(event_name, priority = 0, &block) ⇒ Object
- #notify_listeners(event_data) ⇒ Object
- #on(event_name, &block) ⇒ Object
- #render_to_buffer ⇒ Object
Constructor Details
#initialize(title: "", content: "", width: 48, height: 8, buttons: [:ok]) ⇒ Dialog
Returns a new instance of Dialog.
6 7 8 9 10 11 12 13 14 15 16 17 18 |
# File 'lib/ruby_rich/dialog.rb', line 6 def initialize(title: "", content: "", width: 48, height: 8, buttons: [:ok]) @width = width @height = height terminal_width = `tput cols`.to_i terminal_height = `tput lines`.to_i @event_listeners = {} @layout = RubyRich::Layout.new(name: :title, width: width, height: height) @panel = RubyRich::Panel.new("", title: title, border_style: :white) @layout.update_content(@panel) @layout.calculate_dimensions(terminal_width, terminal_height) @button_str = () @panel.content = " \n \n#{content}"+AnsiCode.reset+"\n \n \n" + " "*((@panel.inner_width - @button_str.display_width)/2) + @button_str end |
Instance Attribute Details
#buttons ⇒ Object
Returns the value of attribute buttons.
3 4 5 |
# File 'lib/ruby_rich/dialog.rb', line 3 def @buttons end |
#content ⇒ Object
Returns the value of attribute content.
3 4 5 |
# File 'lib/ruby_rich/dialog.rb', line 3 def content @content end |
#height ⇒ Object
Returns the value of attribute height.
4 5 6 |
# File 'lib/ruby_rich/dialog.rb', line 4 def height @height end |
#live ⇒ Object
Returns the value of attribute live.
3 4 5 |
# File 'lib/ruby_rich/dialog.rb', line 3 def live @live end |
#title ⇒ Object
Returns the value of attribute title.
3 4 5 |
# File 'lib/ruby_rich/dialog.rb', line 3 def title @title end |
#width ⇒ Object
Returns the value of attribute width.
4 5 6 |
# File 'lib/ruby_rich/dialog.rb', line 4 def width @width end |
Instance Method Details
#build_button(buttons) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/ruby_rich/dialog.rb', line 24 def () str = "" .each do |btn| case btn when :ok str += " "+AnsiCode.color(:blue) + "OK(enter)"+ AnsiCode.reset when :cancel str += " "+AnsiCode.color(:red) + "Cancel(esc)"+ AnsiCode.reset end end str.strip end |
#key(event_name, priority = 0, &block) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/ruby_rich/dialog.rb', line 41 def key(event_name, priority = 0, &block) unless @event_listeners[event_name] @event_listeners[event_name] = [] end @event_listeners[event_name] << { priority: priority, block: block } @event_listeners[event_name].sort_by! { |l| -l[:priority] } # Higher priority first end |
#notify_listeners(event_data) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/ruby_rich/dialog.rb', line 55 def notify_listeners(event_data) event_name = event_data[:name] result = nil if @event_listeners[event_name] @event_listeners[event_name].each do |listener| result = listener[:block].call(event_data, @live) end return result end end |
#on(event_name, &block) ⇒ Object
49 50 51 52 53 |
# File 'lib/ruby_rich/dialog.rb', line 49 def on(event_name, &block) if event_name==:close @event_listeners[event_name] = [{block: block}] end end |
#render_to_buffer ⇒ Object
37 38 39 |
# File 'lib/ruby_rich/dialog.rb', line 37 def render_to_buffer @layout.render_to_buffer end |