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.
This crate is related to the first thing that's described above. It provides lots of useful functionality to help you build TUI (text user interface) apps, along w/ general niceties & ergonomics that all Rustaceans ๐ฆ can enjoy ๐:
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/206881196-37cf1220-8c1b-460e-a2cb-7e06d22d6a02.mp4
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:
Quite a few scripts are provided for your convenience in the root folder of the repo. They are all
fish
script files. Here are a few worth mentioning here which are related to running the example.
run.fish
: This will simply run the examples. You can watch the logs by runninglog.fish
.run-with-flamegraph-profiling.fish
: This will run the examples and generate a flamegraph at the end so you can see profile the performance of the app.run-with-crash-reporting.fish
: This will run the examples and generate acrash_log.txt
file (in thetui
folder) in case the app crashes. This is useful for debugging.
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.
The R3BL TUI engine uses a high performance compositor to render the UI to the terminal. This
ensures that only "pixels" that have changed are painted to the terminal. This is done by creating a
concept of PixelChar
which represents a single "pixel" in the terminal screen at a given col and
row index position. There are only as many PixelChar
s as there are rows and cols in a terminal
screen. And the index maps directly to the position of the pixel in the terminal screen.
Here is an example of what a single row of rendered output might look like in a row of the
OffscreenBuffer
. This diagram shows each PixelChar
in row_index: 1
of the OffscreenBuffer
.
In this example, there are 80 columns in the terminal screen. This actual log output generated by
the TUI engine when logging is enabled.
text
row_index: 1
000 S โโโโโโโโณโโโโโโโโ001 P 'j'โfgโbg 002 P 'a'โfgโbg 003 P 'l'โfgโbg 004 P 'd'โfgโbg 005 P 'k'โfgโbg
006 P 'f'โfgโbg 007 P 'j'โfgโbg 008 P 'a'โfgโbg 009 P 'l'โfgโbg 010 P 'd'โfgโbg 011 P 'k'โfgโbg
012 P 'f'โfgโbg 013 P 'j'โfgโbg 014 P 'a'โfgโbg 015 P 'โ'โrev 016 S โโโโโโโโณโโโโโโโโ017 S โโโโโโโโณโโโโโโโโ
018 S โโโโโโโโณโโโโโโโโ019 S โโโโโโโโณโโโโโโโโ020 S โโโโโโโโณโโโโโโโโ021 S โโโโโโโโณโโโโโโโโ022 S โโโโโโโโณโโโโโโโโ023 S โโโโโโโโณโโโโโโโโ
024 S โโโโโโโโณโโโโโโโโ025 S โโโโโโโโณโโโโโโโโ026 S โโโโโโโโณโโโโโโโโ027 S โโโโโโโโณโโโโโโโโ028 S โโโโโโโโณโโโโโโโโ029 S โโโโโโโโณโโโโโโโโ
030 S โโโโโโโโณโโโโโโโโ031 S โโโโโโโโณโโโโโโโโ032 S โโโโโโโโณโโโโโโโโ033 S โโโโโโโโณโโโโโโโโ034 S โโโโโโโโณโโโโโโโโ035 S โโโโโโโโณโโโโโโโโ
036 S โโโโโโโโณโโโโโโโโ037 S โโโโโโโโณโโโโโโโโ038 S โโโโโโโโณโโโโโโโโ039 S โโโโโโโโณโโโโโโโโ040 S โโโโโโโโณโโโโโโโโ041 S โโโโโโโโณโโโโโโโโ
042 S โโโโโโโโณโโโโโโโโ043 S โโโโโโโโณโโโโโโโโ044 S โโโโโโโโณโโโโโโโโ045 S โโโโโโโโณโโโโโโโโ046 S โโโโโโโโณโโโโโโโโ047 S โโโโโโโโณโโโโโโโโ
048 S โโโโโโโโณโโโโโโโโ049 S โโโโโโโโณโโโโโโโโ050 S โโโโโโโโณโโโโโโโโ051 S โโโโโโโโณโโโโโโโโ052 S โโโโโโโโณโโโโโโโโ053 S โโโโโโโโณโโโโโโโโ
054 S โโโโโโโโณโโโโโโโโ055 S โโโโโโโโณโโโโโโโโ056 S โโโโโโโโณโโโโโโโโ057 S โโโโโโโโณโโโโโโโโ058 S โโโโโโโโณโโโโโโโโ059 S โโโโโโโโณโโโโโโโโ
060 S โโโโโโโโณโโโโโโโโ061 S โโโโโโโโณโโโโโโโโ062 S โโโโโโโโณโโโโโโโโ063 S โโโโโโโโณโโโโโโโโ064 S โโโโโโโโณโโโโโโโโ065 S โโโโโโโโณโโโโโโโโ
066 S โโโโโโโโณโโโโโโโโ067 S โโโโโโโโณโโโโโโโโ068 S โโโโโโโโณโโโโโโโโ069 S โโโโโโโโณโโโโโโโโ070 S โโโโโโโโณโโโโโโโโ071 S โโโโโโโโณโโโโโโโโ
072 S โโโโโโโโณโโโโโโโโ073 S โโโโโโโโณโโโโโโโโ074 S โโโโโโโโณโโโโโโโโ075 S โโโโโโโโณโโโโโโโโ076 S โโโโโโโโณโโโโโโโโ077 S โโโโโโโโณโโโโโโโโ
078 S โโโโโโโโณโโโโโโโโ079 S โโโโโโโโณโโโโโโโโ080 S โโโโโโโโณโโโโโโโโspacer [ 0, 16-80 ]
When RenderOps
are executed and used to create an OffscreenBuffer
that maps to the size of the
terminal window, clipping is performed automatically. This means that it isn't possible to move the
caret outside of the bounds of the viewport (terminal window size). And it isn't possible to paint
text that is larger than the size of the offscreen buffer. The buffer really represents the current
state of the viewport. Scrolling has to be handled by the component itself (an example of this is
the editor component).
Each PixelChar
can be one of 4 things:
PixelChar::PlainText
and is used to
paint the screen via the diffing algorithm which is smart enough to "stack" styles that appear
beside each other for quicker rendering in terminals.lolcat_api.rs
generates these ANSI strings for
the rainbow effect. An example of this is the outline around a modal dialog box.The following diagram provides a high level overview of how apps (that contain components, which may contain components, and so on) are rendered to the terminal screen.
Each component produces a RenderPipeline
, which is a map of ZOrder
and Vec<RenderOps>
.
RenderOps
are the instructions that are grouped together, such as move the caret to a position,
set a color, and paint some text.
Inside of each RenderOps
the caret is stateful, meaning that the caret position is remembered
after each RenderOp
is executed. However, once a new RenderOps
is executed, the caret position
reset just for that RenderOps
. Caret position is not stored globally. You should read more about
"atomic paint operations" in the RenderOp
documentation.
Once a set of these RenderPipeline
s have been generated, typically after the user enters some
input event, and that produces a new state which then has to be rendered, they are combined and
painted into an OffscreenBuffer
.
The paint.rs
file contains the paint
function, which is the entry point for all rendering. Once
the first render occurs, the OffscreenBuffer
that is generated is saved to GlobalSharedState
.
The following table shows the various tasks that have to be performed in order to render to an
OffscreenBuffer
. There is a different code path that is taken for ANSI text and plain text (which
includes StyledText
which is just plain text with a color). Syntax highlighted text is also just
StyledText
. The ANSI text is an example of text that is generated by the lolcat_api.rs
.
| UTF-8 | ANSI | Task |
| ----- | ---- | ------------------------------------------------------------------------------------------------------- |
| Y | Y | convert RenderPipeline
to List<List<PixelChar>>
(OffscreenBuffer
) |
| Y | Y | paint each PixelChar
in List<List<PixelChar>>
to stdout using OffscreenBufferPainterImplCrossterm
|
| Y | Y | save the List<List<PixelChar>>
to GlobalSharedState
|
Currently only crossterm
is supported for actually painting to the terminal. But this process is
really simple making it very easy to swap out other terminal libraries such as termion
, or even a
GUI backend, or some other custom output driver.
Since the OffscreenBuffer
is cached in GlobalSharedState
a diff to be performed for subsequent
renders. And only those diff chunks are painted to the screen. This ensures that there is no flicker
when the content of the screen changes. It also minimizes the amount of work that the terminal or
terminal emulator has to do put the PixelChar
s on the screen.
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 ๐.