Class: RubyTUI::Widgets::ListState
- Inherits:
-
Object
- Object
- RubyTUI::Widgets::ListState
- Defined in:
- lib/rubytui/widgets/list.rb
Overview
ListState holds external state for the List widget.
selected is the index of the selected item and offset the index of the
first visible item. RubyTUI::Widgets::List#render clamps both in place.
Instance Attribute Summary collapse
-
#offset ⇒ Object
Returns the value of attribute offset.
-
#selected ⇒ Object
Returns the value of attribute selected.
Instance Method Summary collapse
-
#initialize(selected: 0, offset: 0) ⇒ ListState
constructor
A new instance of ListState.
-
#select_first ⇒ void
Select the first item and reset the scroll offset to 0.
-
#select_last(total) ⇒ void
Select the last item.
-
#select_next(total) ⇒ void
Select the next item, wrapping from the last item to the first.
-
#select_previous(total) ⇒ void
Select the previous item, wrapping from the first item to the last.
Constructor Details
#initialize(selected: 0, offset: 0) ⇒ ListState
Returns a new instance of ListState.
16 17 18 19 |
# File 'lib/rubytui/widgets/list.rb', line 16 def initialize(selected: 0, offset: 0) @selected = selected @offset = offset end |
Instance Attribute Details
#offset ⇒ Object
Returns the value of attribute offset.
12 13 14 |
# File 'lib/rubytui/widgets/list.rb', line 12 def offset @offset end |
#selected ⇒ Object
Returns the value of attribute selected.
12 13 14 |
# File 'lib/rubytui/widgets/list.rb', line 12 def selected @selected end |
Instance Method Details
#select_first ⇒ void
This method returns an undefined value.
Select the first item and reset the scroll offset to 0.
44 45 46 47 |
# File 'lib/rubytui/widgets/list.rb', line 44 def select_first @selected = 0 @offset = 0 end |
#select_last(total) ⇒ void
This method returns an undefined value.
Select the last item.
The offset is not changed; RubyTUI::Widgets::List#render scrolls to keep the selection visible.
55 56 57 |
# File 'lib/rubytui/widgets/list.rb', line 55 def select_last(total) @selected = [total - 1, 0].max end |
#select_next(total) ⇒ void
This method returns an undefined value.
Select the next item, wrapping from the last item to the first.
Does nothing when total is 0 or negative.
27 28 29 |
# File 'lib/rubytui/widgets/list.rb', line 27 def select_next(total) @selected = (@selected + 1) % total if total > 0 end |
#select_previous(total) ⇒ void
This method returns an undefined value.
Select the previous item, wrapping from the first item to the last.
Does nothing when total is 0 or negative.
37 38 39 |
# File 'lib/rubytui/widgets/list.rb', line 37 def select_previous(total) @selected = (@selected - 1) % total if total > 0 end |