ableos_userland/libraries/table/src/lib.rs
2022-12-04 05:19:23 -06:00

81 lines
1.8 KiB
Rust

use core::fmt::Display;
use std::collections::HashMap;
pub type Header = String;
#[derive(Debug)]
pub struct Row(pub Vec<String>, pub usize);
impl Display for Row {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// let header = &self.0;
let column = &self.0;
let longest_length = &self.1;
// write!(f, "{}", header)?;
// for _ in 0..longest_length - header.len() {
// write!(f, " ")?;
// }
// write!(f, "|\n")?;
// for _ in 0..*longest_length {
// write!(f, "-")?;
// }
// write!(f, "|\n")?;
for xys in column {
write!(f, "{}", xys)?;
for _ in 0..longest_length - xys.len() {
write!(f, " ")?;
}
write!(f, "|\n")?;
}
Ok(())
}
}
pub struct Table {
pub table: HashMap<Header, Row>,
}
impl Display for Table {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (header, row) in &self.table {
write!(f, "{}", header)?;
for _ in 0..row.1 - header.len() {
write!(f, " ")?;
}
write!(f, "|\n")?;
for _ in 0..row.1 {
write!(f, "-")?;
}
write!(f, "|\n")?;
write!(f, "{}", row)?;
}
Ok(())
}
}
#[test]
fn test_print() {
let tbl = Table {
table: HashMap::new(),
};
/* tbl.table.append(&mut vec![Row(
"header".to_string(),
vec![
"hi".to_string(),
"hey whats up there".to_string(),
"hey".to_string(),
"whats".to_string(),
"up".to_string(),
"there".to_string(),
],
18,
)]);
*/
println!("{}", tbl);
}