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:) ⇒ 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
Constructor Details
#initialize(top: 0, height:) ⇒ Viewport
Returns a new instance of Viewport.
8 9 10 11 |
# File 'lib/fatty/viewport.rb', line 8 def initialize(top: 0, height:) @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
13 14 15 |
# File 'lib/fatty/viewport.rb', line 13 def adjust_for_trim(n) @top = [@top - n, 0].max end |
#at_bottom?(total_lines = nil) ⇒ Boolean
17 18 19 20 21 |
# File 'lib/fatty/viewport.rb', line 17 def at_bottom?(total_lines = nil) return false unless total_lines @top >= max_top(total_lines) end |
#at_top? ⇒ Boolean
23 24 25 |
# File 'lib/fatty/viewport.rb', line 23 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.
28 29 30 31 32 |
# File 'lib/fatty/viewport.rb', line 28 def bottom_index(total_lines) return 0 if total_lines <= 0 [@top + @height, total_lines].min end |
#clamp!(lines) ⇒ Object
50 51 52 |
# File 'lib/fatty/viewport.rb', line 50 def clamp!(lines) @top = @top.clamp(0, max_top(lines.size)) end |
#max_top(total_lines) ⇒ Object
46 47 48 |
# File 'lib/fatty/viewport.rb', line 46 def max_top(total_lines) [total_lines - @height, 0].max end |
#page_bottom(lines) ⇒ Object
Jump to the very bottom.
82 83 84 |
# File 'lib/fatty/viewport.rb', line 82 def page_bottom(lines) @top = max_top(lines.size) end |
#page_down(lines) ⇒ Object
72 73 74 |
# File 'lib/fatty/viewport.rb', line 72 def page_down(lines) scroll_down(lines, @height) end |
#page_top ⇒ Object
Jump to the very top.
77 78 79 |
# File 'lib/fatty/viewport.rb', line 77 def page_top @top = 0 end |
#page_up(lines) ⇒ Object
68 69 70 |
# File 'lib/fatty/viewport.rb', line 68 def page_up(lines) scroll_up(lines, @height) end |
#position_percent(total_lines) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/fatty/viewport.rb', line 34 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
86 87 88 |
# File 'lib/fatty/viewport.rb', line 86 def reset @top = 0 end |
#scroll_down(lines, n = 1) ⇒ Object
59 60 61 62 |
# File 'lib/fatty/viewport.rb', line 59 def scroll_down(lines, n = 1) @top += n clamp!(lines) end |
#scroll_to_bottom(lines) ⇒ Object
64 65 66 |
# File 'lib/fatty/viewport.rb', line 64 def scroll_to_bottom(lines) @top = max_top(lines.size) end |
#scroll_up(lines, n = 1) ⇒ Object
54 55 56 57 |
# File 'lib/fatty/viewport.rb', line 54 def scroll_up(lines, n = 1) @top -= n clamp!(lines) end |
#slice(lines) ⇒ Object
42 43 44 |
# File 'lib/fatty/viewport.rb', line 42 def slice(lines) lines[@top, @height] || [] end |