Class: Vedeu::VirtualTerminal

Inherits:
Object
  • Object
show all
Defined in:
lib/vedeu/output/virtual_terminal.rb

Overview

Represents a Terminal view as a grid of Cell objects.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(height, width, renderer = Vedeu::Renderers::HTML) ⇒ Vedeu::VirtualTerminal

Returns a new instance of Vedeu::VirtualTerminal.

Parameters:

  • height (Fixnum)
  • width (Fixnum)
  • renderer (Object|Renderers::HTML) (defaults to: Vedeu::Renderers::HTML)

    An object responding to .render.



24
25
26
27
28
# File 'lib/vedeu/output/virtual_terminal.rb', line 24

def initialize(height, width, renderer = Vedeu::Renderers::HTML)
  @height   = height
  @width    = width
  @renderer = renderer
end

Instance Attribute Details

#heightFixnum (readonly)

Returns:

  • (Fixnum)


12
13
14
# File 'lib/vedeu/output/virtual_terminal.rb', line 12

def height
  @height
end

#renderervoid

This method returns an undefined value.



8
9
10
# File 'lib/vedeu/output/virtual_terminal.rb', line 8

def renderer
  @renderer
end

#widthFixnum (readonly)

Returns:

  • (Fixnum)


16
17
18
# File 'lib/vedeu/output/virtual_terminal.rb', line 16

def width
  @width
end

Instance Method Details

#cellsArray<Array<Vedeu::Char>>

Return a grid of Char objects defined by the height and width of this virtual terminal.

Returns:



34
35
36
# File 'lib/vedeu/output/virtual_terminal.rb', line 34

def cells
  @cells ||= new_virtual_terminal
end

#fetch(from, which) ⇒ Array<Vedeu::Char>|Array (private)

Parameters:

  • from (Array)

    An Array of rows, or an Array of cells.

  • which (Fixnum)

    A Fixnum representing the index; the row number or the cell number for a row.

Returns:



109
110
111
# File 'lib/vedeu/output/virtual_terminal.rb', line 109

def fetch(from, which)
  from[which] || []
end

#new_virtual_terminalArray<Array<Vedeu::Cell>> (private)

Returns:

See Also:

  • Vedeu::VirtualTerminal.{Vedeu{Vedeu::VirtualTerminal{Vedeu::VirtualTerminal#cells}


115
116
117
# File 'lib/vedeu/output/virtual_terminal.rb', line 115

def new_virtual_terminal
  Array.new(height) { Array.new(width) { Vedeu::Cell.new } }
end

#output(data) ⇒ Array<Array<Vedeu::Char>>

Write a collection of cells to the virtual terminal.

Parameters:

Returns:



61
62
63
64
65
66
67
# File 'lib/vedeu/output/virtual_terminal.rb', line 61

def output(data)
  Array(data).flatten.each do |char|
    write(char.y, char.x, char) if char.is_a?(Vedeu::Char)
  end

  cells
end

#read(y, x) ⇒ Vedeu::Char

Note:

Given two actual coordinates (y, x) e.g. (1, 1) Convert to coordinate indices (cy, cx) e.g. (0, 0) Fetch the row at cy and return the cell from cx

Read a single cell from the virtual terminal.

Parameters:

  • y (Fixnum)
  • x (Fixnum)

Returns:



48
49
50
51
52
53
54
55
# File 'lib/vedeu/output/virtual_terminal.rb', line 48

def read(y, x)
  cy, cx = Vedeu::PositionIndex[y, x]

  row  = fetch(cells, cy)
  cell = fetch(row, cx)

  cell
end

#renderString|void

Send the cells to the renderer and return the rendered result.

Returns:

  • (String|void)

    Most likely to be a String.



72
73
74
# File 'lib/vedeu/output/virtual_terminal.rb', line 72

def render
  renderer.render(cells)
end

#resetArray<Array<Vedeu::Char>> Also known as: clear

Removes all content from the virtual terminal; effectively clearing it.

Returns:



79
80
81
# File 'lib/vedeu/output/virtual_terminal.rb', line 79

def reset
  @cells = new_virtual_terminal
end

#write(y, x, data) ⇒ Vedeu::Char

Note:

If the position (y, x) is nil; we’re out of bounds. Otherwise, write the data to (cy, cx).

Write a single cell to the virtual terminal.

Parameters:

Returns:



94
95
96
97
98
99
100
101
# File 'lib/vedeu/output/virtual_terminal.rb', line 94

def write(y, x, data)
  return false unless read(y, x).is_a?(Vedeu::Cell)

  cy, cx = Vedeu::PositionIndex[y, x]
  cells[cy][cx] = data

  true
end