From 1597921942f1cd59fb763bc541c3eccf6800ff24 Mon Sep 17 00:00:00 2001 From: realname Date: Sat, 9 Oct 2021 19:03:32 +0200 Subject: [PATCH 1/2] Fixed loop boundaries. --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index fceddd7..20ea10a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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*. From 075eb09156116176fe115cabf055d0ec19571a8c Mon Sep 17 00:00:00 2001 From: realname Date: Sat, 9 Oct 2021 19:04:10 +0200 Subject: [PATCH 2/2] Allowed grid to try to utilize the maximum width it can, optimizing for line-count. --- src/lib.rs | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 20ea10a..c6b0b8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::() < adjusted_width { + while potential_dimensions.widths.iter().sum::() < 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 } }