Skip to content

Instantly share code, notes, and snippets.

@kalzoo
Created March 15, 2023 00:06
Show Gist options
  • Save kalzoo/8b6c7f557b1715d60dc97dcbffc91986 to your computer and use it in GitHub Desktop.
Save kalzoo/8b6c7f557b1715d60dc97dcbffc91986 to your computer and use it in GitHub Desktop.
[rust] `tabled` truncation/wrapping
/*
[dependencies]
tabled = "*"
*/
fn main() {
let mut builder = tabled::builder::Builder::default();
let cell_data = (0..10).map(|v| format!("#{v:02}")).collect::<Vec<_>>().join("\n");
builder.add_record(vec![cell_data.clone(), cell_data.clone()]);
let common_table = builder.build();
let mut truncate_table = common_table.clone();
truncate_table.with(tabled::Width::truncate(12));
println!("Narrow data, truncation:");
println!("{truncate_table}");
let mut wrap_table = common_table.clone();
wrap_table.with(tabled::Width::wrap(12));
println!("Narrow data, wrapping:");
println!("{wrap_table}");
// Wider data, same table, behavior changes
let mut builder = tabled::builder::Builder::default();
let cell_data = (0..10).map(|v| format!("#{v:05}")).collect::<Vec<_>>().join("\n");
builder.add_record(vec![cell_data.clone(), cell_data.clone()]);
let common_table = builder.build();
let mut truncate_table = common_table.clone();
truncate_table.with(tabled::Width::truncate(12));
println!("Wide data, truncation:");
println!("{truncate_table}");
let mut wrap_table = common_table.clone();
wrap_table.with(tabled::Width::wrap(12));
println!("Wide data, wrapping:");
println!("{wrap_table}");
}
Narrow data, truncation:
+----+-----+
| #0 | #00 |
| | #01 |
| | #02 |
| | #03 |
| | #04 |
| | #05 |
| | #06 |
| | #07 |
| | #08 |
| | #09 |
+----+-----+
Narrow data, wrapping:
+----+-----+
| #0 | #00 |
| 0 | #01 |
| # | #02 |
| 01 | #03 |
| | #04 |
| #0 | #05 |
| 2 | #06 |
| # | #07 |
| 03 | #08 |
| | #09 |
| #0 | |
| 4 | |
| # | |
| 05 | |
| | |
| #0 | |
| 6 | |
| # | |
| 07 | |
| | |
| #0 | |
| 8 | |
| # | |
| 09 | |
+----+-----+
Wide data, truncation:
+----+-----+
| #0 | #00 |
+----+-----+
Wide data, wrapping:
+----+-----+
| #0 | #00 |
| 00 | 000 |
| 00 | |
| | #00 |
| #0 | 001 |
| 00 | |
| 01 | #00 |
| | 002 |
| #0 | |
| 00 | #00 |
| 02 | 003 |
| | |
| #0 | #00 |
| 00 | 004 |
| 03 | |
| | #00 |
| #0 | 005 |
| 00 | |
| 04 | #00 |
| | 006 |
| #0 | |
| 00 | #00 |
| 05 | 007 |
| | |
| #0 | #00 |
| 00 | 008 |
| 06 | |
| | #00 |
| #0 | 009 |
| 00 | |
| 07 | |
| | |
| #0 | |
| 00 | |
| 08 | |
| | |
| #0 | |
| 00 | |
| 09 | |
+----+-----+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment