Struct Program

Source
pub struct Program<M: Model> {
    pub config: ProgramConfig,
    /* private fields */
}
Expand description

The main Program struct that coordinates the application.

The Program is responsible for setting up the terminal, managing the event loop, executing commands, and rendering the model’s view.

Fields§

§config: ProgramConfig

The configuration for this Program instance.

Implementations§

Source§

impl<M: Model> Program<M>

Source

pub fn builder() -> ProgramBuilder<M>

Creates a new ProgramBuilder for configuring and building a Program.

Source

pub async fn run(self) -> Result<M, Error>

Runs the bubbletea-rs application.

This method initializes the terminal, starts the event loop, and manages the application’s lifecycle. It will continue to run until a QuitMsg is received or an unrecoverable error occurs.

§Returns

A Result containing the final Model state or an Error if the program terminates abnormally.

Source

pub fn sender(&self) -> EventSender

Returns a sender that can be used to send messages to the Program’s event loop.

This is useful for sending messages from outside the Model’s update method, for example, from asynchronous tasks or other threads.

§Returns

An EventSender that can be used to send messages.

Source

pub fn send(&self, msg: Msg) -> Result<(), Error>

Sends a message to the Program’s event loop.

This is a convenience method that wraps the sender() method. The message will be processed by the model’s update method.

§Arguments
  • msg - The Msg to send to the event loop.
§Returns

A Result indicating success or a channel-related error if the message could not be sent.

§Errors

Returns an Error if:

  • The event channel is full (for bounded channels)
  • The receiver has been dropped
§Example
let program = Program::<MyModel>::builder().build()?;
let key_msg = KeyMsg {
    key: crossterm::event::KeyCode::Enter,
    modifiers: crossterm::event::KeyModifiers::empty(),
};
program.send(Box::new(key_msg))?;
Source

pub fn quit(&self)

Sends a QuitMsg to the Program’s event loop, initiating a graceful shutdown.

This causes the event loop to terminate gracefully after processing any remaining messages in the queue. The terminal state will be properly restored.

§Example
let program = Program::<MyModel>::builder().build()?;
program.quit(); // Gracefully shutdown the program
Source

pub fn memory_monitor(&self) -> Option<&MemoryMonitor>

Get a reference to the memory monitor, if enabled.

Returns None if memory monitoring is disabled.

Source

pub fn memory_health(&self) -> Option<MemoryHealth>

Get memory usage health information, if monitoring is enabled.

Returns None if memory monitoring is disabled.

Source

pub fn kill(&self)

Sends a KillMsg to the Program’s event loop, initiating an immediate termination.

Unlike quit(), which performs a graceful shutdown, kill() causes the event loop to stop as soon as possible and returns Error::ProgramKilled.

Source

pub async fn wait(&self)

Waits for the Program to finish execution.

This method blocks until the program’s event loop has exited.

§Note

This is currently a no-op since the Program is consumed by run(). In a real implementation, you’d need to track the program’s state separately, similar to how Go’s context.Context works with goroutines.

§Future Implementation

A future version might track program state separately to enable proper waiting functionality without consuming the Program instance.

Source

pub async fn release_terminal(&mut self) -> Result<(), Error>

Releases control of the terminal.

This method restores the terminal to its original state, disabling raw mode, exiting alternate screen, disabling mouse and focus reporting, and showing the cursor.

Source

pub async fn restore_terminal(&mut self) -> Result<(), Error>

Restores control of the terminal.

This method re-initializes the terminal based on the ProgramConfig, enabling raw mode, entering alternate screen, enabling mouse and focus reporting, and hiding the cursor.

Source

pub async fn println(&mut self, s: String) -> Result<(), Error>

Prints a line to the terminal without going through the renderer.

This is useful for debugging or for outputting messages that shouldn’t be part of the managed UI. The output bypasses the normal rendering pipeline and goes directly to stdout.

§Arguments
  • s - The string to print, a newline will be automatically added.
§Returns

A Result indicating success or an IO error if printing fails.

§Errors

Returns an Error if stdout flushing fails.

§Warning

Using this method while the program is running may interfere with the normal UI rendering. It’s recommended to use this only for debugging purposes or when the renderer is disabled.

Source

pub async fn printf(&mut self, s: String) -> Result<(), Error>

Prints formatted text to the terminal without going through the renderer.

This is useful for debugging or for outputting messages that shouldn’t be part of the managed UI. The output bypasses the normal rendering pipeline and goes directly to stdout without adding a newline.

§Arguments
  • s - The string to print without adding a newline.
§Returns

A Result indicating success or an IO error if printing fails.

§Errors

Returns an Error if stdout flushing fails.

§Warning

Using this method while the program is running may interfere with the normal UI rendering. It’s recommended to use this only for debugging purposes or when the renderer is disabled.

Auto Trait Implementations§

§

impl<M> Freeze for Program<M>

§

impl<M> !RefUnwindSafe for Program<M>

§

impl<M> Send for Program<M>

§

impl<M> !Sync for Program<M>

§

impl<M> Unpin for Program<M>
where M: Unpin,

§

impl<M> !UnwindSafe for Program<M>

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.