2021-04-07 09:47:05 +00:00
|
|
|
//! pixel-cell example
|
2021-04-05 21:55:24 +00:00
|
|
|
//!
|
2021-04-06 10:30:32 +00:00
|
|
|
//! Shows how to get the size of a cell in pixels
|
|
|
|
//!
|
|
|
|
//! It works on the following terminals:
|
|
|
|
//! - kitty
|
|
|
|
//! - xterm (invoked with `xterm -ti vt340`)
|
|
|
|
//! - alacritty (WIP https://github.com/ayosec/alacritty/tree/graphics)
|
2021-04-05 21:55:24 +00:00
|
|
|
|
|
|
|
use rand::{distributions::Uniform, Rng};
|
|
|
|
|
2021-04-06 10:30:32 +00:00
|
|
|
use libnotcurses_sys::*;
|
|
|
|
|
2021-04-05 21:55:24 +00:00
|
|
|
fn main() -> NcResult<()> {
|
2021-05-05 17:33:00 +00:00
|
|
|
let mut nc = Notcurses::new()?;
|
2021-04-05 21:55:24 +00:00
|
|
|
|
|
|
|
if !nc.check_pixel_support()? {
|
|
|
|
return Err(NcError::new_msg("Current terminal doesn't support pixels."));
|
|
|
|
}
|
|
|
|
|
2021-04-17 20:13:43 +00:00
|
|
|
let mut stdplane = nc.stdplane();
|
|
|
|
let pg = stdplane.pixelgeom();
|
2021-04-05 21:55:24 +00:00
|
|
|
|
|
|
|
// print visual delimiters around our pixelized cell
|
2021-04-06 10:30:32 +00:00
|
|
|
println!("0▗│▖\n│─ ─\n2▝│▘");
|
2021-04-17 20:13:43 +00:00
|
|
|
println!("a cell is {}x{} pixels", pg.cell_y, pg.cell_x);
|
2021-04-18 11:56:36 +00:00
|
|
|
println!("\nscaled: inflated:");
|
2021-04-05 21:55:24 +00:00
|
|
|
|
|
|
|
// fill the buffer with random color pixels
|
|
|
|
let mut rng = rand::thread_rng();
|
|
|
|
let range = Uniform::from(50..=180);
|
2021-04-17 20:13:43 +00:00
|
|
|
let mut buffer = Vec::<u8>::with_capacity((pg.cell_y * pg.cell_x * 4) as usize);
|
2021-04-05 21:55:24 +00:00
|
|
|
#[allow(unused_parens)]
|
2021-04-17 20:13:43 +00:00
|
|
|
for _byte in (0..={ pg.cell_y * pg.cell_x }) {
|
2021-04-05 21:55:24 +00:00
|
|
|
buffer.push(rng.sample(&range));
|
|
|
|
buffer.push(rng.sample(&range));
|
|
|
|
buffer.push(rng.sample(&range));
|
|
|
|
buffer.push(255);
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:30:32 +00:00
|
|
|
// show the newly created ncvisual delimited with the box drawing characters
|
2021-04-18 11:56:36 +00:00
|
|
|
let vframe1 = NcVisual::from_rgba(buffer.as_slice(), pg.cell_y, pg.cell_x * 4, pg.cell_x)?;
|
2021-04-17 20:13:43 +00:00
|
|
|
let voptions =
|
|
|
|
NcVisualOptions::without_plane(1, 2, 0, 0, pg.cell_y, pg.cell_x, NCBLIT_PIXEL, 0, 0);
|
2021-04-18 11:56:36 +00:00
|
|
|
vframe1.render(&mut nc, &voptions)?;
|
2021-04-18 09:33:58 +00:00
|
|
|
rsleep![&mut nc, 1];
|
2021-04-05 21:55:24 +00:00
|
|
|
|
2021-04-17 20:13:43 +00:00
|
|
|
// show the ncvisual, scaled
|
2021-04-18 11:56:36 +00:00
|
|
|
let mut vplane2 = NcPlane::new_bound(&mut stdplane, 6, 2, 4, 4)?;
|
2021-04-17 20:13:43 +00:00
|
|
|
let voptions2 = NcVisualOptions::with_plane(
|
|
|
|
&mut vplane2,
|
|
|
|
NCSCALE_SCALE,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
pg.cell_y,
|
|
|
|
pg.cell_x,
|
|
|
|
NCBLIT_PIXEL,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
);
|
2021-04-18 11:56:36 +00:00
|
|
|
vframe1.render(&mut nc, &voptions2)?;
|
2021-04-18 09:33:58 +00:00
|
|
|
rsleep![&mut nc, 1];
|
|
|
|
|
|
|
|
// show the ncvisual, inflated
|
2021-04-18 11:56:36 +00:00
|
|
|
let voptions3 =
|
|
|
|
NcVisualOptions::without_plane(6, 9, 0, 0, pg.cell_y, pg.cell_x, NCBLIT_PIXEL, 0, 0);
|
|
|
|
vframe1.inflate(4)?;
|
|
|
|
vframe1.render(&mut nc, &voptions3)?;
|
|
|
|
rsleep![&mut nc, 1];
|
|
|
|
|
|
|
|
let vframe4 = NcVisual::from_rgba(buffer.as_slice(), pg.cell_y, pg.cell_x * 4, pg.cell_x)?;
|
|
|
|
let _voptions4 =
|
|
|
|
NcVisualOptions::without_plane(6, 14, 0, 0, pg.cell_y, pg.cell_x, NCBLIT_PIXEL, 0, 0);
|
|
|
|
vframe4.resize(2, 2)?;
|
|
|
|
// FIXME: render function fails
|
|
|
|
// vframe4.render(&mut nc, &_voptions4)?;
|
2021-04-17 20:13:43 +00:00
|
|
|
rsleep![&mut nc, 2];
|
2021-04-05 21:55:24 +00:00
|
|
|
|
2021-04-18 11:56:36 +00:00
|
|
|
vframe1.destroy();
|
|
|
|
vframe4.destroy();
|
2021-04-05 21:55:24 +00:00
|
|
|
|
2021-05-05 17:33:00 +00:00
|
|
|
nc.stop()?;
|
2021-04-05 21:55:24 +00:00
|
|
|
Ok(())
|
|
|
|
}
|