Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement column/table max-width #47

Open
nicklan opened this issue Apr 13, 2017 · 4 comments
Open

Implement column/table max-width #47

nicklan opened this issue Apr 13, 2017 · 4 comments
Milestone

Comments

@nicklan
Copy link

nicklan commented Apr 13, 2017

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.

@phsym
Copy link
Owner

phsym commented Apr 25, 2017

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

@h4llow3En
Copy link

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

@RazrFalcon
Copy link

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

@AlexanderThaller
Copy link

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

[package]
name = "textwrap_prettytable"
version = "0.1.0"
authors = ["Alexander Thaller <alexander.thaller@trivago.com>"]

[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();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants