FixedWidth

Struct FixedWidth 

Source
pub struct FixedWidth<E, T = ()> { /* private fields */ }
Expand description

An element that pads or truncates its contents to a constant width.

Implementations§

Source§

impl<'s, E: Element<'s>> FixedWidth<E, ()>

Source

pub fn new(width: usize, content: E) -> Self

Creates a new FixedWidth with the specified width and content.

Source§

impl<'s, E: Element<'s>, T: Element<'s>> FixedWidth<E, T>

Source

pub fn truncated(self, truncate: Direction) -> Self

Changes the side on which the content is truncated.

This option only takes effect if the content is wider than the width.

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

pub fn padded(self, pad: Direction) -> Self

Changes the side on which padding is added.

This option only takes effect if the content is narrower than the width.

Source

pub fn truncated_with<U: Element<'s>>(self, truncation: U) -> FixedWidth<E, U>

Changes the element displayed when truncation occurs.

This option only takes effect if the content is wider than the width. When this happens, this element is displayed on the side that is truncated. This element’s width must not exceed the width of the FixedWidth.

Trait Implementations§

Source§

impl<E: Clone, T: Clone> Clone for FixedWidth<E, T>

Source§

fn clone(&self) -> FixedWidth<E, T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<E: Debug, T: Debug> Debug for FixedWidth<E, T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'s, E, T> Element<'s> for FixedWidth<E, T>
where E: Element<'s>, T: Element<'s>,

Source§

fn width(&self) -> usize

The width of the element, in columns.
Source§

fn render(&self) -> impl DoubleEndedIterator<Item = RenderChunk<'s>>

Renders the element into a sequence of chunks.

Auto Trait Implementations§

§

impl<E, T> Freeze for FixedWidth<E, T>
where E: Freeze, T: Freeze,

§

impl<E, T> RefUnwindSafe for FixedWidth<E, T>

§

impl<E, T> Send for FixedWidth<E, T>
where E: Send, T: Send,

§

impl<E, T> Sync for FixedWidth<E, T>
where E: Sync, T: Sync,

§

impl<E, T> Unpin for FixedWidth<E, T>
where E: Unpin, T: Unpin,

§

impl<E, T> UnwindSafe for FixedWidth<E, T>
where E: UnwindSafe, T: 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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<'s, E> IntoElement<'s> for E
where E: Element<'s>,

Source§

type ElementType = E

The element type to be converted into.
Source§

fn into_element(self) -> <E as IntoElement<'s>>::ElementType

Converts this type into an Element.
Source§

fn fixed_width(self, width: usize) -> FixedWidth<Self::ElementType>

Convenience function to wrap this element in a FixedWidth.
Source§

fn styled(self, style: Style) -> Styled<Self::ElementType>

Convenience function to wrap this element in a Styled.
Source§

fn boxed(self) -> BoxElement<'s>

Convenience function to box this element.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.