diff --git a/rust/examples/direct-cursor.rs b/rust/examples/direct-cursor.rs index afd3dc62e..7b004c21b 100644 --- a/rust/examples/direct-cursor.rs +++ b/rust/examples/direct-cursor.rs @@ -10,18 +10,18 @@ use libnotcurses_sys::*; fn main() -> NcResult<()> { let mut rng = thread_rng(); - let ncd = NcDirect::new()?; + let mut dm = DirectMode::new()?; - let cols = ncd.dim_x(); - let rows = ncd.dim_y(); + let cols = dm.dim_x(); + let rows = dm.dim_y(); println!("terminal size (rows, cols): {}, {}", rows, cols); let mut channels = NcChannelPair::combine(NcChannel::with_rgb(0xAA2244), NcChannel::with_rgb(0x112233)); - ncd.putstr(channels, "The current coordinates are")?; + dm.putstr(channels, "The current coordinates are")?; for _n in 0..40 { - fsleep![ncd, 0, 30]; + fsleep![&mut dm, 0, 30]; channels.set_fg_rgb8( rng.gen_range(0x66..=0xEE), rng.gen_range(0x66..=0xEE), @@ -32,11 +32,11 @@ fn main() -> NcResult<()> { rng.gen_range(0..=0x9), rng.gen_range(0..=0x9), ); - ncd.putstr(channels, ".")?; + dm.putstr(channels, ".")?; } - let (cy, cx) = ncd.cursor_yx()?; - ncd.putstr(channels, &format!(" ({},{})\n", cy, cx))?; + let (cy, cx) = dm.cursor_yx()?; + dm.putstr(channels, &format!(" ({},{})\n", cy, cx))?; sleep![1]; let sentence = vec![ @@ -45,16 +45,14 @@ fn main() -> NcResult<()> { for word in sentence { channels.set_fg_rgb(channels.fg_rgb().wrapping_sub(0x050505)); channels.set_bg_rgb(channels.bg_rgb().wrapping_add(0x090909)); - ncd.putstr(channels, &format!["{} ", word])?; - fsleep![ncd, 0, 150]; + dm.putstr(channels, &format!["{} ", word])?; + fsleep![&mut dm, 0, 150]; } sleep![0, 300]; channels.set_fg_rgb(0xFFFFFF); channels.set_bg_default(); - ncd.putstr(channels, "\nbye!\n\n")?; - fsleep![ncd, 0, 600]; - ncd.clear()?; - sleep![2]; - ncd.stop()?; + dm.putstr(channels, "\nbye!\n\n")?; + fsleep![&mut dm, 0, 600]; + dm.clear()?; Ok(()) } diff --git a/rust/examples/direct-image-rust.rs b/rust/examples/direct-image-rust.rs index 10d7ecc57..398de0b29 100644 --- a/rust/examples/direct-image-rust.rs +++ b/rust/examples/direct-image-rust.rs @@ -7,21 +7,20 @@ use libnotcurses_sys::*; fn main() -> NcResult<()> { - let ncd = NcDirect::new()?; + let mut dm = DirectMode::new()?; - render_image(ncd, NCBLIT_1x1)?; - render_image(ncd, NCBLIT_2x1)?; - render_image(ncd, NCBLIT_BRAILLE)?; + render_image(&mut dm, NCBLIT_1x1)?; + render_image(&mut dm, NCBLIT_2x1)?; + render_image(&mut dm, NCBLIT_BRAILLE)?; - ncd.stop()?; Ok(()) } -fn render_image(ncd: &mut NcDirect, blit: NcBlitter) -> NcResult<()> { - if let Err(nc_error) = ncd.render_image("image-16x16.png", NCALIGN_CENTER, blit, NCSCALE_NONE) { +fn render_image(dm: &mut DirectMode, blit: NcBlitter) -> NcResult<()> { + if let Err(nc_error) = dm.render_image("image-16x16.png", NCALIGN_CENTER, blit, NCSCALE_NONE) { return Err(NcError::with_msg( nc_error.int, - "ERROR: ncdirect_render_image(). Make sure you \ + "ERROR: dmirect_render_image(). Make sure you \ are running this example from the examples folder", )); } diff --git a/rust/examples/poc-direct.rs b/rust/examples/poc-direct.rs index b12bf7921..829d30b3a 100644 --- a/rust/examples/poc-direct.rs +++ b/rust/examples/poc-direct.rs @@ -3,33 +3,33 @@ use libnotcurses_sys::*; fn main() -> NcResult<()> { - let ncd = NcDirect::new()?; + let mut dm = DirectMode::new()?; - let dimy = ncd.dim_y(); - let dimx = ncd.dim_x(); + let dimy = dm.dim_y(); + let dimx = dm.dim_x(); for _ in 0..dimy { for _ in 0..dimx { printf!("X"); } } - ncd.flush()?; + dm.flush()?; - ncd.set_fg_rgb(0xff8080)?; - ncd.styles_on(NCSTYLE_STANDOUT)?; + dm.set_fg_rgb(0xff8080)?; + dm.styles_on(NCSTYLE_STANDOUT)?; printf!(" erp erp \n"); - ncd.set_fg_rgb(0x80ff80)?; + dm.set_fg_rgb(0x80ff80)?; printf!(" erp erp \n"); - ncd.styles_off(NCSTYLE_STANDOUT)?; + dm.styles_off(NCSTYLE_STANDOUT)?; printf!(" erp erp \n"); - ncd.set_fg_rgb(0xff8080)?; + dm.set_fg_rgb(0xff8080)?; printf!(" erp erp \n"); - ncd.cursor_right(dimx / 2)?; - ncd.cursor_up(dimy / 2)?; + dm.cursor_right(dimx / 2)?; + dm.cursor_up(dimy / 2)?; printf!(" erperperp! \n"); let (mut y, x); - if let Ok((_y, _x)) = ncd.cursor_yx() { + if let Ok((_y, _x)) = dm.cursor_yx() { y = _y; x = _x; printf!("\n\tRead cursor position: y: %d x: %d\n", y, x); @@ -37,12 +37,12 @@ fn main() -> NcResult<()> { y += 2; while y > 3 { let up = if y >= 3 { 3 } else { y }; - ncd.cursor_up(up)?; - ncd.flush()?; + dm.cursor_up(up)?; + dm.flush()?; y -= up; let newy; - if let Ok((_y, _)) = ncd.cursor_yx() { + if let Ok((_y, _)) = dm.cursor_yx() { newy = _y; } else { break; @@ -59,6 +59,5 @@ fn main() -> NcResult<()> { return Err(NcError::with_msg(-10, "Couldn't read cursor position.")); } - ncd.stop()?; Ok(()) } diff --git a/rust/examples/poc-kittyzapper.rs b/rust/examples/poc-kittyzapper.rs index 40cb3c5a2..307d8104c 100644 --- a/rust/examples/poc-kittyzapper.rs +++ b/rust/examples/poc-kittyzapper.rs @@ -3,23 +3,22 @@ use libnotcurses_sys::*; fn main() -> NcResult<()> { - let ncd = NcDirect::new()?; + let mut dm = DirectMode::new()?; - ncd.set_fg_rgb8(100, 100, 100)?; - ncd.set_bg_rgb8(0xff, 0xff, 0xff)?; + dm.set_fg_rgb8(100, 100, 100)?; + dm.set_bg_rgb8(0xff, 0xff, 0xff)?; printf!("a"); - ncd.set_bg_rgb8(0, 0, 0)?; + dm.set_bg_rgb8(0, 0, 0)?; printf!("b"); printf!(" "); printf!(" "); - ncd.set_bg_rgb8(0, 0, 1)?; + dm.set_bg_rgb8(0, 0, 1)?; printf!("c"); printf!(" "); printf!(" "); - ncd.set_bg_rgb8(0xff, 0xff, 0xff)?; + dm.set_bg_rgb8(0xff, 0xff, 0xff)?; printf!("d"); printf!("\n"); - ncd.stop()?; Ok(()) } diff --git a/rust/src/direct/mod.rs b/rust/src/direct/mod.rs index 842e13621..6430de9b1 100644 --- a/rust/src/direct/mod.rs +++ b/rust/src/direct/mod.rs @@ -66,9 +66,14 @@ mod test; mod methods; mod reimplemented; -pub use reimplemented::*; +mod wrapper; -/// Minimal notcurses instances for styling text +pub use reimplemented::*; +pub use wrapper::*; + +/// Minimal notcurses instance for styling text. +/// +/// This is the internal type safely wrapped by [Direct]. pub type NcDirect = crate::bindings::ffi::ncdirect; /// Flags (options) for [`NcDirect`] diff --git a/rust/src/direct/wrapper.rs b/rust/src/direct/wrapper.rs new file mode 100644 index 000000000..e6cf12469 --- /dev/null +++ b/rust/src/direct/wrapper.rs @@ -0,0 +1,71 @@ +//! `Direct` wrapper struct and traits implementations. + +use std::ops::{Deref, DerefMut}; + +use crate::{ + raw_wrap, NcDirect, NcResult +}; + +/// Minimal notcurses instance for styling text. +/// +/// Safely wraps an [NcDirect], +/// and implements Drop, AsRef, AsMut, Deref & DerefMut around it. +pub struct Direct<'a> { + raw: &'a mut NcDirect, +} + +impl<'a> AsRef for Direct<'a> { + fn as_ref(&self) -> &NcDirect { + self.raw + } +} + +impl<'a> AsMut for Direct<'a> { + fn as_mut(&mut self) -> &mut NcDirect { + self.raw + } +} + +impl<'a> Deref for Direct<'a> { + type Target = NcDirect; + + fn deref(&self) -> &Self::Target { + self.as_ref() + } +} + +impl<'a> DerefMut for Direct<'a> { + fn deref_mut(&mut self) -> &mut Self::Target { + self.as_mut() + } +} + +impl<'a> Drop for Direct<'a> { + /// Destroys the Direct context. + fn drop(&mut self) { + let _ = self.raw.stop(); + } +} + +/// # Constructors and methods overriden from NcDirect +impl<'a> Direct<'a> { + // wrap constructors + + /// New Direct (without banners). + pub fn new() -> NcResult { + raw_wrap![NcDirect::new()] + } + + /// New Direct, expects `NCOPTION_*` flags. + pub fn with_flags(flags: u64) -> NcResult { + raw_wrap![NcDirect::with_flags(flags)] + } + + // disable destructor + + /// Since Direct already implements [Drop](#impl-Drop), + /// this function is made no-op. + pub fn stop(&mut self) -> NcResult<()> { + Ok(()) + } +} diff --git a/rust/src/notcurses/mod.rs b/rust/src/notcurses/mod.rs index cda075d1d..7ddbcb53e 100644 --- a/rust/src/notcurses/mod.rs +++ b/rust/src/notcurses/mod.rs @@ -78,7 +78,6 @@ pub use wrapper::*; /// provide reasonably portable vivid character displays. /// /// This is the internal type safely wrapped by [Notcurses]. -/// pub type NcNotcurses = crate::bindings::ffi::notcurses; /// Options struct for [`NcNotcurses`]