But it is also possible to draw to the Buffer manually:
let backend =ratatui::backend::TestBackend::new(80,5);letmut terminal =ratatui::terminal::Terminal::new(backend).unwrap();terminal.draw(|frame|{let area = frame.size();letmut buffer = frame.buffer_mut();ratatui::widgets::Block::bordered().render(area,&mut buffer);let x =15;let y =2;let string ="The quick brown fox jumps over the lazy dog.";let style =ratatui::style::Style::default(); buffer.set_string(x, y, string, style); show_html(buffer_to_html(buffer));});
The Widget trait in ratatui allows you to define what you want to draw to a Buffer:
struct MyCustomWidget { counter:usize}impl Widget for MyCustomWidget {fn render(self, area:ratatui::prelude::Rect, buf:&mutratatui::prelude::Buffer) {let x =15;let y =2;let string =format!("The quick brown fox jumps over the lazy dog - {}",self.counter);let style =ratatui::style::Style::default(); buf.set_string(x, y, string, style);}}
let backend =ratatui::backend::TestBackend::new(80,5);letmut terminal =ratatui::terminal::Terminal::new(backend).unwrap();terminal.draw(|frame|{let area = frame.size();letmut buffer = frame.buffer_mut();ratatui::widgets::Block::bordered().render(area,&mut buffer); MyCustomWidget{ counter:2}.render(area,&mut buffer); show_html(buffer_to_html(buffer));});
Here’s an example of filling the entire buffer with the symbol ▒:
struct Hatcher;impl Widget for Hatcher {fn render(self, area:ratatui::prelude::Rect, buf:&mutratatui::prelude::Buffer) {for x in area.left()..area.right() {for y in area.top()..area.bottom() { buf.set_string(x, y,"▒",ratatui::style::Style::default());}};}}
Here’s the output of a Hatcher being rendered:
let backend =ratatui::backend::TestBackend::new(80,5);letmut terminal =ratatui::terminal::Terminal::new(backend).unwrap();terminal.draw(|frame|{let area = frame.size();letmut buffer = frame.buffer_mut(); Hatcher.render(area,&mut buffer); show_html(buffer_to_html(buffer));});