rust: refactor examples

dankamongmen/clock_nanosleep_portable
joseLuís 4 years ago
parent 2e47618a6a
commit 107cb05fce

@ -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);
}
}

@ -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");
}
}
}

@ -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);
}
}

Loading…
Cancel
Save