Class: Twterm::Tab::Scrollable::Scroller
- Inherits:
-
Object
- Object
- Twterm::Tab::Scrollable::Scroller
- Extended by:
- Forwardable
- Defined in:
- lib/twterm/tab/scrollable.rb
Instance Attribute Summary collapse
-
#delegate ⇒ Object
Returns the value of attribute delegate.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#offset ⇒ Object
readonly
Returns the value of attribute offset.
Instance Method Summary collapse
- #after_move(&block) ⇒ Object
- #current_item ⇒ Object
- #current_item?(i) ⇒ Boolean
- #drawable_items ⇒ Object
-
#initialize ⇒ Scroller
constructor
A new instance of Scroller.
- #item_appended! ⇒ Object
- #item_prepended! ⇒ Object
- #move_down ⇒ Object
- #move_to_bottom ⇒ Object
- #move_to_top ⇒ Object
- #move_up ⇒ Object
- #no_cursor_mode? ⇒ Boolean
- #nth_item_drawable?(n) ⇒ Boolean
- #respond_to_key(key) ⇒ Object
- #set_no_cursor_mode! ⇒ Object
Constructor Details
#initialize ⇒ Scroller
Returns a new instance of Scroller.
50 51 52 53 54 |
# File 'lib/twterm/tab/scrollable.rb', line 50 def initialize @index = 0 @offset = 0 @no_cursor_mode = false end |
Instance Attribute Details
#delegate ⇒ Object
Returns the value of attribute delegate.
31 32 33 |
# File 'lib/twterm/tab/scrollable.rb', line 31 def delegate @delegate end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
29 30 31 |
# File 'lib/twterm/tab/scrollable.rb', line 29 def index @index end |
#offset ⇒ Object (readonly)
Returns the value of attribute offset.
29 30 31 |
# File 'lib/twterm/tab/scrollable.rb', line 29 def offset @offset end |
Instance Method Details
#after_move(&block) ⇒ Object
34 35 36 |
# File 'lib/twterm/tab/scrollable.rb', line 34 def after_move(&block) add_hook(:after_move, &block) end |
#current_item ⇒ Object
38 39 40 |
# File 'lib/twterm/tab/scrollable.rb', line 38 def current_item items[index] end |
#current_item?(i) ⇒ Boolean
42 43 44 |
# File 'lib/twterm/tab/scrollable.rb', line 42 def current_item?(i) index == offset + i end |
#drawable_items ⇒ Object
56 57 58 |
# File 'lib/twterm/tab/scrollable.rb', line 56 def drawable_items items.drop(offset).take(drawable_item_count) end |
#item_appended! ⇒ Object
60 61 62 63 |
# File 'lib/twterm/tab/scrollable.rb', line 60 def item_appended! @index -= 1 @offset -= 1 if @offset > 0 end |
#item_prepended! ⇒ Object
65 66 67 68 69 |
# File 'lib/twterm/tab/scrollable.rb', line 65 def item_prepended! @index += 1 @offset += 1 unless @index < 4 # keep cursor position unless it is on the top end |
#move_down ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/twterm/tab/scrollable.rb', line 71 def move_down return if count == 0 || index == count - 1 # return when there are no items or cursor is at the bottom @index += 1 @offset += 1 if (no_cursor_mode? || cursor_on_the_downside?) && !last_item_shown? hook :after_move end |
#move_to_bottom ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/twterm/tab/scrollable.rb', line 81 def move_to_bottom return if count == 0 || index == count - 1 @index = count - 1 @offset = [count - drawable_item_count + 1, 0].max @offset += 1 until last_item_shown? hook :after_move end |
#move_to_top ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/twterm/tab/scrollable.rb', line 92 def move_to_top return if count.zero? || index.zero? @index = 0 @offset = 0 hook :after_move end |
#move_up ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/twterm/tab/scrollable.rb', line 101 def move_up return if count.zero? || index.zero? @index -= 1 @offset -= 1 if cursor_on_the_upside? && !first_item_shown? hook :after_move end |
#no_cursor_mode? ⇒ Boolean
46 47 48 |
# File 'lib/twterm/tab/scrollable.rb', line 46 def no_cursor_mode? !!@no_cursor_mode end |
#nth_item_drawable?(n) ⇒ Boolean
110 111 112 |
# File 'lib/twterm/tab/scrollable.rb', line 110 def nth_item_drawable?(n) n.between?(offset, offset + drawable_item_count) end |
#respond_to_key(key) ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/twterm/tab/scrollable.rb', line 114 def respond_to_key(key) case key when ?d, 4 10.times { move_down } when ?g move_to_top when ?G move_to_bottom when ?j, 14, Curses::Key::DOWN move_down when ?k, 16, Curses::Key::UP move_up when ?u, 21 10.times { move_up } else return false end true end |
#set_no_cursor_mode! ⇒ Object
135 136 137 |
# File 'lib/twterm/tab/scrollable.rb', line 135 def set_no_cursor_mode! @no_cursor_mode = true end |