Class: Vedeu::Output::Viewport
- Inherits:
-
Object
- Object
- Vedeu::Output::Viewport
- Extended by:
- Forwardable
- Defined in:
- lib/vedeu/output/viewport.rb
Overview
A Viewport is the visible part of the content within an interface.
When a buffer has more lines than the defined height, or more columns than the defined width of the interface, this class provides ‘scrolling’ via the cursor’s position.
Instance Attribute Summary collapse
- #view ⇒ Vedeu::Views::View readonly protected
Class Method Summary collapse
Instance Method Summary collapse
-
#border ⇒ Object
private
Return the border associated with the interface/view we are drawing.
- #bx ⇒ Fixnum private
- #by ⇒ Fixnum private
-
#columns ⇒ Range
private
Using the current cursor’s x position, return a range of visible columns.
-
#content_offset(offset, dimension) ⇒ Fixnum
private
Returns the offset for the content (the number of rows or columns to change the viewport by on either the y or x axis) determined by the offset (the cursor’s y or x offset position..
- #cursor ⇒ Vedeu::Cursors::Cursor private
-
#initialize(view) ⇒ Vedeu::Output::Viewport
constructor
Returns an instance of Vedeu::Output::Viewport.
- #left ⇒ Fixnum private
- #output ⇒ Array<Array<Vedeu::Views::Char>> private
-
#render ⇒ Array<Array<String>>|NilClass
Returns the content for the view.
-
#rows ⇒ Range
private
Using the current cursor’s y position, return a range of visible lines.
-
#show ⇒ Array
private
Returns the visible content for the view.
-
#to_s ⇒ String
(also: #to_str)
Returns a string representation of the viewport.
- #top ⇒ Fixnum private
Constructor Details
#initialize(view) ⇒ Vedeu::Output::Viewport
Returns an instance of Vedeu::Output::Viewport.
39 40 41 |
# File 'lib/vedeu/output/viewport.rb', line 39 def initialize(view) @view = view end |
Instance Attribute Details
#view ⇒ Vedeu::Views::View (readonly, protected)
62 63 64 |
# File 'lib/vedeu/output/viewport.rb', line 62 def view @view end |
Class Method Details
.render(view) ⇒ Array<Array<String>>|NilClass
31 32 33 |
# File 'lib/vedeu/output/viewport.rb', line 31 def self.render(view) new(view).render end |
Instance Method Details
#border ⇒ Object (private)
Return the border associated with the interface/view we are drawing.
152 153 154 |
# File 'lib/vedeu/output/viewport.rb', line 152 def border @border ||= Vedeu.borders.by_name(name) end |
#bx ⇒ Fixnum (private)
157 158 159 |
# File 'lib/vedeu/output/viewport.rb', line 157 def bx @bx ||= border.bx end |
#by ⇒ Fixnum (private)
162 163 164 |
# File 'lib/vedeu/output/viewport.rb', line 162 def by @by ||= border.by end |
#columns ⇒ Range (private)
The width is reduced by one as #columns is a range of Array elements.
Using the current cursor’s x position, return a range of visible columns.
Scrolls the content horizontally when the stored cursor’s x position for the interface is outside of the visible area.
121 122 123 |
# File 'lib/vedeu/output/viewport.rb', line 121 def columns left...(left + width) end |
#content_offset(offset, dimension) ⇒ Fixnum (private)
Returns the offset for the content (the number of rows or columns to change the viewport by on either the y or x axis) determined by the offset (the cursor’s y or x offset position.
142 143 144 145 146 |
# File 'lib/vedeu/output/viewport.rb', line 142 def content_offset(offset, dimension) return 0 unless offset >= dimension offset - dimension end |
#cursor ⇒ Vedeu::Cursors::Cursor (private)
168 169 170 |
# File 'lib/vedeu/output/viewport.rb', line 168 def cursor @cursor ||= Vedeu.cursors.by_name(name) end |
#left ⇒ Fixnum (private)
126 127 128 |
# File 'lib/vedeu/output/viewport.rb', line 126 def left @left ||= content_offset(ox, width) end |
#output ⇒ Array<Array<Vedeu::Views::Char>> (private)
67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/vedeu/output/viewport.rb', line 67 def output Vedeu.timer("Rendering content: '#{name}'".freeze) do out = [] show.each_with_index do |line, iy| line.each_with_index do |column, ix| column.position = [by + iy, bx + ix] out << column end end out end end |
#render ⇒ Array<Array<String>>|NilClass
Returns the content for the view.
46 47 48 |
# File 'lib/vedeu/output/viewport.rb', line 46 def render Vedeu.render_output(output) if visible? end |
#rows ⇒ Range (private)
The height is reduced by one as #rows is a range of Array elements.
Using the current cursor’s y position, return a range of visible lines.
Scrolls the content vertically when the stored cursor’s y position for the interface is outside of the visible area.
106 107 108 |
# File 'lib/vedeu/output/viewport.rb', line 106 def rows top...(top + height) end |
#show ⇒ Array (private)
If there are no lines of content, we return an empty array. If there are no more columns of content we return a space enclosed in an array; this prevents a weird line hopping bug which occurs when the current line has no more content, but subsequent lines do.
Returns the visible content for the view.
91 92 93 |
# File 'lib/vedeu/output/viewport.rb', line 91 def show (lines[rows] || []).map { |line| (line.chars[columns] || []) } end |
#to_s ⇒ String Also known as: to_str
Returns a string representation of the viewport.
53 54 55 |
# File 'lib/vedeu/output/viewport.rb', line 53 def to_s Array(render).map(&:to_s).join("\n") end |
#top ⇒ Fixnum (private)
131 132 133 |
# File 'lib/vedeu/output/viewport.rb', line 131 def top @top ||= content_offset(oy, height) end |