Module: Vedeu::Terminal::Buffer
Overview
All output will be written to this singleton, and #render will be called at the end of each run of MainLoop; effectively rendering this buffer to each registered renderer. This buffer is not cleared after this action though, as subsequent actions will modify the contents. This means that individual parts of Vedeu can write content here at various points and only at the end of each run of MainLoop will it be actually output ‘somewhere’.
Class Method Summary collapse
-
.buffer ⇒ Array<Array<Vedeu::Models::Cell>>
(also: #cells)
Return a grid of Models::Cell objects defined by the height and width of this virtual terminal.
- .empty_buffer ⇒ Array<Array<Vedeu::Models::Cell>>
- .fetch(from, which) ⇒ Array<Vedeu::Views::Char>|Array private
- .output ⇒ Vedeu::Models::Page
-
.position?(value) ⇒ Boolean
private
Returns a boolean indicating the value has a position attribute.
-
.read(y, x) ⇒ Vedeu::Views::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::Models::Cell>>
(also: #clear)
Removes all content from the virtual terminal; effectively clearing it.
-
.valid_position?(value) ⇒ Boolean
private
Returns a boolean indicating the value has a position attribute and is within the terminal boundary.
-
.within_terminal_boundary?(value) ⇒ Boolean
private
Returns a boolean indicating the position of the value object is valid for this terminal.
-
.write(value) ⇒ Array<Array<Vedeu::Views::Char>>
Write a collection of cells to the virtual terminal.
Instance Method Summary collapse
-
#buffer ⇒ Array<Array<Vedeu::Models::Cell>>
(also: #cells)
Return a grid of Models::Cell objects defined by the height and width of this virtual terminal.
- #empty_buffer ⇒ Array<Array<Vedeu::Models::Cell>>
- #fetch(from, which) ⇒ Array<Vedeu::Views::Char>|Array private
- #output ⇒ Vedeu::Models::Page
-
#position?(value) ⇒ Boolean
private
Returns a boolean indicating the value has a position attribute.
-
#read(y, x) ⇒ Vedeu::Views::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::Models::Cell>>
(also: #clear)
Removes all content from the virtual terminal; effectively clearing it.
-
#valid_position?(value) ⇒ Boolean
private
Returns a boolean indicating the value has a position attribute and is within the terminal boundary.
-
#within_terminal_boundary?(value) ⇒ Boolean
private
Returns a boolean indicating the position of the value object is valid for this terminal.
-
#write(value) ⇒ Array<Array<Vedeu::Views::Char>>
Write a collection of cells to the virtual terminal.
Class Method Details
.buffer ⇒ Array<Array<Vedeu::Models::Cell>> Also known as: cells
Return a grid of Models::Cell objects defined by the height and width of this virtual terminal.
21 22 23 |
# File 'lib/vedeu/terminal/buffer.rb', line 21 def buffer @output ||= empty_buffer.dup end |
.empty_buffer ⇒ Array<Array<Vedeu::Models::Cell>>
27 28 29 30 31 32 33 |
# File 'lib/vedeu/terminal/buffer.rb', line 27 def empty_buffer Array.new(Vedeu.height) do |y| Array.new(Vedeu.width) do |x| Vedeu::Models::Cell.new(position: [y + 1, x + 1]) end end end |
.fetch(from, which) ⇒ Array<Vedeu::Views::Char>|Array (private)
95 96 97 |
# File 'lib/vedeu/terminal/buffer.rb', line 95 def fetch(from, which) from[which] || [] end |
.output ⇒ Vedeu::Models::Page
36 37 38 |
# File 'lib/vedeu/terminal/buffer.rb', line 36 def output Vedeu::Models::Page.coerce(buffer) end |
.position?(value) ⇒ Boolean (private)
Returns a boolean indicating the value has a position attribute.
104 105 106 107 |
# File 'lib/vedeu/terminal/buffer.rb', line 104 def position?(value) value.respond_to?(:position) && value.position.is_a?(Vedeu::Geometry::Position) end |
.read(y, x) ⇒ Vedeu::Views::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.
50 51 52 53 54 55 56 57 |
# File 'lib/vedeu/terminal/buffer.rb', line 50 def read(y, x) cy, cx = Vedeu::Geometry::Position[y, x].as_indices row = fetch(cells, cy) cell = fetch(row, cx) cell end |
.render ⇒ String|void
Send the cells to the renderer and return the rendered result.
62 63 64 |
# File 'lib/vedeu/terminal/buffer.rb', line 62 def render Vedeu.renderers.render(output) if Vedeu.ready? end |
.reset ⇒ Array<Array<Vedeu::Models::Cell>> Also known as: clear
Removes all content from the virtual terminal; effectively clearing it.
70 71 72 |
# File 'lib/vedeu/terminal/buffer.rb', line 70 def reset @output = empty_buffer end |
.valid_position?(value) ⇒ Boolean (private)
Returns a boolean indicating the value has a position attribute and is within the terminal boundary.
114 115 116 |
# File 'lib/vedeu/terminal/buffer.rb', line 114 def valid_position?(value) position?(value) && within_terminal_boundary?(value) end |
.within_terminal_boundary?(value) ⇒ Boolean (private)
Returns a boolean indicating the position of the value object is valid for this terminal.
123 124 125 126 |
# File 'lib/vedeu/terminal/buffer.rb', line 123 def within_terminal_boundary?(value) value.position.y > 0 && value.position.y <= Vedeu.height && value.position.x > 0 && value.position.x <= Vedeu.width end |
.write(value) ⇒ Array<Array<Vedeu::Views::Char>>
Write a collection of cells to the virtual terminal.
79 80 81 82 83 84 85 86 87 |
# File 'lib/vedeu/terminal/buffer.rb', line 79 def write(value) values = Array(value).flatten values.each do |v| buffer[v.position.y][v.position.x] = v if valid_position?(v) end self end |
Instance Method Details
#buffer ⇒ Array<Array<Vedeu::Models::Cell>> Also known as: cells
Return a grid of Models::Cell objects defined by the height and width of this virtual terminal.
21 22 23 |
# File 'lib/vedeu/terminal/buffer.rb', line 21 def buffer @output ||= empty_buffer.dup end |
#empty_buffer ⇒ Array<Array<Vedeu::Models::Cell>>
27 28 29 30 31 32 33 |
# File 'lib/vedeu/terminal/buffer.rb', line 27 def empty_buffer Array.new(Vedeu.height) do |y| Array.new(Vedeu.width) do |x| Vedeu::Models::Cell.new(position: [y + 1, x + 1]) end end end |
#fetch(from, which) ⇒ Array<Vedeu::Views::Char>|Array (private)
95 96 97 |
# File 'lib/vedeu/terminal/buffer.rb', line 95 def fetch(from, which) from[which] || [] end |
#output ⇒ Vedeu::Models::Page
36 37 38 |
# File 'lib/vedeu/terminal/buffer.rb', line 36 def output Vedeu::Models::Page.coerce(buffer) end |
#position?(value) ⇒ Boolean (private)
Returns a boolean indicating the value has a position attribute.
104 105 106 107 |
# File 'lib/vedeu/terminal/buffer.rb', line 104 def position?(value) value.respond_to?(:position) && value.position.is_a?(Vedeu::Geometry::Position) end |
#read(y, x) ⇒ Vedeu::Views::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.
50 51 52 53 54 55 56 57 |
# File 'lib/vedeu/terminal/buffer.rb', line 50 def read(y, x) cy, cx = Vedeu::Geometry::Position[y, x].as_indices row = fetch(cells, cy) cell = fetch(row, cx) cell end |
#render ⇒ String|void
Send the cells to the renderer and return the rendered result.
62 63 64 |
# File 'lib/vedeu/terminal/buffer.rb', line 62 def render Vedeu.renderers.render(output) if Vedeu.ready? end |
#reset ⇒ Array<Array<Vedeu::Models::Cell>> Also known as: clear
Removes all content from the virtual terminal; effectively clearing it.
70 71 72 |
# File 'lib/vedeu/terminal/buffer.rb', line 70 def reset @output = empty_buffer end |
#valid_position?(value) ⇒ Boolean (private)
Returns a boolean indicating the value has a position attribute and is within the terminal boundary.
114 115 116 |
# File 'lib/vedeu/terminal/buffer.rb', line 114 def valid_position?(value) position?(value) && within_terminal_boundary?(value) end |
#within_terminal_boundary?(value) ⇒ Boolean (private)
Returns a boolean indicating the position of the value object is valid for this terminal.
123 124 125 126 |
# File 'lib/vedeu/terminal/buffer.rb', line 123 def within_terminal_boundary?(value) value.position.y > 0 && value.position.y <= Vedeu.height && value.position.x > 0 && value.position.x <= Vedeu.width end |
#write(value) ⇒ Array<Array<Vedeu::Views::Char>>
Write a collection of cells to the virtual terminal.
79 80 81 82 83 84 85 86 87 |
# File 'lib/vedeu/terminal/buffer.rb', line 79 def write(value) values = Array(value).flatten values.each do |v| buffer[v.position.y][v.position.x] = v if valid_position?(v) end self end |