2 个版本

0.1.1 2019年1月1日
0.1.0 2019年1月1日

#582命令行界面

Download history 21/week @ 2024-02-12 69/week @ 2024-02-19 68/week @ 2024-02-26 64/week @ 2024-03-04 50/week @ 2024-03-11 60/week @ 2024-03-18 38/week @ 2024-03-25 65/week @ 2024-04-01 27/week @ 2024-04-08 57/week @ 2024-04-15 47/week @ 2024-04-22 62/week @ 2024-04-29 53/week @ 2024-05-06 53/week @ 2024-05-13 55/week @ 2024-05-20 10/week @ 2024-05-27

每月 189 次下载

Apache-2.0/MIT

17KB
366

一个用于构建交互式终端应用程序的库。

与终端交互的通常方式是使用 ANSI 转义序列。这些是像“使用绿色前景”或“光标上移一位”这样的命令。此库向用户提供一个空白缓冲区进行绘制,并将此缓冲区转换为发送到终端的最小(理想)字节数。这有时被称为即时模式绘制,即每次绘制所有内容。

此库还提供了一个迭代器,用于迭代自上次请求迭代器以来接收到的所有输入事件。

示例

use termbuffer::{App, Event, Key, char, Color};
use std::time::{Duration, Instant};
use std::thread;

const FRAME_TIME: Duration = Duration::from_millis(1000 / 60); // 60 fps

fn main() {
    let mut shutdown = false;

    // Currently there are no options, but I'm planning to add for things like
    // what color to clear the screen to, etc.
    let mut app = App::builder().build().unwrap();
    // As this counter is incremented, we will move along the rows.
    let mut counter = 0;
    loop {
        if shutdown {
            break;
        }
        let time_start = Instant::now();
        {
            // Call draw when you are ready to start rendering the next frame.
            let mut draw = app.draw();
            // The draw object contains the new number of rows and columns
            // (this will change if the user resizes the terminal).
            let cols = draw.columns();
            let rows = draw.rows();
            // Math to convert counter to position.
            let row = counter / cols;
            let col = counter % cols;
            // We set all the characters we want.
            draw.set(row, col, char!('.', Color::Default, Color::Red));
            counter = (counter + 1) % (cols * rows);
        }
        // Call app.events to pump the event loop.
        for evt in app.events() {
            match evt.unwrap() {
                Event::Key(Key::Char('q')) => {
                    shutdown = true;
                }
                _ => ()
            }
        }
        let time_end = Instant::now();
        // Sleep for the remainder of the frame.
        if time_end < time_start + FRAME_TIME {
            thread::sleep(FRAME_TIME - (time_end - time_start));
        }
    }
}

依赖项

~170KB