Skip to content

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

@chris-laplante

Description

@chris-laplante

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions