#terminal #2d #graphics #color

term2d

Simple drawing engine for terminal applications

7 releases (4 breaking)

Uses new Rust 2021

new 0.5.0 Dec 5, 2022
0.4.0 Nov 23, 2022
0.3.2 Nov 23, 2022
0.2.0 Nov 7, 2022
0.1.0 Nov 5, 2022

#136 in Graphics APIs

Download history 37/week @ 2022-11-03 8/week @ 2022-11-10 58/week @ 2022-11-17 47/week @ 2022-11-24 31/week @ 2022-12-01

144 downloads per month

MIT license

24KB
743 lines

term2d

A simple 2d drawing engine for terminal emulators.

Example which draws some text and a red pixel:

use term2d::{
    color::{Color, Rgba},
    point::Point,
    view::canvas::{halfblock::HalfblockCanvas, Canvas},
    view::screen::DefaultScreen,
    Controller, Event, Key,
};

struct DotController {
    frame: u32,
    canvas: HalfblockCanvas,
}

impl Controller for DotController {
    fn update(&mut self, event: Event) -> bool {
        match event {
            Event::Key(key) => match key {
                Key::Char('q') => return false,
                Key::Ctrl('c') => return false,
                _ => {}
            },
            Event::Resize => {}
            Event::Elapse => {}
        }

        self.canvas.clear();
        self.canvas.draw_text(
            &Point::new(2, 0),
            &Color {
                fg: Rgba::white(),
                bg: Rgba::transparent(),
            },
            &format!("press 'q' to quit, frame: {}", self.frame),
        );
        self.canvas.draw_pixel(&Point::new(10, 7), &Rgba::red());
        self.canvas.display();

        self.frame += 1;

        true
    }

    fn init(&mut self, screen: DefaultScreen) -> term2d::Config {
        self.canvas.init(screen);
        term2d::Config { fps: 10 }
    }
}

fn main() {
    let controller = DotController {
        frame: 0,
        canvas: HalfblockCanvas::new(),
    };
    term2d::run(controller);
}

Dependencies

~295KB