Skip to main content

html_view/
main.rs

1//! The [`Html`] view on its own: a whole HTML fragment as the screen.
2//!
3//! ```bash
4//! cargo run -p tuika-html --example html_view            # a real terminal (q quits)
5//! cargo run -p tuika-html --example html_view -- --dump  # one frame as text
6//! ```
7//!
8//! The companion example, `html_markdown`, shows the *seam* — HTML blocks
9//! inside a markdown document. This one shows the **component**: no markdown
10//! anywhere, `Html` placed in a layout exactly like `Markdown` would be, fitting
11//! its content to whatever width the pane gives it.
12
13use tuika::prelude::*;
14use tuika::testing::{grid, render};
15use tuika_html::Html;
16
17/// Shared with the demo generator so the recording cannot drift from the app.
18const PAGE: &str = include_str!("page.html");
19
20fn scene() -> Element {
21    let page = Boxed::new(element(Html::new(PAGE)))
22        .title(" ada.html ")
23        .padding(Padding::symmetric(2, 1));
24    let hints = KeyHints::new([("q", "quit")]);
25    view! {
26        col(padding = Padding::all(1), gap = 1) {
27            grow(1) { node(page) }
28            fixed(1) { node(hints) }
29        }
30    }
31}
32
33fn main() -> std::io::Result<()> {
34    let theme = Theme::default();
35    if std::env::args().any(|a| a == "--dump") {
36        for line in grid(&render(scene().as_ref(), 78, 44, &theme)).lines() {
37            println!("{}", line.trim_end());
38        }
39        return Ok(());
40    }
41
42    let runner = Runner::new(RunnerConfig::default());
43    runner.run(
44        &theme,
45        &mut (),
46        |(), _frame| scene(),
47        |(), signal| match signal {
48            Signal::Event(Event::Key(key))
49                if matches!(key.code, KeyCode::Char('q') | KeyCode::Esc) =>
50            {
51                UpdateResult::Exit
52            }
53            _ => UpdateResult::Clean,
54        },
55    )
56}