Class: Vedeu::Viewport

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(interface) ⇒ Viewport

Returns an instance of Viewport.

Parameters:

  • interface (Interface)

    An instance of interface.



31
32
33
# File 'lib/vedeu/output/viewport.rb', line 31

def initialize(interface)
  @interface = interface
end

Instance Attribute Details

#interfaceObject (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

#borderVedeu::Border (private)

Return the border associated with the interface we are drawing.

Returns:



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.

Returns:

  • (Boolean)


214
215
216
# File 'lib/vedeu/output/viewport.rb', line 214

def border?
  Vedeu.borders.registered?(interface.name)
end

#bordered_heightFixnum (private)

When the viewport has a border, we need to account for that in our redrawing.

Returns:



198
199
200
201
202
# File 'lib/vedeu/output/viewport.rb', line 198

def bordered_height
  return border.height if border?

  height
end

#bordered_widthFixnum (private)

When the viewport has a border, we need to account for that in our redrawing.

Returns:



188
189
190
191
192
# File 'lib/vedeu/output/viewport.rb', line 188

def bordered_width
  return border.width if border?

  width
end

#columnsRange (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.

Returns:

  • (Range)


134
135
136
# File 'lib/vedeu/output/viewport.rb', line 134

def columns
  left..(left + (width - 1))
end

#leftFixnum (private)

Returns the offset for the content based on the offset.

Returns:



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.

Parameters:

  • visible (Array<Array<String>>|Array<String>)
  • dimension (Symbol)

    The dimension to pad (:height or :width).

Returns:

  • (Array<Array<String>>|Array<String>)


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.

Parameters:

  • line (Array<String>)

Returns:

  • (Array<String>)


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_linesArray<Array<String>> (private)

Returns the lines, padded where necessary for the viewport.

Returns:

  • (Array<Array<String>>)


87
88
89
90
91
# File 'lib/vedeu/output/viewport.rb', line 87

def padded_lines
  visible = lines[rows] || []

  pad(visible, :height)
end

#renderArray<Array<String>>

Returns the interface with border (if enabled) and the content for the interface.

Returns:

  • (Array<Array<String>>)


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_xFixnum (private)

Returns the number of columns to change the viewport by on the x axis, determined by the position of the x offset.

Returns:



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.

Returns:

  • (Boolean)


156
157
158
# File 'lib/vedeu/output/viewport.rb', line 156

def reposition_x?
  ox >= bordered_width
end

#reposition_yFixnum (private)

Returns the number of rows to change the viewport by on the y axis, determined by the position of the y offset.

Returns:



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.

Returns:

  • (Boolean)


164
165
166
# File 'lib/vedeu/output/viewport.rb', line 164

def reposition_y?
  oy >= bordered_height
end

#rowsRange (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.

Returns:

  • (Range)


124
125
126
# File 'lib/vedeu/output/viewport.rb', line 124

def rows
  top..(top + (height - 1))
end

#showArray (private)

Note:

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.

Returns:

  • (Array)


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_sString

Returns a string representation of the viewport.

Returns:

  • (String)


62
63
64
# File 'lib/vedeu/output/viewport.rb', line 62

def to_s
  render.map(&:join).join("\n")
end

#topFixnum (private)

Returns the offset for the content based on the offset.

Returns:



148
149
150
# File 'lib/vedeu/output/viewport.rb', line 148

def top
  @top ||= reposition_y? ? reposition_y : 0
end