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 ⇒ Object
readonly
private
Returns the value of attribute interface.
Instance Method Summary collapse
-
#border ⇒ Vedeu::Border
private
Return the border associated with the interface we are drawing.
-
#border? ⇒ Boolean
private
Returns a boolean indicating the interface we are drawing has a border.
-
#bordered_height ⇒ Fixnum
private
When the viewport has a border, we need to account for that in our redrawing.
-
#bordered_width ⇒ Fixnum
private
When the viewport has a border, we need to account for that in our redrawing.
-
#columns ⇒ Range
private
Using the current cursor’s x position, return a range of visible columns.
-
#initialize(interface) ⇒ Viewport
constructor
Returns an instance of Viewport.
-
#left ⇒ Fixnum
private
Returns the offset for the content based on the offset.
-
#pad(visible, dimension) ⇒ Array<Array<String>>|Array<String>
private
Pads the number of rows or columns to always return an Array of the same length for each viewport or line respectively.
-
#padded_columns(line) ⇒ Array<String>
private
Returns the columns, padded where necessary for the given line.
-
#padded_lines ⇒ Array<Array<String>>
private
Returns the lines, padded where necessary for the viewport.
-
#render ⇒ Array<Array<String>>
Returns the interface with border (if enabled) and the content for the interface.
-
#reposition_x ⇒ Fixnum
private
Returns the number of columns to change the viewport by on the x axis, determined by the position of the x offset.
-
#reposition_x? ⇒ Boolean
private
Returns a boolean indicating whether the x offset is greater than or equal to the bordered width.
-
#reposition_y ⇒ Fixnum
private
Returns the number of rows to change the viewport by on the y axis, determined by the position of the y offset.
-
#reposition_y? ⇒ Boolean
private
Returns a boolean indicating whether the y offset is greater than or equal to the bordered height.
-
#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
Returns the offset for the content based on the offset.
Constructor Details
#initialize(interface) ⇒ Viewport
Returns an instance of Viewport.
31 32 33 |
# File 'lib/vedeu/output/viewport.rb', line 31 def initialize(interface) @interface = interface end |
Instance Attribute Details
#interface ⇒ Object (readonly, private)
Returns the value of attribute interface.
68 69 70 |
# File 'lib/vedeu/output/viewport.rb', line 68 def interface @interface end |
Instance Method Details
#border ⇒ Vedeu::Border (private)
Return the border associated with the interface we are drawing.
207 208 209 |
# File 'lib/vedeu/output/viewport.rb', line 207 def border @border ||= Vedeu.borders.find(interface.name) end |
#border? ⇒ Boolean (private)
Returns a boolean indicating the interface we are drawing has a border.
214 215 216 |
# File 'lib/vedeu/output/viewport.rb', line 214 def border? Vedeu.borders.registered?(interface.name) end |
#bordered_height ⇒ Fixnum (private)
When the viewport has a border, we need to account for that in our redrawing.
198 199 200 201 202 |
# File 'lib/vedeu/output/viewport.rb', line 198 def bordered_height return border.height if border? height end |
#bordered_width ⇒ Fixnum (private)
When the viewport has a border, we need to account for that in our redrawing.
188 189 190 191 192 |
# File 'lib/vedeu/output/viewport.rb', line 188 def bordered_width return border.width if border? width end |
#columns ⇒ Range (private)
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.
134 135 136 |
# File 'lib/vedeu/output/viewport.rb', line 134 def columns left..(left + (width - 1)) end |
#left ⇒ Fixnum (private)
Returns the offset for the content based on the offset.
141 142 143 |
# File 'lib/vedeu/output/viewport.rb', line 141 def left @left ||= reposition_x? ? reposition_x : 0 end |
#pad(visible, dimension) ⇒ Array<Array<String>>|Array<String> (private)
Pads the number of rows or columns to always return an Array of the same length for each viewport or line respectively.
109 110 111 112 113 114 115 116 |
# File 'lib/vedeu/output/viewport.rb', line 109 def pad(visible, dimension) dim = send(dimension) size = visible.size return visible unless size < dim visible + [" "] * (dim - size) end |
#padded_columns(line) ⇒ Array<String> (private)
Returns the columns, padded where necessary for the given line.
97 98 99 100 101 |
# File 'lib/vedeu/output/viewport.rb', line 97 def padded_columns(line) visible = line.chars[columns] || [] pad(visible, :width) end |
#padded_lines ⇒ Array<Array<String>> (private)
Returns the lines, padded where necessary for the viewport.
87 88 89 90 91 |
# File 'lib/vedeu/output/viewport.rb', line 87 def padded_lines visible = lines[rows] || [] pad(visible, :height) end |
#render ⇒ Array<Array<String>>
Returns the interface with border (if enabled) and the content for the interface.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/vedeu/output/viewport.rb', line 39 def render if border? out = [] out << border.top if border.top? show[0...bordered_height].each do |line| out << [border.left, line[0...bordered_width], border.right].flatten end out << border.bottom if border.bottom? out else show end end |
#reposition_x ⇒ Fixnum (private)
Returns the number of columns to change the viewport by on the x axis, determined by the position of the x offset.
172 173 174 |
# File 'lib/vedeu/output/viewport.rb', line 172 def reposition_x ((ox - bordered_width) <= 0) ? 0 : (ox - bordered_width) end |
#reposition_x? ⇒ Boolean (private)
Returns a boolean indicating whether the x offset is greater than or equal to the bordered width.
156 157 158 |
# File 'lib/vedeu/output/viewport.rb', line 156 def reposition_x? ox >= bordered_width end |
#reposition_y ⇒ Fixnum (private)
Returns the number of rows to change the viewport by on the y axis, determined by the position of the y offset.
180 181 182 |
# File 'lib/vedeu/output/viewport.rb', line 180 def reposition_y ((oy - bordered_height) <= 0) ? 0 : (oy - bordered_height) end |
#reposition_y? ⇒ Boolean (private)
Returns a boolean indicating whether the y offset is greater than or equal to the bordered height.
164 165 166 |
# File 'lib/vedeu/output/viewport.rb', line 164 def reposition_y? oy >= bordered_height end |
#rows ⇒ Range (private)
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.
124 125 126 |
# File 'lib/vedeu/output/viewport.rb', line 124 def rows top..(top + (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.
78 79 80 81 82 |
# File 'lib/vedeu/output/viewport.rb', line 78 def show return [] unless lines? padded_lines.map { |line| padded_columns(line) }.compact end |
#to_s ⇒ String
Returns a string representation of the viewport.
62 63 64 |
# File 'lib/vedeu/output/viewport.rb', line 62 def to_s render.map(&:join).join("\n") end |
#top ⇒ Fixnum (private)
Returns the offset for the content based on the offset.
148 149 150 |
# File 'lib/vedeu/output/viewport.rb', line 148 def top @top ||= reposition_y? ? reposition_y : 0 end |