From a1cec2cd60d64208caa2cbd11d8e6f885e80f630 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 17 May 2023 13:01:37 +0200 Subject: Split format_table into separate crate and reduce k2v-client dependencies --- src/format-table/Cargo.toml | 12 +++++++++++ src/format-table/README.md | 13 ++++++++++++ src/format-table/lib.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 src/format-table/Cargo.toml create mode 100644 src/format-table/README.md create mode 100644 src/format-table/lib.rs (limited to 'src/format-table') diff --git a/src/format-table/Cargo.toml b/src/format-table/Cargo.toml new file mode 100644 index 00000000..b2f074d7 --- /dev/null +++ b/src/format-table/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "format_table" +version = "0.1.0" +authors = ["Alex Auvolat "] +edition = "2018" +license = "AGPL-3.0" +description = "Format tables with a stupid API" +repository = "https://git.deuxfleurs.fr/Deuxfleurs/garage" +readme = "README.md" + +[lib] +path = "lib.rs" diff --git a/src/format-table/README.md b/src/format-table/README.md new file mode 100644 index 00000000..d918bdf4 --- /dev/null +++ b/src/format-table/README.md @@ -0,0 +1,13 @@ +# `format_table` + +Format tables with a stupid API. [Documentation](https://docs.rs/format_table). + +Example: + +```rust +let mut table = vec!["product\tquantity\tprice".to_string()]; +for (p, q, r) in [("tomato", 12, 15), ("potato", 10, 20), ("rice", 5, 12)] { + table.push(format!("{}\t{}\t{}", p, q, r)); +} +format_table::format_table(table); +``` diff --git a/src/format-table/lib.rs b/src/format-table/lib.rs new file mode 100644 index 00000000..72fd10b2 --- /dev/null +++ b/src/format-table/lib.rs @@ -0,0 +1,49 @@ +//! Format tables with a stupid API. +//! +//! Example: +//! +//! ```rust +//! let mut table = vec!["product\tquantity\tprice".to_string()]; +//! for (p, q, r) in [("tomato", 12, 15), ("potato", 10, 20), ("rice", 5, 12)] { +//! table.push(format!("{}\t{}\t{}", p, q, r)); +//! } +//! format_table::format_table(table); +//! ``` +//! +//! A table to be formatted is a `Vec`, containing one string per line. +//! Table columns in each line are separated by a `\t` character. + +/// Format a table and return the result as a string. +pub fn format_table_to_string(data: Vec) -> String { + let data = data + .iter() + .map(|s| s.split('\t').collect::>()) + .collect::>(); + + let columns = data.iter().map(|row| row.len()).fold(0, std::cmp::max); + let mut column_size = vec![0; columns]; + + let mut out = String::new(); + + for row in data.iter() { + for (i, col) in row.iter().enumerate() { + column_size[i] = std::cmp::max(column_size[i], col.chars().count()); + } + } + + for row in data.iter() { + for (col, col_len) in row[..row.len() - 1].iter().zip(column_size.iter()) { + out.push_str(col); + (0..col_len - col.chars().count() + 2).for_each(|_| out.push(' ')); + } + out.push_str(row[row.len() - 1]); + out.push('\n'); + } + + out +} + +/// Format a table and print the result to stdout. +pub fn format_table(data: Vec) { + print!("{}", format_table_to_string(data)); +} -- cgit v1.2.3 From 217d4299379b70d1b081cb88e208ab5f2d1808a7 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Wed, 17 May 2023 13:06:37 +0200 Subject: fix clippy lint in format-table crate --- src/format-table/Cargo.toml | 2 +- src/format-table/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/format-table') diff --git a/src/format-table/Cargo.toml b/src/format-table/Cargo.toml index b2f074d7..9e31e211 100644 --- a/src/format-table/Cargo.toml +++ b/src/format-table/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "format_table" -version = "0.1.0" +version = "0.1.1" authors = ["Alex Auvolat "] edition = "2018" license = "AGPL-3.0" diff --git a/src/format-table/lib.rs b/src/format-table/lib.rs index 72fd10b2..55252ba9 100644 --- a/src/format-table/lib.rs +++ b/src/format-table/lib.rs @@ -5,7 +5,7 @@ //! ```rust //! let mut table = vec!["product\tquantity\tprice".to_string()]; //! for (p, q, r) in [("tomato", 12, 15), ("potato", 10, 20), ("rice", 5, 12)] { -//! table.push(format!("{}\t{}\t{}", p, q, r)); +//! table.push(format!("{}\t{}\t{}", p, q, r)); //! } //! format_table::format_table(table); //! ``` -- cgit v1.2.3