csv_to_table

Crates.iocsv_to_table
lib.rscsv_to_table
version0.4.0
sourcesrc
created_at2023-04-11 23:09:44.239782
updated_at2023-12-20 01:39:55.520071
descriptionA library for pretty print CSV as a table
homepagehttps://github.com/zhiburt/tabled
repositoryhttps://github.com/zhiburt/tabled
max_upload_size
id836406
size51,148
Maxim Zhiburt (zhiburt)

documentation

https://docs.rs/csv_to_table

README

A library for converting csv to a table.

It uses tabled as a rendering backend.

Install

Add the library to a dependency list.

[dependencies]
csv_to_table = "0.3"

Usage

There's 2 approaches the library provides.

  • In memory apporach; where we load CSV into memory and then construct a table.
  • Sniffing a csv; so the used memory will be limited.
  • Setting your constrains so no memory will be used.
Example of in memory approach
fn main() {
    let syscalls = "\
        0,INDIR,,\"int sys_syscall(int number, ...)\"\n\
        1,STD,,\"void sys_exit(int rval)\"\n\
        2,STD,,\"int sys_fork(void)\"\n\
        3,STD,NOLOCK,\"ssize_t sys_read(int fd, void *buf, size_t nbyte)\"\n\
        4,STD,NOLOCK,\"ssize_t sys_write(int fd, const void *buf, size_t nbyte)\""; 

    let table = csv_to_table::from_reader(syscalls.as_bytes()).unwrap();

    println!("{table}")
}
Result
+---+-------+--------+----------------------------------------------------------+
| 0 | INDIR |        | int sys_syscall(int number, ...)                         |
+---+-------+--------+----------------------------------------------------------+
| 1 | STD   |        | void sys_exit(int rval)                                  |
+---+-------+--------+----------------------------------------------------------+
| 2 | STD   |        | int sys_fork(void)                                       |
+---+-------+--------+----------------------------------------------------------+
| 3 | STD   | NOLOCK | ssize_t sys_read(int fd, void *buf, size_t nbyte)        |
+---+-------+--------+----------------------------------------------------------+
| 4 | STD   | NOLOCK | ssize_t sys_write(int fd, const void *buf, size_t nbyte) |
+---+-------+--------+----------------------------------------------------------+
Example of sniffing approach
fn main() {
    let syscalls = "\
        0,INDIR,,\"int sys_syscall(int number, ...)\"\n\
        1,STD,,\"void sys_exit(int rval)\"\n\
        2,STD,,\"int sys_fork(void)\"\n\
        3,STD,NOLOCK,\"ssize_t sys_read(int fd, void *buf, size_t nbyte)\"\n\
        4,STD,NOLOCK,\"ssize_t sys_write(int fd, const void *buf, size_t nbyte)\"";

    let table = csv_to_table::iter::from_reader(syscalls.as_bytes()).sniff(3);

    table.build(std::io::stdout()).unwrap();
}
Result
+---+-------+--+----------------------------------+
| 0 | INDIR |  | int sys_syscall(int number, ...) |
+---+-------+--+----------------------------------+
| 1 | STD   |  | void sys_exit(int rval)          |
+---+-------+--+----------------------------------+
| 2 | STD   |  | int sys_fork(void)               |
+---+-------+--+----------------------------------+
| 3 | STD   |  | ssize_t sys_read(int fd, void *b |
+---+-------+--+----------------------------------+
| 4 | STD   |  | ssize_t sys_write(int fd, const  |
+---+-------+--+----------------------------------+
Notice that the last 2 rows are truncated.
Commit count: 1166

cargo fmt