Class: TerminalCalendar::Selection::Grid

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal_calendar/selection/grid.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, pastel: Pastel.new) ⇒ Grid

Returns a new instance of Grid.

Parameters:

  • width (Integer)

    the width of the grid

  • height (Integer)

    the height of the grid

  • pastel (Pastel) (defaults to: Pastel.new)

    The pastel object to used for decorating text



34
35
36
37
38
39
# File 'lib/terminal_calendar/selection/grid.rb', line 34

def initialize(width, height, pastel: Pastel.new)
  @grid = Array.new(height) do
    Array.new(width) { NullCell.new }
  end
  @pastel = pastel
end

Instance Attribute Details

#gridArray<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.

Returns:

  • (Array<Array>)


8
9
10
# File 'lib/terminal_calendar/selection/grid.rb', line 8

def grid
  @grid
end

#highlighted_positionObject

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_atInteger

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.

Returns:

  • (Integer)


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

Parameters:

  • objects (Array<Array>)

    The objects to build the grid from.

  • opts (Hash)

Returns:



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_gridInteger

Returns the y value of the bottom of the grid

Returns:

  • (Integer)

    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_positionArray<Integer>

Returns the first cell in the grid that is not null working from right to left, bottom to top.

Examples:

bottom_right_live_cell_position #=> [3, 3]

Returns:

  • (Array<Integer>)

    the bottom right live cell position in the grid format x,y



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_rowArray<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.

Parameters:

  • x (Integer)

    the x coordinate

  • y (Integer)

    the y coordinate

Returns:

  • (Cell)

    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

Returns:

  • (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

Parameters:

  • objects (Array<Array>)

    the objects to populate the grid with

Returns:



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.

Parameters:

  • x (Integer)

    the x-coordinate of the position

  • y (Integer)

    the y-coordinate of the position

  • object (Object)

    the object to wrap in the cell

Returns:

  • (Cell)

    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_linesObject



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

Parameters:

  • count (Integer, Symbol) (defaults to: :all)

    The number of lines to render. If set to :all, all lines will be rendered.

Returns:

  • (Array<String>)

    An array of strings representing the rendered lines.



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_endObject



169
170
171
# File 'lib/terminal_calendar/selection/grid.rb', line 169

def row_end
  grid.first.length - 1
end

#selected_cellsArray<Cell>

Returns all selected cells in the grid

Returns:

  • (Array<Cell>)

    Selected cells



161
162
163
# File 'lib/terminal_calendar/selection/grid.rb', line 161

def selected_cells
  grid.flatten.select(&:selected?)
end

#top_left_live_cell_positionArray<Integer>

Returns the first cell in the grid that is not null working from left to right, top to bottom.

Examples:

top_left_live_cell_position #=> [2, 0]

Returns:

  • (Array<Integer>)

    the top left live cell position in the grid format x, y



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_gridInteger

Returns the y value of the top of the grid

Returns:

  • (Integer)

    the top of the grid



104
105
106
# File 'lib/terminal_calendar/selection/grid.rb', line 104

def top_of_grid
  0
end

#top_rowArray<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