Remove boring empty trailing lines from thumbnails
This commit is contained in:
parent
9086aae060
commit
2e9b0a4718
@ -36,7 +36,7 @@ class AsciicastDecorator < ApplicationDecorator
|
|||||||
|
|
||||||
def thumbnail(width = THUMBNAIL_WIDTH, height = THUMBNAIL_HEIGHT)
|
def thumbnail(width = THUMBNAIL_WIDTH, height = THUMBNAIL_HEIGHT)
|
||||||
snapshot = Snapshot.build(model.snapshot || [])
|
snapshot = Snapshot.build(model.snapshot || [])
|
||||||
thumbnail = snapshot.crop(width, height)
|
thumbnail = snapshot.rstrip.crop(width, height).expand(height)
|
||||||
SnapshotPresenter.new(thumbnail).to_html
|
SnapshotPresenter.new(thumbnail).to_html
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -21,6 +21,24 @@ class Snapshot
|
|||||||
min_height = [height, lines.size].min
|
min_height = [height, lines.size].min
|
||||||
new_lines = lines.drop(lines.size - min_height).map { |line| line.crop(width) }
|
new_lines = lines.drop(lines.size - min_height).map { |line| line.crop(width) }
|
||||||
|
|
||||||
|
self.class.new(new_lines)
|
||||||
|
end
|
||||||
|
|
||||||
|
def rstrip
|
||||||
|
i = lines.size - 1
|
||||||
|
|
||||||
|
while i >= 0 && lines[i].empty?
|
||||||
|
i -= 1
|
||||||
|
end
|
||||||
|
|
||||||
|
new_lines = i > -1 ? lines[0..i] : []
|
||||||
|
|
||||||
|
self.class.new(new_lines)
|
||||||
|
end
|
||||||
|
|
||||||
|
def expand(height)
|
||||||
|
new_lines = lines
|
||||||
|
|
||||||
while new_lines.size < height
|
while new_lines.size < height
|
||||||
new_lines << []
|
new_lines << []
|
||||||
end
|
end
|
||||||
|
@ -21,4 +21,8 @@ class SnapshotFragment # TODO: rename to Cell or SnapshotCell
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def empty?
|
||||||
|
text.blank? && brush.default?
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
@ -37,6 +37,10 @@ class SnapshotLine
|
|||||||
self.class.new(new_fragments)
|
self.class.new(new_fragments)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def empty?
|
||||||
|
fragments.all?(&:empty?)
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
attr_reader :fragments
|
attr_reader :fragments
|
||||||
|
@ -135,14 +135,22 @@ describe AsciicastDecorator do
|
|||||||
|
|
||||||
describe '#thumbnail' do
|
describe '#thumbnail' do
|
||||||
let(:json) { [:qux] }
|
let(:json) { [:qux] }
|
||||||
let(:snapshot) { double('snapshot', :crop => thumbnail) }
|
let(:snapshot) { double('snapshot') }
|
||||||
let(:thumbnail) { double('thumbnail') }
|
|
||||||
let(:snapshot_presenter) { double('snapshot_presenter', :to_html => '<pre></pre>') }
|
let(:snapshot_presenter) { double('snapshot_presenter', :to_html => '<pre></pre>') }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
allow(asciicast).to receive(:snapshot) { json }
|
allow(asciicast).to receive(:snapshot) { json }
|
||||||
allow(Snapshot).to receive(:build).with(json) { snapshot }
|
allow(Snapshot).to receive(:build).with(json) { snapshot }
|
||||||
allow(SnapshotPresenter).to receive(:new).with(thumbnail) { snapshot_presenter }
|
allow(snapshot).to receive(:rstrip) { snapshot }
|
||||||
|
allow(snapshot).to receive(:crop) { snapshot }
|
||||||
|
allow(snapshot).to receive(:expand) { snapshot }
|
||||||
|
allow(SnapshotPresenter).to receive(:new).with(snapshot) { snapshot_presenter }
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'removes empty trailing lines from the snapshot' do
|
||||||
|
decorated.thumbnail(21, 13)
|
||||||
|
|
||||||
|
expect(snapshot).to have_received(:rstrip)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'crops the snapshot' do
|
it 'crops the snapshot' do
|
||||||
@ -151,6 +159,12 @@ describe AsciicastDecorator do
|
|||||||
expect(snapshot).to have_received(:crop).with(21, 13)
|
expect(snapshot).to have_received(:crop).with(21, 13)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it 'adds the missing lines to the end of the snapshot' do
|
||||||
|
decorated.thumbnail(21, 13)
|
||||||
|
|
||||||
|
expect(snapshot).to have_received(:expand).with(13)
|
||||||
|
end
|
||||||
|
|
||||||
it 'returns html snapshot rendered by SnapshotPresenter#to_html' do
|
it 'returns html snapshot rendered by SnapshotPresenter#to_html' do
|
||||||
expect(decorated.thumbnail).to eq('<pre></pre>')
|
expect(decorated.thumbnail).to eq('<pre></pre>')
|
||||||
end
|
end
|
||||||
|
@ -94,14 +94,5 @@ describe Snapshot do
|
|||||||
cropped_line_3]))
|
cropped_line_3]))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context "when height is greater than lines count" do
|
|
||||||
let(:height) { 4 }
|
|
||||||
|
|
||||||
it 'returns a new Snapshot with all lines cropped and missing lines added' do
|
|
||||||
expect(subject).to eq(Snapshot.new([cropped_line_1, cropped_line_2,
|
|
||||||
cropped_line_3, []]))
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user