Class: Grid

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

Direct Known Subclasses

Snapshot

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines) ⇒ Grid

Returns a new instance of Grid.



6
7
8
9
10
# File 'lib/asciinema/grid.rb', line 6

def initialize(lines)
  @lines = lines
  @width = lines.first && lines.first.sum(&:size) || 0
  @height = lines.size
end

Instance Attribute Details

#heightObject (readonly)

Returns the value of attribute height.



4
5
6
# File 'lib/asciinema/grid.rb', line 4

def height
  @height
end

#linesObject (readonly)

Returns the value of attribute lines.



4
5
6
# File 'lib/asciinema/grid.rb', line 4

def lines
  @lines
end

#widthObject (readonly)

Returns the value of attribute width.



4
5
6
# File 'lib/asciinema/grid.rb', line 4

def width
  @width
end

Instance Method Details

#as_jsonObject



26
27
28
# File 'lib/asciinema/grid.rb', line 26

def as_json(*)
  lines.as_json
end

#crop(x, y, width, height) ⇒ Object



12
13
14
15
16
# File 'lib/asciinema/grid.rb', line 12

def crop(x, y, width, height)
  cropped_lines = lines[y...y+height].map { |line| crop_line(line, x, width) }

  self.class.new(cropped_lines)
end

#diff(other) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/asciinema/grid.rb', line 18

def diff(other)
  (0...height).each_with_object({}) do |y, diff|
    if other.nil? || other.lines[y] != lines[y]
      diff[y] = lines[y]
    end
  end
end