R3BL TUI library & suite of apps focused on developer productivity
We are working on building command line apps in Rust which have rich text user interfaces (TUI). We want to lean into the terminal as a place of productivity, and build all kinds of awesome apps for it.
🔮 Instead of just building one app, we are building a library to enable any kind of rich TUI development w/ a twist: taking concepts that work really well for the frontend mobile and web development world and re-imagining them for TUI & Rust.
🌎 We are building apps to enhance developer productivity & workflows.
You can build fully async TUI (text user interface) apps with a modern API that brings the best of the web frontend development ideas to TUI apps written in Rust:
And since this is using Rust and Tokio you get the advantages of concurrency and parallelism built-in. No more blocking the main thread for user input, for async middleware, or even rendering 🎉.
This framework is loosely coupled and strongly coherent meaning that you can pick and choose whatever pieces you would like to use w/out having the cognitive load of having to grok all the things in the codebase. Its more like a collection of mostly independent modules that work well w/ each other, but know very little about each other.
Here are some framework highlights:
Here's a video of the demo in action:
https://user-images.githubusercontent.com/2966499/200138653-c0cf925f-2c91-4908-9ed5-1e216b5dd547.webm
You can run cargo run --example demo
in the tui/examples
folder to see a demo of the library
in action and play with it. The examples cover the entire surface area of the TUI API. You can
also take a look at the tests in the source as well tui/src/
.
The design docs and architecture diagrams in the
docs
folder are a good place to
start to get a feel for the architecture of the framework. You can get a mental model of how
everything fits and what the TUI lifecycle is.
Additionally the r3blrsutils_core has the
tui_core
module which contains dependencies that are used by the tui
module. They include:
There is a clear separation of concerns in this module. To illustrate what goes where, and how things work let's look at an example that puts the main event loop front and center & deals w/ how the system handles an input event (key press or mouse).
cargo run
).text
🧍⌨️🖱️
input → [TerminalWindow]
event ↑ ↓ [ComponentRegistry] creates
┊ [App] ───────────■ [Component]s at 1st render
┊ │
┊ │ ┌──────■ id=1 has focus
┊ │ │
┊ ├→ [Component] id=1 ───┐
┊ ├→ [Component] id=2 │
┊ └→ [Component] id=3 │
default │
handler ←───────────────────────┘
Let's trace the journey through the diagram when an input even is generated by the user (eg: a key
press, or mouse event). When the app is started via cargo run
it sets up a main loop, and lays out
all the 3 components, sizes, positions, and then paints them. Then it asynchronously listens for
input events (no threads are blocked). When the user types something, this input is processed by the
main loop of [TerminalWindow].
id=1
currently has focus.Now that we have seen this whirlwind overview of the life of an input event, let's look at the details in each of the sections below.
Here's an architecture diagram that will be useful to keep in mind as we go through the details of the following sections:
The main building blocks of a TUI app are:
Inside of your [App] if you want to use flexbox like layout and CSS like styling you can think of composing your code in the following way:
Typically your [App] will look like this:
```rust /// Async trait object that implements the [App] trait.
pub struct AppWithLayout {
pub componentregistry: ComponentRegistry
As we look at [Component] & [App] more closely we will find a curious thing [ComponentRegistry] (that is managed by the [App]). The reason this exists is for input event routing. The input events are routed to the [Component] that currently has focus.
The [HasFocus] struct takes care of this. This provides 2 things:
id
of a [FlexBox] / [Component] that has focus.id
. This is used to represent a
cursor (whatever that means to your app & component). This cursor is maintained for each id
.
This allows a separate cursor for each [Component] that has focus. This is needed to build apps
like editors and viewers that maintains a cursor position between focus switches.Another thing to keep in mind is that the [App] and [TerminalWindow] is persistent between re-renders. The Redux store is also persistent between re-renders.
[TerminalWindow] gives [Component] first dibs when it comes to handling input events. If it punts handling this event, it will be handled by the default input event handler. And if nothing there matches this event, then it is simply dropped.
If you use Redux for state management, then you will create a [crate::redux] [crate::Store] that is passed into the [TerminalWindow]. Here's an example of this.
```rust use crossterm::event::; use r3bl_rs_utils::; use super::*;
const DEBUG: bool = true;
pub async fn runapp() -> CommonResult<()> { throws!({ if DEBUG { trytosetloglevel(log::LevelFilter::Trace)?; } else { trytosetlog_level(log::LevelFilter::Off)?; }
// Create store.
let store = create_store().await;
// Create an App (renders & responds to user input).
let shared_app = AppWithLayout::new_shared();
// Exit if these keys are pressed.
let exit_keys: Vec<KeyEvent> = vec![KeyEvent {
code: KeyCode::Char('q'),
modifiers: KeyModifiers::CONTROL,
}];
// Create a window.
TerminalWindow::main_event_loop(store, shared_app, exit_keys).await?
}); }
async fn createstore() -> Store
Unicode is supported (to an extent). There are some caveats. The [crate::UnicodeStringExt] trait has lots of great information on this graphemes and what is supported and what is not.
An implementation of [crate::lolcat::cat] w/ a color wheel is provided.
This crate is a dependency of the following crates:
r3bl_rs_utils
crates (the "main" library)Please report any issues to the issue tracker. And if you have any feature requests, feel free to add them there too 👍.