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.
-
.clear ⇒ String|void
Clear the output.
- .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
(also: #refresh)
Send the cells to the renderer and return the rendered result.
-
.reset ⇒ Array<Array<Vedeu::Models::Cell>>
Removes all content from the virtual terminal; effectively clearing it.
-
.update(value) ⇒ Array<Array<Vedeu::Views::Char>>
Write a collection of cells to the virtual terminal, but do not send to a renderer.
- .update_buffer(value) ⇒ Array<Array<Vedeu::Views::Char>> private
-
.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, will then send written content to be rendered by a renderer.
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.
-
#clear ⇒ String|void
Clear the output.
- #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
(also: #refresh)
Send the cells to the renderer and return the rendered result.
-
#reset ⇒ Array<Array<Vedeu::Models::Cell>>
Removes all content from the virtual terminal; effectively clearing it.
-
#update(value) ⇒ Array<Array<Vedeu::Views::Char>>
Write a collection of cells to the virtual terminal, but do not send to a renderer.
- #update_buffer(value) ⇒ Array<Array<Vedeu::Views::Char>> private
-
#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, will then send written content to be rendered by a renderer.
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.
22 23 24 25 26 27 28 |
# File 'lib/vedeu/terminal/buffer.rb', line 22 def buffer @output ||= Array.new(Vedeu.height + 1) do |y| Array.new(Vedeu.width + 1) do |x| Vedeu::Models::Cell.new(position: [y, x]) end end end |
.clear ⇒ String|void
Clear the output.
37 38 39 40 41 |
# File 'lib/vedeu/terminal/buffer.rb', line 37 def clear reset Vedeu.renderers.clear if Vedeu.ready? end |
.fetch(from, which) ⇒ Array<Vedeu::Views::Char>|Array (private)
117 118 119 |
# File 'lib/vedeu/terminal/buffer.rb', line 117 def fetch(from, which) from[which] || [] end |
.output ⇒ Vedeu::Models::Page
44 45 46 |
# File 'lib/vedeu/terminal/buffer.rb', line 44 def output Vedeu::Models::Page.coerce(buffer) end |
.position?(value) ⇒ Boolean (private)
Returns a boolean indicating the value has a position attribute.
126 127 128 129 |
# File 'lib/vedeu/terminal/buffer.rb', line 126 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.
58 59 60 61 62 63 64 65 |
# File 'lib/vedeu/terminal/buffer.rb', line 58 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 Also known as: refresh
Send the cells to the renderer and return the rendered result.
70 71 72 |
# File 'lib/vedeu/terminal/buffer.rb', line 70 def render Vedeu.renderers.render(output) if Vedeu.ready? end |
.reset ⇒ Array<Array<Vedeu::Models::Cell>>
Removes all content from the virtual terminal; effectively clearing it.
79 80 81 82 83 84 85 |
# File 'lib/vedeu/terminal/buffer.rb', line 79 def reset @output = Array.new(Vedeu.height + 1) do |y| Array.new(Vedeu.width + 1) do |x| Vedeu::Models::Cell.new(position: [y, x]) end end end |
.update(value) ⇒ Array<Array<Vedeu::Views::Char>>
Write a collection of cells to the virtual terminal, but do not send to a renderer.
105 106 107 108 109 |
# File 'lib/vedeu/terminal/buffer.rb', line 105 def update(value) update_buffer(value) self end |
.update_buffer(value) ⇒ Array<Array<Vedeu::Views::Char>> (private)
133 134 135 136 137 138 139 |
# File 'lib/vedeu/terminal/buffer.rb', line 133 def update_buffer(value) values = Array(value).flatten values.each do |v| buffer[v.position.y][v.position.x] = v if valid_position?(v) end end |
.valid_position?(value) ⇒ Boolean (private)
Returns a boolean indicating the value has a position attribute and is within the terminal boundary.
146 147 148 |
# File 'lib/vedeu/terminal/buffer.rb', line 146 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.
155 156 157 |
# File 'lib/vedeu/terminal/buffer.rb', line 155 def within_terminal_boundary?(value) buffer[value.position.y] && buffer[value.position.y][value.position.x] end |
.write(value) ⇒ Array<Array<Vedeu::Views::Char>>
Write a collection of cells to the virtual terminal, will then send written content to be rendered by a renderer.
92 93 94 95 96 97 98 |
# File 'lib/vedeu/terminal/buffer.rb', line 92 def write(value) update_buffer(value) render 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.
22 23 24 25 26 27 28 |
# File 'lib/vedeu/terminal/buffer.rb', line 22 def buffer @output ||= Array.new(Vedeu.height + 1) do |y| Array.new(Vedeu.width + 1) do |x| Vedeu::Models::Cell.new(position: [y, x]) end end end |
#clear ⇒ String|void
Clear the output.
37 38 39 40 41 |
# File 'lib/vedeu/terminal/buffer.rb', line 37 def clear reset Vedeu.renderers.clear if Vedeu.ready? end |
#fetch(from, which) ⇒ Array<Vedeu::Views::Char>|Array (private)
117 118 119 |
# File 'lib/vedeu/terminal/buffer.rb', line 117 def fetch(from, which) from[which] || [] end |
#output ⇒ Vedeu::Models::Page
44 45 46 |
# File 'lib/vedeu/terminal/buffer.rb', line 44 def output Vedeu::Models::Page.coerce(buffer) end |
#position?(value) ⇒ Boolean (private)
Returns a boolean indicating the value has a position attribute.
126 127 128 129 |
# File 'lib/vedeu/terminal/buffer.rb', line 126 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.
58 59 60 61 62 63 64 65 |
# File 'lib/vedeu/terminal/buffer.rb', line 58 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 Also known as: refresh
Send the cells to the renderer and return the rendered result.
70 71 72 |
# File 'lib/vedeu/terminal/buffer.rb', line 70 def render Vedeu.renderers.render(output) if Vedeu.ready? end |
#reset ⇒ Array<Array<Vedeu::Models::Cell>>
Removes all content from the virtual terminal; effectively clearing it.
79 80 81 82 83 84 85 |
# File 'lib/vedeu/terminal/buffer.rb', line 79 def reset @output = Array.new(Vedeu.height + 1) do |y| Array.new(Vedeu.width + 1) do |x| Vedeu::Models::Cell.new(position: [y, x]) end end end |
#update(value) ⇒ Array<Array<Vedeu::Views::Char>>
Write a collection of cells to the virtual terminal, but do not send to a renderer.
105 106 107 108 109 |
# File 'lib/vedeu/terminal/buffer.rb', line 105 def update(value) update_buffer(value) self end |
#update_buffer(value) ⇒ Array<Array<Vedeu::Views::Char>> (private)
133 134 135 136 137 138 139 |
# File 'lib/vedeu/terminal/buffer.rb', line 133 def update_buffer(value) values = Array(value).flatten values.each do |v| buffer[v.position.y][v.position.x] = v if valid_position?(v) end end |
#valid_position?(value) ⇒ Boolean (private)
Returns a boolean indicating the value has a position attribute and is within the terminal boundary.
146 147 148 |
# File 'lib/vedeu/terminal/buffer.rb', line 146 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.
155 156 157 |
# File 'lib/vedeu/terminal/buffer.rb', line 155 def within_terminal_boundary?(value) buffer[value.position.y] && buffer[value.position.y][value.position.x] end |
#write(value) ⇒ Array<Array<Vedeu::Views::Char>>
Write a collection of cells to the virtual terminal, will then send written content to be rendered by a renderer.
92 93 94 95 96 97 98 |
# File 'lib/vedeu/terminal/buffer.rb', line 92 def write(value) update_buffer(value) render self end |