Skip to main content

Align

Struct Align 

Source
pub struct Align { /* private fields */ }
Expand description

Align a renderable by adding spaces.

Align wraps a renderable and positions it within the available space by adding padding. Supports both horizontal (left, center, right) and vertical (top, middle, bottom) alignment.

§Example

use rich_rs::{Align, Text, Style};
use rich_rs::align::VerticalAlignMethod;

// Simple center alignment
let text = Text::plain("Centered");
let aligned = Align::center(Box::new(text));

// Right-aligned with background style
let text = Text::plain("Right");
let aligned = Align::right(Box::new(text))
    .with_style(Style::new().with_bold(true));

// Full alignment with vertical centering
let text = Text::plain("Middle");
let aligned = Align::center(Box::new(text))
    .with_vertical(VerticalAlignMethod::Middle)
    .with_height(10);

Implementations§

Source§

impl Align

Source

pub fn new( renderable: Box<dyn Renderable + Send + Sync>, align: AlignMethod, ) -> Self

Create a new Align wrapper with the specified alignment.

§Arguments
  • renderable - The content to align.
  • align - Horizontal alignment method.
§Example
use rich_rs::{Align, Text, AlignMethod};

let text = Text::plain("Hello");
let aligned = Align::new(Box::new(text), AlignMethod::Center);
Source

pub fn left(renderable: Box<dyn Renderable + Send + Sync>) -> Self

Create a left-aligned wrapper.

§Example
use rich_rs::{Align, Text};

let text = Text::plain("Left-aligned");
let aligned = Align::left(Box::new(text));
Source

pub fn center(renderable: Box<dyn Renderable + Send + Sync>) -> Self

Create a center-aligned wrapper.

§Example
use rich_rs::{Align, Text};

let text = Text::plain("Centered");
let aligned = Align::center(Box::new(text));
Source

pub fn right(renderable: Box<dyn Renderable + Send + Sync>) -> Self

Create a right-aligned wrapper.

§Example
use rich_rs::{Align, Text};

let text = Text::plain("Right-aligned");
let aligned = Align::right(Box::new(text));
Source

pub fn with_style(self, style: Style) -> Self

Set the style for padding spaces.

The style is applied to the padding characters (spaces) used for alignment. This is useful for setting a background color on the aligned area.

Source

pub fn with_vertical(self, vertical: VerticalAlignMethod) -> Self

Set the vertical alignment.

Vertical alignment requires a height to be set (either via with_height() or from ConsoleOptions::height).

Source

pub fn with_pad(self, pad: bool) -> Self

Set whether to pad the right side.

When true (default), padding is added to the right to fill the width. When false, only left padding is added for center/right alignment.

Source

pub fn with_width(self, width: usize) -> Self

Set a fixed width constraint.

The content will be constrained to this width. If not set, uses ConsoleOptions::max_width.

Source

pub fn with_height(self, height: usize) -> Self

Set a fixed height constraint.

Required for vertical alignment. If not set but vertical alignment is specified, falls back to ConsoleOptions::height.

Source

pub fn align(&self) -> AlignMethod

Get the horizontal alignment.

Source

pub fn vertical(&self) -> Option<VerticalAlignMethod>

Get the vertical alignment.

Source

pub fn style(&self) -> Style

Get the style.

Source

pub fn pad(&self) -> bool

Get whether padding is enabled.

Source

pub fn width(&self) -> Option<usize>

Get the width constraint.

Source

pub fn height(&self) -> Option<usize>

Get the height constraint.

Trait Implementations§

Source§

impl Debug for Align

Source§

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

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

impl Renderable for Align

Source§

fn render( &self, console: &Console<Stdout>, options: &ConsoleOptions, ) -> Segments

Render this object to a sequence of segments.
Source§

fn measure( &self, console: &Console<Stdout>, options: &ConsoleOptions, ) -> Measurement

Measure the minimum and maximum width requirements. Read more

Auto Trait Implementations§

§

impl Freeze for Align

§

impl !RefUnwindSafe for Align

§

impl Send for Align

§

impl Sync for Align

§

impl Unpin for Align

§

impl !UnwindSafe for Align

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.