cursive-image 0.0.6

Image view for the Cursive TUI library
Documentation
use {
    cursive::{align::*, view::*, views::*, *},
    cursive_image::*,
};

// Here we'll demonstrate the various positioning configurations

// Press "s" to switch sizing mode (shrink/fit/scale=3)
// Press "h" to switch horizontal alignment mode (left/center/right)
// Press "v" to switch vertical alignment mode (top/center/bottom)

const FILE: &str = "assets/media/linux.png";
const NAME: &str = "Image";

fn main() {
    let mut cursive = default();

    cursive.add_layer(Panel::new(
        ImageView::default()
            // We're also showing the use of new_png_file(), which discovers the image size for us
            // (requires the "png" feature)
            .with_image(Image::new_png_file(FILE).expect("new_png_file"))
            .with_name(NAME)
            .fixed_size((50, 20)),
    ));

    cursive.add_global_callback('s', |cursive| {
        _ = cursive.call_on_name(NAME, |image_view: &mut ImageView| {
            image_view.set_sizing(match image_view.sizing() {
                Sizing::Shrink => Sizing::Fit,
                Sizing::Fit => Sizing::Scale(3.),
                Sizing::Scale(_) => Sizing::Shrink,
            })
        });
    });

    cursive.add_global_callback('h', |cursive| {
        _ = cursive.call_on_name(NAME, |image_view: &mut ImageView| {
            image_view.set_align(match image_view.align() {
                Align { h: HAlign::Left, v } => Align { h: HAlign::Center, v },
                Align { h: HAlign::Center, v } => Align { h: HAlign::Right, v },
                Align { h: HAlign::Right, v } => Align { h: HAlign::Left, v },
            })
        });
    });

    cursive.add_global_callback('v', |cursive| {
        _ = cursive.call_on_name(NAME, |image_view: &mut ImageView| {
            image_view.set_align(match image_view.align() {
                Align { h, v: VAlign::Top } => Align { h, v: VAlign::Center },
                Align { h, v: VAlign::Center } => Align { h, v: VAlign::Bottom },
                Align { h, v: VAlign::Bottom } => Align { h, v: VAlign::Top },
            })
        });
    });

    cursive.add_global_callback('q', |cursive| cursive.quit());

    cursive.run();
}