Class: LessCurse::Widgets::List
- Defined in:
- lib/less_curse/widgets/list.rb
Instance Attribute Summary collapse
-
#display_func ⇒ Object
Returns the value of attribute display_func.
-
#on_select ⇒ Object
Returns the value of attribute on_select.
-
#selected_data ⇒ Object
Returns the value of attribute selected_data.
-
#top_element_idx ⇒ Object
Returns the value of attribute top_element_idx.
Attributes inherited from Base
#actions, #data, #focus, #title
Instance Method Summary collapse
- #draw(window) ⇒ Object
- #handle_input(key) ⇒ Object
-
#initialize(data: [], title: "") ⇒ List
constructor
A new instance of List.
-
#select_next ⇒ Object
Select next data element in list (roll over if nexessary).
-
#select_previous ⇒ Object
Select previous data element in list (roll over if nexessary).
- #set_default_actions ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(data: [], title: "") ⇒ List
Returns a new instance of List.
9 10 11 |
# File 'lib/less_curse/widgets/list.rb', line 9 def initialize data: [], title: "" super(data: data, title: title) end |
Instance Attribute Details
#display_func ⇒ Object
Returns the value of attribute display_func.
7 8 9 |
# File 'lib/less_curse/widgets/list.rb', line 7 def display_func @display_func end |
#on_select ⇒ Object
Returns the value of attribute on_select.
4 5 6 |
# File 'lib/less_curse/widgets/list.rb', line 4 def on_select @on_select end |
#selected_data ⇒ Object
Returns the value of attribute selected_data.
5 6 7 |
# File 'lib/less_curse/widgets/list.rb', line 5 def selected_data @selected_data end |
#top_element_idx ⇒ Object
Returns the value of attribute top_element_idx.
6 7 8 |
# File 'lib/less_curse/widgets/list.rb', line 6 def top_element_idx @top_element_idx end |
Instance Method Details
#draw(window) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/less_curse/widgets/list.rb', line 20 def draw window LessCurse::Renderer::bold_if focused?, window do LessCurse::Renderer::box_with_title window, @title end # Do we have kind of a display-window? visible_data.each_with_index do |d, idx| LessCurse::Renderer::bold_if(@selected_data == d, window) do if @display_func item_text = display_func.call(d) else item_text = d.to_s end LessCurse::Renderer::write_line window, idx, item_text end end if visible_data.size != @data.size LessCurse::Renderer::box_foot window, "..." end end |
#handle_input(key) ⇒ Object
42 43 44 45 46 47 |
# File 'lib/less_curse/widgets/list.rb', line 42 def handle_input key action = @actions[key] LessCurse::debug_msg "List will execute action: #{action}" return false if !action send(action) end |
#select_next ⇒ Object
Select next data element in list (roll over if nexessary)
64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/less_curse/widgets/list.rb', line 64 def select_next if @selected_data.nil? && @data.size >= 0 @selected_data = @data.to_a[0] else @selected_data = @data.to_a[(selected_data_index + 1) % @data.size] end recalc_top_index if @on_select @on_select.call @selected_data end @selected_data end |
#select_previous ⇒ Object
Select previous data element in list (roll over if nexessary)
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/less_curse/widgets/list.rb', line 50 def select_previous if @selected_data.nil? && @data.size >= 0 @selected_data = @data.to_a[0] else @selected_data = @data.to_a[(selected_data_index - 1) % @data.size] end recalc_top_index if @on_select @on_select.call @selected_data end @selected_data end |
#set_default_actions ⇒ Object
15 16 17 18 |
# File 'lib/less_curse/widgets/list.rb', line 15 def set_default_actions @actions = { FFI::NCurses::KEY_UP => :select_previous, FFI::NCurses::KEY_DOWN => :select_next} end |