Skip to main content

Crate ntui

Crate ntui 

Source
Expand description

ntui is an Ink-style library for building terminal UIs in Rust out of declarative components and hooks. Layouts are described with View/Text elements (often via the element! macro) and arranged with a flexbox model powered by taffy; components re-render in response to state and effect hooks, similar to React. render drives the component tree fullscreen against the real terminal using crossterm, while testing::TestTerminal drives the same tree headlessly for tests.

use ntui::{component, element, render};

#[component]
fn Counter(hooks: &mut ntui::Hooks) -> ntui::Element {
    let count = hooks.use_state(|| 0i32);
    element! {
        View(padding: 1) {
            Text(content: format!("count: {}", count.get()))
        }
    }
}

#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), ntui::Error> {
    render(element!(Counter)).await
}

Core types:

  • Element / Node: the tree of views, text, fragments, and components built each render, typically via element!.
  • Component: the trait implemented (usually via #[component]) to render props and hooks into an Element.
  • Hooks: per-fiber hook state, exposing use_state, use_effect, use_input, use_paste, and friends during a component’s render.
  • render: runs a component tree fullscreen against the real terminal until the app exits.
  • testing::TestTerminal: drives a component tree headlessly, frame by frame, for deterministic tests.

§Semver note

ntui re-exports crossterm::event key types (KeyCode, KeyEvent, KeyEventKind, KeyModifiers) as part of its public API. A breaking change in a future major version of crossterm would therefore surface as a breaking change in ntui even if no ntui code changes.

Re-exports§

pub use backend::Backend;
pub use backend::FullscreenBackend;
pub use backend::InlineBackend;
pub use backend::TestBackend;
pub use component::Component;
pub use element::Element;
pub use element::Node;
pub use error::Error;
pub use hooks::Hooks;
pub use hooks::app::AppHandle;
pub use hooks::effect::Cleanup;
pub use hooks::input::InputCtx;
pub use hooks::list::ListSelection;
pub use hooks::scroll::Scroll;
pub use hooks::scrollback::Scrollback;
pub use hooks::state::State;
pub use props::AlignItems;
pub use props::Anchor;
pub use props::Dimension;
pub use props::FlexDirection;
pub use props::GradientDirection;
pub use props::JustifyContent;
pub use props::Overflow;
pub use props::TextProps;
pub use props::TextWrap;
pub use props::ViewProps;
pub use shared::Shared;
pub use style::Attrs;
pub use style::BorderStyle;
pub use style::Color;
pub use style::Weight;

Modules§

backend
buffer
component
element
error
hooks
props
shared
Shared: a prop payload compared by pointer instead of by value.
style
testing
Public test harness: drive an ntui app frame by frame without a terminal.
widgets
A small, first-party widgets library built entirely out of ntui’s five element kinds (View, Text, Fragment, Provider, Component) — no new node kind is introduced anywhere in this module.

Macros§

element
JSX-alike: element! { View(gap: 1) { Text(content: "hi") #(iter) } }. Hosts: View, Text, Fragment, ContextProvider(value: ..). Anything else is a component; its props type is {Name}Props by convention (Default-filled).

Structs§

KeyEvent
Represents a key event.
KeyModifiers
Represents key modifiers (shift, control, alt, etc.).

Enums§

KeyCode
Represents a key.
KeyEventKind
Represents a keyboard event kind.

Functions§

render
Run the app fullscreen until a component calls use_app().exit(). Note: the returned future is !Send — use #[tokio::main(flavor = "current_thread")].
render_inline
Run an app inline: finished output committed via use_scrollback is printed permanently into the terminal’s real scrollback, while a live region at the bottom is redrawn in place. Unlike render, this does not use the alternate screen.

Attribute Macros§

component
Turns fn Name(props: &NameProps, hooks: &mut Hooks) -> Element { .. } (or the props-less form fn Name(hooks: &mut Hooks) -> Element) into a unit struct implementing ntui::Component.