Skip to content

Commit 9cfb306

Browse files
Keep orginal API and auto add tab
1 parent 29eb3de commit 9cfb306

File tree

4 files changed

+9
-65
lines changed

4 files changed

+9
-65
lines changed

examples/basic.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ fn main() {
1515
let mut grid = Grid::new(GridOptions {
1616
direction: Direction::TopToBottom,
1717
filling: Filling::Text(" | ".into()),
18-
tab_size: 8,
1918
});
2019

2120
for i in 0..48 {

examples/big.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ fn main() {
88
let mut grid = Grid::new(GridOptions {
99
direction: Direction::TopToBottom,
1010
filling: Filling::Text(" | ".into()),
11-
tab_size: 8,
1211
});
1312

1413
let mut n: u64 = 1234;

src/lib.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
//! let mut grid = Grid::new(GridOptions {
1717
//! filling: Filling::Spaces(1),
1818
//! direction: Direction::LeftToRight,
19-
//! tab_size: 8,
2019
//! });
2120
//!
2221
//! for s in &["one", "two", "three", "four", "five", "six", "seven",
@@ -42,7 +41,7 @@
4241
//! To add data to a grid, first create a new [`Grid`] value, and then add
4342
//! cells to them with the `add` function.
4443
//!
45-
//! There are three options that must be specified in the [`GridOptions`] value
44+
//! There are two options that must be specified in the [`GridOptions`] value
4645
//! that dictate how the grid is formatted:
4746
//!
4847
//! - `filling`: what to put in between two columns — either a number of
@@ -55,8 +54,6 @@
5554
//! - `Direction::TopToBottom` starts them in the top left and moves
5655
//! *downwards*, going to the top of a new column after reaching the final
5756
//! row.
58-
//! - `tab_size`: the size of the tab field (default: 8 spaces) that will replace 8
59-
//! consecutive spaces with a tab in the separator.
6057
//!
6158
//!
6259
//! ## Displaying a grid
@@ -181,9 +178,6 @@ pub struct GridOptions {
181178

182179
/// The number of spaces to put in between each column of cells.
183180
pub filling: Filling,
184-
185-
/// The size of the tab field based on space (default: 8 spaces).
186-
pub tab_size: i32,
187181
}
188182

189183
#[derive(PartialEq, Eq, Debug)]
@@ -418,13 +412,8 @@ impl fmt::Display for Display<'_> {
418412
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
419413
let separator = match &self.grid.options.filling {
420414
Filling::Spaces(n) => {
421-
if self.grid.options.tab_size <= 0 {
422-
" ".to_string().repeat(*n)
423-
} else {
424-
let tab_count = n / self.grid.options.tab_size as usize;
425-
let remaining_spaces = n % self.grid.options.tab_size as usize;
426-
"\t".repeat(tab_count) + &" ".repeat(remaining_spaces)
427-
}
415+
// Calculate tab count and remaining spaces
416+
"\t".repeat(n / 8) + &" ".repeat(n % 8)
428417
}
429418
Filling::Text(s) => s.clone(),
430419
};

tests/test.rs

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ fn no_items() {
55
let grid = Grid::new(GridOptions {
66
direction: Direction::TopToBottom,
77
filling: Filling::Spaces(2),
8-
tab_size: 8,
98
});
109

1110
let display = grid.fit_into_width(40).unwrap();
@@ -17,7 +16,6 @@ fn one_item() {
1716
let mut grid = Grid::new(GridOptions {
1817
direction: Direction::TopToBottom,
1918
filling: Filling::Spaces(2),
20-
tab_size: 8,
2119
});
2220

2321
grid.add(Cell::from("1"));
@@ -31,7 +29,6 @@ fn one_item_exact_width() {
3129
let mut grid = Grid::new(GridOptions {
3230
direction: Direction::TopToBottom,
3331
filling: Filling::Spaces(2),
34-
tab_size: 8,
3532
});
3633

3734
grid.add(Cell::from("1234567890"));
@@ -45,7 +42,6 @@ fn one_item_just_over() {
4542
let mut grid = Grid::new(GridOptions {
4643
direction: Direction::TopToBottom,
4744
filling: Filling::Spaces(2),
48-
tab_size: 8,
4945
});
5046

5147
grid.add(Cell::from("1234567890!"));
@@ -58,7 +54,6 @@ fn two_small_items() {
5854
let mut grid = Grid::new(GridOptions {
5955
direction: Direction::TopToBottom,
6056
filling: Filling::Spaces(2),
61-
tab_size: 8,
6257
});
6358

6459
grid.add(Cell::from("1"));
@@ -75,7 +70,6 @@ fn two_medium_size_items() {
7570
let mut grid = Grid::new(GridOptions {
7671
direction: Direction::TopToBottom,
7772
filling: Filling::Spaces(2),
78-
tab_size: 8,
7973
});
8074

8175
grid.add(Cell::from("hello there"));
@@ -92,7 +86,6 @@ fn two_big_items() {
9286
let mut grid = Grid::new(GridOptions {
9387
direction: Direction::TopToBottom,
9488
filling: Filling::Spaces(2),
95-
tab_size: 8,
9689
});
9790

9891
grid.add(Cell::from(
@@ -110,7 +103,6 @@ fn that_example_from_earlier() {
110103
let mut grid = Grid::new(GridOptions {
111104
filling: Filling::Spaces(1),
112105
direction: Direction::LeftToRight,
113-
tab_size: 8,
114106
});
115107

116108
for s in &[
@@ -130,7 +122,6 @@ fn number_grid_with_pipe() {
130122
let mut grid = Grid::new(GridOptions {
131123
filling: Filling::Text("|".into()),
132124
direction: Direction::LeftToRight,
133-
tab_size: 8,
134125
});
135126

136127
for s in &[
@@ -150,7 +141,6 @@ fn huge_separator() {
150141
let mut grid = Grid::new(GridOptions {
151142
filling: Filling::Spaces(100),
152143
direction: Direction::LeftToRight,
153-
tab_size: 8,
154144
});
155145

156146
grid.add("a".into());
@@ -164,7 +154,6 @@ fn huge_yet_unused_separator() {
164154
let mut grid = Grid::new(GridOptions {
165155
filling: Filling::Spaces(100),
166156
direction: Direction::LeftToRight,
167-
tab_size: 8,
168157
});
169158

170159
grid.add("abcd".into());
@@ -183,7 +172,6 @@ fn emoji() {
183172
let mut grid = Grid::new(GridOptions {
184173
direction: Direction::LeftToRight,
185174
filling: Filling::Spaces(2),
186-
tab_size: 8,
187175
});
188176

189177
for s in ["🦀", "hello", "👩‍🔬", "hello"] {
@@ -218,7 +206,6 @@ mod uutils_ls {
218206
let mut grid = Grid::new(GridOptions {
219207
direction: Direction::TopToBottom,
220208
filling: Filling::Spaces(2),
221-
tab_size: 8,
222209
});
223210

224211
for s in [
@@ -240,7 +227,6 @@ mod uutils_ls {
240227
let mut grid = Grid::new(GridOptions {
241228
direction: Direction::LeftToRight,
242229
filling: Filling::Spaces(2),
243-
tab_size: 8,
244230
});
245231

246232
for s in [
@@ -264,7 +250,6 @@ mod uutils_ls {
264250
let mut grid = Grid::new(GridOptions {
265251
direction: Direction::TopToBottom,
266252
filling: Filling::Spaces(2),
267-
tab_size: 8,
268253
});
269254

270255
for s in [
@@ -288,7 +273,6 @@ mod uutils_ls {
288273
let mut grid = Grid::new(GridOptions {
289274
direction: Direction::TopToBottom,
290275
filling: Filling::Spaces(2),
291-
tab_size: 8,
292276
});
293277

294278
for s in ["a", "b", "a-long-name", "z"] {
@@ -304,7 +288,6 @@ mod uutils_ls {
304288
let mut grid = Grid::new(GridOptions {
305289
filling: Filling::Spaces(10),
306290
direction: Direction::LeftToRight,
307-
tab_size: 8,
308291
});
309292

310293
for s in &[
@@ -330,7 +313,6 @@ ten eleven twelve
330313
let mut grid = Grid::new(GridOptions {
331314
filling: Filling::Spaces(30),
332315
direction: Direction::LeftToRight,
333-
tab_size: 8,
334316
});
335317

336318
for s in &[
@@ -352,11 +334,10 @@ nine ten eleven twelve
352334
}
353335

354336
#[test]
355-
fn zero_tab_size() {
337+
fn six_tabs_two_spaces() {
356338
let mut grid = Grid::new(GridOptions {
357-
filling: Filling::Spaces(10),
339+
filling: Filling::Spaces(50),
358340
direction: Direction::LeftToRight,
359-
tab_size: 0,
360341
});
361342

362343
for s in &[
@@ -366,36 +347,12 @@ nine ten eleven twelve
366347
grid.add(Cell::from(*s));
367348
}
368349

369-
let display = grid.fit_into_width(50).unwrap();
370-
assert_eq!(
371-
r"one two three four
372-
five six seven eight
373-
nine ten eleven twelve
374-
",
375-
display.to_string()
376-
);
377-
}
378-
379-
#[test]
380-
fn negative_tab_size() {
381-
let mut grid = Grid::new(GridOptions {
382-
filling: Filling::Spaces(10),
383-
direction: Direction::LeftToRight,
384-
tab_size: -1,
385-
});
386-
387-
for s in &[
388-
"one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
389-
"eleven", "twelve",
390-
] {
391-
grid.add(Cell::from(*s));
392-
}
350+
let display = grid.fit_into_width(250).unwrap();
393351

394-
let display = grid.fit_into_width(50).unwrap();
395352
assert_eq!(
396-
r"one two three four
397-
five six seven eight
398-
nine ten eleven twelve
353+
r"one two three four
354+
five six seven eight
355+
nine ten eleven twelve
399356
",
400357
display.to_string()
401358
);

0 commit comments

Comments
 (0)