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

output of a table to a string with styles -- is it possible? #141

Open
faried opened this issue Jun 6, 2022 · 5 comments
Open

output of a table to a string with styles -- is it possible? #141

faried opened this issue Jun 6, 2022 · 5 comments

Comments

@faried
Copy link

faried commented Jun 6, 2022

I have a Vec<String> that represents my output lines. I want to append a table to it but I can't seem to do that and preserve cell styles. I've tried

let mut output = Vec::<String>::new();

let mut table = Table::new();
table.add_row(row![Fg => "first", "second"]);

output.push(table.to_string());
// also tried
output.push(format!("{}", table));

I'm new to Rust, so I don't know if there's a way to do this (prettytable-rs 0.8.0).

@Saruniks
Copy link

Saruniks commented Nov 7, 2022

@faried
Did you find the solution to preserve the style?

I just use print in my project, but it doesn't preserve the style.

table.print(&mut output).unwrap()

@faried
Copy link
Author

faried commented Nov 7, 2022

No, I didn't find a solution. The package's internals need to be modified to support this.

@pinkforest
Copy link
Collaborator

Feel free to send a PR, p.s. it's now 0.10

@krpatter-intc
Copy link

I was going to experiment with using the print_term to help with this by capturing the terminal output. However, I couldn't get the print_term to work

    let mut my_term = term::stdout().unwrap();
    let _ = table.print_term(&mut my_term);

This throws an error for

^ the trait `Terminal` is not implemented for `Box<dyn Terminal<Output = Stdout> + Send>`

Not 100% sure how to capture the input back from the Terminal either, but getting this to work seems like the first step

@krpatter-intc
Copy link

This seems to work for capturing the output in to a string:

    let temp_cursor = Cursor::new(Vec::new());
    let temp_term = term::terminfo::TerminfoTerminal::new(temp_cursor);
    let mut temp_term = temp_term.unwrap();
    let _ = table.print_term(&mut temp_term);
    // now get cursor, then get vector
    let vector = temp_term.get_mut().get_ref();
    let mystr = str::from_utf8(&vector).unwrap();
    println!("{}", mystr);
    println!("----");

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

No branches or pull requests

4 participants