Class: TerminalCalendar::Selection::Grid
- Inherits:
-
Object
- Object
- TerminalCalendar::Selection::Grid
- Defined in:
- lib/terminal_calendar/selection/grid.rb
Instance Attribute Summary collapse
- #grid ⇒ Array<Array> readonly private
-
#highlighted_position ⇒ Object
Returns the value of attribute highlighted_position.
- #redraw_at ⇒ Integer private
Class Method Summary collapse
-
.build_from_objects(objects, _opts = {}) ⇒ Grid
Builds a new grid from a array of arrays of objects.
Instance Method Summary collapse
-
#bottom_of_grid ⇒ Integer
Returns the y value of the bottom of the grid.
-
#bottom_right_live_cell_position ⇒ Array<Integer>
Returns the first cell in the grid that is not null working from right to left, bottom to top.
-
#bottom_row ⇒ Array<TerminalCalendar::Selection::Cell>
Returns the bottom row of the grid.
-
#cell(x, y) ⇒ Cell
Returns the cell at the given coordinates.
- #clear_highlight! ⇒ Object
- #highlighted? ⇒ Boolean
-
#initialize(width, height, pastel: Pastel.new) ⇒ Grid
constructor
A new instance of Grid.
-
#populate_from_objects(objects) ⇒ Grid
Builds a the grid from an array of arrays of objects.
-
#populate_position(x, y, object) ⇒ Cell
The created cell.
- #redraw_lines ⇒ Object
-
#render_lines(count = :all) ⇒ Array<String>
Renders specified number of lines from the bottom of the grid as printable strings.
- #row_end ⇒ Object
-
#selected_cells ⇒ Array<Cell>
Returns all selected cells in the grid.
-
#top_left_live_cell_position ⇒ Array<Integer>
Returns the first cell in the grid that is not null working from left to right, top to bottom.
-
#top_of_grid ⇒ Integer
Returns the y value of the top of the grid.
-
#top_row ⇒ Array<TerminalCalendar::Selection::Cell>
Returns the top row of the grid.
Constructor Details
Instance Attribute Details
#grid ⇒ Array<Array> (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
8 9 10 |
# File 'lib/terminal_calendar/selection/grid.rb', line 8 def grid @grid end |
#highlighted_position ⇒ Object
Returns the value of attribute highlighted_position.
10 11 12 |
# File 'lib/terminal_calendar/selection/grid.rb', line 10 def highlighted_position @highlighted_position end |
#redraw_at ⇒ Integer
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
14 15 16 |
# File 'lib/terminal_calendar/selection/grid.rb', line 14 def redraw_at @redraw_at end |
Class Method Details
.build_from_objects(objects, _opts = {}) ⇒ Grid
Builds a new grid from a array of arrays of objects
23 24 25 26 27 |
# File 'lib/terminal_calendar/selection/grid.rb', line 23 def self.build_from_objects(objects, _opts={}) new(objects.first.length, objects.length).tap do |new_grid| new_grid.populate_from_objects(objects) end end |
Instance Method Details
#bottom_of_grid ⇒ Integer
Returns the y value of the bottom of the grid
95 96 97 |
# File 'lib/terminal_calendar/selection/grid.rb', line 95 def bottom_of_grid grid.length - 1 end |
#bottom_right_live_cell_position ⇒ Array<Integer>
Returns the first cell in the grid that is not null working from right to left, bottom to top.
134 135 136 137 138 139 140 141 |
# File 'lib/terminal_calendar/selection/grid.rb', line 134 def bottom_right_live_cell_position bottom_of_grid.downto(top_of_grid).each do |y| row = grid[y] (row.length - 1).downto(0).each do |x| return [x, y] unless row[x].null? end end end |
#bottom_row ⇒ Array<TerminalCalendar::Selection::Cell>
Returns the bottom row of the grid
113 114 115 |
# File 'lib/terminal_calendar/selection/grid.rb', line 113 def bottom_row grid[bottom_of_grid] end |
#cell(x, y) ⇒ Cell
Returns the cell at the given coordinates.
75 76 77 |
# File 'lib/terminal_calendar/selection/grid.rb', line 75 def cell(x, y) grid[y][x] end |
#clear_highlight! ⇒ Object
177 178 179 |
# File 'lib/terminal_calendar/selection/grid.rb', line 177 def clear_highlight! @highlighted_position = nil end |
#highlighted? ⇒ Boolean
181 182 183 |
# File 'lib/terminal_calendar/selection/grid.rb', line 181 def highlighted? @highlighted_position.nil? end |
#populate_from_objects(objects) ⇒ Grid
Builds a the grid from an array of arrays of objects
47 48 49 50 51 52 53 54 |
# File 'lib/terminal_calendar/selection/grid.rb', line 47 def populate_from_objects(objects) objects.each_with_index do |object_row, y| object_row.each_with_index do |obj, x| populate_position(x, y, obj) end end self end |
#populate_position(x, y, object) ⇒ Cell
Returns the created cell.
62 63 64 65 66 |
# File 'lib/terminal_calendar/selection/grid.rb', line 62 def populate_position(x, y, object) return grid[y][x] if object.null? grid[y][x] = Cell.new(object) end |
#redraw_lines ⇒ Object
165 166 167 |
# File 'lib/terminal_calendar/selection/grid.rb', line 165 def redraw_lines render_lines(redraw_at.nil? || redraw_at <= 0 ? :all : (grid.length - redraw_at)) end |
#render_lines(count = :all) ⇒ Array<String>
Renders specified number of lines from the bottom of the grid as printable strings
82 83 84 85 86 87 88 |
# File 'lib/terminal_calendar/selection/grid.rb', line 82 def render_lines(count=:all) start_at = render_start(count) (start_at..bottom_of_grid).map do |i| render_row(i) end end |
#row_end ⇒ Object
169 170 171 |
# File 'lib/terminal_calendar/selection/grid.rb', line 169 def row_end grid.first.length - 1 end |
#selected_cells ⇒ Array<Cell>
Returns all selected cells in the grid
161 162 163 |
# File 'lib/terminal_calendar/selection/grid.rb', line 161 def selected_cells grid.flatten.select(&:selected?) end |
#top_left_live_cell_position ⇒ Array<Integer>
Returns the first cell in the grid that is not null working from left to right, top to bottom.
151 152 153 154 155 156 157 |
# File 'lib/terminal_calendar/selection/grid.rb', line 151 def top_left_live_cell_position (top_of_grid..bottom_of_grid).each do |y| grid[y].each_with_index do |cell, x| return [x, y] unless cell.null? end end end |
#top_of_grid ⇒ Integer
Returns the y value of the top of the grid
104 105 106 |
# File 'lib/terminal_calendar/selection/grid.rb', line 104 def top_of_grid 0 end |
#top_row ⇒ Array<TerminalCalendar::Selection::Cell>
Returns the top row of the grid
122 123 124 |
# File 'lib/terminal_calendar/selection/grid.rb', line 122 def top_row grid[top_of_grid] end |