Write raw ansi piped from other cli command

I am trying to write some session multiplexer like tmux (but without a server), I spawn a new process, with fork, create a slave pty and read from the master pty

the piped program (in my example cmatrix) sends ansi escape codes, I convert them to string via str::from_utf8, save them on a local buffer and when render is called, it dump the buffer

my buffer is Vec<String> , each string is a line, my render function is literally this

pub fn render(&self, area: Rect, buf: &mut Buffer) {
        let buffer = self.process.buffer.lock().unwrap();
        Text::from_iter(
            buffer
                .iter()
                .skip(buffer.len().saturating_sub(area.height as usize))
                .map(|s| s.as_str())
                .map(Line::from),
        )
        .render(area, buf);
    }

here is what I get when I run cmatrix

I guess it has to do something with the string conversions, but I am not sure how I can write prestyled text to ratatui and render the colors

You are correct. Due to the way Ratatui handles cells and styles, there isn’t a way to directly pass ansi escapes through directly.

See this github issue.

However, you can use this crate (ansi-to-tui) to convert an input string into a ratatui Text with the styling applied!

bummer, thanks for the answer and for the crate suggestion, I am going to to use that

1 Like