Skip to content

Commit

Permalink
Tweak debugger line wrapping algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
agardiner committed Jun 18, 2008
1 parent dd34e5c commit be2c728
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
16 changes: 13 additions & 3 deletions lib/debugger/output.rb
Expand Up @@ -86,10 +86,19 @@ def update_widths(cells)
end

# Redistributes the calculated widths, ensuring the overall line width is
# no greater than the specified page width.
# no greater than the specified page width. Reductions are made in the
# following order:
# - first, all columns that have a width specification are reduced to that
# width (if they exceed it)
# - next, any columns with a variable width specification are reduced
# (from largest to smallest) proportionately based on the needed reduction
def redistribute_widths(page_width, indent=0)
if page_width
# Reduce page_wdith by any requirements for indentation and column separators
page_width -= indent + (@widths.size-1) * @col_separator.length
raise ArgumentError, "Page width is insufficient to display any content" if page_width < 1

# Determine the fixed and variable width columns
fixed_width = 0
var_width = 0
cum_width = 0
Expand Down Expand Up @@ -122,10 +131,11 @@ def redistribute_widths(page_width, indent=0)
if variable_widths.size > 0 and (fixed_width + variable_widths.size) < page_width
# Next, reduce variable widths proportionately to needs
cum_adj = 0
variable_widths.each do |i|
adj = (@widths[i].to_f / var_width * (cum_width - page_width)).round
variable_widths.sort.each do |i|
adj = (@widths[i].to_f / var_width * (cum_width - page_width)).ceil
@widths[i] -= adj
cum_adj += adj
break if cum_width - cum_adj <= page_width
end
cum_width -= cum_adj
end
Expand Down
10 changes: 10 additions & 0 deletions spec/debugger/output_spec.rb
Expand Up @@ -135,6 +135,16 @@
cols.redistribute_widths(20).should == true
cols.widths.should == [5, 14]
end

it "only reduces variable column widths until content width == page width" do
cols = Debugger::Output::Columns.new(["%-5s","%*s", "%*s"])
cols.update_widths(["abcdefg", "abcdefghij", "klmnop"])
cols.widths.should == [7, 10, 6]
cols.redistribute_widths(23).should == true
cols.widths.should == [5, 10, 6]
cols.redistribute_widths(22).should == true
cols.widths.should == [5, 9, 6]
end

it "reduces multiple variable width columns in proportion to their content widths" do
cols = Debugger::Output::Columns.new(["%-5s","%*s", "%*s"])
Expand Down

0 comments on commit be2c728

Please sign in to comment.