Class: UnicodePlot::GridLayout
- Inherits:
-
Object
- Object
- UnicodePlot::GridLayout
- Defined in:
- lib/unicode_plot/layout.rb
Constant Summary collapse
- DEFAULT_WIDTH =
80
Instance Method Summary collapse
- #<<(plot) ⇒ Object
- #[](i, j) ⇒ Object
- #[]=(i, j, plot) ⇒ Object
-
#initialize(n_rows, n_columns, width: Layout::DEFAULT_WIDTH) ⇒ GridLayout
constructor
A new instance of GridLayout.
- #render(out) ⇒ Object
- #render_cell(out, i, j) ⇒ Object
Constructor Details
#initialize(n_rows, n_columns, width: Layout::DEFAULT_WIDTH) ⇒ GridLayout
Returns a new instance of GridLayout.
5 6 7 8 9 |
# File 'lib/unicode_plot/layout.rb', line 5 def initialize(n_rows, n_columns, width: Layout::DEFAULT_WIDTH) @n_rows = n_rows @n_columns = n_columns @width = width end |
Instance Method Details
#<<(plot) ⇒ Object
19 20 21 |
# File 'lib/unicode_plot/layout.rb', line 19 def <<(plot) @plots << plot end |
#[](i, j) ⇒ Object
11 12 13 |
# File 'lib/unicode_plot/layout.rb', line 11 def [](i, j) @plots[i * n_cols + j] end |
#[]=(i, j, plot) ⇒ Object
15 16 17 |
# File 'lib/unicode_plot/layout.rb', line 15 def []=(i, j, plot) @plots[i * n_cols + j] = plot end |
#render(out) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/unicode_plot/layout.rb', line 23 def render(out) buffers = [] (0 ... n_rows).each do |i| (0 ... n_columns).each do |j| StringIO.open do |sio| def sio.tty?; true; end render_cell(sio, i, j) sio.close buffers << sio.string end end end end |
#render_cell(out, i, j) ⇒ Object
37 38 39 40 41 |
# File 'lib/unicode_plot/layout.rb', line 37 def render_cell(out, i, j) plot = self[i, j] return unless plot plot.width = cell_width end |