diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 8f8e0eec1..f5e6a7e3c 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -30,6 +30,8 @@ pkg-config = "0.3.19" [dev-dependencies] serial_test = ">= 0.5.0" serial_test_derive = ">= 0.5.0" +rand = "0.7.3" + [lib] doctest = false diff --git a/rust/examples/full-basics.rs b/rust/examples/full-basics.rs index d2a7aef27..d32f0ad10 100644 --- a/rust/examples/full-basics.rs +++ b/rust/examples/full-basics.rs @@ -17,14 +17,18 @@ use libnotcurses_sys::*; fn main() { unsafe { let nc = Notcurses::new(); - let stdplane = notcurses_stdplane(nc); - let c1 = cell_char_initializer!('A'); - ncplane_putc(&mut *stdplane, &c1); + // use standard plane + let stdplane = notcurses_stdplane(nc); - let _ = notcurses_render(nc); + for ch in "Initializing cells...".chars() { + let cell = NcCell::with_char(ch); + sleep![60]; + ncplane_putc(&mut *stdplane, &cell); + let _ = notcurses_render(nc); + } + sleep![900]; - sleep![1200]; notcurses_stop(nc); } } diff --git a/rust/src/cells.rs b/rust/src/cells.rs index b39441fa9..942ca12ea 100644 --- a/rust/src/cells.rs +++ b/rust/src/cells.rs @@ -1,3 +1,5 @@ +//! NcCell constructors and cell_* static functions reimplementations + // functions already exported by bindgen : 6 // ----------------------------------------- // cell_duplicate @@ -66,13 +68,53 @@ use crate::{ channels_set_bg_default, channels_set_bg_rgb, channels_set_bg_rgb8, channels_set_fchannel, channels_set_fg_alpha, channels_set_fg_default, channels_set_fg_rgb, channels_set_fg_rgb8, types::{ - NcAlphaBits, NcCell, NcChannel, NcChannels, NcChar, NcColor, NcPaletteIndex, NcPlane, + NcAlphaBits, NcCell, NcChannel, NcChannels, NcChar, NcCharBackstop, NcColor, NcPaletteIndex, NcPlane, NcResult, NcStyleMask, NCCELL_ALPHA_OPAQUE, NCCELL_BGDEFAULT_MASK, NCCELL_BG_PALETTE, NCCELL_FGDEFAULT_MASK, NCCELL_FG_PALETTE, NCCELL_NOBACKGROUND_MASK, NCCELL_WIDEASIAN_MASK, }, NCSTYLE_MASK, }; +// Constructors ---------------------------------------------------------------- + +impl NcCell { + /// [`NcCell`] constructor expecting [`char`], [`NcStyleMask`] and [`NcChannels`] + /// + /// This is analogous to the [`cell_initializer`] macro + #[inline] + pub const fn new(ch: char, stylemask: NcStyleMask, channels: NcChannels) -> Self { + NcCell { + gcluster: ch as u32, + gcluster_backstop: 0 as NcCharBackstop, + reserved: 0, + stylemask, + channels, + } + } + + /// [`NcCell`] simple constructor just expecting a [`char`] + /// + /// This is analogous to the [`cell_char_initializer`] macro + #[inline] + pub const fn with_char(ch: char) -> Self { + Self::new( + ch, + 0 as NcStyleMask, + 0 as NcChannels, + ) + } + + /// [`NcCell`] simple constructor for an empty cell + /// + /// This is analogous to the [`cell_trivial_initializer`] macro + #[inline] + pub const fn new_blank() -> Self { + Self::with_char(0 as char) + } +} + +// Static Functions ------------------------------------------------------------ + /// cell_load(), plus blast the styling with 'style' and 'channels'. /// /// - Breaks the UTF-8 string in 'gcluster' down, setting up the cell 'cell'. diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 24fa1a20e..2aa738a0d 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -25,9 +25,6 @@ pub use bindings::*; #[doc(inline)] pub use types::*; -#[macro_use] -mod macros; - mod cells; mod channel; mod direct; diff --git a/rust/src/macros.rs b/rust/src/macros.rs deleted file mode 100644 index 3c8f70c2b..000000000 --- a/rust/src/macros.rs +++ /dev/null @@ -1,61 +0,0 @@ -#[allow(unused_imports)] -use crate::{NcCell, NcChannels, NcChar, NcStyleMask}; - -// NcCell ------------------------------------------------------------------------ - -/// Initializes a cell providing an [`NcChar`], -/// a [`NcStyleMask`] and [`NcChannels`] -#[macro_export] -macro_rules! cell_initializer { - ( $c:expr, $s:expr, $chan:expr ) => { - NcCell { - gcluster: $c as u32, - gcluster_backstop: 0 as NcCharBackstop, - reserved: 0, - stylemask: $s, - channels: $chan, - } - }; -} - -/// Initializes a cell providing just an [`NcChar`], -#[macro_export] -macro_rules! cell_char_initializer { - ( $c:expr ) => { - cell_initializer![$c, 0, 0] - }; -} - -/// Initializes an empty cell -#[macro_export] -macro_rules! cell_trivial_initializer { - ( ) => { - cell_char_initializer![0] - }; -} - -// ncmetric -------------------------------------------------------------------- - -// /// Used as arguments to a variable field width (i.e. "%*s" -- these are the *). -// /// -// /// We need this convoluted grotesquery to properly handle 'ยต'. -// // -// // TODO: TEST -// // -// // NCMETRICFWIDTH(x, cols) ((int)(strlen(x) - ncstrwidth(x) + (cols))) -// #[macro_export] -// macro_rules! NCMETRICFWIDTH { -// ( $x:expr, $cols:expr ) => { -// libc::strlen($x) as i32 - ncstrwidth($x) as i32 + $cols as i32 -// } -// } -// -// Proof of concept as const fn (wont work because strlen call) -// pub const unsafe fn NCMETRICFWIDTH(x: i8, cols: usize) -> usize { -// libc::strlen(&x) - crate::ncstrwidth(&x) as usize + cols -// } - -// #define NCMETRICFWIDTH(x, cols) ((int)(strlen(x) - ncstrwidth(x) + (cols))) -// #define PREFIXFMT(x) NCMETRICFWIDTH((x), PREFIXCOLUMNS), (x) -// #define IPREFIXFMT(x) NCMETRIXFWIDTH((x), IPREFIXCOLUMNS), (x) -// #define BPREFIXFMT(x) NCMETRICFWIDTH((x), BPREFIXCOLUMNS), (x) diff --git a/rust/src/plane.rs b/rust/src/plane.rs index 10d3b689a..14199b245 100644 --- a/rust/src/plane.rs +++ b/rust/src/plane.rs @@ -1,3 +1,5 @@ +//! NcPlane constructors and ncplane_* static functions reimplementations + // functions already exported by bindgen : 97 // ------------------------------------------ // ncplane_above @@ -166,7 +168,7 @@ use crate::{ ncplane_hline_interp, ncplane_putc_yx, ncplane_putegc_yx, ncplane_putnstr_yx, ncplane_putstr_yx, ncplane_resize, ncplane_vline_interp, ncplane_vprintf_yx, notcurses_align, types::{ - NcAlign, NcAlphaBits, NcCell, NcChannel, NcChannels, NcCharBackstop, NcColor, NcPlane, + NcAlign, NcAlphaBits, NcCell, NcChannel, NcChannels, NcColor, NcPlane, NcPlaneOptions, NcResult, NcStyleMask, Notcurses, NCPLANE_OPTION_HORALIGNED, }, }; @@ -328,12 +330,12 @@ pub fn ncplane_double_box( #[allow(unused_assignments)] let mut ret = 0; - let mut ul = cell_trivial_initializer![]; - let mut ur = cell_trivial_initializer![]; - let mut ll = cell_trivial_initializer![]; - let mut lr = cell_trivial_initializer![]; - let mut hl = cell_trivial_initializer![]; - let mut vl = cell_trivial_initializer![]; + let mut ul = NcCell::new_blank(); + let mut ur = NcCell::new_blank(); + let mut ll = NcCell::new_blank(); + let mut lr = NcCell::new_blank(); + let mut hl = NcCell::new_blank(); + let mut vl = NcCell::new_blank(); unsafe { ret = cells_double_box( @@ -426,12 +428,12 @@ pub fn ncplane_perimeter_double( unsafe { ncplane_dim_yx(plane, &mut dimy, &mut dimx); } - let mut ul = cell_trivial_initializer![]; - let mut ur = cell_trivial_initializer![]; - let mut ll = cell_trivial_initializer![]; - let mut lr = cell_trivial_initializer![]; - let mut hl = cell_trivial_initializer![]; - let mut vl = cell_trivial_initializer![]; + let mut ul = NcCell::new_blank(); + let mut ur = NcCell::new_blank(); + let mut ll = NcCell::new_blank(); + let mut lr = NcCell::new_blank(); + let mut hl = NcCell::new_blank(); + let mut vl = NcCell::new_blank(); if unsafe { cells_double_box( plane, @@ -475,12 +477,12 @@ pub fn ncplane_perimeter_rounded( unsafe { ncplane_dim_yx(plane, &mut dimy, &mut dimx); } - let mut ul = cell_trivial_initializer![]; - let mut ur = cell_trivial_initializer![]; - let mut ll = cell_trivial_initializer![]; - let mut lr = cell_trivial_initializer![]; - let mut hl = cell_trivial_initializer![]; - let mut vl = cell_trivial_initializer![]; + let mut ul = NcCell::new_blank(); + let mut ur = NcCell::new_blank(); + let mut ll = NcCell::new_blank(); + let mut lr = NcCell::new_blank(); + let mut hl = NcCell::new_blank(); + let mut vl = NcCell::new_blank(); if unsafe { cells_rounded_box( plane, @@ -713,12 +715,12 @@ pub fn ncplane_rounded_box( #[allow(unused_assignments)] let mut ret = 0; - let mut ul = cell_trivial_initializer![]; - let mut ur = cell_trivial_initializer![]; - let mut ll = cell_trivial_initializer![]; - let mut lr = cell_trivial_initializer![]; - let mut hl = cell_trivial_initializer![]; - let mut vl = cell_trivial_initializer![]; + let mut ul = NcCell::new_blank(); + let mut ur = NcCell::new_blank(); + let mut ll = NcCell::new_blank(); + let mut lr = NcCell::new_blank(); + let mut hl = NcCell::new_blank(); + let mut vl = NcCell::new_blank(); unsafe { ret = cells_rounded_box(