Class: Vedeu::Viewport
- Inherits:
-
Object
- Object
- Vedeu::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, the Viewport class provides ‘scrolling’ via the cursor’s position.
Instance Attribute Summary collapse
- #interface ⇒ Vedeu::Interface readonly protected
Class Method Summary collapse
Instance Method Summary collapse
-
#border ⇒ Object
private
Return the border associated with the interface we are drawing.
-
#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::Cursor
private
Fetch the cursor associated with the interface we are drawing.
-
#geometry ⇒ Object
private
Return the geometry associated with the interface we are drawing.
-
#initialize(interface) ⇒ Viewport
constructor
Returns an instance of Vedeu::Viewport.
- #left ⇒ Fixnum private
-
#render ⇒ Array<Array<String>>
Returns the interface with border (if enabled) and the content for the interface.
-
#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 interface.
-
#to_s ⇒ String
Returns a string representation of the viewport.
- #top ⇒ Fixnum private
Constructor Details
#initialize(interface) ⇒ Viewport
Returns an instance of Vedeu::Viewport.
44 45 46 |
# File 'lib/vedeu/output/viewport.rb', line 44 def initialize(interface) @interface = interface end |
Instance Attribute Details
#interface ⇒ Vedeu::Interface (readonly, protected)
78 79 80 |
# File 'lib/vedeu/output/viewport.rb', line 78 def interface @interface end |
Class Method Details
.render(interface) ⇒ Array<Array<Vedeu::Char>>
30 31 32 33 34 35 36 37 38 |
# File 'lib/vedeu/output/viewport.rb', line 30 def self.render(interface) if interface.visible? new(interface).render else [] end end |
Instance Method Details
#border ⇒ Object (private)
Return the border associated with the interface we are drawing.
152 153 154 |
# File 'lib/vedeu/output/viewport.rb', line 152 def border @border ||= Vedeu.borders.by_name(name) 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.
118 119 120 |
# File 'lib/vedeu/output/viewport.rb', line 118 def columns left..(left + (geometry.width - 1)) 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.
139 140 141 142 143 144 145 146 147 |
# File 'lib/vedeu/output/viewport.rb', line 139 def content_offset(offset, dimension) if offset >= dimension && ((offset - dimension) > 0) offset - dimension else 0 end end |
#cursor ⇒ Vedeu::Cursor (private)
Fetch the cursor associated with the interface we are drawing.
159 160 161 |
# File 'lib/vedeu/output/viewport.rb', line 159 def cursor @cursor ||= Vedeu.cursors.by_name(name) end |
#geometry ⇒ Object (private)
Return the geometry associated with the interface we are drawing.
166 167 168 |
# File 'lib/vedeu/output/viewport.rb', line 166 def geometry @geometry || Vedeu.geometries.by_name(name) end |
#left ⇒ Fixnum (private)
123 124 125 |
# File 'lib/vedeu/output/viewport.rb', line 123 def left @left ||= content_offset(ox, width) end |
#render ⇒ Array<Array<String>>
Returns the interface with border (if enabled) and the content for the interface.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/vedeu/output/viewport.rb', line 52 def render return [] unless visible? Vedeu.log(type: :output, message: "Rendering: '#{name}'") out = [] show[0...height].each_with_index do |line, iy| line[0...width].each_with_index do |column, ix| column.position = Vedeu::IndexPosition[iy, ix, by, bx] out << column end end out 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.
105 106 107 |
# File 'lib/vedeu/output/viewport.rb', line 105 def rows top..(top + (geometry.height - 1)) 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 interface.
90 91 92 93 94 |
# File 'lib/vedeu/output/viewport.rb', line 90 def show return [] unless lines? (lines[rows] || []).map { |line| (line.chars[columns] || []) } end |
#to_s ⇒ String
Returns a string representation of the viewport.
70 71 72 |
# File 'lib/vedeu/output/viewport.rb', line 70 def to_s render.map(&:to_s).join("\n") end |
#top ⇒ Fixnum (private)
128 129 130 |
# File 'lib/vedeu/output/viewport.rb', line 128 def top @top ||= content_offset(oy, height) end |