Class: Fatty::Viewport
- Inherits:
-
Object
- Object
- Fatty::Viewport
- Defined in:
- lib/fatty/viewport.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#top ⇒ Object
Returns the value of attribute top.
Instance Method Summary collapse
- #adjust_for_trim(n) ⇒ Object
- #at_bottom?(total_lines = nil) ⇒ Boolean
- #at_top? ⇒ Boolean
-
#bottom_index(total_lines) ⇒ Object
1-based index of the last visible line (clamped), or 0 if no lines.
- #clamp!(lines) ⇒ Object
-
#initialize(top: 0, height: 10) ⇒ Viewport
constructor
A new instance of Viewport.
- #max_top(total_lines) ⇒ Object
-
#page_bottom(lines) ⇒ Object
Jump to the very bottom.
- #page_down(lines) ⇒ Object
-
#page_top ⇒ Object
Jump to the very top.
- #page_up(lines) ⇒ Object
- #position_percent(total_lines) ⇒ Object
- #reset ⇒ Object
- #scroll_down(lines, n = 1) ⇒ Object
- #scroll_to_bottom(lines) ⇒ Object
- #scroll_up(lines, n = 1) ⇒ Object
- #slice(lines) ⇒ Object
- #state ⇒ Object
Constructor Details
#initialize(top: 0, height: 10) ⇒ Viewport
Returns a new instance of Viewport.
8 9 10 11 |
# File 'lib/fatty/viewport.rb', line 8 def initialize(top: 0, height: 10) @top = top @height = height end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
6 7 8 |
# File 'lib/fatty/viewport.rb', line 6 def height @height end |
#top ⇒ Object
Returns the value of attribute top.
5 6 7 |
# File 'lib/fatty/viewport.rb', line 5 def top @top end |
Instance Method Details
#adjust_for_trim(n) ⇒ Object
20 21 22 |
# File 'lib/fatty/viewport.rb', line 20 def adjust_for_trim(n) @top = [@top - n, 0].max end |
#at_bottom?(total_lines = nil) ⇒ Boolean
24 25 26 27 28 |
# File 'lib/fatty/viewport.rb', line 24 def at_bottom?(total_lines = nil) return false unless total_lines @top >= max_top(total_lines) end |
#at_top? ⇒ Boolean
30 31 32 |
# File 'lib/fatty/viewport.rb', line 30 def at_top? @top <= 0 end |
#bottom_index(total_lines) ⇒ Object
1-based index of the last visible line (clamped), or 0 if no lines.
35 36 37 38 39 |
# File 'lib/fatty/viewport.rb', line 35 def bottom_index(total_lines) return 0 if total_lines <= 0 [@top + @height, total_lines].min end |
#clamp!(lines) ⇒ Object
57 58 59 |
# File 'lib/fatty/viewport.rb', line 57 def clamp!(lines) @top = @top.clamp(0, max_top(lines.size)) end |
#max_top(total_lines) ⇒ Object
53 54 55 |
# File 'lib/fatty/viewport.rb', line 53 def max_top(total_lines) [total_lines - @height, 0].max end |
#page_bottom(lines) ⇒ Object
Jump to the very bottom.
89 90 91 |
# File 'lib/fatty/viewport.rb', line 89 def page_bottom(lines) @top = max_top(lines.size) end |
#page_down(lines) ⇒ Object
79 80 81 |
# File 'lib/fatty/viewport.rb', line 79 def page_down(lines) scroll_down(lines, @height) end |
#page_top ⇒ Object
Jump to the very top.
84 85 86 |
# File 'lib/fatty/viewport.rb', line 84 def page_top @top = 0 end |
#page_up(lines) ⇒ Object
75 76 77 |
# File 'lib/fatty/viewport.rb', line 75 def page_up(lines) scroll_up(lines, @height) end |
#position_percent(total_lines) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/fatty/viewport.rb', line 41 def position_percent(total_lines) return 100 if total_lines <= 0 b = bottom_index(total_lines) pct = (b.to_f / total_lines.to_f) * 100.0 pct.round end |
#reset ⇒ Object
93 94 95 |
# File 'lib/fatty/viewport.rb', line 93 def reset @top = 0 end |
#scroll_down(lines, n = 1) ⇒ Object
66 67 68 69 |
# File 'lib/fatty/viewport.rb', line 66 def scroll_down(lines, n = 1) @top += n clamp!(lines) end |
#scroll_to_bottom(lines) ⇒ Object
71 72 73 |
# File 'lib/fatty/viewport.rb', line 71 def scroll_to_bottom(lines) @top = max_top(lines.size) end |
#scroll_up(lines, n = 1) ⇒ Object
61 62 63 64 |
# File 'lib/fatty/viewport.rb', line 61 def scroll_up(lines, n = 1) @top -= n clamp!(lines) end |
#slice(lines) ⇒ Object
49 50 51 |
# File 'lib/fatty/viewport.rb', line 49 def slice(lines) lines[@top, @height] || [] end |
#state ⇒ Object
13 14 15 16 17 18 |
# File 'lib/fatty/viewport.rb', line 13 def state [ top, height, ] end |