From 107cb05fce06623761d35575369c7f297f669f75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?joseLu=C3=ADs?= Date: Mon, 16 Nov 2020 23:53:07 +0100 Subject: [PATCH] rust: refactor examples --- rust/examples/direct-cursor.rs | 60 ++++++++++++++++++++++------------ rust/examples/direct-image.rs | 29 +++++++++------- rust/examples/full-text.rs | 31 +++++++++++++----- 3 files changed, 78 insertions(+), 42 deletions(-) diff --git a/rust/examples/direct-cursor.rs b/rust/examples/direct-cursor.rs index afa1b4900..abba13bd8 100644 --- a/rust/examples/direct-cursor.rs +++ b/rust/examples/direct-cursor.rs @@ -1,42 +1,60 @@ +//! Example 'direct-cursor' +//! +//! Explore cursor functions in direct mode +//! + use std::thread::sleep; use std::time::Duration; - use std::ffi::CString; -use libnotcurses_sys as nc; +/// utility macro: sleep for $s seconds +macro_rules! sleep { + ($s:expr) => { + sleep(Duration::new($s, 0)); + }; +} + +/// utility macro: convert the String $s to *mut CString +macro_rules! cstring { + ($s:expr) => { + CString::new($s).unwrap().as_ptr(); + } +} + +use libnotcurses_sys::*; fn main() { unsafe { - let ncd = nc::NcDirect::new(); + let ncd = NcDirect::new(); - let cols = nc::ncdirect_dim_x(ncd); - let rows = nc::ncdirect_dim_y(ncd); + let cols = ncdirect_dim_x(ncd); + let rows = ncdirect_dim_y(ncd); println!("terminal size (rows, cols): {}, {}", rows, cols); // show current coordinates - let (mut cy, mut cx) = (0,0); - nc::ncdirect_cursor_yx(ncd, &mut cy, &mut cx); - nc::ncdirect_putstr(ncd, 0, CString::new(format!("({},{})\n", cy, cx)).unwrap().as_ptr()); - - // Write HELLO WORLD in steps + let (mut cy, mut cx) = (0, 0); + ncdirect_cursor_yx(ncd, &mut cy, &mut cx); + ncdirect_putstr(ncd, 0, cstring![format!(" ({},{})\n", cy, cx)], + ); - sleep(Duration::new(1, 0)); + sleep![1]; - nc::ncdirect_putstr(ncd, 0, CString::new("HELLO").unwrap().as_ptr()); - nc::ncdirect_flush(ncd); + ncdirect_putstr(ncd, 0, cstring!["HELLO"]); + ncdirect_flush(ncd); - sleep(Duration::new(1, 0)); + sleep![1]; - nc::ncdirect_putstr(ncd, 0, CString::new(" WORLD").unwrap().as_ptr()); - nc::ncdirect_flush(ncd); + ncdirect_putstr(ncd, 0, cstring!["HELLO"]); + ncdirect_flush(ncd); - sleep(Duration::new(1, 0)); + sleep![2]; // show current coordinates - nc::ncdirect_cursor_yx(ncd, &mut cy, &mut cx); - nc::ncdirect_putstr(ncd, 0, CString::new(format!(" ({},{})\n", cy, cx)).unwrap().as_ptr()); + ncdirect_cursor_yx(ncd, &mut cy, &mut cx); + ncdirect_putstr( ncd, 0, cstring![format!(" ({},{})\n", cy, cx)], + ); - sleep(Duration::new(1, 0)); - nc::ncdirect_stop(ncd); + sleep![1]; + ncdirect_stop(ncd); } } diff --git a/rust/examples/direct-image.rs b/rust/examples/direct-image.rs index 7d177ade7..bad3bfacb 100644 --- a/rust/examples/direct-image.rs +++ b/rust/examples/direct-image.rs @@ -1,31 +1,36 @@ +//! Example 'direct-image' +//! +//! Explore image rendering in direct mode + use std::ffi::CString; -use libnotcurses_sys as nc; +// This time we are gonna use the notcurses library through the `sys` namespace +use libnotcurses_sys as sys; fn main() { unsafe { - let ncd = nc::NcDirect::new(); + let ncd = sys::NcDirect::new(); - render_image(&mut *ncd, nc::NCBLIT_1x1); - render_image(&mut *ncd, nc::NCBLIT_2x1); - render_image(&mut *ncd, nc::NCBLIT_BRAILLE); + render_image(&mut *ncd, sys::NCBLIT_1x1); + render_image(&mut *ncd, sys::NCBLIT_2x1); + render_image(&mut *ncd, sys::NCBLIT_BRAILLE); - nc::ncdirect_stop(ncd); + sys::ncdirect_stop(ncd); } } -fn render_image(ncd: &mut nc::NcDirect, blit: nc::NcBlitter) { +fn render_image(ncd: &mut sys::NcDirect, blit: sys::NcBlitter) { unsafe { - if nc::ncdirect_render_image( + if sys::ncdirect_render_image( ncd, CString::new("image-16x16.png").unwrap().as_ptr(), - nc::NCALIGN_CENTER, + sys::NCALIGN_CENTER, blit, - nc::NCSCALE_NONE, + sys::NCSCALE_NONE, ) != 0 { - panic!("ERR: ncdirect_render_image. \ - Make sure you are running this example from the examples folder"); + panic!("ERR: ncdirect_render_image. Make sure \ + you are running this example from the examples folder"); } } } diff --git a/rust/examples/full-text.rs b/rust/examples/full-text.rs index 442a489b0..b4bd4839f 100644 --- a/rust/examples/full-text.rs +++ b/rust/examples/full-text.rs @@ -1,18 +1,31 @@ -//use std::ffi::Cstring; +use std::thread::sleep; +use std::time::Duration; +use std::ffi::CString; + +/// utility macro: sleep for $s seconds +macro_rules! sleep { + ($s:expr) => { + sleep(Duration::new($s, 0)); + }; +} -use libnotcurses_sys as nc; +/// utility macro: convert the String $s to *mut CString +macro_rules! cstring { + ($s:expr) => { + CString::new($s).unwrap().as_ptr(); + } +} -fn main() { +use libnotcurses_sys::*; +fn main() { unsafe { - // let options = nc::NotcursesOptions::new(); - // let app = nc::Notcurses::with_options(&options); - - let app = nc::Notcurses::new(); - + let nc = Notcurses::new(); + println!("WIP"); + sleep![2]; - nc::notcurses_stop(app); + notcurses_stop(nc); } }