prettytable-rs icon indicating copy to clipboard operation
prettytable-rs copied to clipboard

Implement column/table max-width

Open nicklan opened this issue 8 years ago • 4 comments

I would be neat if you could specify a max width for columns and/or tables and have prettytable automatically truncate strings (and probably add a configurable indicator like ...) that exceed that length.

nicklan avatar Apr 13 '17 19:04 nicklan

That would be great. I'll try to find some time to implement this, unless someone wants to do it.

phsym avatar Apr 25 '17 12:04 phsym

Optional there could be inserted a \n to make a multiline entry if the length is exceeded. This could be default for the terminal width

h4llow3En avatar Apr 27 '17 12:04 h4llow3En

Also, it would be great if a table can be fit to the terminal window by specifying columns that can be shrunk.

RazrFalcon avatar Jan 11 '18 13:01 RazrFalcon

I kinda worked around it with the help of textwrap and unicode_width:

[package]
name = "textwrap_prettytable"
version = "0.1.0"
authors = ["Alexander Thaller <[email protected]>"]

[dependencies]
prettytable-rs = "0.7"
unicode-width = "0.1"

[dependencies.textwrap]
version = "0.10"
features = ["term_size"]
#[macro_use]
extern crate prettytable;
extern crate textwrap;
extern crate unicode_width;

use prettytable::{
    format,
    Table,
};
use unicode_width::UnicodeWidthStr;

fn main() {
    let entries = vec![
        ("first", "first description"),
        ("second", "second description"),
        (
            "third",
            "third description which is a bit longer than usual and probably has to be wrapped. \
             if not it should also be fine.",
        ),
        (
            "fourth",
            "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam felis risus, faucibus \
             a accumsan a, blandit in sem. Ut venenatis vulputate ante, in volutpat mi pharetra \
             non.",
        ),
    ];

    // We are gonna get the width the description should be wrapped with by getting
    // the largest name and substracting that from the terminal width.
    let description_width = {
        let mut max_realm_width = 0;
        for (name, _) in &entries {
            // Width of the string largest string + surrounding formatting
            // Something like this
            // this is a name | The description of the name.
            let width = UnicodeWidthStr::width(format!(" {} |  ", name).as_str());

            if max_realm_width < width {
                max_realm_width = width
            }
        }

        let termwidth = textwrap::termwidth();
        termwidth - max_realm_width
    };

    let mut table = Table::new();
    table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
    table.add_row(row![b -> "Name", b -> "Description"]);

    for (name, description) in &entries {
        table.add_row(row![name, textwrap::fill(description, description_width),]);
    }

    table.printstd();
}

AlexanderThaller avatar Jun 29 '18 15:06 AlexanderThaller