Class: Vedeu::VirtualTerminal
- Inherits:
-
Object
- Object
- Vedeu::VirtualTerminal
- Defined in:
- lib/vedeu/output/virtual_terminal.rb
Overview
Instance Attribute Summary collapse
- #height ⇒ Fixnum readonly
- #renderer ⇒ void
- #width ⇒ Fixnum readonly
Instance Method Summary collapse
-
#cells ⇒ Array<Array<Vedeu::Char>>
Return a grid of Char objects defined by the height and width of this virtual terminal.
- #fetch(from, which) ⇒ Array<Vedeu::Char>|Array private
-
#initialize(height, width, renderer = Vedeu::Renderers::HTML) ⇒ Vedeu::VirtualTerminal
constructor
Returns a new instance of Vedeu::VirtualTerminal.
- #new_virtual_terminal ⇒ Array<Array<Vedeu::Cell>> private
-
#output(data) ⇒ Array<Array<Vedeu::Char>>
Write a collection of cells to the virtual terminal.
-
#read(y, x) ⇒ Vedeu::Char
Read a single cell from the virtual terminal.
-
#render ⇒ String|void
Send the cells to the renderer and return the rendered result.
-
#reset ⇒ Array<Array<Vedeu::Char>>
(also: #clear)
Removes all content from the virtual terminal; effectively clearing it.
-
#write(y, x, data) ⇒ Vedeu::Char
Write a single cell to the virtual terminal.
Constructor Details
#initialize(height, width, renderer = Vedeu::Renderers::HTML) ⇒ Vedeu::VirtualTerminal
Returns a new instance of Vedeu::VirtualTerminal.
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
#height ⇒ Fixnum (readonly)
12 13 14 |
# File 'lib/vedeu/output/virtual_terminal.rb', line 12 def height @height end |
#renderer ⇒ void
This method returns an undefined value.
8 9 10 |
# File 'lib/vedeu/output/virtual_terminal.rb', line 8 def renderer @renderer end |
#width ⇒ Fixnum (readonly)
16 17 18 |
# File 'lib/vedeu/output/virtual_terminal.rb', line 16 def width @width end |
Instance Method Details
#cells ⇒ Array<Array<Vedeu::Char>>
Return a grid of Char objects defined by the height and width of this virtual terminal.
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)
109 110 111 |
# File 'lib/vedeu/output/virtual_terminal.rb', line 109 def fetch(from, which) from[which] || [] end |
#new_virtual_terminal ⇒ Array<Array<Vedeu::Cell>> (private)
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.
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
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.
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 |
#render ⇒ String|void
Send the cells to the renderer and return the rendered result.
72 73 74 |
# File 'lib/vedeu/output/virtual_terminal.rb', line 72 def render renderer.render(cells) end |
#reset ⇒ Array<Array<Vedeu::Char>> Also known as: clear
Removes all content from the virtual terminal; effectively clearing it.
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
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.
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 |