Renderer

Struct Renderer 

Source
pub struct Renderer<W: Write> { /* private fields */ }
Expand description

A struct that outputs lines to a writer.

Implementations§

Source§

impl<W: Write> Renderer<W>

Source

pub fn new(writer: W) -> Self

Creates a new Renderer that writes to the given writer.

Examples found in repository?
examples/input.rs (line 13)
11fn main() -> std::io::Result<()> {
12    let stdout = std::io::stdout().into_raw_mode()?;
13    let mut r = Renderer::new(stdout);
14
15    let mut name = String::new();
16
17    let mut events = std::io::stdin().events();
18    loop {
19        r.clear()?;
20        r.render((
21            "Enter your name: ".into_element(),
22            (name.into_element(), Cursor, Gap(1))
23                .fixed_width(20)
24                .truncated(Direction::Left)
25                .with_style(Style::bg(240)),
26        ))?;
27        r.finish()?;
28
29        let Some(event) = events.next().transpose()? else {
30            break;
31        };
32        match event {
33            Event::Key(Key::Char(ch)) if !ch.is_ascii_control() => name.push(ch),
34            Event::Key(Key::Char('\n' | '\r')) => break,
35            Event::Key(Key::Backspace) => {
36                name.pop();
37            }
38            _ => {}
39        }
40    }
41
42    r.clear()?;
43    drop(r);
44    println!("Your name is {name:?}");
45    Ok(())
46}
Source

pub fn reset(&mut self) -> Result<&mut Self>

Resets the cursor position. This should be called before render.

Source

pub fn clear(&mut self) -> Result<()>

Clears the rendering area, resetting the terminal back to its initial state.

Note that this function is automatically called when the Renderer is dropped.

Examples found in repository?
examples/input.rs (line 19)
11fn main() -> std::io::Result<()> {
12    let stdout = std::io::stdout().into_raw_mode()?;
13    let mut r = Renderer::new(stdout);
14
15    let mut name = String::new();
16
17    let mut events = std::io::stdin().events();
18    loop {
19        r.clear()?;
20        r.render((
21            "Enter your name: ".into_element(),
22            (name.into_element(), Cursor, Gap(1))
23                .fixed_width(20)
24                .truncated(Direction::Left)
25                .with_style(Style::bg(240)),
26        ))?;
27        r.finish()?;
28
29        let Some(event) = events.next().transpose()? else {
30            break;
31        };
32        match event {
33            Event::Key(Key::Char(ch)) if !ch.is_ascii_control() => name.push(ch),
34            Event::Key(Key::Char('\n' | '\r')) => break,
35            Event::Key(Key::Backspace) => {
36                name.pop();
37            }
38            _ => {}
39        }
40    }
41
42    r.clear()?;
43    drop(r);
44    println!("Your name is {name:?}");
45    Ok(())
46}
Source

pub fn render<E: Element>(&mut self, line: E) -> Result<&mut Self>

Renders a line.

Examples found in repository?
examples/input.rs (lines 20-26)
11fn main() -> std::io::Result<()> {
12    let stdout = std::io::stdout().into_raw_mode()?;
13    let mut r = Renderer::new(stdout);
14
15    let mut name = String::new();
16
17    let mut events = std::io::stdin().events();
18    loop {
19        r.clear()?;
20        r.render((
21            "Enter your name: ".into_element(),
22            (name.into_element(), Cursor, Gap(1))
23                .fixed_width(20)
24                .truncated(Direction::Left)
25                .with_style(Style::bg(240)),
26        ))?;
27        r.finish()?;
28
29        let Some(event) = events.next().transpose()? else {
30            break;
31        };
32        match event {
33            Event::Key(Key::Char(ch)) if !ch.is_ascii_control() => name.push(ch),
34            Event::Key(Key::Char('\n' | '\r')) => break,
35            Event::Key(Key::Backspace) => {
36                name.pop();
37            }
38            _ => {}
39        }
40    }
41
42    r.clear()?;
43    drop(r);
44    println!("Your name is {name:?}");
45    Ok(())
46}
Source

pub fn finish(&mut self) -> Result<()>

Finishes rendering. This should be called after render and before polling inputs.

Examples found in repository?
examples/input.rs (line 27)
11fn main() -> std::io::Result<()> {
12    let stdout = std::io::stdout().into_raw_mode()?;
13    let mut r = Renderer::new(stdout);
14
15    let mut name = String::new();
16
17    let mut events = std::io::stdin().events();
18    loop {
19        r.clear()?;
20        r.render((
21            "Enter your name: ".into_element(),
22            (name.into_element(), Cursor, Gap(1))
23                .fixed_width(20)
24                .truncated(Direction::Left)
25                .with_style(Style::bg(240)),
26        ))?;
27        r.finish()?;
28
29        let Some(event) = events.next().transpose()? else {
30            break;
31        };
32        match event {
33            Event::Key(Key::Char(ch)) if !ch.is_ascii_control() => name.push(ch),
34            Event::Key(Key::Char('\n' | '\r')) => break,
35            Event::Key(Key::Backspace) => {
36                name.pop();
37            }
38            _ => {}
39        }
40    }
41
42    r.clear()?;
43    drop(r);
44    println!("Your name is {name:?}");
45    Ok(())
46}

Trait Implementations§

Source§

impl<W: Write> Drop for Renderer<W>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<W> Freeze for Renderer<W>
where W: Freeze,

§

impl<W> RefUnwindSafe for Renderer<W>
where W: RefUnwindSafe,

§

impl<W> Send for Renderer<W>
where W: Send,

§

impl<W> Sync for Renderer<W>
where W: Sync,

§

impl<W> Unpin for Renderer<W>
where W: Unpin,

§

impl<W> UnwindSafe for Renderer<W>
where W: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.