mirror of
https://github.com/dankamongmen/notcurses.git
synced 2024-11-20 03:25:47 +00:00
rust: add new wrapper type over NcDirect called DirectMode.
This commit is contained in:
parent
32bacf19e5
commit
f5f9397f7b
@ -10,18 +10,18 @@ use libnotcurses_sys::*;
|
|||||||
fn main() -> NcResult<()> {
|
fn main() -> NcResult<()> {
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
|
|
||||||
let ncd = NcDirect::new()?;
|
let mut dm = DirectMode::new()?;
|
||||||
|
|
||||||
let cols = ncd.dim_x();
|
let cols = dm.dim_x();
|
||||||
let rows = ncd.dim_y();
|
let rows = dm.dim_y();
|
||||||
println!("terminal size (rows, cols): {}, {}", rows, cols);
|
println!("terminal size (rows, cols): {}, {}", rows, cols);
|
||||||
|
|
||||||
let mut channels =
|
let mut channels =
|
||||||
NcChannelPair::combine(NcChannel::with_rgb(0xAA2244), NcChannel::with_rgb(0x112233));
|
NcChannelPair::combine(NcChannel::with_rgb(0xAA2244), NcChannel::with_rgb(0x112233));
|
||||||
ncd.putstr(channels, "The current coordinates are")?;
|
dm.putstr(channels, "The current coordinates are")?;
|
||||||
|
|
||||||
for _n in 0..40 {
|
for _n in 0..40 {
|
||||||
fsleep![ncd, 0, 30];
|
fsleep![&mut dm, 0, 30];
|
||||||
channels.set_fg_rgb8(
|
channels.set_fg_rgb8(
|
||||||
rng.gen_range(0x66..=0xEE),
|
rng.gen_range(0x66..=0xEE),
|
||||||
rng.gen_range(0x66..=0xEE),
|
rng.gen_range(0x66..=0xEE),
|
||||||
@ -32,11 +32,11 @@ fn main() -> NcResult<()> {
|
|||||||
rng.gen_range(0..=0x9),
|
rng.gen_range(0..=0x9),
|
||||||
rng.gen_range(0..=0x9),
|
rng.gen_range(0..=0x9),
|
||||||
);
|
);
|
||||||
ncd.putstr(channels, ".")?;
|
dm.putstr(channels, ".")?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (cy, cx) = ncd.cursor_yx()?;
|
let (cy, cx) = dm.cursor_yx()?;
|
||||||
ncd.putstr(channels, &format!(" ({},{})\n", cy, cx))?;
|
dm.putstr(channels, &format!(" ({},{})\n", cy, cx))?;
|
||||||
sleep![1];
|
sleep![1];
|
||||||
|
|
||||||
let sentence = vec![
|
let sentence = vec![
|
||||||
@ -45,16 +45,14 @@ fn main() -> NcResult<()> {
|
|||||||
for word in sentence {
|
for word in sentence {
|
||||||
channels.set_fg_rgb(channels.fg_rgb().wrapping_sub(0x050505));
|
channels.set_fg_rgb(channels.fg_rgb().wrapping_sub(0x050505));
|
||||||
channels.set_bg_rgb(channels.bg_rgb().wrapping_add(0x090909));
|
channels.set_bg_rgb(channels.bg_rgb().wrapping_add(0x090909));
|
||||||
ncd.putstr(channels, &format!["{} ", word])?;
|
dm.putstr(channels, &format!["{} ", word])?;
|
||||||
fsleep![ncd, 0, 150];
|
fsleep![&mut dm, 0, 150];
|
||||||
}
|
}
|
||||||
sleep![0, 300];
|
sleep![0, 300];
|
||||||
channels.set_fg_rgb(0xFFFFFF);
|
channels.set_fg_rgb(0xFFFFFF);
|
||||||
channels.set_bg_default();
|
channels.set_bg_default();
|
||||||
ncd.putstr(channels, "\nbye!\n\n")?;
|
dm.putstr(channels, "\nbye!\n\n")?;
|
||||||
fsleep![ncd, 0, 600];
|
fsleep![&mut dm, 0, 600];
|
||||||
ncd.clear()?;
|
dm.clear()?;
|
||||||
sleep![2];
|
|
||||||
ncd.stop()?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -7,21 +7,20 @@
|
|||||||
use libnotcurses_sys::*;
|
use libnotcurses_sys::*;
|
||||||
|
|
||||||
fn main() -> NcResult<()> {
|
fn main() -> NcResult<()> {
|
||||||
let ncd = NcDirect::new()?;
|
let mut dm = DirectMode::new()?;
|
||||||
|
|
||||||
render_image(ncd, NCBLIT_1x1)?;
|
render_image(&mut dm, NCBLIT_1x1)?;
|
||||||
render_image(ncd, NCBLIT_2x1)?;
|
render_image(&mut dm, NCBLIT_2x1)?;
|
||||||
render_image(ncd, NCBLIT_BRAILLE)?;
|
render_image(&mut dm, NCBLIT_BRAILLE)?;
|
||||||
|
|
||||||
ncd.stop()?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn render_image(ncd: &mut NcDirect, blit: NcBlitter) -> NcResult<()> {
|
fn render_image(dm: &mut DirectMode, blit: NcBlitter) -> NcResult<()> {
|
||||||
if let Err(nc_error) = ncd.render_image("image-16x16.png", NCALIGN_CENTER, blit, NCSCALE_NONE) {
|
if let Err(nc_error) = dm.render_image("image-16x16.png", NCALIGN_CENTER, blit, NCSCALE_NONE) {
|
||||||
return Err(NcError::with_msg(
|
return Err(NcError::with_msg(
|
||||||
nc_error.int,
|
nc_error.int,
|
||||||
"ERROR: ncdirect_render_image(). Make sure you \
|
"ERROR: dmirect_render_image(). Make sure you \
|
||||||
are running this example from the examples folder",
|
are running this example from the examples folder",
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
@ -3,33 +3,33 @@
|
|||||||
use libnotcurses_sys::*;
|
use libnotcurses_sys::*;
|
||||||
|
|
||||||
fn main() -> NcResult<()> {
|
fn main() -> NcResult<()> {
|
||||||
let ncd = NcDirect::new()?;
|
let mut dm = DirectMode::new()?;
|
||||||
|
|
||||||
let dimy = ncd.dim_y();
|
let dimy = dm.dim_y();
|
||||||
let dimx = ncd.dim_x();
|
let dimx = dm.dim_x();
|
||||||
for _ in 0..dimy {
|
for _ in 0..dimy {
|
||||||
for _ in 0..dimx {
|
for _ in 0..dimx {
|
||||||
printf!("X");
|
printf!("X");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ncd.flush()?;
|
dm.flush()?;
|
||||||
|
|
||||||
ncd.set_fg_rgb(0xff8080)?;
|
dm.set_fg_rgb(0xff8080)?;
|
||||||
ncd.styles_on(NCSTYLE_STANDOUT)?;
|
dm.styles_on(NCSTYLE_STANDOUT)?;
|
||||||
printf!(" erp erp \n");
|
printf!(" erp erp \n");
|
||||||
ncd.set_fg_rgb(0x80ff80)?;
|
dm.set_fg_rgb(0x80ff80)?;
|
||||||
printf!(" erp erp \n");
|
printf!(" erp erp \n");
|
||||||
ncd.styles_off(NCSTYLE_STANDOUT)?;
|
dm.styles_off(NCSTYLE_STANDOUT)?;
|
||||||
printf!(" erp erp \n");
|
printf!(" erp erp \n");
|
||||||
ncd.set_fg_rgb(0xff8080)?;
|
dm.set_fg_rgb(0xff8080)?;
|
||||||
printf!(" erp erp \n");
|
printf!(" erp erp \n");
|
||||||
ncd.cursor_right(dimx / 2)?;
|
dm.cursor_right(dimx / 2)?;
|
||||||
ncd.cursor_up(dimy / 2)?;
|
dm.cursor_up(dimy / 2)?;
|
||||||
printf!(" erperperp! \n");
|
printf!(" erperperp! \n");
|
||||||
|
|
||||||
let (mut y, x);
|
let (mut y, x);
|
||||||
|
|
||||||
if let Ok((_y, _x)) = ncd.cursor_yx() {
|
if let Ok((_y, _x)) = dm.cursor_yx() {
|
||||||
y = _y;
|
y = _y;
|
||||||
x = _x;
|
x = _x;
|
||||||
printf!("\n\tRead cursor position: y: %d x: %d\n", y, x);
|
printf!("\n\tRead cursor position: y: %d x: %d\n", y, x);
|
||||||
@ -37,12 +37,12 @@ fn main() -> NcResult<()> {
|
|||||||
y += 2;
|
y += 2;
|
||||||
while y > 3 {
|
while y > 3 {
|
||||||
let up = if y >= 3 { 3 } else { y };
|
let up = if y >= 3 { 3 } else { y };
|
||||||
ncd.cursor_up(up)?;
|
dm.cursor_up(up)?;
|
||||||
ncd.flush()?;
|
dm.flush()?;
|
||||||
y -= up;
|
y -= up;
|
||||||
|
|
||||||
let newy;
|
let newy;
|
||||||
if let Ok((_y, _)) = ncd.cursor_yx() {
|
if let Ok((_y, _)) = dm.cursor_yx() {
|
||||||
newy = _y;
|
newy = _y;
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
@ -59,6 +59,5 @@ fn main() -> NcResult<()> {
|
|||||||
return Err(NcError::with_msg(-10, "Couldn't read cursor position."));
|
return Err(NcError::with_msg(-10, "Couldn't read cursor position."));
|
||||||
}
|
}
|
||||||
|
|
||||||
ncd.stop()?;
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -3,23 +3,22 @@
|
|||||||
use libnotcurses_sys::*;
|
use libnotcurses_sys::*;
|
||||||
|
|
||||||
fn main() -> NcResult<()> {
|
fn main() -> NcResult<()> {
|
||||||
let ncd = NcDirect::new()?;
|
let mut dm = DirectMode::new()?;
|
||||||
|
|
||||||
ncd.set_fg_rgb8(100, 100, 100)?;
|
dm.set_fg_rgb8(100, 100, 100)?;
|
||||||
ncd.set_bg_rgb8(0xff, 0xff, 0xff)?;
|
dm.set_bg_rgb8(0xff, 0xff, 0xff)?;
|
||||||
printf!("a");
|
printf!("a");
|
||||||
ncd.set_bg_rgb8(0, 0, 0)?;
|
dm.set_bg_rgb8(0, 0, 0)?;
|
||||||
printf!("b");
|
printf!("b");
|
||||||
printf!(" ");
|
printf!(" ");
|
||||||
printf!(" ");
|
printf!(" ");
|
||||||
ncd.set_bg_rgb8(0, 0, 1)?;
|
dm.set_bg_rgb8(0, 0, 1)?;
|
||||||
printf!("c");
|
printf!("c");
|
||||||
printf!(" ");
|
printf!(" ");
|
||||||
printf!(" ");
|
printf!(" ");
|
||||||
ncd.set_bg_rgb8(0xff, 0xff, 0xff)?;
|
dm.set_bg_rgb8(0xff, 0xff, 0xff)?;
|
||||||
printf!("d");
|
printf!("d");
|
||||||
printf!("\n");
|
printf!("\n");
|
||||||
ncd.stop()?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -66,9 +66,14 @@ mod test;
|
|||||||
|
|
||||||
mod methods;
|
mod methods;
|
||||||
mod reimplemented;
|
mod reimplemented;
|
||||||
pub use reimplemented::*;
|
mod wrapper;
|
||||||
|
|
||||||
/// Minimal notcurses instances for styling text
|
pub use reimplemented::*;
|
||||||
|
pub use wrapper::*;
|
||||||
|
|
||||||
|
/// Minimal notcurses instance for styling text.
|
||||||
|
///
|
||||||
|
/// This is the internal type safely wrapped by [Direct].
|
||||||
pub type NcDirect = crate::bindings::ffi::ncdirect;
|
pub type NcDirect = crate::bindings::ffi::ncdirect;
|
||||||
|
|
||||||
/// Flags (options) for [`NcDirect`]
|
/// Flags (options) for [`NcDirect`]
|
||||||
|
71
rust/src/direct/wrapper.rs
Normal file
71
rust/src/direct/wrapper.rs
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
//! `Direct` wrapper struct and traits implementations.
|
||||||
|
|
||||||
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
raw_wrap, NcDirect, NcResult
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Minimal notcurses instance for styling text.
|
||||||
|
///
|
||||||
|
/// Safely wraps an [NcDirect],
|
||||||
|
/// and implements Drop, AsRef, AsMut, Deref & DerefMut around it.
|
||||||
|
pub struct Direct<'a> {
|
||||||
|
raw: &'a mut NcDirect,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> AsRef<NcDirect> for Direct<'a> {
|
||||||
|
fn as_ref(&self) -> &NcDirect {
|
||||||
|
self.raw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> AsMut<NcDirect> for Direct<'a> {
|
||||||
|
fn as_mut(&mut self) -> &mut NcDirect {
|
||||||
|
self.raw
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Deref for Direct<'a> {
|
||||||
|
type Target = NcDirect;
|
||||||
|
|
||||||
|
fn deref(&self) -> &Self::Target {
|
||||||
|
self.as_ref()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> DerefMut for Direct<'a> {
|
||||||
|
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||||
|
self.as_mut()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Drop for Direct<'a> {
|
||||||
|
/// Destroys the Direct context.
|
||||||
|
fn drop(&mut self) {
|
||||||
|
let _ = self.raw.stop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// # Constructors and methods overriden from NcDirect
|
||||||
|
impl<'a> Direct<'a> {
|
||||||
|
// wrap constructors
|
||||||
|
|
||||||
|
/// New Direct (without banners).
|
||||||
|
pub fn new() -> NcResult<Self> {
|
||||||
|
raw_wrap![NcDirect::new()]
|
||||||
|
}
|
||||||
|
|
||||||
|
/// New Direct, expects `NCOPTION_*` flags.
|
||||||
|
pub fn with_flags(flags: u64) -> NcResult<Self> {
|
||||||
|
raw_wrap![NcDirect::with_flags(flags)]
|
||||||
|
}
|
||||||
|
|
||||||
|
// disable destructor
|
||||||
|
|
||||||
|
/// Since Direct already implements [Drop](#impl-Drop),
|
||||||
|
/// this function is made no-op.
|
||||||
|
pub fn stop(&mut self) -> NcResult<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
@ -78,7 +78,6 @@ pub use wrapper::*;
|
|||||||
/// provide reasonably portable vivid character displays.
|
/// provide reasonably portable vivid character displays.
|
||||||
///
|
///
|
||||||
/// This is the internal type safely wrapped by [Notcurses].
|
/// This is the internal type safely wrapped by [Notcurses].
|
||||||
///
|
|
||||||
pub type NcNotcurses = crate::bindings::ffi::notcurses;
|
pub type NcNotcurses = crate::bindings::ffi::notcurses;
|
||||||
|
|
||||||
/// Options struct for [`NcNotcurses`]
|
/// Options struct for [`NcNotcurses`]
|
||||||
|
Loading…
Reference in New Issue
Block a user