rtlibs-tui 0.1.4

rtools library: ratatui widgets
Documentation
use ratatui::buffer::Buffer;
use ratatui::layout::Alignment;
use ratatui::layout::Rect;
use ratatui::style::Style;
use ratatui::widgets::Clear;
use ratatui::widgets::Paragraph;
use ratatui::widgets::StatefulWidget;
use ratatui::widgets::Widget;

use super::InputNumberState;

pub struct InputNumberWidget
{
    style: Style,
}

impl std::default::Default for InputNumberWidget
{
    fn default() -> Self
    {
        Self::new()
    }
}

impl InputNumberWidget
{
    pub fn new() -> Self
    {
        Self {
            style: Style::default(),
        }
    }

    pub fn style<S: Into<Style>>(
        mut self,
        style: S,
    ) -> Self
    {
        self.style = style.into();
        self
    }
}

impl StatefulWidget for InputNumberWidget
{
    type State = InputNumberState;

    fn render(
        self,
        area: Rect,
        buf: &mut Buffer,
        state: &mut Self::State,
    )
    {
        Clear.render(
            area, buf,
        );

        Paragraph::new(
            state
                .input
                .as_str(),
        )
        .style(self.style)
        .alignment(
            if state.right_aligned
            {
                Alignment::Right
            }
            else
            {
                Alignment::Left
            },
        )
        .render(
            area, buf,
        );

        if state.right_aligned
        {
            state
                .cursor
                .x = area.x + area.width
                - state
                    .input
                    .chars()
                    .count() as u16
                + state.character_index as u16;
        }
        else
        {
            state
                .cursor
                .x = area.x + state.character_index as u16;
        }
        state
            .cursor
            .y = area.y;
    }
}