2013-07-09 21:40:21 +00:00
|
|
|
class Snapshot
|
2013-07-25 16:02:58 +00:00
|
|
|
|
2013-08-25 18:49:23 +00:00
|
|
|
attr_reader :width, :height
|
2013-08-04 21:12:18 +00:00
|
|
|
|
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
|
2013-07-09 21:40:21 +00:00
|
|
|
end
|
|
|
|
|
2013-08-25 18:49:23 +00:00
|
|
|
def cell(column, line)
|
|
|
|
lines[line][column]
|
2013-07-09 21:40:21 +00:00
|
|
|
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-05 16:18:49 +00:00
|
|
|
|
2013-08-25 18:49:23 +00:00
|
|
|
self.class.new(new_lines, false)
|
2013-08-14 15:24:36 +00:00
|
|
|
end
|
|
|
|
|
2013-08-25 18:49:23 +00:00
|
|
|
protected
|
|
|
|
|
|
|
|
def strip_trailing_blank_lines(lines)
|
2013-08-14 15:24:36 +00:00
|
|
|
i = lines.size - 1
|
|
|
|
|
2013-08-25 18:49:23 +00:00
|
|
|
while i >= 0 && empty_line?(lines[i])
|
2013-08-14 15:24:36 +00:00
|
|
|
i -= 1
|
|
|
|
end
|
|
|
|
|
2013-08-25 18:49:23 +00:00
|
|
|
i > -1 ? lines[0..i] : []
|
2013-08-14 15:24:36 +00:00
|
|
|
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-14 15:24:36 +00:00
|
|
|
|
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
|
2013-08-05 16:18:49 +00:00
|
|
|
end
|
|
|
|
|
2013-08-25 18:49:23 +00:00
|
|
|
lines
|
2013-07-09 21:40:21 +00:00
|
|
|
end
|
2013-07-25 16:02:58 +00:00
|
|
|
|
2013-08-25 18:49:23 +00:00
|
|
|
private
|
2013-08-04 21:12:18 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2013-07-09 21:40:21 +00:00
|
|
|
end
|