@@ -79,7 +79,7 @@ pub struct GridOptions {
79
79
#[ derive( PartialEq , Eq , Debug ) ]
80
80
struct Dimensions {
81
81
/// The number of lines in the grid.
82
- num_lines : usize ,
82
+ num_rows : usize ,
83
83
84
84
/// The width of each column in the grid. The length of this vector serves
85
85
/// as the number of columns.
@@ -120,7 +120,7 @@ impl<T: AsRef<str>> Grid<T> {
120
120
widths,
121
121
widest_cell_width,
122
122
dimensions : Dimensions {
123
- num_lines : 0 ,
123
+ num_rows : 0 ,
124
124
widths : Vec :: new ( ) ,
125
125
} ,
126
126
} ;
@@ -140,7 +140,7 @@ impl<T: AsRef<str>> Grid<T> {
140
140
141
141
/// The number of rows this display takes up.
142
142
pub fn row_count ( & self ) -> usize {
143
- self . dimensions . num_lines
143
+ self . dimensions . num_rows
144
144
}
145
145
146
146
/// The width of each column
@@ -172,7 +172,7 @@ impl<T: AsRef<str>> Grid<T> {
172
172
}
173
173
174
174
Dimensions {
175
- num_lines,
175
+ num_rows : num_lines,
176
176
widths : column_widths,
177
177
}
178
178
}
@@ -181,7 +181,7 @@ impl<T: AsRef<str>> Grid<T> {
181
181
if self . cells . len ( ) == 1 {
182
182
let cell_widths = self . widths [ 0 ] ;
183
183
return Dimensions {
184
- num_lines : 1 ,
184
+ num_rows : 1 ,
185
185
widths : vec ! [ cell_widths] ,
186
186
} ;
187
187
}
@@ -191,36 +191,41 @@ impl<T: AsRef<str>> Grid<T> {
191
191
// If it exceeds terminal's width, return, since it is impossible to fit.
192
192
if widest_column > self . options . width {
193
193
return Dimensions {
194
- num_lines : self . cells . len ( ) ,
194
+ num_rows : self . cells . len ( ) ,
195
195
widths : vec ! [ self . widest_cell_width] ,
196
196
} ;
197
197
}
198
198
199
- // Calculate minimum number of columns with the widest column size.
199
+ // Calculate the number of columns if all columns had the size of the largest
200
+ // column. This is a lower bound on the number of columns.
200
201
let min_columns = self
201
202
. cells
202
203
. len ( )
203
204
. min ( ( self . options . width + self . options . filling . width ( ) ) / widest_column) ;
204
- // Caculate minimum number of rows.
205
- let min_lines = div_ceil ( self . cells . len ( ) , min_columns) ;
205
+
206
+ // Calculate maximum number of lines and columns.
207
+ let max_rows = div_ceil ( self . cells . len ( ) , min_columns) ;
206
208
207
209
// This is a potential dimension, which can definitely fit all of the cells.
208
- let mut potential_dimension = self . compute_dimensions ( min_lines, min_columns) ;
209
- // If all of the cells can be fit on one line, return.
210
- if min_lines == 1 {
210
+ let mut potential_dimension = self . compute_dimensions ( max_rows, min_columns) ;
211
+
212
+ // If all of the cells can be fit on one line, return immediately.
213
+ if max_rows == 1 {
211
214
return potential_dimension;
212
215
}
213
216
214
217
// Try to increase number of columns, to see if new dimension can still fit.
215
- for num_columns in min_columns + 1 .. {
216
- let new_width = ( num_columns - 1 ) * self . options . filling . width ( ) ;
217
- if new_width > self . options . width {
218
+ for num_columns in min_columns + 1 ..self . cells . len ( ) {
219
+ let Some ( adjusted_width) = self
220
+ . options
221
+ . width
222
+ . checked_sub ( ( num_columns - 1 ) * self . options . filling . width ( ) )
223
+ else {
218
224
break ;
219
- }
220
-
225
+ } ;
221
226
let num_rows = div_ceil ( self . cells . len ( ) , num_columns) ;
222
227
let new_dimension = self . compute_dimensions ( num_rows, num_columns) ;
223
- if new_dimension. widths . iter ( ) . sum :: < usize > ( ) <= self . options . width - new_width {
228
+ if new_dimension. widths . iter ( ) . sum :: < usize > ( ) <= adjusted_width {
224
229
potential_dimension = new_dimension;
225
230
}
226
231
}
@@ -252,7 +257,7 @@ impl<T: AsRef<str>> fmt::Display for Grid<T> {
252
257
// get exactly right.
253
258
let padding = " " . repeat ( self . widest_cell_width + self . options . filling . width ( ) ) ;
254
259
255
- for y in 0 ..self . dimensions . num_lines {
260
+ for y in 0 ..self . dimensions . num_rows {
256
261
// Current position on the line.
257
262
let mut cursor: usize = 0 ;
258
263
for x in 0 ..self . dimensions . widths . len ( ) {
@@ -261,7 +266,7 @@ impl<T: AsRef<str>> fmt::Display for Grid<T> {
261
266
let ( current, offset) = match self . options . direction {
262
267
Direction :: LeftToRight => ( y * self . dimensions . widths . len ( ) + x, 1 ) ,
263
268
Direction :: TopToBottom => {
264
- ( y + self . dimensions . num_lines * x, self . dimensions . num_lines )
269
+ ( y + self . dimensions . num_rows * x, self . dimensions . num_rows )
265
270
}
266
271
} ;
267
272
0 commit comments