Skip to main content

Crate altui_core

Crate altui_core 

Source
Expand description

altui-cure is a library used to build rich terminal users interfaces and dashboards.

§Get started

§Adding altui-core as a dependency

Add altui-core to your Cargo.toml:

[dependencies]
altui_core = "0.1"
crossterm = "0.29"

By default, altui-core uses the Crossterm backend, which works on most platforms (Linux, macOS, Windows) and requires no additional configuration.

§Using a different backend

If you want to use another backend (for example, termion), disable default features and enable the corresponding backend feature:

[dependencies]
termion = "4"
altui_core = { version = "0.1", default-features = false, features = ["termion"] }

The same approach applies to all other available backends.


The easiest and safest way to initialize a terminal is via AltuiInit. It takes care of:

  • enabling raw mode
  • entering the alternate screen
  • optional mouse support
  • restoring the terminal on panic
use std::io;
use altui_core::AltuiInit;

fn main() -> io::Result<()> {
    let mut ui = AltuiInit::new(true)?   // enable mouse support
        .set_panic_hook();               // restore terminal on panic

    ui.run(|terminal| {
        terminal.draw(|f| {
            let size = f.size();
            // draw UI here
        }).expect("Fail to draw to the terminal");
        Ok(())
    })
}

§Mouse support

Mouse input is optional and can be disabled:

use altui_core::AltuiInit;

let mut ui = AltuiInit::new(false);

§Manual terminal initialization (advanced)

If you need full control over terminal initialization, you may construct a Terminal and backend manually.

use std::io;
use altui_core::{backend::CrosstermBackend, Terminal};

fn main() -> io::Result<()> {
    let stdout = io::stdout();
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;
    Ok(())
}

⚠️ When using this approach, you are responsible for:

  • enabling and disabling raw mode
  • entering and leaving the alternate screen
  • restoring cursor visibility on exit or panic

§See also


§Building a User Interface (UI)

User interfaces in altui-core are composed of widgets implementing the [Widget] trait.

Widgets use a builder-style API and are rendered via Frame::render_widget.

use altui_core::{
    widgets::{Block, Borders},
};

terminal.draw(|f| {
    let size = f.size();
    let mut block = Block::default()
        .title("Block")
        .borders(Borders::ALL);
    f.render_widget(&mut block, size);
})?;

§Layout

Layout management is handled by [Layout], which allows building responsive terminal UIs by splitting available space into regions.

The example below creates a centered block that occupies 80% of the terminal width and 80% of the terminal height. The remaining space is evenly distributed around the block, keeping it centered both vertically and horizontally.

This pattern is useful for dialogs, popups, and other UI elements that should stay visually centered regardless of terminal size.

use altui_core::{
    layout::{Constraint, Layout, Flex},
    widgets::{Block, Borders},
};

fn ui<B: altui_core::backend::Backend>(f: &mut altui_core::Frame<B>) {
    let chunks = Layout::vertical([Constraint::Percentage(80)])
    .flex(Flex::Center)
    .cross_size(Constraint::Percentage(80))
    .cross_flex(Flex::Center)
    .margin(1)
    .split(f.size());

    let mut block = Block::default()
        .title("Header")
        .borders(Borders::ALL);
    f.render_widget(&mut block, chunks[0]);
}

Layouts can be nested to create complex, adaptive terminal interfaces. Unused layout regions may be left empty to create spacing.

Re-exports§

pub use self::terminal::Frame;
pub use self::terminal::Terminal;
pub use self::terminal::TerminalOptions;
pub use self::terminal::Viewport;

Modules§

backend
buffer
layout
style
style contains the primitives used to control how your user interface will look.
symbols
terminal
text
Primitives for styled text.
widgets
widgets is a collection of types that implement Widget.

Macros§

assert_buffer_eq
Assert that two buffers are equal by comparing their areas and content.

Structs§

AltuiInit
Crossterm terminal initialization helper which restores the original terminal state on drop.