Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ impl Grid {
// Instead of numbers of columns, try to find the fewest number of *lines*
// that the output will fit in.
let mut smallest_dimensions_yet = None;
for num_lines in (1 .. theoretical_max_num_lines).rev() {
for num_lines in (1 ..= theoretical_max_num_lines).rev() {

// The number of columns is the number of cells divided by the number
// of lines, *rounded up*.
Expand All @@ -397,21 +397,31 @@ impl Grid {
// also serves as a speed-up.
let total_separator_width = (num_columns - 1) * self.options.filling.width();
if maximum_width < total_separator_width {
continue;
return None;
}

// Remove the separator width from the available space.
let adjusted_width = maximum_width - total_separator_width;
let mut adjusted_width = maximum_width - total_separator_width;
let mut potential_dimensions = self.column_widths(num_lines, num_columns);

let potential_dimensions = self.column_widths(num_lines, num_columns);
if potential_dimensions.widths.iter().sum::<Width>() < adjusted_width {
while potential_dimensions.widths.iter().sum::<Width>() < adjusted_width {
smallest_dimensions_yet = Some(potential_dimensions);
} else {
return smallest_dimensions_yet;

if self.cell_count % num_lines != 0 {
num_columns += 1;
let total_separator_width = (num_columns - 1) * self.options.filling.width();
if maximum_width < total_separator_width {
break;
}
adjusted_width = maximum_width - total_separator_width;
potential_dimensions = self.column_widths(num_lines, num_columns);
} else {
break
}
}
}

None
smallest_dimensions_yet
}
}

Expand Down