line_ui/
element.rs

1/*
2 * Copyright (c) 2025 Jasmine Tai. All rights reserved.
3 */
4
5//! The [`Element`] trait, and various elements.
6
7mod boxed;
8mod cursor;
9mod fixed_width;
10mod gap;
11mod impls;
12mod into;
13mod styled;
14mod text;
15
16use crate::render::RenderChunk;
17
18pub use boxed::*;
19pub use cursor::*;
20pub use fixed_width::*;
21pub use gap::*;
22pub use into::*;
23pub use styled::*;
24pub use text::*;
25
26/// A particular widget that can be rendered to the TUI.
27///
28/// # Lifetime parameter
29///
30/// The `'s` lifetime parameter indicates the lifetime of any values (e.g.,
31/// strings) borrowed by the `Element`. For example:
32///
33/// ```
34/// use line_ui::element::Text;
35///
36/// let my_string = String::from("hello");
37/// let my_element = Text::new(&my_string);
38/// ```
39///
40/// Here, `my_element` implements `Element<'s>`, where `'s` is the lifetime
41/// of `my_string`.
42pub trait Element<'s> {
43    /// The width of the element, in columns.
44    fn width(&self) -> usize;
45
46    /// Renders the element into a sequence of chunks.
47    fn render(&self) -> impl DoubleEndedIterator<Item = RenderChunk<'s>>;
48}