Class: TuiTui::ScrollList
- Inherits:
-
Object
- Object
- TuiTui::ScrollList
- Defined in:
- lib/tui_tui/scroll_list.rb
Overview
Cursor and viewport arithmetic shared by list-like widgets.
Instance Attribute Summary collapse
-
#count ⇒ Object
Returns the value of attribute count.
-
#cursor ⇒ Object
readonly
Returns the value of attribute cursor.
-
#top ⇒ Object
readonly
Returns the value of attribute top.
Instance Method Summary collapse
- #at_end? ⇒ Boolean
- #each_visible(height) ⇒ Object
- #empty? ⇒ Boolean
- #ensure_visible(height) ⇒ Object
- #go_to(index) ⇒ Object
-
#initialize(count = 0) ⇒ ScrollList
constructor
A new instance of ScrollList.
- #last ⇒ Object
- #move(delta) ⇒ Object
- #page(height) ⇒ Object
- #to_end ⇒ Object
- #to_top ⇒ Object
Constructor Details
#initialize(count = 0) ⇒ ScrollList
Returns a new instance of ScrollList.
8 9 10 11 12 |
# File 'lib/tui_tui/scroll_list.rb', line 8 def initialize(count = 0) @count = count @cursor = 0 @top = 0 end |
Instance Attribute Details
#count ⇒ Object
Returns the value of attribute count.
19 20 21 |
# File 'lib/tui_tui/scroll_list.rb', line 19 def count @count end |
#cursor ⇒ Object (readonly)
Returns the value of attribute cursor.
6 7 8 |
# File 'lib/tui_tui/scroll_list.rb', line 6 def cursor @cursor end |
#top ⇒ Object (readonly)
Returns the value of attribute top.
6 7 8 |
# File 'lib/tui_tui/scroll_list.rb', line 6 def top @top end |
Instance Method Details
#at_end? ⇒ Boolean
23 |
# File 'lib/tui_tui/scroll_list.rb', line 23 def at_end? = @cursor == last |
#each_visible(height) ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/tui_tui/scroll_list.rb', line 44 def each_visible(height) return enum_for(:each_visible, height) unless block_given? height.times do |offset| index = @top + offset break if index >= @count yield index, offset end self end |
#empty? ⇒ Boolean
21 |
# File 'lib/tui_tui/scroll_list.rb', line 21 def empty? = @count.zero? |
#ensure_visible(height) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/tui_tui/scroll_list.rb', line 35 def ensure_visible(height) return self if height <= 0 @top = @cursor if @cursor < @top @top = @cursor - height + 1 if @cursor >= @top + height @top = 0 if @top.negative? self end |
#go_to(index) ⇒ Object
30 31 32 33 |
# File 'lib/tui_tui/scroll_list.rb', line 30 def go_to(index) @cursor = index.clamp(0, last) self end |
#last ⇒ Object
22 |
# File 'lib/tui_tui/scroll_list.rb', line 22 def last = [@count - 1, 0].max |
#move(delta) ⇒ Object
25 |
# File 'lib/tui_tui/scroll_list.rb', line 25 def move(delta) = go_to(@cursor + delta) |
#page(height) ⇒ Object
26 |
# File 'lib/tui_tui/scroll_list.rb', line 26 def page(height) = move(height) |
#to_end ⇒ Object
28 |
# File 'lib/tui_tui/scroll_list.rb', line 28 def to_end = go_to(last) |
#to_top ⇒ Object
27 |
# File 'lib/tui_tui/scroll_list.rb', line 27 def to_top = go_to(0) |