#tabular #table #print #console

tprint

A simple crate to print tabular data

2 stable releases

1.0.1 Jan 21, 2025

#423 in Command-line interface

Download history 180/week @ 2025-01-18 25/week @ 2025-01-25 7/week @ 2025-02-01 1/week @ 2025-02-08

213 downloads per month

MIT license

53KB
497 lines

tprint

Crates.io Documentation Dependency status License

A simple Rust crate to print tabular data. A similar C library: libtprint

Features

  • Optional support for Unicode
  • output to a String / file
  • output as HTML table
  • ASCII borders / Unicode borders

Documentation

Examples

Basic example

use tprint::{TPrint, TPrintAlign};

fn main() -> std::io::Result<()> {
    TPrint::new(true, true, 10, 3).
        column_add("Name", TPrintAlign::Center, TPrintAlign::Left).
        column_add("Age", TPrintAlign::Center, TPrintAlign::Center).
        column_add("City", TPrintAlign::Center, TPrintAlign::Right).
        column_add("Job title", TPrintAlign::Center, TPrintAlign::Left).
        column_add("โ„•๐• ๐•ฅ๐•–๐•ค", TPrintAlign::Center, TPrintAlign::Center).
        add_data("Fernando Alameda").
        add_data(42).
        add_data("London").
        add_data("Software developer").
        add_data("โ’ฝโ“”โ“›โ“›โ“ž โ“Œโ“žโ“กโ“›โ““").
        add_data("Teo Ocaรฑa").
        add_data(1).
        add_data("Madrid").
        add_data("Accountant").
        add_data("โค๏ธ๐Ÿ˜ด๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ").
        print()
}

Basic usage

Output to a String

use tprint::{TPrint, TPrintAlign, TPrintOutputString, TPrintBordersUnicode};
use std::cell::RefCell;
use std::rc::Rc;

fn main() -> std::io::Result<()> {
    let str_output = Rc::new(RefCell::new(TPrintOutputString::new()));
    let borders = Rc::new(RefCell::new(TPrintBordersUnicode {}));
    let mut str_tprint = TPrint::new_with_borders_output(borders, str_output.clone(), true, true, 0, 1);

    str_tprint.column_add("Left", TPrintAlign::Left, TPrintAlign::Left);
    str_tprint.column_add("Center", TPrintAlign::Center, TPrintAlign::Center);
    str_tprint.column_add("Right", TPrintAlign::Right, TPrintAlign::Right);
    str_tprint.add_data("");
    str_tprint.add_data("");
    str_tprint.add_data("");

    let j:i64 = 10;
    const MAX:u32 = 10;
    for i in 0..MAX {
        str_tprint.add_data(j.pow(i).to_string());
        str_tprint.add_data(j.pow(i).to_string());
        str_tprint.add_data(j.pow(MAX-i-1).to_string());
    }
    str_tprint.print();

    println!("{}", str_output.borrow().get_str());
    Ok(())
}

Output to a string

Output to a HTML file

use tprint::{TPrint, TPrintAlign, TPrintBordersHTML, TPrintOutputFile};
use std::cell::RefCell;
use std::rc::Rc;

const OUT_FILE: &str = "/tmp/tprint.html";

fn main() -> std::io::Result<()> {
    let output = Rc::new(RefCell::new(TPrintOutputFile::new(OUT_FILE).unwrap()));
    let borders = Rc::new(RefCell::new(TPrintBordersHTML {}));
    TPrint::new_with_borders_output(borders, output, true, true, 0, 1).
        column_add("Name", TPrintAlign::Center, TPrintAlign::Left).
        column_add("Age", TPrintAlign::Center, TPrintAlign::Center).
        column_add("City", TPrintAlign::Center, TPrintAlign::Right).
        column_add("Job title", TPrintAlign::Center, TPrintAlign::Left).
        column_add("โ„•๐• ๐•ฅ๐•–๐•ค", TPrintAlign::Center, TPrintAlign::Center).
        add_data("Fernando Alameda").
        add_data(42).
        add_data("London").
        add_data("Software developer").
        add_data("โ’ฝโ“”โ“›โ“›โ“ž โ“Œโ“žโ“กโ“›โ““").
        add_data("Teo Ocaรฑa").
        add_data(1).
        add_data("Madrid").
        add_data("Accountant").
        add_data("โค๏ธ๐Ÿ˜ด๐Ÿคฆ๐Ÿผโ€โ™‚๏ธ").
        print()
}

Output to a HTML

More examples in examples folder.

License

Licensed under the Apache License, Version 2.0 (the "License").

Dependencies

~0โ€“380KB