aboutsummaryrefslogtreecommitdiff
path: root/src/format-table/lib.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2023-05-17 13:01:37 +0200
committerAlex Auvolat <alex@adnab.me>2023-05-17 13:01:37 +0200
commita1cec2cd60d64208caa2cbd11d8e6f885e80f630 (patch)
treed4e73ac7a8b1e90a998cf2795e20f95a0be9b871 /src/format-table/lib.rs
parentb66f247580dcfb733718949c35240500903a0802 (diff)
downloadgarage-a1cec2cd60d64208caa2cbd11d8e6f885e80f630.tar.gz
garage-a1cec2cd60d64208caa2cbd11d8e6f885e80f630.zip
Split format_table into separate crate and reduce k2v-client dependenciesformat_table-v0.1.0
Diffstat (limited to 'src/format-table/lib.rs')
-rw-r--r--src/format-table/lib.rs49
1 files changed, 49 insertions, 0 deletions
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<String>`, 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>) -> String {
+ let data = data
+ .iter()
+ .map(|s| s.split('\t').collect::<Vec<_>>())
+ .collect::<Vec<_>>();
+
+ 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<String>) {
+ print!("{}", format_table_to_string(data));
+}