X-Git-Url: http://git.scottworley.com/tablify/blobdiff_plain/de408c29be1f465f08fc0ba4114704d3ef8bdcef..7067975b593ef6a8b639f61dda710b023ec26a25:/src/lib.rs
diff --git a/src/lib.rs b/src/lib.rs
index 9504c7f..a3878c5 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,4 +1,5 @@
use std::collections::{HashMap, HashSet};
+use std::fmt::Write;
use std::io::BufRead;
use std::iter::Iterator;
@@ -32,7 +33,8 @@ const HEADER: &str = "
- ";
+
+";
const FOOTER: &str = "
@@ -186,6 +188,16 @@ fn render_row(columns: &[String], row: &RowInput) -> String {
)
}
+fn render_column_headers(columns: &[String]) -> String {
+ // TODO: Escape HTML special characters
+ String::from(" | ")
+ + &columns.iter().fold(String::new(), |mut acc, c| {
+ write!(&mut acc, "{c} | ").unwrap();
+ acc
+ })
+ + "\n"
+}
+
/// # Errors
///
/// Will return `Err` if
@@ -196,6 +208,7 @@ pub fn tablify(input: impl std::io::Read) -> Result {
let rows = read_rows(input).collect::, _>>()?;
let columns = column_order(&rows);
Ok(String::from(HEADER)
+ + &render_column_headers(&columns)
+ &rows
.into_iter()
.map(|r| render_row(&columns, &r))