From 0d38adc60a5ebf0d80f1a871e934b8553c84d525 Mon Sep 17 00:00:00 2001 From: nick black Date: Sat, 2 May 2020 08:13:15 -0400 Subject: [PATCH] rust: move inline replacements to libnotcurses-sys --- rust/libnotcurses-sys/src/lib.rs | 80 ++++++++++++++++++++++++++++++++ rust/notcurses/src/lib.rs | 80 -------------------------------- 2 files changed, 80 insertions(+), 80 deletions(-) diff --git a/rust/libnotcurses-sys/src/lib.rs b/rust/libnotcurses-sys/src/lib.rs index 534cf3d59..34356d3a4 100644 --- a/rust/libnotcurses-sys/src/lib.rs +++ b/rust/libnotcurses-sys/src/lib.rs @@ -4,6 +4,86 @@ include!(concat!(env!("OUT_DIR"), "/bindings.rs")); +pub fn ncplane_putstr_yx(_n: *mut ncplane, mut _y: i32, mut _x: i32, _str: &str) -> usize { + let mut ret = 0; + while ret < _str.len() { + let mut wcs = 0; + unsafe { + let col =ncplane_putegc_yx(_n, -1, -1, std::ffi::CString::new(&_str[ret..]).expect("Bad string").as_ptr(), &mut wcs); + if col < 0 { + return ret; // FIXME return error result + } + ret += col as usize; + } + } + return ret; +} + +pub fn ncplane_putstr(_n: *mut ncplane, _str: &str) -> usize { + return ncplane_putstr_yx(_n, -1, -1, _str); +} + +pub fn ncplane_dim_y(_n: *const ncplane) -> i32 { + unsafe { + let mut y = 0; + ncplane_dim_yx(_n, &mut y, std::ptr::null_mut()); + return y; + } +} + +pub fn ncplane_dim_x(_n: *const ncplane) -> i32 { + unsafe { + let mut x = 0; + ncplane_dim_yx(_n, std::ptr::null_mut(), &mut x); + return x; + } +} + +pub fn ncplane_perimeter(_n: *mut ncplane, _ul: *const cell, + _ur: *const cell, _ll: *const cell, + _lr: *const cell, _hl: *const cell, + _vl: *const cell, _ctlword: u32) + -> std::result::Result<(), std::io::Error> { + let r; + unsafe { + r =ncplane_cursor_move_yx(_n, 0, 0); + } + if r != 0 { + return Err(std::io::Error::new(std::io::ErrorKind::Other, "error moving cursor")); + } + Ok(()) +} + +pub fn cell_prime(_n: *mut ncplane, _c: *mut cell, _egc: &str, + _attr: u32, _channels: u64) -> i32 { + unsafe{ + (*_c).attrword = _attr; + (*_c).channels = _channels; + return cell_load(_n, _c, std::ffi::CString::new(_egc).unwrap().as_ptr()); + } +} + +pub fn cells_load_box(_n: *mut ncplane, _attrs: u32, _channels: u64, + _ul: *mut cell, _ur: *mut cell, + _ll: *mut cell, _lr: *mut cell, + _hl: *mut cell, _vl: *mut cell, _egcs: &str) + -> std::result::Result<(), std::io::Error> { + let rul = cell_prime(_n, _ul, _egcs, _attrs, _channels); + if rul <= 0 { + return Err(std::io::Error::new(std::io::ErrorKind::Other, "error priming cell")); + // FIXME cell_prime()s + } + Ok(()) +} + +pub fn cells_rounded_box(_n: *mut ncplane, _attr: u32, _channels: u64, + _ul: *mut cell, _ur: *mut cell, + _ll: *mut cell, _lr: *mut cell, + _hl: *mut cell, _vl: *mut cell) + -> std::result::Result<(), std::io::Error> { + return cells_load_box(_n, _attr, _channels, _ul, _ur, _ll, _lr, _hl, _vl, "╭╮╰╯─│"); +} + #[cfg(test)] mod tests { use super::*; diff --git a/rust/notcurses/src/lib.rs b/rust/notcurses/src/lib.rs index b471e64dc..6251a6eda 100644 --- a/rust/notcurses/src/lib.rs +++ b/rust/notcurses/src/lib.rs @@ -8,41 +8,6 @@ pub fn getc_blocking(_n: *mut ffi::notcurses, _ni: &mut ffi::ncinput) -> u32 { } } -pub fn ncplane_putstr_yx(_n: *mut ffi::ncplane, mut _y: i32, mut _x: i32, _str: &str) -> usize { - let mut ret = 0; - while ret < _str.len() { - let mut wcs = 0; - unsafe { - let col = ffi::ncplane_putegc_yx(_n, -1, -1, std::ffi::CString::new(&_str[ret..]).expect("Bad string").as_ptr(), &mut wcs); - if col < 0 { - return ret; // FIXME return error result - } - ret += col as usize; - } - } - return ret; -} - -pub fn ncplane_putstr(_n: *mut ffi::ncplane, _str: &str) -> usize { - return ncplane_putstr_yx(_n, -1, -1, _str); -} - -pub fn ncplane_dim_y(_n: *const ffi::ncplane) -> i32 { - unsafe { - let mut y = 0; - ffi::ncplane_dim_yx(_n, &mut y, std::ptr::null_mut()); - return y; - } -} - -pub fn ncplane_dim_x(_n: *const ffi::ncplane) -> i32 { - unsafe { - let mut x = 0; - ffi::ncplane_dim_yx(_n, std::ptr::null_mut(), &mut x); - return x; - } -} - pub fn render(_n: *mut ffi::notcurses) -> std::result::Result<(), std::io::Error> { unsafe { let r = ffi::notcurses_render(_n); @@ -67,51 +32,6 @@ pub fn dim_yx(_n: *const ffi::notcurses, _dimy: &mut i32, _dimx: &mut i32) { } } -pub fn ncplane_perimeter(_n: *mut ffi::ncplane, _ul: *const ffi::cell, - _ur: *const ffi::cell, _ll: *const ffi::cell, - _lr: *const ffi::cell, _hl: *const ffi::cell, - _vl: *const ffi::cell, _ctlword: u32) - -> std::result::Result<(), std::io::Error> { - let r; - unsafe { - r = ffi::ncplane_cursor_move_yx(_n, 0, 0); - } - if r != 0 { - return Err(std::io::Error::new(std::io::ErrorKind::Other, "error moving cursor")); - } - Ok(()) -} - -pub fn cell_prime(_n: *mut ffi::ncplane, _c: *mut ffi::cell, _egc: &str, - _attr: u32, _channels: u64) -> i32 { - unsafe{ - (*_c).attrword = _attr; - (*_c).channels = _channels; - return ffi::cell_load(_n, _c, std::ffi::CString::new(_egc).unwrap().as_ptr()); - } -} - -pub fn cells_load_box(_n: *mut ffi::ncplane, _attrs: u32, _channels: u64, - _ul: *mut ffi::cell, _ur: *mut ffi::cell, - _ll: *mut ffi::cell, _lr: *mut ffi::cell, - _hl: *mut ffi::cell, _vl: *mut ffi::cell, _egcs: &str) - -> std::result::Result<(), std::io::Error> { - let rul = cell_prime(_n, _ul, _egcs, _attrs, _channels); - if rul <= 0 { - return Err(std::io::Error::new(std::io::ErrorKind::Other, "error priming cell")); - // FIXME cell_prime()s - } - Ok(()) -} - -pub fn cells_rounded_box(_n: *mut ffi::ncplane, _attr: u32, _channels: u64, - _ul: *mut ffi::cell, _ur: *mut ffi::cell, - _ll: *mut ffi::cell, _lr: *mut ffi::cell, - _hl: *mut ffi::cell, _vl: *mut ffi::cell) - -> std::result::Result<(), std::io::Error> { - return cells_load_box(_n, _attr, _channels, _ul, _ur, _ll, _lr, _hl, _vl, "╭╮╰╯─│"); -} - #[cfg(test)] mod tests { use super::*;