#tui #terminal #applications #screen #double-buffering #events #color

andiskaz

适用于在 TUI 中编写游戏和其他应用的便利库

2 个不稳定版本

0.2.0 2021 年 10 月 30 日
0.1.0 2021 年 5 月 24 日

#1193 in 异步

自定义许可

175KB
4K SLoC

andiskaz

Andiskaz "endish",一个用于 TUI 应用和游戏的异步终端库

主分支文档

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

功能

plane

启用此功能,使 andiskazVec2 成为 crate gardizVec2 的别名。否则,andiskazVec2 是一个新的结构体。

示例蛇形游戏

请参阅 此文件夹

screenshot of snake game example


lib.rs:

此 crate 提供了编写终端用户界面 (TUI) 应用程序的基本实用工具。它提供了一个事件监听器,并提供了一个指向双缓冲屏幕渲染器的句柄。

示例

这是一个简单 Hello, World! 应用的示例。有关更多示例,请参阅示例文件夹。

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(())
}

依赖项

~5–7.5MB
~131K SLoC