From 6df960623b2c1645b84f5b5efc4ea830fa61076b Mon Sep 17 00:00:00 2001 From: Anthony Cook Date: Mon, 9 Oct 2023 07:06:51 -0500 Subject: [PATCH] Refactor Frame#compute_actual_size and add tests --- lib/remedy/frame.rb | 44 +++++++++++++++++++++++++++++--------------- spec/frame_spec.rb | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 15 deletions(-) diff --git a/lib/remedy/frame.rb b/lib/remedy/frame.rb index 84090bf..f299244 100644 --- a/lib/remedy/frame.rb +++ b/lib/remedy/frame.rb @@ -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 diff --git a/spec/frame_spec.rb b/spec/frame_spec.rb index da96f85..cb25d42 100644 --- a/spec/frame_spec.rb +++ b/spec/frame_spec.rb @@ -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