mirror of
https://github.com/dankamongmen/notcurses
synced 2025-03-10 01:29:05 -04:00
rust: refactor sleep macros
- receive a variable number of arguments. - make rsleep use methods and deal with NcResult. - new fsleep to flush an NcDirect context. - fix macro usages in examples.
This commit is contained in:
parent
763f3efdc5
commit
fbe4352233
@ -6,41 +6,35 @@
|
||||
use libnotcurses_sys::*;
|
||||
|
||||
fn main() -> NcResult<()> {
|
||||
unsafe {
|
||||
let ncd = NcDirect::new()?;
|
||||
let ncd = NcDirect::new()?;
|
||||
|
||||
let cols = ncdirect_dim_x(ncd);
|
||||
let rows = ncdirect_dim_y(ncd);
|
||||
println!("terminal size (rows, cols): {}, {}", rows, cols);
|
||||
let cols = ncd.dim_x();
|
||||
let rows = ncd.dim_y();
|
||||
println!("terminal size (rows, cols): {}, {}", rows, cols);
|
||||
|
||||
ncd.putstr(0, "The current coordinates are")?;
|
||||
ncd.flush()?;
|
||||
ncd.putstr(0, "The current coordinates are")?;
|
||||
|
||||
for _n in 0..20 {
|
||||
ncd.putstr(0, ".")?;
|
||||
ncd.flush()?;
|
||||
sleep![50];
|
||||
}
|
||||
|
||||
let (cy, cx) = ncd.cursor_yx()?;
|
||||
ncd.putstr(0, &format!(" ({},{})\n", cy, cx))?;
|
||||
sleep![1000];
|
||||
|
||||
let sentence = vec!["And", "now", "I", "will", "clear", "the", "screen", ".", ".", "."];
|
||||
for word in sentence {
|
||||
ncd.putstr(0, &format!["{} ", word])?;
|
||||
ncd.flush()?;
|
||||
sleep![200];
|
||||
}
|
||||
sleep![300];
|
||||
ncd.putstr(0, "\nbye!\n\n")?;
|
||||
ncd.flush()?;
|
||||
sleep![600];
|
||||
|
||||
ncd.clear()?;
|
||||
sleep![1000];
|
||||
|
||||
ncd.stop()?;
|
||||
for _n in 0..40 {
|
||||
fsleep![ncd, 0, 30];
|
||||
ncd.putstr(0, ".")?;
|
||||
}
|
||||
|
||||
let (cy, cx) = ncd.cursor_yx()?;
|
||||
ncd.putstr(0, &format!(" ({},{})\n", cy, cx))?;
|
||||
sleep![1];
|
||||
|
||||
let sentence = vec!["And", "now", "I", "will", "clear", "the", "screen", ".", ".", "."];
|
||||
for word in sentence {
|
||||
ncd.putstr(0, &format!["{} ", word])?;
|
||||
fsleep![ncd, 0, 150];
|
||||
}
|
||||
sleep![0, 300];
|
||||
ncd.putstr(0, "\nbye!\n\n")?;
|
||||
fsleep![ncd, 0, 600];
|
||||
|
||||
ncd.clear()?;
|
||||
sleep![1];
|
||||
|
||||
ncd.stop()?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -27,7 +27,7 @@ fn main() -> NcResult<()> {
|
||||
println!("'{0}' ({1:x})\n{2:?}", key, key as u32, input);
|
||||
}
|
||||
|
||||
rsleep![nc, 100];
|
||||
rsleep![nc, 0, 10];
|
||||
|
||||
match key {
|
||||
NCKEY_F01 => break,
|
||||
@ -37,7 +37,7 @@ fn main() -> NcResult<()> {
|
||||
|
||||
println!("\nExiting...");
|
||||
|
||||
rsleep![nc, 1000];
|
||||
rsleep![nc, 1, 500];
|
||||
nc.stop()?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ fn main() -> NcResult<()> {
|
||||
let mut wc = '\u{4e00}'; // 一
|
||||
|
||||
loop {
|
||||
sleep![1];
|
||||
sleep![0, 0, 50];
|
||||
|
||||
if plane.putchar(wc) == NCRESULT_ERR {
|
||||
break;
|
||||
|
@ -4,46 +4,74 @@
|
||||
|
||||
#[allow(unused_imports)]
|
||||
// enjoy briefer doc comments
|
||||
use crate::{NcError, NCRESULT_ERR, NCRESULT_OK};
|
||||
use crate::{
|
||||
notcurses_render, NcError, NcResult, Notcurses, NCRESULT_ERR, NCRESULT_OK, NcDirect,
|
||||
};
|
||||
|
||||
// General Utility Macros ------------------------------------------------------
|
||||
// Sleep, Render & Flush Macros ------------------------------------------------
|
||||
|
||||
/// Sleeps for `$ms` milliseconds.
|
||||
/// Sleeps for `$ns` seconds + `$ms` milliseconds
|
||||
/// + `$us` microseconds + `$ns` nanoseconds
|
||||
#[macro_export]
|
||||
macro_rules! sleep {
|
||||
($ms:expr) => {
|
||||
std::thread::sleep(std::time::Duration::from_millis($ms));
|
||||
($s:expr) => {
|
||||
std::thread::sleep(std::time::Duration::from_secs($s));
|
||||
};
|
||||
|
||||
($s:expr, $ms:expr) => {
|
||||
std::thread::sleep(std::time::Duration::from_millis($s * 1000 + $ms));
|
||||
};
|
||||
($s:expr, $ms:expr, $us:expr) => {
|
||||
std::thread::sleep(std::time::Duration::from_micros(
|
||||
$s * 1_000_000 + $ms * 1_000 + $us,
|
||||
));
|
||||
};
|
||||
($s:expr, $ms:expr, $us:expr, $ns:expr) => {
|
||||
std::thread::sleep(std::time::Duration::from_nanos(
|
||||
$s * 1_000_000_000 + $ms * 1_000_000 + $us * 1_000 + $ns,
|
||||
));
|
||||
};
|
||||
}
|
||||
|
||||
/// Renders the `$nc` [Notcurses][crate::Notcurses] object,
|
||||
/// then sleeps for `$ms` milliseconds.
|
||||
/// Notcurses.[render()][Notcurses#method.render]? plus [sleep]!(`sleep_args`).
|
||||
///
|
||||
/// Renders the `$nc` [Notcurses] object and, if there's no error,
|
||||
/// calls the sleep macro with the rest of the arguments.
|
||||
///
|
||||
/// Returns [NcResult].
|
||||
#[macro_export]
|
||||
macro_rules! rsleep {
|
||||
($nc:expr, $ms:expr) => {{
|
||||
($nc:expr, $( $sleep_args:expr),+ ) => {
|
||||
// Rust style, with methods & NcResult
|
||||
crate::$nc.render();
|
||||
std::thread::sleep(std::time::Duration::from_millis($ms));
|
||||
}};
|
||||
Notcurses::render($nc)?;
|
||||
sleep![$( $sleep_args ),+];
|
||||
};
|
||||
($nc:expr, $( $sleep_args:expr),+ ,) => {
|
||||
rsleep![$nc, $( $sleep_args ),* ]
|
||||
};
|
||||
}
|
||||
|
||||
/// Renders the `$nc` [Notcurses][crate::Notcurses] object,
|
||||
/// then sleeps for `$ms` milliseconds and returns the result of
|
||||
/// [notcurses_render][crate::notcurses_render].
|
||||
/// NcDirect.[flush()][NcDirect#method.flush]? plus [sleep]!(`sleep_args`).
|
||||
///
|
||||
/// Flushes the `$ncd` [NcDirect] object and, if there's no error,
|
||||
/// calls the sleep macro with the rest of the arguments.
|
||||
///
|
||||
/// Returns [NcResult].
|
||||
#[macro_export]
|
||||
macro_rules! rsleep_c {
|
||||
($nc:expr, $ms:expr) => {{
|
||||
// C style, with functions & NcIntResult
|
||||
let mut res: crate::NcIntResult = 0;
|
||||
unsafe {
|
||||
res = crate::notcurses_render($nc);
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_millis($ms));
|
||||
res
|
||||
}};
|
||||
macro_rules! fsleep {
|
||||
($ncd:expr, $( $sleep_args:expr),+ ) => {
|
||||
// Rust style, with methods & NcResult
|
||||
NcDirect::flush($ncd)?;
|
||||
sleep![$( $sleep_args ),+];
|
||||
};
|
||||
($ncd:expr, $( $sleep_args:expr),+ ,) => {
|
||||
rsleep![$ncd, $( $sleep_args ),* ]
|
||||
};
|
||||
}
|
||||
|
||||
/// Converts `&str` to `*mut CString`, for when `*const c_char` is needed.
|
||||
// General Utility Macros ------------------------------------------------------
|
||||
|
||||
/// Converts an `&str` into `*mut CString`, for when a `*const c_char` is needed.
|
||||
#[macro_export]
|
||||
macro_rules! cstring {
|
||||
($s:expr) => {
|
||||
@ -51,7 +79,7 @@ macro_rules! cstring {
|
||||
};
|
||||
}
|
||||
|
||||
/// Simple wrapper around [libc::printf].
|
||||
/// Wrapper around [libc::printf].
|
||||
#[macro_export]
|
||||
macro_rules! printf {
|
||||
($s:expr) => {
|
||||
@ -112,7 +140,7 @@ macro_rules! error_ptr {
|
||||
};
|
||||
}
|
||||
|
||||
/// Returns an Ok([`String`]) from a `*const` pointer to a C string,
|
||||
/// Returns an Ok(String) from a `*const` pointer to a C string,
|
||||
/// or an Err([NcError]) if the pointer is null.
|
||||
///
|
||||
/// In other words:
|
||||
|
Loading…
x
Reference in New Issue
Block a user