Ratatui is a crate for building terminal user interfaces in Rust.
In this post, we’ll discuss the Rect and Layout primitives of ratatui.
Code
:dep ratatui ="0.26.2":dep ratatui-macros ="0.4.0"fn span_to_html(s:ratatui::text::Span) ->String{letmut html =String::new(); html.push_str("<span style=\"");// Set foreground colorifletSome(color) =&s.style.fg { html.push_str(&format!("color: {};", color));}// Set background colorifletSome(color) =&s.style.bg { html.push_str(&format!("background-color: {};", color));}// Add modifiersmatch s.style.add_modifier {ratatui::style::Modifier::BOLD => html.push_str("font-weight: bold;"),ratatui::style::Modifier::ITALIC => html.push_str("font-style: italic;"),ratatui::style::Modifier::UNDERLINED => html.push_str("text-decoration: underline;"), _ =>{}} html.push_str("\">"); html.push_str(&s.content); html.push_str("</span>"); html}fn buffer_to_html(buf:&ratatui::buffer::Buffer) ->String{fn escape_special_html_characters(text:&str) ->String{ text.replace("&","&").replace("<","<").replace(">",">").replace("\"",""").replace("'","'")}letmut html =String::from("<pre><code>");let w = buf.area.width;let h = buf.area.height;for y in0..h {for x in0..w {let s = buf.get(x, y).symbol();let escaped = escape_special_html_characters(s);let style = buf.get(x, y).style();let span =ratatui::text::Span::styled(s, style); html.push_str(&span_to_html(span));} html.push('\n');} html.push_str("</code></pre>"); html }fn show_html<D>(content: D) where D:std::fmt::Display{println!(r#"EVCXR_BEGIN_CONTENT text/html<div style="display: flex; justify-content:start; gap: 1em; margin: 1em">{}</div>EVCXR_END_CONTENT"#, content);}
Layout primitives
We already saw that Rect is one of the primitives for rendering a widget.
We can create a Rect using Rect::new(x, y, width, height):
useratatui::widgets::Widget;let (x, y, width, height) = (0,0,50,5);let area =ratatui::layout::Rect::new(x, y, width, height);letmut buf =ratatui::buffer::Buffer::empty(area);ratatui::widgets::Block::bordered().render(area,&mut buf);show_html(buffer_to_html(&buf))