Skip to main content

ContainerBuilder

Struct ContainerBuilder 

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

ContainerBuilder is used to create Container instances using the builder pattern. This allows for a flexible and readable way to construct complex containers by chaining method calls.

§Example

// Create a container with a header, two options, a separator, some text,
// and a selector.
let container: Container = ContainerBuilder::new()
    .header(...)?
    .option(...)?
    .option(...)?
    .separator_normal(...)
    .text(...)?
    .selector(...)?
    .build();

Implementations§

Source§

impl ContainerBuilder

Source

pub fn new() -> Self

Constructs a new ContainerBuilder.

§Return

ContainerBuilder: A new instance of ContainerBuilder.

§Example
let _ = ContainerBuilder::new();
Source

pub fn header_expl(self, header: Header) -> Self

Explicitly sets a Header component for the Container. Unlike the header method, which takes a label and internally constructs a Header, this method allows you to directly provide a preconstructed Header component.

§Parameters
  • header: A Header component.
§Returns
  • ContainerBuilder: Returns self.
§Example
// Create a `Header` component.
let header = Header::new(...)?;

// Set a preconstructed `Header` component.
ContainerBuilder::new()
    .header_expl(header);
Source

pub fn header(self, label: &str) -> FtuiResult<Self>

Sets a Header component for the Container.

§Parameters
  • label: A &str representing the text to display in the header.
§Returns
  • Ok(ContainerBuilder): Returns self.
  • Err(FtuiError): Returns an error.
§Example
// Sets a `Header` component with the label "Welcome".
ContainerBuilder::new()
    .header("Welcome")?;
Source

pub fn option_expl(self, option: Option) -> Self

Explicitly add an Option component to the Container. Unlike the option method, which takes a label and an optional callback to internally constructs an Option, this method allows you to directly provide a preconstructed Option component.

§Parameters
  • option: An Option component.
§Returns
  • ContainerBuilder: Returns self.
§Example
// Create an `Option` component.
let option = Option::new(...)?;

// Set a preconstructed `Option` component.
ContainerBuilder::new()
    .option_expl(option);
Source

pub fn option( self, label: &str, callback: impl Into<Option<Callback>>, ) -> FtuiResult<Self>

Adds an Option component to the Container.

§Parameters
  • label: A &str representing the text displayed for this option.
  • callback: An optional Callback invoked when the option is selected.
§Returns
  • Ok(ContainerBuilder): Returns self.
  • Err(FtuiError): Returns an error.
§Example
// Add an `Option` component with the label "Option" and no `Callback`.
ContainerBuilder::new()
    .option("Option", None)?;
Source

pub fn option_id_expl(self, option: Option, store_id: &mut u16) -> Self

Explicitly add an Option component to the Container and stores its ID. Unlike the option_id method, which takes a label and an optional callback to internally constructs an Option, this method allows you to directly provide a preconstructed Option component.

§Parameters
  • option: An Option component.
  • store_id: A &mut u16 to store the created Option component ID.
§Returns
  • Ok(ContainerBuilder): Returns self.
  • Err(FtuiError): Returns an error.
§Example
let mut id = 0u16;

// Create an `Option` component.
let option = Option::new(...)?;

// Add the create `Option` component and storing the generated ID in `id`.
ContainerBuilder::new()
    .option_id(option, &mut id)?;
Source

pub fn option_id( self, label: &str, callback: impl Into<Option<Callback>>, store_id: &mut u16, ) -> FtuiResult<Self>

Adds an Option component to the Container and stores its ID.

§Parameters
  • label: The text displayed for this option.
  • callback: An optional Callback invoked when the option is selected.
  • store_id: A &mut u16 to store the created Option component ID.
§Returns
  • Ok(ContainerBuilder): Returns self.
  • Err(FtuiError): Returns an error.
§Example
let mut id = 0u16;

// Add an `Option` labeled "Option" with no `Callback`,
// storing the generated ID in `id`.
ContainerBuilder::new()
    .option_id("Option", None, &mut id)?;
Source

pub fn text_expl(self, text: Text) -> Self

Explicitly add a Text component to the Container. Unlike the text method, which takes a label and flags to internally constructs an Text, this method allows you to directly provide a preconstructed Text component.

§Parameters
  • text: A Text component.
§Returns
  • ContainerBuilder: Returns self.
§Example
// Create an `Text` component.
let text = Text::new(...)?;

// Set a preconstructed `Text` component.
ContainerBuilder::new()
    .text_expl(text);
Source

pub fn text( self, label: &str, flags: impl Into<Option<TextFlags>>, ) -> FtuiResult<Self>

Adds a Text component to the Container.

§Parameters
  • label: A &str representing the text to display.
  • flags: A set of TextFlags, combined using the bitwise OR operator.
§Notes
  • This is what bitwise OR operator look like -> flag1 | flag2 | flag3 ...
§Returns
  • Ok(ContainerBuilder): Returns self.
  • Err(FtuiError): Returns an error.
§Example
// Create a `Text` component labeled "Text", right-aligned and with
// a magenta background.
ContainerBuilder::new()
    .text("Text", TextFlags::ALIGN_RIGHT | TextFlags::COLOR_MAGENTA_BACK)?;
Source

pub fn text_id_expl(self, text: Text, store_id: &mut u16) -> Self

Explicitly add a Text component to the Container and stores its ID. Unlike the text_id method, which takes a label and flags to internally constructs an Text, this method allows you to directly provide a preconstructed Text component.

§Parameters
  • text: An Text component.
  • store_id: A &mut u16 to store the created Option component ID.
§Returns
  • ContainerBuilder: Returns self.
§Example
let mut id = 0u16;

// Create an `Text` component.
let text = Text::new(...)?;

// Add the create `Text` component and storing the generated ID in `id`.
ContainerBuilder::new()
    .text_id_expl(text, &mut id)?;
Source

pub fn text_id( self, label: &str, flags: impl Into<Option<TextFlags>>, store_id: &mut u16, ) -> FtuiResult<Self>

Adds a Text component to the Container and stores its ID.

§Parameters
  • label: A &str representing the text to display.
  • flags: A set of TextFlags, combined using the bitwise OR operator.
  • store_id: A &mut u16 to store the created Text component ID.
§Notes
  • This is what bitwise OR operator look like -> flag1 | flag2 | flag3 ...
§Returns
  • Ok(ContainerBuilder): Returns self.
  • Err(FtuiError): Returns an error.
§Example
let mut id = 0u16;

// Create a `Text` component labeled "Text", right-aligned and with
// a magenta background. storing the generated ID in `id`.
ContainerBuilder::new()
    .text(
        "Text",
        TextFlags::ALIGN_RIGHT | TextFlags::COLOR_MAGENTA_BACK, &mut id)?;
Source

pub fn separator_normal_expl(self, separator: Separator) -> Self

Explicitly add a normal Separator component to the Container. Unlike the Separator method, which takes a style and internally constructs a Separator, this method allows you to directly provide a preconstructed Separator component.

§Parameters
  • separator: A Separator component.
§Returns
  • ContainerBuilder: Returns self.
§Example
// Create a normal `Separator` component.
let separator = Separator::normal(...)?;

// Set a preconstructed `Header` component.
ContainerBuilder::new()
    .separator_normal_expl(separator);
Source

pub fn separator_normal(self, style: SeparatorStyle) -> Self

Add a standard (non-dotted) Separator with the given style.

§Parameters
  • style: The visual style of the separator, specified as a SeparatorStyle.
§Returns
  • ContainerBuilder: Returns self.
§Example
// Add a normal separator with a solid style.
ContainerBuilder::new()
    separator_normal(SeparatorStyle::Solid);
Source

pub fn separator_dotted_expl(self, separator: Separator) -> Self

Explicitly add a dotted Separator component to the Container. Unlike the Separator method, which takes a style and internally constructs a Separator, this method allows you to directly provide a preconstructed Separator component.

§Parameters
  • separator: A Separator component.
§Returns
  • ContainerBuilder: Returns self.
§Example
// Create a dotted `Separator` component.
let separator = Separator::dotted(...)?;

// Set a preconstructed `Header` component.
ContainerBuilder::new()
    .separator_dotted_expl(separator);
Source

pub fn separator_dotted(self, style: SeparatorStyle) -> Self

Add a dotted Separator with the given style.

§Parameters
  • style: The visual style of the separator, specified as a SeparatorStyle.
§Returns
  • ContainerBuilder: Returns self.
§Example
// Add a dotted separator with a solid style.
ContainerBuilder::new()
    separator_dotted(SeparatorStyle::Solid);
Source

pub fn selector_expl(self, selector: Selector) -> Self

Explicitly sets a Selector component for the Container. Unlike the selector method, which takes triggers and internally constructs a selector, this method allows you to directly provide a preconstructed Selector component.

§Parameters
  • selector: A Selector component.
§Returns
  • ContainerBuilder: Returns self.
§Example
// Create a `Header` component.
let selector = Selector::new(...)?;

// Set a preconstructed `Selector` component.
ContainerBuilder::new()
    .selector_expl(selector);
Source

pub fn selector( self, up_trig: Trigger, down_trig: Trigger, selc_trig: Trigger, ) -> Self

Set a Selector for the Container to handle user navigation.

§Parameters
  • up_trig: A Trigger that moves the selector up when activated.
  • down_trig: A Trigger that moves the selector down when activated.
  • selc_trig: A Trigger that confirms the selection when activated.
§Returns
  • ContainerBuilder: Returns self.
§Example
// A `Trigger` that always activate.
tui::trg_new_trigger_func!(always_true_trigger, _arg, {
    Ok(true)
});
 
// A `Trigger` that never activate .
tui::trg_new_trigger_func!(always_false_trigger, _arg, {
    Ok(false)
});

// Set a `Selector` that always moves down.
ContainerBuilder::new()
    .selector(
        Trigger::no_arg(always_false_trigger),
        Trigger::no_arg(always_true_trigger),
        Trigger::no_arg(always_false_trigger),
    );
Source

pub fn selector_no_triggers(self) -> Self

Sets a Selector with no Triggers for the Container.

In this case, navigation must be handled manually using the following methods:

  • Container::selector_up
  • Container::selector_down
  • Container::selector_select
§Returns
  • ContainerBuilder: Returns self.
§Example
// Set a `Selector` with no `Trigger`s.
ContainerBuilder::new()
    .selector_no_triggers();
Source

pub fn build(self) -> Container

Finalizes the construction of a Container. This method should be called after all desired components have been added using the builder pattern. It consumes self and returns the completed Container.

§Returns
  • Container: Returns the created Container.
§Example
let container: Container = ContainerBuilder::new()
    .header(...)?
    .option(...)?
    .option(...)?
    .separator_normal(...)
    .text(...)?
    .selector(...)?
    .build(); // Finalize and retrieve the constructed container.

Auto Trait Implementations§

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.