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

Any sane way of getting visible row-by-row contents? #6

Open
chris-laplante opened this issue May 4, 2022 · 0 comments
Open

Any sane way of getting visible row-by-row contents? #6

chris-laplante opened this issue May 4, 2022 · 0 comments

Comments

@chris-laplante
Copy link

chris-laplante commented May 4, 2022

Let's say we have a terminal with 30 cols. I want to write some text to it that may contain lines over 30 chars. When that's done, I want to see what the terminal looks like, row-by-row. As far as I can tell there is no sane way to do this currently. Here's an example:

let mut parser = vt100::Parser::new(20, 30, 0);
for i in 0..4 {
    write!(parser, "short line {}\n\r", i).unwrap();
}

write!(parser, "this is a line longer than 30 characters long but you wouldn't know it looking at `contents()`").unwrap();
dgb!(parser.contents());

contents() is not helpful because it shows me exactly what I gave the terminal, not how it is rendered:

short line 0\nshort line 1\nshort line 2\nshort line 3\nthis is a line longer than 30 characters long but you wouldn't know it looking at contents()

I want to get this:

[
    "short line 0",
    "short line 1",
    "short line 2",
    "short line 3",
    "this is a line longer than 30",
    "characters long but you wouldn",
    "'t know it looking at `content",
    "s()`",
]

So far the way I'm getting that is with this code:

let mut rows = parser
    .screen()
    .rows(0, 30)
    .collect::<Vec<_>>();

// Reverse the rows and trim empty lines from the end
rows = rows
    .into_iter()
    .rev()
    .skip_while(|line| line.is_empty())
    .map(|line| line.trim_end().to_string())
    .collect();

// Un-reverse the rows and join them up with newlines
rows.reverse();

(The reason I have to reverse, skip_while, then un-reverse is because if you use control characters to mess with cursor position and erase lines, you will end up with empty lines at the bottom of the terminal that I want to exclude).

Seems like there should be a simpler way.

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

1 participant