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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interface) ⇒ Viewport

Returns an instance of Vedeu::Viewport.

Parameters:

  • interface (Interface)

    An instance of interface.



44
45
46
# File 'lib/vedeu/output/viewport.rb', line 44

def initialize(interface)
  @interface = interface
end

Instance Attribute Details

#interfaceVedeu::Interface (readonly, protected)

Returns:



78
79
80
# File 'lib/vedeu/output/viewport.rb', line 78

def interface
  @interface
end

Class Method Details

.render(interface) ⇒ Array<Array<Vedeu::Char>>

Parameters:

Returns:



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

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

#columnsRange (private)

Note:

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.

Returns:

  • (Range)


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.

Parameters:

  • offset (Fixnum)

    The cursor’s oy or ox values.

  • dimension (Fixnum)

    Either the height or width.

Returns:

  • (Fixnum)


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

#cursorVedeu::Cursor (private)

Fetch the cursor associated with the interface we are drawing.

Returns:



159
160
161
# File 'lib/vedeu/output/viewport.rb', line 159

def cursor
  @cursor ||= Vedeu.cursors.by_name(name)
end

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

#leftFixnum (private)

Returns:

  • (Fixnum)


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

def left
  @left ||= content_offset(ox, width)
end

#renderArray<Array<String>>

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

Returns:

  • (Array<Array<String>>)


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

#rowsRange (private)

Note:

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.

Returns:

  • (Range)


105
106
107
# File 'lib/vedeu/output/viewport.rb', line 105

def rows
  top..(top + (geometry.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)


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_sString

Returns a string representation of the viewport.

Returns:

  • (String)


70
71
72
# File 'lib/vedeu/output/viewport.rb', line 70

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

#topFixnum (private)

Returns:

  • (Fixnum)


128
129
130
# File 'lib/vedeu/output/viewport.rb', line 128

def top
  @top ||= content_offset(oy, height)
end