#terminal #tui #double-buffering #event-listener

andiskaz

A convenience library for writing games and other apps in TUI

2 unstable releases

0.2.0 Oct 30, 2021
0.1.0 May 24, 2021

#1121 in Asynchronous

Custom license

175KB
4K SLoC

andiskaz

Andiskaz "endish", an asynchronous terminal library for TUI applications and games

Main Branch Docs

https://brunoczim.github.io/andiskaz/andiskaz/

Features

plane

Enable this to make andiskaz's Vec2 be an alias for the crate gardiz's Vec2. Otherwise, andiskaz's Vec2 is a new struct.

Example Snake Game

See this folder.

screenshot of snake game example


lib.rs:

This crate provides basic utilities for writing applications with Terminal User Interface (TUI). It provides an event listener, and it provides a handle to a double buffered screen renderer.

Examples

This is an example for a simple Hello, World! application. For more examples, see examples folder.

use andiskaz::{
    color::Color2,
    emergency_restore,
    error::Error,
    event::Event,
    style::Style,
    terminal::Terminal,
    tstring,
};
use std::{panic, process::exit};

/// Asynchronous main of a tokio project.
#[tokio::main]
async fn main() {
    // Sets panic hook so we can see the panic even if terminal was being used
    // in raw mode.
    panic::set_hook(Box::new(|info| {
        let _ = emergency_restore();
        eprintln!("{}", info);
    }));

// Creates a terminal with default settings and runs it.
    let result = Terminal::run(term_main).await;
    // If error, prints it out and exits with bad code.
    if let Ok(Err(error)) | Err(error) = result {
        eprintln!("{}", error);
        exit(-1);
    }
}

/// The terminal main function.
async fn term_main(mut term: Terminal) -> Result<(), Error> {
    // Allocates memory for a string which is safe to be printed.
    let string = tstring!["Hello, World! Press any key..."];
    // Style for the string.
    let style = Style::with_colors(Color2::default());
    // Initial rendering.
    term.lock_now().await?.screen().styled_text(&string, style);

    loop {
        // Awaits for an event (key pressed, screen resized, etc).
        let mut session = term.listen().await?;

        // Gets the event for the current terminal session.
        match session.event() {
            // We expect a key to exit the example.
            Some(Event::Key(_)) => break,
            // User resized screen? Then the whole screen was thrown out,
            // re-rendering is required.
            Some(Event::Resize(_)) => {
                session.screen().styled_text(&string, style);
            },
            // Won't really happen since we waited for an event.
            None => (),
        }
    }

    Ok(())
}

Dependencies

~5–12MB
~126K SLoC