nestable 0.2.3

Unicode-aware text table rendering with support for nested tables
Documentation
use nestable::*;
use std::thread::sleep;
use std::time::Duration;

fn cls() {
    print!("\x1b[2J\x1b[H");
}

#[rustfmt::skip]
fn hello_world() -> Table<'static> {
    Table::new()
    .add_rows([
        vec!["Building tables in your terminal with nestable"],
        vec!["Example", "use nestable::*;\n\nfn main() {\n    let table = Table::new()\n        .add_rows([[\"Hello\",\"World\"]]);\n    println!(\"{}\", table);\n}"],
        vec!["Outputs", "╭─────┬─────╮\n│Hello│World│\n╰─────┴─────╯" ],
    ])
    .set_column_attr(0, attr![ansi::BOLD])
}

#[rustfmt::skip]
fn multiply() -> Vec<Vec<String>> {
    (1..=16).map(|row| {
        (1..=16).map(|col| {
            format!("{:3}", row * col)
        }).collect()
    }).collect()
}

#[rustfmt::skip]
fn multiply_table() -> Table<'static> {
    Table::new()
    .add_row([ "Multiplication Table" ])
    .add_rows(multiply())
    .set_table_attr(attr![ansi::GREEN])
    .set_row_align(0, Some(Horizontal::Centre), None)
}

#[rustfmt::skip]
fn color256() -> Vec<Vec<String>> {
    (0..16).map(|row| {
        (0..16).map(|col| {
            let colour = row * 16 + col;
            format!("{} {:3} {}", ansi::bg_256(colour as u8), colour, ansi::RESET)
        }).collect()
    }).collect()
}

#[rustfmt::skip]
fn color256_table() -> Table<'static> {
    Table::new()
    .add_row(["ANSI 256 Colors"])
    .add_rows(color256())
    .set_row_align(0, Some(Horizontal::Centre), None)
    .set_table_attr(attr![ansi::BRIGHT_WHITE])
}

#[rustfmt::skip]
fn main() {
    for table_fn in [
        hello_world,
        multiply_table,
        color256_table
    ] {
        cls();
        print!("{}", table_fn());
        sleep(Duration::from_secs(3));
    }

    for (name, style) in styles![
        ASCII_BORDER,
        ASCII_NO_BORDER,
        ASCII_NO_ROW_SEPARATOR,
        GLYPHS_SQUARE,
        GLYPHS_ROUNDED,
        GLYPHS_NO_BORDER,
        GLYPHS_NO_ROW_SEPARATOR,
        GLYPHS_ROUNDED_SPACED,
        SPACE,
        HEAVY,
        DOUBLE,
        DOUBLE_HEAVY,
        HEAVY_MIXED
    ] {
        cls();

        let table = Table::new()
            .add_rows([
                ["Language", "Text"],
                ["English", "nestable\nrendering tables\nacross multiple lines"],
                ["Japanese", "ネスタブル\n複数行にわたる\nテーブル表示 🙂"],
                ["Chinese\n(Simplified)", "可嵌套表格\n支持多行文本\n终端显示"],
                ["Korean", "중첩 가능한 테이블\n여러 줄 텍스트\n터미널 출력"],
                ["Persian", "جداول تو در تو\nمتن چند خطی\nخروجی ترمینال"],
                ["Sanskrit", "नेस्टेबल टेबल्स\nबहुपङ्क्तिः पाठः\nटर्मिनल आउटपुट"],
            ])
            .set_table_style(&style)
            .set_border_attr(attr![ansi::GREEN])
            .set_column_attr(0, attr![ansi::CYAN]);

        let cwd = std::env::current_dir().map(|p| p.to_string_lossy().to_string()).unwrap_or_else(|_| "<unknown>".to_string());

        let user = std::env::var("USER").map(|p| p.to_string()).unwrap_or_else(|_| "<unknown>".to_string());

        let system = Table::new()
            .add_rows([
                ["Property", "Value"],
                ["OS", std::env::consts::OS],
                ["Architecture", std::env::consts::ARCH],
                ["Directory", &cwd],
                ["User", &user],
            ])
            .set_table_style(&style)
            .set_border_attr(attr![ansi::BLUE])
            .set_column_attr(0, attr![ansi::MAGENTA]);

        let nested = Table::new()
            .add_rows([
                vec!["System Details", "Unicode Demo"],
                vec![&system.to_string(), &table.to_string()],
                vec![name],
            ])
            .set_table_style(&style)
            .set_border_attr(attr![ansi::RED])
            .set_row_align(2, Some(Horizontal::Centre), None)
            .set_cell_align(1, 0, None, Some(Vertical::Centre));

        print!("{}", nested);

        sleep(Duration::from_secs(3));
    }
}