Skip to content

Commit

Permalink
Refactor Frame#compute_actual_size and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
acook committed Oct 9, 2023
1 parent aef2f19 commit 6df9606
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 15 deletions.
44 changes: 29 additions & 15 deletions lib/remedy/frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,32 +175,46 @@ def compile_contents
buffer.to_a
end

def compute_actual_size merged_size
if size == :none then
frame.content_size
elsif size == :fill then
compile_size = available_size
elsif size == :auto then
compile_size = merged_size
elsif Tuple === size then
compile_size = size.dup
# Determine what the actual output size would be based on the size option and contents.
#
# Most of the time the output size can be determined statically based on the available
# size information and the one parameter.
#
# In practice `:none` and `:auto` output the same maximum height and width - despite rendering differences.
# Technically, `:none` will always result in `content_size`,
# while `:auto` could in theory be further modified by the alignment and later processing.
#
# @parram arranged_size [Remedy::Tuple] an externally determined size after preprocessing
# @return [Remedy::Tuple] output size in rows/height and columns/width
def compute_actual_size arranged_size = content_size
case size
when :none
# generally identical to `arranged_size`
# if needed, using that here could save processing
content_size
when :fill
available_size
when :auto
arranged_size
when Tuple
actual_size = size.dup

if size.height == 0 then
compile_size[0] = available_size.height
actual_size[0] = available_size.height
elsif size.height < 1 then
compile_size[0] = (available_size.height * size.height).floor
actual_size[0] = (available_size.height * size.height).floor
end

if size.width == 0 then
compile_size[1] = available_size.width
actual_size[1] = available_size.width
elsif size.width < 1 then
compile_size[1] = (available_size.width * size.width).floor
actual_size[1] = (available_size.width * size.width).floor
end

actual_size
else
raise "Unknown max_size:#{size}"
end

compile_size
end

def compute_horizontal_offset original_size, actual_size
Expand Down
33 changes: 33 additions & 0 deletions spec/frame_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,39 @@
end
end

describe "#compute_actual_size" do
it "returns a Tuple of the rendered size" do
f << "1234"
f << "567"
arranged_size = Tuple(5, 5)

f.size = :none
expected = Tuple 2, 4
actual = f.compute_actual_size arranged_size
expect(actual).to eq expected

f.size = :fill
actual = f.compute_actual_size arranged_size
expect(actual).to eq console_size

f.size = :auto
actual = f.compute_actual_size arranged_size
expect(actual).to eq arranged_size

f.size = sizeclass.zero
actual = f.compute_actual_size arranged_size
expect(actual).to eq console_size

f.size = sizeclass.new 2, 2
actual = f.compute_actual_size arranged_size
expect(actual).to eq sizeclass.new(2, 2)

f.size = sizeclass.new 0.5, 0.74
actual = f.compute_actual_size arranged_size
expect(actual).to eq sizeclass.new(10, 29)
end
end

describe "#to_s" do
it "returns a string" do
expected = String
Expand Down

0 comments on commit 6df9606

Please sign in to comment.