Skip to content

Commit

Permalink
Fix bottom origins underflowing buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
acook committed Oct 11, 2023
1 parent a13603d commit b713df9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 9 deletions.
16 changes: 16 additions & 0 deletions lib/remedy/align.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ def hv_center content, buffer
buffer[voffset,hoffset] = content
end

# Middle offset.
#
# Given the actual space something takes up,
# determine what the offset to get it centered in the available space.
#
Expand All @@ -50,6 +52,20 @@ def mido actual, available
offset = ((available - actual) / 2.0).floor
end

# Bottom offset.
#
# Given the actual space something takes up,
# determine what the offset to get it at the far edge in the available space.
#
# @param actual [Numeric] the space already taken
# @param available [Numeric] the available space
# @return [Integer] the offset from the end of the availabe space to place the actual content at the end
def boto actual, available
return available unless actual < available

offset = available - actual
end

# Given the actual space something takes up,
# determine what the offset to get it centered in the available space,
# including the trailing space remaining.
Expand Down
15 changes: 10 additions & 5 deletions lib/remedy/frame.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,28 +274,32 @@ def arrange_arbitrary content_to_arrange

result = depth_sort(content_to_arrange).each do |frame|
# FIXME: what happens when the buffer size is zero? the buffer will grow, right?
frame.available_size = arrange_buffer.size
frame.available_size = buffer_size
content = frame.compile_contents
fsize = frame.computed_size || frame.content_size

case frame.vorigin
when :top
voffset = 0
when :center
voffset = Align.mido fsize.height, arrange_buffer.size.height
voffset = Align.mido fsize.height, buffer_size.height
when :bottom
voffset = arrange_buffer.size.height - fsize.height
voffset = Align.boto fsize.height, buffer_size.height
else
raise "Unknown vorigin:#{frame.vorigin}"
end

# this line works around an edge case where only :top vorigins would
# be rendered when available_size was zero and the frame size was :none
voffset = 0 if frame.vorigin != :top && buffer_size.height == 0 && voffset < 0

case frame.horigin
when :left
hoffset = 0
when :center
hoffset = Align.mido fsize.width, arrange_buffer.size.width
hoffset = Align.mido fsize.width, buffer_size.width
when :right
hoffset = arrange_buffer.size.width - fsize.width
voffset = Align.boto fsize.height, buffer_size.height
else
raise "Unknown horigin:#{frame.horigin}"
end
Expand All @@ -306,6 +310,7 @@ def arrange_arbitrary content_to_arrange
offset = Tuple voffset, hoffset

arrange_buffer[offset] = content
buffer_size = arrange_buffer.size
end

arrange_buffer.to_a
Expand Down
20 changes: 16 additions & 4 deletions spec/frame_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -509,11 +509,8 @@
f.arrangement = :arbitrary
f.reset!


f1.size = Tuple 3, 3
f1.fill = "."
f1.valign = :center
f1.halign = :center

f1.horigin = :center
f1.vorigin = :bottom
Expand All @@ -526,7 +523,7 @@
expect(actual).to eq expected
end

context "available size is zero" do
context "available_size.zero? = true" do
before do
f.available_size = sizeclass.zero
end
Expand All @@ -536,6 +533,21 @@
actual = f.to_s
expect(actual).to eq expected
end

context "size = :none" do
before do
f.size = :none
f1.depth = 2
f2.size = Tuple 2,1
f << f2
end

it "puts the frame at the bottom of the actual space" do
expected = "b \n...\n.a.\n..."
actual = f.to_s
expect(actual).to eq expected
end
end
end
end
end
Expand Down

0 comments on commit b713df9

Please sign in to comment.