Class: TerminalCalendar::Selection::Selector

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/terminal_calendar/selection/selector.rb

Constant Summary collapse

DIRECTIONS =
%i(up down left right).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, page, wrap: []) ⇒ Selector

Initializes a new selector

Parameters:

  • x (Integer)

    the x-coordinate of the selector

  • y (Integer)

    the y-coordinate of the selector

  • selection_grid (Array<Array<TerminalCalendar::Selection::Cell>>)

    the selection grid

  • wrap (Symbol) (defaults to: [])

    (optional) the wrap direction, defaults to :all



31
32
33
34
35
36
37
38
# File 'lib/terminal_calendar/selection/selector.rb', line 31

def initialize(x, y, page, wrap: [])
  @x = x
  @page = page
  @top_of_grid = 0
  @y = y
  @wrap_directions = wrap == :all ? DIRECTIONS : wrap
  post_move
end

Instance Attribute Details

#selection_gridObject (readonly)

Returns the value of attribute selection_grid.



10
11
12
# File 'lib/terminal_calendar/selection/selector.rb', line 10

def selection_grid
  @selection_grid
end

#xObject

Returns the value of attribute x.



10
11
12
# File 'lib/terminal_calendar/selection/selector.rb', line 10

def x
  @x
end

#yObject

Returns the value of attribute y.



10
11
12
# File 'lib/terminal_calendar/selection/selector.rb', line 10

def y
  @y
end

Class Method Details

.build(page, initial_spot) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/terminal_calendar/selection/selector.rb', line 15

def self.build(page, initial_spot)
  if initial_spot == :bottom
    x, y = page.selection_grid.bottom_right_live_cell_position
  else
    x, y = page.selection_grid.top_left_live_cell_position
  end

  new(x, y, page)
end

Instance Method Details

#leftmost_gridsquareInteger

Returns the leftmost grid square.

Returns:

  • (Integer)

    the leftmost grid square



64
65
66
# File 'lib/terminal_calendar/selection/selector.rb', line 64

def leftmost_gridsquare
  0
end

#move(direction) ⇒ void

This method returns an undefined value.

Moves the selector in the specified direction.

Parameters:

  • direction (Symbol)

    The direction to move in. Valid directions are: :up, :down, :left, :right.

Raises:

  • (ArgumentError)

    if the specified direction is not valid.



74
75
76
77
78
79
80
81
82
83
84
# File 'lib/terminal_calendar/selection/selector.rb', line 74

def move(direction)
  fail ArgumentError.new("Unknown direction #{direction}") unless DIRECTIONS.include?(direction)

  pre_move

  result = send("move_#{direction}")

  post_move

  result
end

#on_grid?Boolean

Determines if the selector is within the selection grid.

Returns:

  • (Boolean)

    Returns true if the point is within the selection grid, false otherwise.



56
57
58
59
# File 'lib/terminal_calendar/selection/selector.rb', line 56

def on_grid?
  x >= leftmost_gridsquare && x <= selection_grid.row_end &&
    y >= selection_grid.top_of_grid && y <= selection_grid.bottom_of_grid
end

#on_header?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/terminal_calendar/selection/selector.rb', line 40

def on_header?
  y == -1
end

#toggle_selected!void

This method returns an undefined value.

Toggles the selected state of the cell at the current position on the grid.



47
48
49
50
51
# File 'lib/terminal_calendar/selection/selector.rb', line 47

def toggle_selected!
  return unless on_grid?

  selection_grid.cell(@x, @y).toggle_selected!
end