icmd 0.1.0

A command-line software framework
Documentation
pub mod canvas;
pub mod console;
mod core;
pub mod event;
pub mod measure;
pub mod node;
pub mod style;
pub mod utils;

#[cfg(test)]
mod tests {

    use crate::{
        console::Console,
        measure::{Fixed, Top},
        node::{Alignable, Container, HasListener, HasStyle, Text},
        style::{
            color::{AsBackground, Black, Blue, Cyan, Green, Red, White, Yellow},
            pattern::{DashedBorder, DoubleBorder},
            GeneralStyle, Style,
        },
    };

    #[test]
    fn print1() {
        let console = Console::new("test");
        let node = Container::new(&console, (10, 5));
        node.set_border(DoubleBorder, White.and(AsBackground(Black)));
        node.fill_background(AsBackground(Black));
        node.top_to(Top::away_from(&console, -1));
        node.clone().on_mouse_event(Box::new(move |_, _| {
            node.fill_background(AsBackground(Green));
            true
        }));

        console.start();
    }

    #[test]
    fn print2() {
        let console = Console::new("test");
        let container = Container::new(&console, (10, 5));
        container.set_border(DashedBorder, Black.and(AsBackground(White)));
        container.fill_background(AsBackground(White));
        container.top_to(Top::away_from(&console, -2));
        console.start();
    }

    #[test]
    fn print3() -> Result<(), std::io::Error> {
        let console = Console::new("test");
        let node = Text::new(&console, "hello", (-2, 0));
        node.set_foreground(AsBackground(White).and(Blue));
        node.clone().on_mouse_event(Box::new(move |_, _| {
            node.fill_background(AsBackground(Green));
            true
        }));
        console.start();
        Ok(())
    }

    #[test]
    fn print() -> Result<(), std::io::Error> {
        let g = 0.0005f32;
        let mut p = vec![0f32, 2f32];
        let mut v = vec![0.05f32, 0.0f32];

        let mut i = 0;
        let colors: [GeneralStyle; 5] = [
            AsBackground(Green).into(),
            AsBackground(Cyan).into(),
            AsBackground(Yellow).into(),
            AsBackground(Blue).into(),
            AsBackground(Red).into(),
        ];

        let console = Console::new("test");
        let context = console.context();
        // Console size is 52, 46
        let node = Container::new(&console, (14, 5));
        node.fill_background(AsBackground(Red));
        node.clone().on_update_event(Box::new(move |_, _| {
            let mut on_collide = |value: &mut f32| {
                *value = -*value;
                i = (i + 1) % colors.len();
                node.fill_background(colors[i].clone());
            };
            if p[0] <= 0. && v[0] < 0. {
                on_collide(&mut v[0]);
            } else if p[0] + node.get_width() as f32 >= context.console_width.into() && v[0] > 0. {
                on_collide(&mut v[0]);
            } else if p[1] <= 0. && v[1] < 0. {
                on_collide(&mut v[1]);
            } else if p[1] + node.get_height() as f32 >= context.console_height.into() && v[1] > 0.
            {
                on_collide(&mut v[1]);
            }
            node.left_to(Fixed(p[0] as i32));
            node.top_to(Fixed(p[1] as i32));

            v[1] += g;

            p[0] += v[0];
            p[1] += v[1];

            true
        }));

        console.start();

        Ok(())
    }
}