Class: Vedeu::VirtualTerminal

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

Overview

Represents a Terminal view.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Parameters:

  • height (Fixnum)
  • width (Fixnum)
  • renderer (Object|HTMLRenderer) (defaults to: HTMLRenderer)

    An object responding to .render.



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

def initialize(height, width, renderer = HTMLRenderer)
  @cell_height, @cell_width = Vedeu::PositionIndex[height, width]
  @height   = height
  @width    = width
  @renderer = renderer
end

Instance Attribute Details

#cell_heightObject (readonly)

Returns the value of attribute cell_height.



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

def cell_height
  @cell_height
end

#cell_widthObject (readonly)

Returns the value of attribute cell_width.



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

def cell_width
  @cell_width
end

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

#rendererObject

Returns the value of attribute renderer.



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

def renderer
  @renderer
end

#widthObject (readonly)

Returns the value of attribute width.



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

def width
  @width
end

Instance Method Details

#cellsArray<Array<Vedeu::Char>>

Returns:



22
23
24
# File 'lib/vedeu/output/virtual_terminal.rb', line 22

def cells
  @cells ||= new_virtual_terminal
end

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

Parameters:

  • from (Array)
  • which (Array)

Returns:



86
87
88
# File 'lib/vedeu/output/virtual_terminal.rb', line 86

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

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

Returns:



91
92
93
# File 'lib/vedeu/output/virtual_terminal.rb', line 91

def new_virtual_terminal
  Array.new(cell_height) { Array.new(cell_width) { Vedeu::Char.new } }
end

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

Write a collection of cells to the virtual terminal.

Parameters:

Returns:



44
45
46
47
48
49
50
# File 'lib/vedeu/output/virtual_terminal.rb', line 44

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

Read a single cell from the virtual terminal.

Parameters:

Returns:



31
32
33
34
35
36
37
38
# File 'lib/vedeu/output/virtual_terminal.rb', line 31

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

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

  cell
end

#rendervoid

This method returns an undefined value.



53
54
55
# File 'lib/vedeu/output/virtual_terminal.rb', line 53

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:



60
61
62
# File 'lib/vedeu/output/virtual_terminal.rb', line 60

def reset
  @cells = new_virtual_terminal
end

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

Write a single cell to the virtual terminal.

Parameters:

Returns:



71
72
73
74
75
76
77
78
79
# File 'lib/vedeu/output/virtual_terminal.rb', line 71

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

  return false unless read(cy, cx).is_a?(Vedeu::Char)

  cells[cy][cx] = data

  true
end