Class: RubyTUI::Widgets::ListState

Inherits:
Object
  • Object
show all
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.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(selected: 0, offset: 0) ⇒ ListState

Returns a new instance of ListState.

Parameters:

  • selected (Integer) (defaults to: 0) —

    initial selected index (default: 0)

  • offset (Integer) (defaults to: 0) —

    initial scroll offset (default: 0)



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.

Parameters:

  • total (Integer) —

    number of items in the list



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.

Parameters:

  • total (Integer) —

    number of items in the list



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.

Parameters:

  • total (Integer) —

    number of items in the list



37
38
39
# File 'lib/rubytui/widgets/list.rb', line 37

def select_previous(total)
  @selected = (@selected - 1) % total if total > 0
end