Module: Twterm::Tab::Scrollable

Includes:
Base
Included in:
New::List, StatusesTab
Defined in:
lib/twterm/tab/scrollable.rb

Instance Attribute Summary

Attributes included from Base

#title

Instance Method Summary collapse

Methods included from Base

#==, #close, #refresh

Instance Method Details

#countObject



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

def count
  @scrollable_count
end

#draw_scroll_barObject



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/twterm/tab/scrollable.rb', line 115

def draw_scroll_bar
  return if count == 0

  height = @window.maxy
  top = height * index / count

  @window.with_color(:black, :white) do
    @scrollable_scrollbar_length.times do |i|
      @window.setpos(top + i, @window.maxx - 1)
      @window.addch(' ')
    end
  end
end

#indexObject



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

def index
  @scrollable_index
end

#initializeObject



6
7
8
9
10
11
12
13
14
# File 'lib/twterm/tab/scrollable.rb', line 6

def initialize
  super

  @scrollable_index = 0
  @scrollable_count = 0
  @scrollable_offset = 0
  @scrollable_last = 0
  @scrollable_scrollbar_length = 0
end

#item_appendedObject



58
59
60
61
62
# File 'lib/twterm/tab/scrollable.rb', line 58

def item_appended
  @scrollable_index -= 1
  @scrollable_offset -= 1 if @scrollable_offset > 0
  update_scrollbar_length
end

#item_prependedObject



52
53
54
55
56
# File 'lib/twterm/tab/scrollable.rb', line 52

def item_prepended
  @scrollable_index += 1
  @scrollable_offset += 1
  update_scrollbar_length
end

#lastObject



48
49
50
# File 'lib/twterm/tab/scrollable.rb', line 48

def last
  @scrollable_last
end

#move_downObject



72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/twterm/tab/scrollable.rb', line 72

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

  @scrollable_index = [index + 1, count - 1].min
  @scrollable_offset = [
    offset + 1,
    count - 1,
    count - offset_from_bottom
  ].min if index > last - 4

  refresh
end

#move_to_bottomObject



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

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

  @scrollable_index = count - 1
  @scrollable_offset = count - 1 - offset_from_bottom
  refresh
end

#move_to_topObject



85
86
87
88
89
90
91
# File 'lib/twterm/tab/scrollable.rb', line 85

def move_to_top
  return if count == 0 || index == 0

  @scrollable_index = 0
  @scrollable_offset = 0
  refresh
end

#move_upObject



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

def move_up
  return if count == 0 || index == 0

  @scrollable_index = [index - 1, 0].max
  @scrollable_offset = [offset - 1, 0].max if index - 4 < offset
  refresh
end

#offsetObject



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

def offset
  @scrollable_offset
end

#offset_from_bottomObject



101
102
103
# File 'lib/twterm/tab/scrollable.rb', line 101

def offset_from_bottom
  0
end

#respond_to_key(key) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/twterm/tab/scrollable.rb', line 16

def respond_to_key(key)
  case key
  when 'g'
    move_to_top
  when 'G'
    move_to_bottom
  when 'j', 14, Key::DOWN
    move_down
  when 'k', 16, Key::UP
    move_up
  when 'd', 4
    10.times { move_down }
  when 'u', 21
    10.times { move_up }
  else
    return false
  end
  true
end

#update_scrollbar_lengthObject



105
106
107
108
109
110
111
112
113
# File 'lib/twterm/tab/scrollable.rb', line 105

def update_scrollbar_length
  @scrollable_scrollbar_length =
    if count == 0
      0
    else
      height = @window.maxy
      [height * (last - index + 1) / count, 1].max
    end
end