Class: Twterm::Tab::Scrollable::Scroller

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Publisher
Defined in:
lib/twterm/tab/scrollable.rb

Direct Known Subclasses

Twterm::Tab::Searchable::Scroller

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Publisher

#publish

Methods included from Utils

check_type

Constructor Details

#initialize(tab) ⇒ Scroller

Returns a new instance of Scroller.



48
49
50
51
52
53
54
# File 'lib/twterm/tab/scrollable.rb', line 48

def initialize(tab)
  @tab = tab

  @index = 0
  @offset = 0
  @no_cursor_mode = false
end

Instance Attribute Details

#delegateObject

Returns the value of attribute delegate.



29
30
31
# File 'lib/twterm/tab/scrollable.rb', line 29

def delegate
  @delegate
end

#indexObject (readonly)

Returns the value of attribute index.



27
28
29
# File 'lib/twterm/tab/scrollable.rb', line 27

def index
  @index
end

#offsetObject (readonly)

Returns the value of attribute offset.



27
28
29
# File 'lib/twterm/tab/scrollable.rb', line 27

def offset
  @offset
end

Instance Method Details

#after_move(&block) ⇒ Object



32
33
34
# File 'lib/twterm/tab/scrollable.rb', line 32

def after_move(&block)
  add_hook(:after_move, &block)
end

#current_index?(i) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/twterm/tab/scrollable.rb', line 36

def current_index?(i)
  index == offset + i
end

#current_itemObject



40
41
42
# File 'lib/twterm/tab/scrollable.rb', line 40

def current_item
  items[index]
end

#drawable_itemsObject



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
# File 'lib/twterm/tab/scrollable.rb', line 60

def item_appended!
  @offset -= 1 if @offset > 0
end

#item_prepended!Object



64
65
66
67
68
# File 'lib/twterm/tab/scrollable.rb', line 64

def item_prepended!
  @index += 1
  @offset += 1 unless @index < 4
  # keep cursor position unless it is on the top
end

#move_downObject



70
71
72
73
74
75
76
77
78
# File 'lib/twterm/tab/scrollable.rb', line 70

def move_down
  return if count == 0 || index == count - 1 || (no_cursor_mode? && offset + drawable_item_count >= count)
  # 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(n) ⇒ Object



91
92
93
94
95
96
97
98
99
# File 'lib/twterm/tab/scrollable.rb', line 91

def move_to(n)
  if nth_item_drawable?(n)
    @index = n
  else
    @index = @offset = n
  end

  hook :after_move
end

#move_to_bottomObject



80
81
82
83
84
85
86
87
88
89
# File 'lib/twterm/tab/scrollable.rb', line 80

def move_to_bottom
  return if count == 0 || index == count - 1

  @index = no_cursor_mode? ? count - drawable_item_count : count - 1
  @offset = [count - drawable_item_count, 0].max

  @offset += 1 until last_item_shown?

  hook :after_move
end

#move_to_topObject



101
102
103
104
105
106
107
108
# File 'lib/twterm/tab/scrollable.rb', line 101

def move_to_top
  return if count.zero? || index.zero?

  @index = 0
  @offset = 0

  hook :after_move
end

#move_upObject



110
111
112
113
114
115
116
117
# File 'lib/twterm/tab/scrollable.rb', line 110

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

Returns:

  • (Boolean)


44
45
46
# File 'lib/twterm/tab/scrollable.rb', line 44

def no_cursor_mode?
  !!@no_cursor_mode
end

#nth_item_drawable?(n) ⇒ Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/twterm/tab/scrollable.rb', line 119

def nth_item_drawable?(n)
  n.between?(offset, offset + drawable_item_count - 1)
end

#respond_to_key(key) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/twterm/tab/scrollable.rb', line 123

def respond_to_key(key)
  k = KeyMapper.instance

  case key
  when k[:general, :page_down]
    10.times { move_down }
  when k[:general, :top]
    move_to_top
  when k[:general, :bottom]
    move_to_bottom
  when k[:general, :down], Curses::Key::DOWN
    move_down
  when k[:general, :up], Curses::Key::UP
    move_up
  when k[:general, :page_up]
    10.times { move_up }
  when k[:cursor, :top_of_window]
    move_to(offset)
  when k[:cursor, :middle_of_window]
    move_to((2 * offset + [drawable_item_count, total_item_count - offset].min - 1) / 2)
  when k[:cursor, :bottom_of_window]
    move_to(offset + [drawable_item_count, total_item_count - offset].min - 1)
  else
    return false
  end

  true
end

#set_no_cursor_mode!Object



152
153
154
# File 'lib/twterm/tab/scrollable.rb', line 152

def set_no_cursor_mode!
  @no_cursor_mode = true
end