asciinema.org/app/models/snapshot.rb

66 lines
1.2 KiB
Ruby
Raw Normal View History

class Snapshot
2013-08-25 18:49:23 +00:00
attr_reader :width, :height
2013-08-25 18:49:23 +00:00
def initialize(data, raw = true)
@lines = raw ? cellify(data) : data
@width = lines.first && lines.first.size || 0
@height = lines.size
end
2013-08-25 18:49:23 +00:00
def cell(column, line)
lines[line][column]
end
2013-08-25 18:49:23 +00:00
def thumbnail(width, height)
new_lines = strip_trailing_blank_lines(lines)
new_lines = crop_at_bottom_left(new_lines, width, height)
new_lines = expand(new_lines, width, height)
2013-08-25 18:49:23 +00:00
self.class.new(new_lines, false)
end
2013-08-25 18:49:23 +00:00
protected
def strip_trailing_blank_lines(lines)
i = lines.size - 1
2013-08-25 18:49:23 +00:00
while i >= 0 && empty_line?(lines[i])
i -= 1
end
2013-08-25 18:49:23 +00:00
i > -1 ? lines[0..i] : []
end
2013-08-25 18:49:23 +00:00
def crop_at_bottom_left(lines, width, height)
min_height = [height, lines.size].min
2013-08-25 18:49:23 +00:00
lines.drop(lines.size - min_height).map { |line| line.take(width) }
end
def expand(lines, width, height)
while lines.size < height
lines << [Cell.new(' ', Brush.new)] * width
end
2013-08-25 18:49:23 +00:00
lines
end
2013-08-25 18:49:23 +00:00
private
attr_reader :lines
2013-08-25 18:49:23 +00:00
def cellify(lines)
lines.map { |cells|
cells.map { |cell|
Cell.new(cell[0], Brush.new(cell[1]))
}
}
end
def empty_line?(cells)
cells.all?(&:empty?)
end
end