rust: add new wrapper type over notcurses struct.
Create new wrapping types that can safely encapsulate the mutable references, and implement Drop and automatic (de)referencing. - Notcurses - rename Notcurses* to NcNotcurses*. - rename NotcursesOptions to NcNotcursesOptions. - new Notcurses struct. - implement Drop, AsRef, AsMut, Deref & DerefMut. - override stop method to be no-op. - reimplement constructors and associated methods. - remove without_altscreen_with_banners constructor. - update examples and tests. - rustfmt.pull/1279/head
parent
af07822d35
commit
6b2805937d
@ -1,17 +1,15 @@
|
||||
use libnotcurses_sys::*;
|
||||
|
||||
fn main() -> NcResult<()> {
|
||||
let nc = Notcurses::new()?;
|
||||
let mut nc = Notcurses::new()?;
|
||||
let stdplane = nc.stdplane()?;
|
||||
|
||||
for ch in "Initializing cells...".chars() {
|
||||
let cell = NcCell::with_char7b(ch);
|
||||
stdplane.putc(&cell)?;
|
||||
sleep![0, 40];
|
||||
nc.render()?;
|
||||
rsleep![&mut nc, 0, 40];
|
||||
}
|
||||
sleep![0, 900];
|
||||
|
||||
nc.stop()?;
|
||||
Ok(())
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
use crate::{notcurses_init, Notcurses, NotcursesOptions, NCOPTION_SUPPRESS_BANNERS};
|
||||
use crate::{notcurses_init, NcNotcurses, NcNotcursesOptions, NCOPTION_SUPPRESS_BANNERS};
|
||||
|
||||
/// Helper function for initializing Notcurses on C style tests.
|
||||
/// Helper function for initializing NcNotcurses on C style tests.
|
||||
#[allow(dead_code)]
|
||||
pub(crate) unsafe fn notcurses_init_test<'a>() -> &'a mut Notcurses {
|
||||
pub(crate) unsafe fn notcurses_init_test<'a>() -> &'a mut NcNotcurses {
|
||||
&mut *notcurses_init(
|
||||
&NotcursesOptions::with_flags(NCOPTION_SUPPRESS_BANNERS),
|
||||
&NcNotcursesOptions::with_flags(NCOPTION_SUPPRESS_BANNERS),
|
||||
core::ptr::null_mut(),
|
||||
)
|
||||
}
|
||||
|
@ -0,0 +1,140 @@
|
||||
//! `Notcurses` wrapper struct and traits implementations.
|
||||
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use crate::{
|
||||
raw_wrap, NcAlign, NcBlitter, NcDimension, NcLogLevel, NcNotcurses, NcNotcursesOptions,
|
||||
NcResult, NcScale,
|
||||
};
|
||||
|
||||
/// The main struct of the TUI library (full mode).
|
||||
///
|
||||
/// Safely wraps an [NcNotcurses],
|
||||
/// and implements Drop, AsRef, AsMut, Deref & DerefMut around it.
|
||||
pub struct Notcurses<'a> {
|
||||
raw: &'a mut NcNotcurses,
|
||||
}
|
||||
|
||||
impl<'a> AsRef<NcNotcurses> for Notcurses<'a> {
|
||||
fn as_ref(&self) -> &NcNotcurses {
|
||||
self.raw
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> AsMut<NcNotcurses> for Notcurses<'a> {
|
||||
fn as_mut(&mut self) -> &mut NcNotcurses {
|
||||
self.raw
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Deref for Notcurses<'a> {
|
||||
type Target = NcNotcurses;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.as_ref()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> DerefMut for Notcurses<'a> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.as_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for Notcurses<'a> {
|
||||
/// Destroys the Notcurses context.
|
||||
fn drop(&mut self) {
|
||||
let _ = self.raw.stop();
|
||||
}
|
||||
}
|
||||
|
||||
/// # Constructors and methods overriden from NcNotcurses
|
||||
impl<'a> Notcurses<'a> {
|
||||
// wrap constructors
|
||||
|
||||
/// New Notcurses (without banners).
|
||||
pub fn new() -> NcResult<Self> {
|
||||
raw_wrap![NcNotcurses::new()]
|
||||
}
|
||||
|
||||
/// New Notcurses, without banners.
|
||||
pub fn with_banners() -> NcResult<Self> {
|
||||
raw_wrap![NcNotcurses::with_banners()]
|
||||
}
|
||||
|
||||
/// New Notcurses, without an alternate screen (nor banners).
|
||||
pub fn without_altscreen() -> NcResult<Self> {
|
||||
raw_wrap![NcNotcurses::without_altscreen()]
|
||||
}
|
||||
|
||||
/// New Notcurses, expects `NCOPTION_*` flags.
|
||||
pub fn with_flags(flags: u64) -> NcResult<Self> {
|
||||
raw_wrap![NcNotcurses::with_flags(flags)]
|
||||
}
|
||||
|
||||
/// New Notcurses, expects [NcNotcursesOptions].
|
||||
pub fn with_options(options: NcNotcursesOptions) -> NcResult<Self> {
|
||||
raw_wrap![NcNotcurses::with_options(options)]
|
||||
}
|
||||
|
||||
/// New Notcurses, expects [NcLogLevel] and flags.
|
||||
pub fn with_debug(loglevel: NcLogLevel, flags: u64) -> NcResult<Self> {
|
||||
raw_wrap![NcNotcurses::with_debug(loglevel, flags)]
|
||||
}
|
||||
|
||||
// disable destructor
|
||||
|
||||
/// Since Notcurses already implements [Drop](#impl-Drop),
|
||||
/// this function is made no-op.
|
||||
pub fn stop(&mut self) -> NcResult<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
// wrap associated functions
|
||||
|
||||
/// Returns the offset into `availcols` at which `cols` ought be output given
|
||||
/// the requirements of `align`.
|
||||
pub fn align(availcols: NcDimension, align: NcAlign, cols: NcDimension) -> NcResult<()> {
|
||||
NcNotcurses::align(availcols, align, cols)
|
||||
}
|
||||
|
||||
/// Gets the name of an [NcBlitter] blitter.
|
||||
pub fn str_blitter(blitter: NcBlitter) -> String {
|
||||
NcNotcurses::str_blitter(blitter)
|
||||
}
|
||||
|
||||
/// Gets the name of an [NcScale] scaling mode.
|
||||
pub fn str_scalemode(scalemode: NcScale) -> String {
|
||||
NcNotcurses::str_scalemode(scalemode)
|
||||
}
|
||||
|
||||
/// Returns an [NcBlitter] from a string representation.
|
||||
pub fn lex_blitter(op: &str) -> NcResult<NcBlitter> {
|
||||
NcNotcurses::lex_blitter(op)
|
||||
}
|
||||
|
||||
/// Lexes a margin argument according to the standard NcNotcurses definition.
|
||||
///
|
||||
/// There can be either a single number, which will define all margins equally,
|
||||
/// or there can be four numbers separated by commas.
|
||||
///
|
||||
pub fn lex_margins(op: &str, options: &mut NcNotcursesOptions) -> NcResult<()> {
|
||||
NcNotcurses::lex_margins(op, options)
|
||||
}
|
||||
|
||||
/// Returns an [NcScale] from a string representation.
|
||||
pub fn lex_scalemode(op: &str) -> NcResult<NcScale> {
|
||||
NcNotcurses::lex_scalemode(op)
|
||||
}
|
||||
|
||||
/// Returns a human-readable string describing the running Notcurses version.
|
||||
pub fn version() -> String {
|
||||
NcNotcurses::version()
|
||||
}
|
||||
|
||||
/// Returns the running NcNotcurses version components
|
||||
/// (major, minor, patch, tweak).
|
||||
pub fn version_components() -> (u32, u32, u32, u32) {
|
||||
NcNotcurses::version_components()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue