11 releases

0.3.2 Apr 22, 2022
0.3.1 Apr 10, 2022
0.2.0 Oct 24, 2021
0.1.6 May 1, 2021
0.1.3 Jul 26, 2020

#101 in Command-line interface

Download history 50/week @ 2023-02-07 84/week @ 2023-02-14 41/week @ 2023-02-21 32/week @ 2023-02-28 30/week @ 2023-03-07 31/week @ 2023-03-14 37/week @ 2023-03-21 33/week @ 2023-03-28 37/week @ 2023-04-04 31/week @ 2023-04-11 33/week @ 2023-04-18 43/week @ 2023-04-25 44/week @ 2023-05-02 66/week @ 2023-05-09 27/week @ 2023-05-16 18/week @ 2023-05-23

166 downloads per month
Used in 2 crates


3.5K SLoC

Zi is a library for building modern terminal user interfaces.

A user interface in Zi is built as a tree of stateful components. Components let you split the UI into independent, reusable pieces, and think about each piece in isolation.

The App runtime keeps track of components as they are mounted, updated and eventually removed and only calls view() on those UI components that have changed and have to be re-rendered. Lower level and independent of the components, the terminal backend will incrementally redraw only those parts of the screen that have changed.

A Basic Example

The following is a complete example of a Zi application which implements a counter. It should provide a good sample of the different Component methods and how they fit together.

A slightly more complex version which includes styling can be found at examples/counter.rs.


Anyone familiar with Yew, Elm or React + Redux should be familiar with all the high-level concepts. Moreover, the names of some types and functions are the same as in Yew.

use zi::{
        border::{Border, BorderProperties},
        text::{Text, TextAlign, TextProperties},
use zi_term::Result;

// Message type handled by the `Counter` component.
enum Message {

// The `Counter` component.
struct Counter {
    // The state of the component -- the current value of the counter.
    count: usize,

    // A `ComponentLink` allows us to send messages to the component in reaction
    // to user input as well as to gracefully exit.
    link: ComponentLink<Self>,

// Components implement the `Component` trait and are the building blocks of the
// UI in Zi. The trait describes stateful components and their lifecycle.
impl Component for Counter {
    // Messages are used to make components dynamic and interactive. For simple
    // or pure components, this will be `()`. Complex, stateful components will
    // typically use an enum to declare multiple Message types. In this case, we
    // will emit two kinds of message (`Increment` or `Decrement`) in reaction
    // to user input.
    type Message = Message;

    // Properties are the inputs to a Component passed in by their parent.
    type Properties = ();

    // Creates ("mounts") a new `Counter` component.
    fn create(
        _properties: Self::Properties,
        _frame: Rect,
        link: ComponentLink<Self>,
    ) -> Self {
        Self { count: 0, link }

    // Returns the current visual layout of the component.
    //  - The `Border` component wraps a component and draws a border around it.
    //  - The `Text` component displays some text.
    fn view(&self) -> Layout {
                .content(format!("Counter: {}", self.count)),

    // Components handle messages in their `update` method and commonly use this
    // method to update their state and (optionally) re-render themselves.
    fn update(&mut self, message: Self::Message) -> ShouldRender {
        self.count = match message {
            Message::Increment => self.count.saturating_add(1),
            Message::Decrement => self.count.saturating_sub(1),

    // Updates the key bindings of the component.
    // This method will be called after the component lifecycle methods. It is
    // used to specify how to react in response to keyboard events, typically
    // by sending a message.
    fn bindings(&self, bindings: &mut Bindings<Self>) {
        // If we already initialised the bindings, nothing to do -- they never
        // change in this example
        if !bindings.is_empty() {
        // Set focus to `true` in order to react to key presses

        // Increment
        bindings.add("increment", [Key::Char('+')], || Message::Increment);
        bindings.add("increment", [Key::Char('=')], || Message::Increment);

        // Decrement
        bindings.add("decrement", [Key::Char('-')], || Message::Decrement);

        // Exit
        bindings.add("exit", [Key::Ctrl('c')], |this: &Self| this.link.exit());
        bindings.add("exit", [Key::Esc], |this: &Self| this.link.exit());

fn main() -> zi_term::Result<()> {

More examples can be found in the examples directory of the git repository.


This project is licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~40K SLoC