rust: refactor error system.

We need to use Result<> to manage error flow,
at least on the methods API. This way it will
finally feel idiomatic Rust and more concise.
Also, we can't return a non Result from main.

- rename NcResult to NcIntResult.
- new NcError & NcResult types.
- new error![] macro to reduce boilerplate.
- change return type of NcDirect methods to NcResult.
  - refactor several examples to return NcResult:
    direct-cursor, poc-direct & poc-kittyzapper.
- BONUS: fix test not stopping notcurses context.
- update lib doc-comment.
- fix typo in readme.
This commit is contained in:
joseLuís 2020-12-25 05:16:34 +01:00
parent 5d1dfe1d02
commit 1687af89e0
18 changed files with 342 additions and 252 deletions

View File

@ -5,7 +5,7 @@
use libnotcurses_sys::*;
fn main() {
fn main() -> NcResult<()> {
unsafe {
let ncd = NcDirect::new();
@ -13,34 +13,35 @@ fn main() {
let rows = ncdirect_dim_y(ncd);
println!("terminal size (rows, cols): {}, {}", rows, cols);
ncdirect_putstr(ncd, 0, cstring![format!("The current coordinates are")]);
ncdirect_flush(ncd);
ncd.putstr(0, "The current coordinates are")?;
ncd.flush()?;
for _n in 0..20 {
ncdirect_putstr(ncd, 0, cstring!("."));
ncdirect_flush(ncd);
ncd.putstr(0, ".")?;
ncd.flush()?;
sleep![50];
}
let (mut cy, mut cx) = (0, 0);
ncdirect_cursor_yx(ncd, &mut cy, &mut cx);
ncdirect_putstr(ncd, 0, cstring![format!(" ({},{})\n", cy, cx)]);
if let Some((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 {
ncdirect_putstr(ncd, 0, cstring!(format!["{} ", word]));
ncdirect_flush(ncd);
ncd.putstr(0, &format!["{} ", word])?;
ncd.flush()?;
sleep![200];
}
sleep![300];
ncdirect_putstr(ncd, 0, cstring!("\nbye!\n\n"));
ncdirect_flush(ncd);
ncd.putstr(0, "\nbye!\n\n")?;
ncd.flush()?;
sleep![600];
ncdirect_clear(ncd);
ncd.clear()?;
sleep![1000];
ncdirect_stop(ncd);
ncd.stop()?;
}
Ok(())
}

View File

@ -2,7 +2,7 @@
use libnotcurses_sys::*;
fn main() {
fn main() -> NcResult<()> {
let ncd = NcDirect::new();
let dimy = ncd.dim_y();
@ -12,20 +12,19 @@ fn main() {
printf!("X");
}
}
ncd.flush();
ncd.flush()?;
let mut ret = 0;
ret |= ncd.fg_rgb(0xff8080);
ret |= ncd.styles_on(NCSTYLE_STANDOUT);
ncd.fg_rgb(0xff8080)?;
ncd.styles_on(NCSTYLE_STANDOUT)?;
printf!(" erp erp \n");
ret |= ncd.fg_rgb(0x80ff80);
ncd.fg_rgb(0x80ff80)?;
printf!(" erp erp \n");
ret |= ncd.styles_off(NCSTYLE_STANDOUT);
ncd.styles_off(NCSTYLE_STANDOUT)?;
printf!(" erp erp \n");
ret |= ncd.fg_rgb(0xff8080);
ncd.fg_rgb(0xff8080)?;
printf!(" erp erp \n");
ret |= ncd.cursor_right(dimx / 2);
ret |= ncd.cursor_up(dimy / 2);
ncd.cursor_right(dimx / 2)?;
ncd.cursor_up(dimy / 2)?;
printf!(" erperperp! \n");
let (mut y, x);
@ -34,14 +33,12 @@ fn main() {
y = _y;
x = _x;
printf!("\n\tRead cursor position: y: %d x: %d\n", y, x);
y += 2;
while y > 3 {
ret = NCRESULT_ERR;
let up = if y >= 3 { 3 } else { y };
if ncd.cursor_up(up) == NCRESULT_ERR {
break;
}
ncd.flush();
ncd.cursor_up(up)?;
ncd.flush()?;
y -= up;
let newy;
@ -50,19 +47,18 @@ fn main() {
} else {
break;
}
if newy != y {
eprintln!("Expected {}, got {}", y, newy);
break;
}
printf!("\n\tRead cursor position: y: %d x: %d\n", newy, x);
y += 2;
ret = NCRESULT_OK;
}
} else {
ret = NCRESULT_ERR - 10;
return Err(NcError::new(-10, "Couldn't read cursor position."));
}
// println!("ret={}", ret); // DEBUG
ncd.stop();
ncd.stop()?;
Ok(())
}

View File

@ -2,21 +2,24 @@
use libnotcurses_sys::*;
fn main() {
fn main() -> NcResult<()> {
let ncd = NcDirect::new();
ncd.fg_rgb8(100, 100, 100);
ncd.bg_rgb8(0xff, 0xff, 0xff);
ncd.fg_rgb8(100, 100, 100)?;
ncd.bg_rgb8(0xff, 0xff, 0xff)?;
printf!("a");
ncd.bg_rgb8(0, 0, 0);
ncd.bg_rgb8(0, 0, 0)?;
printf!("b");
printf!(" ");
printf!(" ");
ncd.bg_rgb8(0, 0, 1);
ncd.bg_rgb8(0, 0, 1)?;
printf!("c");
printf!(" ");
printf!(" ");
ncd.bg_rgb8(0xff, 0xff, 0xff);
ncd.bg_rgb8(0xff, 0xff, 0xff)?;
printf!("d");
printf!("\n");
ncd.stop();
ncd.stop()?;
Ok(())
}

View File

@ -3,8 +3,8 @@
use libc::strcmp;
use crate::{
cell_release, NcAlphaBits, NcCell, NcChannel, NcChannelPair, NcColor, NcEgc, NcPaletteIndex,
NcPlane, NcResult, NcRgb, NcStyleMask, NCCELL_ALPHA_OPAQUE, NCCELL_BGDEFAULT_MASK,
cell_release, NcAlphaBits, NcCell, NcChannel, NcChannelPair, NcColor, NcEgc, NcIntResult,
NcPaletteIndex, NcPlane, NcRgb, NcStyleMask, NCCELL_ALPHA_OPAQUE, NCCELL_BGDEFAULT_MASK,
NCCELL_BG_PALETTE, NCCELL_FGDEFAULT_MASK, NCCELL_FG_PALETTE, NCCELL_NOBACKGROUND_MASK,
NCCELL_WIDEASIAN_MASK, NCRESULT_ERR, NCRESULT_OK, NCSTYLE_MASK,
};
@ -462,7 +462,7 @@ pub fn cell_prime(
gcluster: NcEgc,
style: NcStyleMask,
channels: NcChannelPair,
) -> NcResult {
) -> NcIntResult {
cell.stylemask = style;
cell.channels = channels;
unsafe { crate::cell_load(plane, cell, gcluster as u32 as *const i8) }
@ -487,10 +487,10 @@ pub fn cells_load_box(
hl: &mut NcCell,
vl: &mut NcCell,
gcluster: NcEgc,
) -> NcResult {
) -> NcIntResult {
// mutable copy for pointer arithmetics:
let mut gclu = gcluster as u32 as *const i8;
let mut ulen: NcResult;
let mut ulen: NcIntResult;
ulen = cell_prime(plane, ul, gcluster, style, channels);

View File

@ -13,4 +13,5 @@ fn constructors() {
let _n0 = Notcurses::new();
let _p0 = NcPlane::new(_n0, 0, 0, 10, 10);
let _c3 = NcCell::with_char('௵', _p0);
_n0.stop();
}

View File

@ -4,9 +4,9 @@ use core::ptr::{null, null_mut};
use crate::ffi::sigset_t;
use crate::{
cstring, NcAlign, NcBlitter, NcChannelPair, NcColor, NcDimension, NcDirect, NcDirectFlags,
NcEgc, NcInput, NcPaletteIndex, NcResult, NcRgb, NcScale, NcStyleMask, NcTime, NCRESULT_ERR,
NCRESULT_OK,
cstring, error, NcAlign, NcBlitter, NcChannelPair, NcColor, NcDimension, NcDirect,
NcDirectFlags, NcEgc, NcError, NcInput, NcPaletteIndex, NcResult, NcRgb, NcScale, NcStyleMask,
NcTime, NCRESULT_ERR, NCRESULT_OK,
};
/// # `NcDirect` constructors and destructors
@ -41,8 +41,12 @@ impl NcDirect {
/// Releases this NcDirect and any associated resources.
///
/// *C style function: [ncdirect_stop()][crate::ncdirect_stop].*
pub fn stop(&mut self) -> NcResult {
unsafe { crate::ncdirect_stop(self) }
pub fn stop(&mut self) -> NcResult<()> {
let res = unsafe { crate::ncdirect_stop(self) };
if res == NCRESULT_OK {
return Ok(());
}
Err(NcError::new(res, ""))
}
}
@ -51,15 +55,15 @@ impl NcDirect {
/// Clears the screen.
///
/// *C style function: [ncdirect_clear()][crate::ncdirect_clear].*
pub fn clear(&mut self) -> NcResult {
unsafe { crate::ncdirect_clear(self) }
pub fn clear(&mut self) -> NcResult<()> {
error![unsafe { crate::ncdirect_clear(self) }]
}
/// Forces a flush.
///
/// *C style function: [ncdirect_flush()][crate::ncdirect_flush].*
pub fn flush(&self) -> NcResult {
unsafe { crate::ncdirect_flush(self) }
pub fn flush(&self) -> NcResult<()> {
error![unsafe { crate::ncdirect_flush(self) }]
}
/// Displays an image using the specified blitter and scaling.
@ -80,8 +84,10 @@ impl NcDirect {
align: NcAlign,
blitter: NcBlitter,
scale: NcScale,
) -> NcResult {
unsafe { crate::ncdirect_render_image(self, cstring![filename], align, blitter, scale) }
) -> NcResult<()> {
error![unsafe {
crate::ncdirect_render_image(self, cstring![filename], align, blitter, scale)
}]
}
}
@ -90,89 +96,94 @@ impl NcDirect {
/// Sets the foreground [NcPaletteIndex].
///
/// *C style function: [ncdirect_fg_palindex()][crate::ncdirect_fg_palindex].*
pub fn fg_palindex(&mut self, index: NcPaletteIndex) -> NcResult {
unsafe { crate::ncdirect_fg_palindex(self, index as i32) }
pub fn fg_palindex(&mut self, index: NcPaletteIndex) -> NcResult<()> {
error![unsafe { crate::ncdirect_fg_palindex(self, index as i32) }]
}
/// Sets the background [NcPaletteIndex].
///
/// *C style function: [ncdirect_bg_palindex()][crate::ncdirect_bg_palindex].*
pub fn bg_palindex(&mut self, index: NcPaletteIndex) -> NcResult {
unsafe { crate::ncdirect_bg_palindex(self, index as i32) }
pub fn bg_palindex(&mut self, index: NcPaletteIndex) -> NcResult<()> {
error![unsafe { crate::ncdirect_bg_palindex(self, index as i32) }]
}
/// Returns the number of simultaneous colors claimed to be supported,
/// or 1 if there is no color support.
/// Returns the number of simultaneous colors claimed to be supported.
///
/// Note that several terminal emulators advertise more colors than they
/// actually support, downsampling internally.
///
/// *C style function: [ncdirect_palette_size()][crate::ncdirect_palette_size].*
pub fn palette_size(&self) -> u32 {
unsafe { crate::ncdirect_palette_size(self) }
pub fn palette_size(&self) -> NcResult<u32> {
let res = unsafe { crate::ncdirect_palette_size(self) };
if res == 1 {
return Err(NcError::new(1, "No color support"));
}
Ok(res)
}
/// Sets the foreground [NcRgb].
///
/// *C style function: [ncdirect_fg_rgb()][crate::ncdirect_fg_rgb].*
pub fn fg_rgb(&mut self, rgb: NcRgb) -> NcResult {
unsafe { crate::ncdirect_fg_rgb(self, rgb) }
pub fn fg_rgb(&mut self, rgb: NcRgb) -> NcResult<()> {
error![unsafe { crate::ncdirect_fg_rgb(self, rgb) }]
}
/// Sets the background [NcRgb].
///
/// *C style function: [ncdirect_bg_rgb()][crate::ncdirect_bg_rgb].*
pub fn bg_rgb(&mut self, rgb: NcRgb) -> NcResult {
unsafe { crate::ncdirect_bg_rgb(self, rgb) }
pub fn bg_rgb(&mut self, rgb: NcRgb) -> NcResult<()> {
error![unsafe { crate::ncdirect_bg_rgb(self, rgb) }]
}
/// Sets the foreground [NcColor] components.
///
/// *C style function: [ncdirect_fg_rgb8()][crate::ncdirect_fg_rgb8].*
pub fn fg_rgb8(&mut self, red: NcColor, green: NcColor, blue: NcColor) -> NcResult {
crate::ncdirect_fg_rgb8(self, red, green, blue)
pub fn fg_rgb8(&mut self, red: NcColor, green: NcColor, blue: NcColor) -> NcResult<()> {
error![crate::ncdirect_fg_rgb8(self, red, green, blue)]
}
/// Sets the background [NcColor] components.
///
/// *C style function: [ncdirect_bg_rgb()][crate::ncdirect_bg_rgb].*
pub fn bg_rgb8(&mut self, red: NcColor, green: NcColor, blue: NcColor) -> NcResult {
crate::ncdirect_bg_rgb8(self, red, green, blue)
pub fn bg_rgb8(&mut self, red: NcColor, green: NcColor, blue: NcColor) -> NcResult<()> {
let res = crate::ncdirect_bg_rgb8(self, red, green, blue);
error![res]
}
/// Removes the specified styles.
///
/// *C style function: [ncdirect_styles_off()][crate::ncdirect_styles_off].*
pub fn styles_off(&mut self, stylebits: NcStyleMask) -> NcResult {
unsafe { crate::ncdirect_styles_off(self, stylebits.into()) }
pub fn styles_off(&mut self, stylebits: NcStyleMask) -> NcResult<()> {
let res = unsafe { crate::ncdirect_styles_off(self, stylebits.into()) };
error![res]
}
/// Adds the specified styles.
///
/// *C style function: [ncdirect_styles_on()][crate::ncdirect_styles_on].*
pub fn styles_on(&mut self, stylebits: NcStyleMask) -> NcResult {
unsafe { crate::ncdirect_styles_on(self, stylebits.into()) }
pub fn styles_on(&mut self, stylebits: NcStyleMask) -> NcResult<()> {
error![unsafe { crate::ncdirect_styles_on(self, stylebits.into()) }]
}
/// Sets just the specified styles.
///
/// *C style function: [ncdirect_styles_set()][crate::ncdirect_styles_set].*
pub fn styles_set(&mut self, stylebits: NcStyleMask) -> NcResult {
unsafe { crate::ncdirect_styles_set(self, stylebits.into()) }
pub fn styles_set(&mut self, stylebits: NcStyleMask) -> NcResult<()> {
error![unsafe { crate::ncdirect_styles_set(self, stylebits.into()) }]
}
/// Indicates to use the "default color" for the foreground.
///
/// *C style function: [ncdirect_fg_default()][crate::ncdirect_fg_default].*
pub fn fg_default(&mut self) -> NcResult {
unsafe { crate::ncdirect_fg_default(self) }
pub fn fg_default(&mut self) -> NcResult<()> {
error![unsafe { crate::ncdirect_fg_default(self) }]
}
/// Indicates to use the "default color" for the background.
///
/// *C style function: [ncdirect_bg_default()][crate::ncdirect_bg_default].*
pub fn bg_default(&mut self) -> NcResult {
unsafe { crate::ncdirect_bg_default(self) }
pub fn bg_default(&mut self) -> NcResult<()> {
error![unsafe { crate::ncdirect_bg_default(self) }]
}
}
@ -199,64 +210,80 @@ impl NcDirect {
/// Disables the terminal's cursor, if supported.
///
/// *C style function: [ncdirect_cursor_disable()][crate::ncdirect_cursor_disable].*
pub fn cursor_disable(&mut self) -> NcResult {
unsafe { crate::ncdirect_cursor_disable(self) }
pub fn cursor_disable(&mut self) -> NcResult<()> {
error![unsafe { crate::ncdirect_cursor_disable(self) }]
}
/// Enables the terminal's cursor, if supported.
///
/// *C style function: [ncdirect_cursor_enable()][crate::ncdirect_cursor_enable].*
pub fn cursor_enable(&mut self) -> NcResult {
unsafe { crate::ncdirect_cursor_enable(self) }
pub fn cursor_enable(&mut self) -> NcResult<()> {
error![unsafe { crate::ncdirect_cursor_enable(self) }]
}
/// Moves the cursor down, `num` rows.
///
/// *C style function: [ncdirect_cursor_down()][crate::ncdirect_cursor_down].*
pub fn cursor_down(&mut self, num: NcDimension) -> NcResult {
unsafe { crate::ncdirect_cursor_down(self, num as i32) }
pub fn cursor_down(&mut self, num: NcDimension) -> NcResult<()> {
let res = unsafe { crate::ncdirect_cursor_down(self, num as i32) };
if res == NCRESULT_OK {
return Ok(());
}
Err(NcError::new(res, ""))
}
/// Moves the cursor left, `num` columns.
///
/// *C style function: [ncdirect_cursor_left()][crate::ncdirect_cursor_left].*
pub fn cursor_left(&mut self, num: NcDimension) -> NcResult {
unsafe { crate::ncdirect_cursor_left(self, num as i32) }
pub fn cursor_left(&mut self, num: NcDimension) -> NcResult<()> {
let res = unsafe { crate::ncdirect_cursor_left(self, num as i32) };
if res == NCRESULT_OK {
return Ok(());
}
Err(NcError::new(res, ""))
}
/// Moves the cursor right, `num` columns.
///
/// *C style function: [ncdirect_cursor_right()][crate::ncdirect_cursor_right].*
pub fn cursor_right(&mut self, num: NcDimension) -> NcResult {
unsafe { crate::ncdirect_cursor_right(self, num as i32) }
pub fn cursor_right(&mut self, num: NcDimension) -> NcResult<()> {
let res = unsafe { crate::ncdirect_cursor_right(self, num as i32) };
if res == NCRESULT_OK {
return Ok(());
}
Err(NcError::new(res, ""))
}
/// Moves the cursor up, `num` rows.
///
/// *C style function: [ncdirect_cursor_up()][crate::ncdirect_cursor_up].*
pub fn cursor_up(&mut self, num: NcDimension) -> NcResult {
unsafe { crate::ncdirect_cursor_up(self, num as i32) }
pub fn cursor_up(&mut self, num: NcDimension) -> NcResult<()> {
let res = unsafe { crate::ncdirect_cursor_up(self, num as i32) };
if res == NCRESULT_OK {
return Ok(());
}
Err(NcError::new(res, ""))
}
/// Moves the cursor in direct mode to the specified row, column.
///
/// *C style function: [ncdirect_cursor_move_yx()][crate::ncdirect_cursor_move_yx].*
pub fn cursor_move_yx(&mut self, y: NcDimension, x: NcDimension) -> NcResult {
unsafe { crate::ncdirect_cursor_move_yx(self, y as i32, x as i32) }
pub fn cursor_move_yx(&mut self, y: NcDimension, x: NcDimension) -> NcResult<()> {
error![unsafe { crate::ncdirect_cursor_move_yx(self, y as i32, x as i32) }]
}
/// Moves the cursor in direct mode to the specified row.
///
/// *(No equivalent C style function)*
pub fn cursor_move_y(&mut self, y: NcDimension) -> NcResult {
unsafe { crate::ncdirect_cursor_move_yx(self, y as i32, -1) }
pub fn cursor_move_y(&mut self, y: NcDimension) -> NcResult<()> {
error![unsafe { crate::ncdirect_cursor_move_yx(self, y as i32, -1) }]
}
/// Moves the cursor in direct mode to the specified column.
///
/// *(No equivalent C style function)*
pub fn cursor_move_x(&mut self, x: NcDimension) -> NcResult {
unsafe { crate::ncdirect_cursor_move_yx(self, -1, x as i32) }
pub fn cursor_move_x(&mut self, x: NcDimension) -> NcResult<()> {
error![unsafe { crate::ncdirect_cursor_move_yx(self, -1, x as i32) }]
}
/// Gets the cursor position, when supported.
@ -280,8 +307,8 @@ impl NcDirect {
/// The depth of this stack, and indeed its existence, is terminal-dependent.
///
/// *C style function: [ncdirect_cursor_push()][crate::ncdirect_cursor_push].*
pub fn cursor_push(&mut self) -> NcResult {
unsafe { crate::ncdirect_cursor_push(self) }
pub fn cursor_push(&mut self) -> NcResult<()> {
error![unsafe { crate::ncdirect_cursor_push(self) }]
}
/// Pops the cursor location from the terminal's stack.
@ -289,8 +316,8 @@ impl NcDirect {
/// The depth of this stack, and indeed its existence, is terminal-dependent.
///
/// *C style function: [ncdirect_cursor_pop()][crate::ncdirect_cursor_pop].*
pub fn cursor_pop(&mut self) -> NcResult {
unsafe { crate::ncdirect_cursor_pop(self) }
pub fn cursor_pop(&mut self) -> NcResult<()> {
error![unsafe { crate::ncdirect_cursor_pop(self) }]
}
/// Gets the current number of rows.
@ -390,18 +417,19 @@ impl NcDirect {
/// with stdin (but it might be!).
///
/// *C style function: [ncdirect_inputready_fd()][crate::ncdirect_inputready_fd].*
pub fn inputready_fd(&mut self) -> NcResult {
unsafe { crate::ncdirect_inputready_fd(self) }
pub fn inputready_fd(&mut self) -> NcResult<()> {
error![unsafe { crate::ncdirect_inputready_fd(self) }]
}
/// Outputs the `string` according to the `channels`.
/// Outputs the `string` according to the `channels`, and
/// returns the total number of characters written on success.
///
/// Note that it does not explicitly flush output buffers, so it will not
/// necessarily be immediately visible.
///
/// *C style function: [ncdirect_putstr()][crate::ncdirect_putstr].*
pub fn putstr(&mut self, channels: NcChannelPair, string: &str) -> NcResult {
unsafe { crate::ncdirect_putstr(self, channels, cstring![string]) }
pub fn putstr(&mut self, channels: NcChannelPair, string: &str) -> NcResult<()> {
error![unsafe { crate::ncdirect_putstr(self, channels, cstring![string]) }]
}
/// Draws a box with its upper-left corner at the current cursor position,
@ -425,8 +453,8 @@ impl NcDirect {
y_len: NcDimension,
x_len: NcDimension,
ctlword: u32,
) -> NcResult {
unsafe {
) -> NcResult<()> {
error![unsafe {
let wchars = core::mem::transmute(wchars);
crate::ncdirect_box(
self,
@ -439,7 +467,7 @@ impl NcDirect {
x_len as i32,
ctlword,
)
}
}]
}
/// NcDirect.[box()][NcDirect#method.box] with the double box-drawing characters.
@ -454,10 +482,10 @@ impl NcDirect {
y_len: NcDimension,
x_len: NcDimension,
ctlword: u32,
) -> NcResult {
unsafe {
) -> NcResult<()> {
error![unsafe {
crate::ncdirect_double_box(self, ul, ur, ll, lr, y_len as i32, x_len as i32, ctlword)
}
}]
}
/// NcDirect.[box()][NcDirect#method.box] with the rounded box-drawing characters.
@ -472,10 +500,10 @@ impl NcDirect {
y_len: NcDimension,
x_len: NcDimension,
ctlword: u32,
) -> NcResult {
unsafe {
) -> NcResult<()> {
error![unsafe {
crate::ncdirect_rounded_box(self, ul, ur, ll, lr, y_len as i32, x_len as i32, ctlword)
}
}]
}
/// Draws horizontal lines using the specified [NcChannelPair]s, interpolating
/// between them as we go.
@ -495,8 +523,8 @@ impl NcDirect {
len: NcDimension,
h1: NcChannelPair,
h2: NcChannelPair,
) -> NcResult {
unsafe { crate::ncdirect_hline_interp(self, &(*egc as i8), len as i32, h1, h2) }
) -> NcResult<()> {
error![unsafe { crate::ncdirect_hline_interp(self, &(*egc as i8), len as i32, h1, h2) }]
}
/// Draws horizontal lines using the specified [NcChannelPair]s, interpolating
@ -517,7 +545,7 @@ impl NcDirect {
len: NcDimension,
h1: NcChannelPair,
h2: NcChannelPair,
) -> NcResult {
unsafe { crate::ncdirect_vline_interp(self, &(*egc as i8), len as i32, h1, h2) }
) -> NcResult<()> {
error![unsafe { crate::ncdirect_vline_interp(self, &(*egc as i8), len as i32, h1, h2) }]
}
}

View File

@ -2,7 +2,7 @@
use core::ptr::null;
use crate::{NcColor, NcDirect, NcInput, NcResult, NcRgb, NcSignalSet, NcTime};
use crate::{NcColor, NcDirect, NcInput, NcIntResult, NcRgb, NcSignalSet, NcTime};
///
/// If no event is ready, returns 0.
@ -42,7 +42,7 @@ pub fn ncdirect_fg_rgb8(
red: NcColor,
green: NcColor,
blue: NcColor,
) -> NcResult {
) -> NcIntResult {
let rgb = (red as NcRgb) << 16 | (green as NcRgb) << 8 | blue as NcRgb;
unsafe { crate::ncdirect_fg_rgb(ncd, rgb) }
}
@ -56,7 +56,7 @@ pub fn ncdirect_bg_rgb8(
red: NcColor,
green: NcColor,
blue: NcColor,
) -> NcResult {
) -> NcIntResult {
let rgb = (red as NcRgb) << 16 | (green as NcRgb) << 8 | blue as NcRgb;
unsafe { crate::ncdirect_bg_rgb(ncd, rgb) }
}

View File

@ -1,19 +1,57 @@
//! `NcResult` for error handling
//! Error handling with `Error`, `NcResult` & `NcIntResult` for error handling
/// `i32` value used to return errors, when value < 0, (usually -1).
use std::{self, error, fmt};
/// The [`i32`] value used to return errors by the underlying C API.
///
/// A value < 0 means error, (usually -1).
///
/// # Defined constants:
///
/// - [`NCRESULT_OK`]
/// - [`NCRESULT_ERR`]
/// - [`NCRESULT_MAX`]
pub type NcResult = i32;
pub type NcIntResult = i32;
/// OK value, for the functions that return [`NcResult`].
/// OK value, for the functions that return [`NcIntResult`].
pub const NCRESULT_OK: i32 = 0;
/// ERROR value, for the functions that return an [`NcResult`].
/// ERROR value, for the functions that return an [`NcIntResult`].
pub const NCRESULT_ERR: i32 = -1;
/// MAX value, for the functions that return [`NcResult`].
/// MAX value, for the functions that return [`NcIntResult`].
pub const NCRESULT_MAX: i32 = i32::MAX;
/// The error type for the Rust methods API.
#[derive(Debug, Clone)]
pub struct NcError {
pub msg: String,
/// [NcIntError].
pub int: i32,
}
impl fmt::Display for NcError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "NcIntError: {}. {}", self.int, self.msg)
}
}
impl error::Error for NcError {
fn description(&self) -> &str {
&self.msg
}
}
impl NcError {
/// New NcError.
pub fn new(int: NcIntResult, msg: &str) -> Self {
Self {
int,
msg: msg.to_string(),
}
}
}
/// The result type for the Rust methods API.
pub type NcResult<T> = Result<T, NcError>;

View File

@ -11,7 +11,7 @@
use std::ffi::c_void;
use crate::{NcPlane, NcResult, NcTime, Notcurses};
use crate::{NcIntResult, NcPlane, NcTime, Notcurses};
/// Called for each fade iteration on the NcPlane.
///
@ -20,7 +20,7 @@ use crate::{NcPlane, NcResult, NcTime, Notcurses};
///
/// The recommended absolute display time target is passed in 'tspec'.
pub type NcFadeCb = Option<
unsafe extern "C" fn(*mut Notcurses, *mut NcPlane, *const NcTime, *mut c_void) -> NcResult,
unsafe extern "C" fn(*mut Notcurses, *mut NcPlane, *const NcTime, *mut c_void) -> NcIntResult,
>;
/// Context for a palette fade operation

View File

@ -75,25 +75,24 @@
//! ## Limitations of this library
//!
//! There are several common patterns in Rust that this library doesn't employ,
//! and focuses instead on remaining at a very close distance to the C API.
//! and focuses instead on remaining at a closer distance to the C API.
//!
//! 1. There are no Drop trait implementations, therefore you must manually stop
//! each context before it goes out of scope ([Notcurses], [NcDirect]), and
//! should manually destroy [NcPlane]s, [NcMenu]s… when no longer needed.
//!
//! 2. Instead of handling errors with `Result` (as customary in Rust),
//! several functions return an [NcResult] with a value of [NCRESULT_ERR],
//! (as customary in C), or [NCRESULT_OK].
//! 2. The C style functions handle errors by the means of returning an i32 value
//! aliased to [NcIntResult]. But the Rust style methods handle errors more
//! idiomatically using [NcResult] and [NcError].
//!
//! The [notcurses]() crate overcomes this limitations by using higher level
//! abstractions, while pulling itself apart from the C API, in ways this
//! The [notcurses]() crate will use higher level abstractions in ways this
//! library can not do.
//!
//! ### Things this library does do
//!
//! - Type aliases every underlying C type to leverage type checking.
//! - Renames types to enforce regularity and consistency. (e.g. [NcCell])
//! - Has handy macros like [sleep], [rsleep] & [cstring].
//! - Has handy macros for common tasks like [sleep], [cstring] & [error].
//!
//! ## The `notcurses` C API docs
//!

View File

@ -15,7 +15,7 @@ macro_rules! sleep {
#[macro_export]
macro_rules! rsleep {
($nc:expr, $ms:expr) => {{
let mut res: NcResult = 0;
let mut res: crate::NcIntResult = 0;
unsafe {
res = crate::notcurses_render($nc);
}
@ -42,3 +42,27 @@ macro_rules! printf {
unsafe { libc::printf(cstring![$s], $($opt),*) }
};
}
/// Returns Ok(`$ok`) if `$res` >= [NCRESULT_OK][crate::NCRESULT_OK],
/// otherwise returns
/// Err([NcError][crate::NcError]::[new][crate::NcError#method.new](`$res`, `$msg`)).
///
/// `$ok` & `$msg` are optional. By default they will be the unit
/// type `()`, and an empty `&str` `""`, respectively.
///
#[macro_export]
macro_rules! error {
($res:expr, $ok:expr, $msg:expr) => {
if $res >= crate::NCRESULT_OK {
return Ok($ok);
} else {
return Err(crate::NcError::new($res, $msg));
}
};
($res:expr, $ok:expr) => {
error![$res, $ok, ""];
};
($res:expr) => {
error![$res, (), ""];
};
}

View File

@ -5,7 +5,7 @@ use std::ffi::CStr;
use crate::{
cstring, notcurses_init, NcAlign, NcBlitter, NcChannelPair, NcDimension, NcEgc, NcFile,
NcInput, NcLogLevel, NcPlane, NcResult, NcScale, NcSignalSet, NcStats, NcStyleMask, NcTime,
NcInput, NcIntResult, NcLogLevel, NcPlane, NcScale, NcSignalSet, NcStats, NcStyleMask, NcTime,
Notcurses, NotcursesOptions, NCOPTION_NO_ALTERNATE_SCREEN, NCOPTION_SUPPRESS_BANNERS,
NCRESULT_ERR, NCRESULT_OK,
};
@ -126,7 +126,7 @@ impl Notcurses {
/// [NCALIGN_UNALIGNED][crate::NCALIGN_UNALIGNED] or invalid [NcAlign].
///
/// *C style function: [notcurses_align()][crate::notcurses_align].*
pub fn align(availcols: NcDimension, align: NcAlign, cols: NcDimension) -> NcResult {
pub fn align(availcols: NcDimension, align: NcAlign, cols: NcDimension) -> NcIntResult {
crate::notcurses_align(availcols, align, cols)
}
@ -226,7 +226,7 @@ impl Notcurses {
/// Immediate effect (no need for a call to notcurses_render()).
///
/// *C style function: [notcurses_cursor_disable()][crate::notcurses_cursor_disable].*
pub fn cursor_disable(&mut self) -> NcResult {
pub fn cursor_disable(&mut self) -> NcIntResult {
unsafe { crate::notcurses_cursor_disable(self) }
}
@ -236,7 +236,7 @@ impl Notcurses {
/// It is an error if `y`, `x` lies outside the standard plane.
///
/// *C style function: [notcurses_cursor_enable()][crate::notcurses_cursor_enable].*
pub fn cursor_enable(&mut self, y: NcDimension, x: NcDimension) -> NcResult {
pub fn cursor_enable(&mut self, y: NcDimension, x: NcDimension) -> NcIntResult {
unsafe { crate::notcurses_cursor_enable(self, y as i32, x as i32) }
}
@ -330,7 +330,7 @@ impl Notcurses {
/// with stdin (but it might be!).
///
/// *C style function: [notcurses_inputready_fd()][crate::notcurses_inputready_fd].*
pub fn inputready_fd(&mut self) -> NcResult {
pub fn inputready_fd(&mut self) -> NcIntResult {
unsafe { crate::notcurses_inputready_fd(self) }
}
@ -352,7 +352,7 @@ impl Notcurses {
/// or there can be four numbers separated by commas.
///
/// *C style function: [notcurses_lex_margins()][crate::notcurses_lex_margins].*
pub fn lex_margins(op: &str, options: &mut NotcursesOptions) -> NcResult {
pub fn lex_margins(op: &str, options: &mut NotcursesOptions) -> NcIntResult {
unsafe { crate::notcurses_lex_margins(cstring![op], options) }
}
@ -372,7 +372,7 @@ impl Notcurses {
/// SIGINT (^C), SIGQUIT (^), and SIGTSTP (^Z). They are enabled by default.
///
/// *C style function: [notcurses_linesigs_disable()][crate::notcurses_linesigs_disable].*
pub fn linesigs_disable(&mut self) -> NcResult {
pub fn linesigs_disable(&mut self) -> NcIntResult {
unsafe { crate::notcurses_linesigs_disable(self) }
}
@ -380,7 +380,7 @@ impl Notcurses {
/// SIGINT (^C), SIGQUIT (^), and SIGTSTP (^Z), if disabled.
///
/// *C style function: [notcurses_linesigs_enable()][crate::notcurses_linesigs_enable].*
pub fn linesigs_enable(&mut self) -> NcResult {
pub fn linesigs_enable(&mut self) -> NcIntResult {
unsafe { crate::notcurses_linesigs_enable(self) }
}
@ -389,7 +389,7 @@ impl Notcurses {
/// Any events in the input queue can still be delivered.
///
/// *C style function: [notcurses_mouse_disable()][crate::notcurses_mouse_disable].*
pub fn mouse_disable(&mut self) -> NcResult {
pub fn mouse_disable(&mut self) -> NcIntResult {
unsafe { crate::notcurses_mouse_disable(self) }
}
@ -400,7 +400,7 @@ impl Notcurses {
/// published to [getc()][Notcurses#method.getc].
///
/// *C style function: [notcurses_mouse_enable()][crate::notcurses_mouse_enable].*
pub fn mouse_enable(&mut self) -> NcResult {
pub fn mouse_enable(&mut self) -> NcIntResult {
unsafe { crate::notcurses_mouse_enable(self) }
}
@ -425,15 +425,15 @@ impl Notcurses {
///
/// *C style function: [notcurses_refresh()][crate::notcurses_refresh].*
//
// TODO: try returning Result<(NcDimension, NcDimension), NcResult>
pub fn refresh(&mut self, y: &mut NcDimension, x: &mut NcDimension) -> NcResult {
// TODO: try returning Result<(NcDimension, NcDimension), NcIntResult>
pub fn refresh(&mut self, y: &mut NcDimension, x: &mut NcDimension) -> NcIntResult {
unsafe { crate::notcurses_refresh(self, &mut (*y as i32), &mut (*x as i32)) }
}
/// Renders and rasterizes the standard pile in one shot. Blocking call.
///
/// *C style function: [notcurses_render()][crate::notcurses_render].*
pub fn render(&mut self) -> NcResult {
pub fn render(&mut self) -> NcIntResult {
unsafe { crate::notcurses_render(self) }
}
@ -449,7 +449,7 @@ impl Notcurses {
/// *C style function: [notcurses_render_to_buffer()][crate::notcurses_render_to_buffer].*
//
// CHECK that this works.
pub fn render_to_buffer(&mut self, buffer: &mut Vec<u8>) -> NcResult {
pub fn render_to_buffer(&mut self, buffer: &mut Vec<u8>) -> NcIntResult {
let mut len = buffer.len() as u64;
let mut buf = buffer.as_mut_ptr() as *mut i8;
unsafe { crate::notcurses_render_to_buffer(self, &mut buf, &mut len) }
@ -461,7 +461,7 @@ impl Notcurses {
/// nothing will be written.
///
/// *C style function: [notcurses_render_to_file()][crate::notcurses_render_to_file].*
pub fn render_to_file(&mut self, fp: &mut NcFile) -> NcResult {
pub fn render_to_file(&mut self, fp: &mut NcFile) -> NcIntResult {
unsafe { crate::notcurses_render_to_file(self, fp.as_nc_ptr()) }
}
@ -542,7 +542,7 @@ impl Notcurses {
/// Destroys the Notcurses context.
///
/// *C style function: [notcurses_stop()][crate::notcurses_stop].*
pub fn stop(&mut self) -> NcResult {
pub fn stop(&mut self) -> NcIntResult {
unsafe { crate::notcurses_stop(self) }
}

View File

@ -1,6 +1,6 @@
//! `NcPalette` methods and associated functions.
use crate::{NcChannel, NcColor, NcPalette, NcPaletteIndex, NcResult, NcRgb, Notcurses};
use crate::{NcChannel, NcColor, NcIntResult, NcPalette, NcPaletteIndex, NcRgb, Notcurses};
impl NcPalette {
/// New NcPalette.
@ -22,7 +22,7 @@ impl NcPalette {
/// Attempts to configure the terminal with this NcPalette.
///
/// *C style function: [palette256_use()][crate::palette256_use].*
pub fn r#use(&self, nc: &mut Notcurses) -> NcResult {
pub fn r#use(&self, nc: &mut Notcurses) -> NcIntResult {
unsafe { crate::palette256_use(nc, self) }
}

View File

@ -5,8 +5,8 @@ use std::ffi::CStr;
use crate::{
cstring, NcAlign, NcAlphaBits, NcBoxMask, NcCell, NcChannel, NcChannelPair, NcColor,
NcDimension, NcEgc, NcFadeCb, NcOffset, NcPaletteIndex, NcPlane, NcPlaneOptions, NcResizeCb,
NcResult, NcRgb, NcStyleMask, NcTime, Notcurses, NCRESULT_OK,
NcDimension, NcEgc, NcFadeCb, NcIntResult, NcOffset, NcPaletteIndex, NcPlane, NcPlaneOptions,
NcResizeCb, NcRgb, NcStyleMask, NcTime, Notcurses, NCRESULT_OK,
};
/// # NcPlaneOptions Constructors
@ -143,7 +143,7 @@ impl NcPlane {
/// It is an error to attempt to destroy the standard plane.
///
/// *C style function: [ncplane_destroy()][crate::ncplane_destroy].*
pub fn destroy(&mut self) -> NcResult {
pub fn destroy(&mut self) -> NcIntResult {
unsafe { crate::ncplane_destroy(self) }
}
}
@ -170,14 +170,14 @@ impl NcPlane {
/// Sets the foreground [NcAlphaBits] from this NcPlane.
///
/// *C style function: [ncplane_set_fg_alpha()][crate::ncplane_set_fg_alpha].*
pub fn set_fg_alpha(&mut self, alpha: NcAlphaBits) -> NcResult {
pub fn set_fg_alpha(&mut self, alpha: NcAlphaBits) -> NcIntResult {
unsafe { crate::ncplane_set_fg_alpha(self, alpha as i32) }
}
/// Sets the background [NcAlphaBits] for this NcPlane.
///
/// *C style function: [ncplane_set_bg_alpha()][crate::ncplane_set_bg_alpha].*
pub fn set_bg_alpha(&mut self, alpha: NcAlphaBits) -> NcResult {
pub fn set_bg_alpha(&mut self, alpha: NcAlphaBits) -> NcIntResult {
unsafe { crate::ncplane_set_bg_alpha(self, alpha as i32) }
}
}
@ -246,7 +246,7 @@ impl NcPlane {
ur: NcChannelPair,
ll: NcChannelPair,
lr: NcChannelPair,
) -> NcResult {
) -> NcIntResult {
unsafe { crate::ncplane_stain(self, y_stop as i32, x_stop as i32, ul, ur, ll, lr) }
}
}
@ -406,7 +406,7 @@ impl NcPlane {
y_stop: NcDimension,
x_stop: NcDimension,
stylemask: NcStyleMask,
) -> NcResult {
) -> NcIntResult {
unsafe { crate::ncplane_format(self, y_stop as i32, x_stop as i32, stylemask as u32) }
}
@ -497,7 +497,7 @@ impl NcPlane {
///
/// *C style function: [ncplane_at_cursor_cell()][crate::ncplane_at_cursor_cell].*
#[inline]
pub fn at_cursor_cell(&mut self, cell: &mut NcCell) -> NcResult {
pub fn at_cursor_cell(&mut self, cell: &mut NcCell) -> NcIntResult {
crate::ncplane_at_cursor_cell(self, cell)
}
@ -532,7 +532,7 @@ impl NcPlane {
y: NcDimension,
x: NcDimension,
cell: &mut NcCell,
) -> NcResult {
) -> NcIntResult {
crate::ncplane_at_yx_cell(self, y, x, cell)
}
@ -541,7 +541,7 @@ impl NcPlane {
/// The reference is invalidated if this NcPlane is destroyed.
///
/// *C style function: [ncplane_base()][crate::ncplane_base].*
pub fn base(&mut self, cell: &mut NcCell) -> NcResult {
pub fn base(&mut self, cell: &mut NcCell) -> NcIntResult {
unsafe { crate::ncplane_base(self, cell) }
}
@ -561,7 +561,7 @@ impl NcPlane {
egc: &NcEgc,
stylemask: NcStyleMask,
channels: NcChannelPair,
) -> NcResult {
) -> NcIntResult {
unsafe { crate::ncplane_set_base(self, &(*egc as i8), stylemask as u32, channels) }
}
@ -577,7 +577,7 @@ impl NcPlane {
/// *C style function: [ncplane_set_base_cell()][crate::ncplane_set_base_cell].*
//
// FIXME: documentation: https://github.com/dankamongmen/notcurses/issues/1238
pub fn set_base_cell(&mut self, cell: &NcCell) -> NcResult {
pub fn set_base_cell(&mut self, cell: &NcCell) -> NcIntResult {
unsafe { crate::ncplane_set_base_cell(self, cell) }
}
@ -639,7 +639,7 @@ impl NcPlane {
/// On failure, -1 is returned.
///
/// *C style function: [ncplane_putc_yx()][crate::ncplane_putc_yx].*
pub fn putc_yx(&mut self, y: NcDimension, x: NcDimension, cell: &NcCell) -> NcResult {
pub fn putc_yx(&mut self, y: NcDimension, x: NcDimension, cell: &NcCell) -> NcIntResult {
unsafe { crate::ncplane_putc_yx(self, y as i32, x as i32, cell) }
}
@ -650,21 +650,21 @@ impl NcPlane {
/// On success, returns the number of columns the cursor was advanced.
///
/// *C style function: [ncplane_putc()][crate::ncplane_putc].*
pub fn putc(&mut self, cell: &NcCell) -> NcResult {
pub fn putc(&mut self, cell: &NcCell) -> NcIntResult {
crate::ncplane_putc(self, cell)
}
/// Calls [putchar_yx][NcPlane#method.putchar_yx] at the current cursor location.
///
/// *C style function: [ncplane_putchar()][crate::ncplane_putchar].*
pub fn putchar(&mut self, ch: char) -> NcResult {
pub fn putchar(&mut self, ch: char) -> NcIntResult {
crate::ncplane_putchar(self, ch)
}
// TODO: call put_egc
// /// Replaces the [NcEgc][crate::NcEgc] to the current location, but retain
// /// the styling. The current styling of the plane will not be changed.
// pub fn putchar_stained(&mut self, y: NcDimension, x: NcDimension, ch: char) -> NcResult {
// pub fn putchar_stained(&mut self, y: NcDimension, x: NcDimension, ch: char) -> NcIntResult {
// crate::ncplane_putchar_stained(self, ch)
// }
@ -672,7 +672,7 @@ impl NcPlane {
/// The current styling of the plane will not be changed.
///
/// *C style function: [ncplane_putchar_yx()][crate::ncplane_putchar_yx].*
pub fn putchar_yx(&mut self, y: NcDimension, x: NcDimension, ch: char) -> NcResult {
pub fn putchar_yx(&mut self, y: NcDimension, x: NcDimension, ch: char) -> NcIntResult {
crate::ncplane_putchar_yx(self, y, x, ch)
}
@ -687,7 +687,7 @@ impl NcPlane {
///
/// *C style function: [ncplane_putstr()][crate::ncplane_putstr].*
#[inline]
pub fn putstr(&mut self, string: &str) -> NcResult {
pub fn putstr(&mut self, string: &str) -> NcIntResult {
crate::ncplane_putstr(self, string)
}
@ -695,7 +695,7 @@ impl NcPlane {
/// cursor to the beginning of the next row.
///
/// *(No equivalent C style function)*
pub fn putstrln(&mut self, string: &str) -> NcResult {
pub fn putstrln(&mut self, string: &str) -> NcIntResult {
let res = crate::ncplane_putstr(self, string);
if res < NCRESULT_OK {
return res;
@ -713,7 +713,7 @@ impl NcPlane {
/// The current styling of the plane will not be changed.
///
/// *C style function: [ncplane_putstr_stained()][crate::ncplane_putstr_stained].*
pub fn putstr_stained(&mut self, string: &str) -> NcResult {
pub fn putstr_stained(&mut self, string: &str) -> NcIntResult {
unsafe { crate::ncplane_putstr_stained(self, cstring![string]) }
}
@ -729,7 +729,7 @@ impl NcPlane {
/// columns which were written before the error.
///
/// *C style function: [ncplane_putstr_yx()][crate::ncplane_putstr_yx].*
pub fn putstr_yx(&mut self, y: NcDimension, x: NcDimension, string: &str) -> NcResult {
pub fn putstr_yx(&mut self, y: NcDimension, x: NcDimension, string: &str) -> NcIntResult {
unsafe { crate::ncplane_putstr_yx(self, y as i32, x as i32, cstring![string]) }
}
}
@ -793,7 +793,7 @@ impl NcPlane {
/// *C style function: [ncplane_move_yx()][crate::ncplane_move_yx].*
//
// CHECK: whether a negative offset is valid
pub fn move_yx(&mut self, y: NcOffset, x: NcOffset) -> NcResult {
pub fn move_yx(&mut self, y: NcOffset, x: NcOffset) -> NcIntResult {
unsafe { crate::ncplane_move_yx(self, y, x) }
}
@ -825,7 +825,7 @@ impl NcPlane {
/// already in the desired location. Both planes must not be the same.
///
/// *C style function: [ncplane_move_above()][crate::ncplane_move_above].*
pub fn move_above(&mut self, above: &mut NcPlane) -> NcResult {
pub fn move_above(&mut self, above: &mut NcPlane) -> NcIntResult {
unsafe { crate::ncplane_move_above(self, above) }
}
@ -835,7 +835,7 @@ impl NcPlane {
/// already in the desired location. Both planes must not be the same.
///
/// *C style function: [ncplane_move_below()][crate::ncplane_move_below].*
pub fn move_below(&mut self, below: &mut NcPlane) -> NcResult {
pub fn move_below(&mut self, below: &mut NcPlane) -> NcIntResult {
unsafe { crate::ncplane_move_below(self, below) }
}
@ -861,7 +861,7 @@ impl NcPlane {
len_x: NcDimension,
target_y: NcDimension,
target_x: NcDimension,
) -> NcResult {
) -> NcIntResult {
unsafe {
crate::ncplane_mergedown(
source,
@ -888,7 +888,7 @@ impl NcPlane {
//
// TODO: maybe create a reversed method, and/or an associated function,
// for `mergedown` too.
pub fn mergedown_simple(&mut self, source: &NcPlane) -> NcResult {
pub fn mergedown_simple(&mut self, source: &NcPlane) -> NcIntResult {
unsafe { crate::ncplane_mergedown_simple(source, self) }
}
@ -947,7 +947,7 @@ impl NcPlane {
/// rendered (doing so will likely result in a blank screen).
///
/// *C style function: [ncpile_rasterize()][crate::ncpile_rasterize].*
pub fn rasterize<'a>(&mut self) -> NcResult {
pub fn rasterize<'a>(&mut self) -> NcIntResult {
unsafe { crate::ncpile_rasterize(self) }
}
@ -956,7 +956,7 @@ impl NcPlane {
/// To actually write out the render, call ncpile_rasterize().
///
/// *C style function: [ncpile_render()][crate::ncpile_render].*
pub fn render<'a>(&mut self) -> NcResult {
pub fn render<'a>(&mut self) -> NcIntResult {
unsafe { crate::ncpile_render(self) }
}
@ -1021,14 +1021,14 @@ impl NcPlane {
/// and the cursor position will remain unchanged.
///
/// *C style function: [ncplane_cursor_move_yx()][crate::ncplane_cursor_move_yx].*
pub fn cursor_move_yx(&mut self, y: NcDimension, x: NcDimension) -> NcResult {
pub fn cursor_move_yx(&mut self, y: NcDimension, x: NcDimension) -> NcIntResult {
unsafe { crate::ncplane_cursor_move_yx(self, y as i32, x as i32) }
}
/// Moves the cursor to the specified row within this NcPlane.
///
/// *(No equivalent C style function)*
pub fn cursor_move_y(&mut self, y: NcDimension) -> NcResult {
pub fn cursor_move_y(&mut self, y: NcDimension) -> NcIntResult {
let x = self.cursor_x();
unsafe { crate::ncplane_cursor_move_yx(self, y as i32, x as i32) }
}
@ -1036,7 +1036,7 @@ impl NcPlane {
/// Moves the cursor to the specified column within this NcPlane.
///
/// *(No equivalent C style function)*
pub fn cursor_move_x(&mut self, x: NcDimension) -> NcResult {
pub fn cursor_move_x(&mut self, x: NcDimension) -> NcIntResult {
let y = self.cursor_y();
unsafe { crate::ncplane_cursor_move_yx(self, y as i32, x as i32) }
}
@ -1046,7 +1046,7 @@ impl NcPlane {
/// It will error if the target row exceeds the plane dimensions.
///
/// *(No equivalent C style function)*
pub fn cursor_move_rows(&mut self, rows: NcOffset) -> NcResult {
pub fn cursor_move_rows(&mut self, rows: NcOffset) -> NcIntResult {
let (y, x) = self.cursor_yx();
self.cursor_move_yx((y as NcOffset + rows) as NcDimension, x)
}
@ -1056,7 +1056,7 @@ impl NcPlane {
/// It will error if the target column exceeds the plane dimensions.
///
/// *(No equivalent C style function)*
pub fn cursor_move_cols(&mut self, cols: NcOffset) -> NcResult {
pub fn cursor_move_cols(&mut self, cols: NcOffset) -> NcIntResult {
let (y, x) = self.cursor_yx();
self.cursor_move_yx(y, (x as NcOffset + cols) as NcDimension)
}
@ -1073,7 +1073,7 @@ impl NcPlane {
///
/// *C style function: [ncplane_align()][crate::ncplane_align].*
#[inline]
pub fn align(&mut self, align: NcAlign, cols: NcDimension) -> NcResult {
pub fn align(&mut self, align: NcAlign, cols: NcDimension) -> NcIntResult {
crate::ncplane_align(self, align, cols)
}
@ -1163,7 +1163,7 @@ impl NcPlane {
x_off: NcOffset,
y_len: NcDimension,
x_len: NcDimension,
) -> NcResult {
) -> NcIntResult {
unsafe {
crate::ncplane_resize(
self,
@ -1187,7 +1187,7 @@ impl NcPlane {
/// *C style function: [ncplane_resize_realign()][crate::ncplane_resize_realign].*
//
// TODO: suitable for use as an NcResizeCb?
pub fn resize_realign(&mut self) -> NcResult {
pub fn resize_realign(&mut self) -> NcIntResult {
unsafe { crate::ncplane_resize_realign(self) }
}
@ -1196,7 +1196,7 @@ impl NcPlane {
///
/// *C style function: [ncplane_resize_simple()][crate::ncplane_resize_simple].*
#[inline]
pub fn resize_simple(&mut self, y_len: NcDimension, x_len: NcDimension) -> NcResult {
pub fn resize_simple(&mut self, y_len: NcDimension, x_len: NcDimension) -> NcIntResult {
crate::ncplane_resize_simple(self, y_len as u32, x_len as u32)
}
@ -1229,7 +1229,7 @@ impl NcPlane {
/// Use the ncvisual rotation for a more flexible approach.
///
/// *C style function: [ncplane_rotate_cw()][crate::ncplane_rotate_cw].*
pub fn rotate_cw(&mut self) -> NcResult {
pub fn rotate_cw(&mut self) -> NcIntResult {
unsafe { crate::ncplane_rotate_cw(self) }
}
@ -1239,7 +1239,7 @@ impl NcPlane {
/// for more information.
///
/// *C style function: [ncplane_rotate_ccw()][crate::ncplane_rotate_ccw].*
pub fn rotate_ccw(&mut self) -> NcResult {
pub fn rotate_ccw(&mut self) -> NcIntResult {
unsafe { crate::ncplane_rotate_ccw(self) }
}
@ -1340,7 +1340,7 @@ impl NcPlane {
y_stop: NcDimension,
x_stop: NcDimension,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
unsafe {
crate::ncplane_box(
self,
@ -1376,7 +1376,7 @@ impl NcPlane {
y_len: NcDimension,
x_len: NcDimension,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
crate::ncplane_box_sized(self, ul, ur, ll, lr, hline, vline, y_len, x_len, boxmask)
}
@ -1391,7 +1391,7 @@ impl NcPlane {
y_stop: NcDimension,
x_stop: NcDimension,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
crate::ncplane_double_box(self, stylemask, channels, y_stop, x_stop, boxmask)
}
@ -1406,7 +1406,7 @@ impl NcPlane {
y_len: NcDimension,
x_len: NcDimension,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
crate::ncplane_double_box(self, stylemask, channels, y_len, x_len, boxmask)
}
@ -1423,7 +1423,7 @@ impl NcPlane {
hline: &NcCell,
vline: &NcCell,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
crate::ncplane_perimeter(self, ul, ur, ll, lr, hline, vline, boxmask)
}
@ -1436,7 +1436,7 @@ impl NcPlane {
stylemask: NcStyleMask,
channels: NcChannelPair,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
crate::ncplane_perimeter_double(self, stylemask, channels, boxmask)
}
@ -1449,7 +1449,7 @@ impl NcPlane {
stylemask: NcStyleMask,
channels: NcChannelPair,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
crate::ncplane_perimeter_rounded(self, stylemask, channels, boxmask)
}
}
@ -1468,7 +1468,7 @@ impl NcPlane {
/// starting from zeroes.
///
/// *C style function: [ncplane_fadein()][crate::ncplane_fadein].*
pub fn fadein(&mut self, time: &NcTime, fader: NcFadeCb) -> NcResult {
pub fn fadein(&mut self, time: &NcTime, fader: NcFadeCb) -> NcIntResult {
unsafe { crate::ncplane_fadein(self, time, fader, null_mut()) }
}
@ -1476,7 +1476,7 @@ impl NcPlane {
/// where 'iter' < 'ncfadectx_iterations(nctx)'.
///
/// *C style function: [ncplane_fadein_iteration()][crate::ncplane_fadein_iteration].*
pub fn fadein_iteration(&mut self, time: &NcTime, fader: NcFadeCb) -> NcResult {
pub fn fadein_iteration(&mut self, time: &NcTime, fader: NcFadeCb) -> NcIntResult {
unsafe { crate::ncplane_fadein(self, time, fader, null_mut()) }
}
@ -1488,7 +1488,7 @@ impl NcPlane {
/// is limited, and affected by the complexity of the rest of the screen).
///
/// *C style function: [ncplane_fadeout()][crate::ncplane_fadeout].*
pub fn fadeout(&mut self, time: &NcTime, fader: NcFadeCb) -> NcResult {
pub fn fadeout(&mut self, time: &NcTime, fader: NcFadeCb) -> NcIntResult {
unsafe { crate::ncplane_fadeout(self, time, fader, null_mut()) }
}
@ -1496,7 +1496,7 @@ impl NcPlane {
/// where 'iter' < 'ncfadectx_iterations(nctx)'.
///
/// *C style function: [ncplane_fadeout_iteration()][crate::ncplane_fadeout_iteration].*
pub fn fadeout_iteration(&mut self, time: &NcTime, fader: NcFadeCb) -> NcResult {
pub fn fadeout_iteration(&mut self, time: &NcTime, fader: NcFadeCb) -> NcIntResult {
unsafe { crate::ncplane_fadeout(self, time, fader, null_mut()) }
}
@ -1511,7 +1511,7 @@ impl NcPlane {
/// specified colors.
///
/// *C style function: [ncplane_pulse()][crate::ncplane_pulse].*
pub fn pulse(&mut self, time: &NcTime, fader: NcFadeCb) -> NcResult {
pub fn pulse(&mut self, time: &NcTime, fader: NcFadeCb) -> NcIntResult {
unsafe { crate::ncplane_pulse(self, time, fader, null_mut()) }
}
@ -1553,7 +1553,7 @@ impl NcPlane {
lr: NcChannelPair,
y_stop: NcDimension,
x_stop: NcDimension,
) -> NcResult {
) -> NcIntResult {
unsafe {
crate::ncplane_gradient(
self,
@ -1586,7 +1586,7 @@ impl NcPlane {
lr: NcChannel,
y_len: NcDimension,
x_len: NcDimension,
) -> NcResult {
) -> NcIntResult {
crate::ncplane_gradient_sized(self, egc, stylemask, ul, ur, ll, lr, y_len, x_len)
}
@ -1607,7 +1607,7 @@ impl NcPlane {
lr: NcChannel,
y_stop: NcDimension,
x_stop: NcDimension,
) -> NcResult {
) -> NcIntResult {
unsafe { crate::ncplane_highgradient(self, ul, ur, ll, lr, y_stop as i32, x_stop as i32) }
}
@ -1623,7 +1623,7 @@ impl NcPlane {
lr: NcChannel,
y_stop: NcDimension,
x_stop: NcDimension,
) -> NcResult {
) -> NcIntResult {
unsafe {
crate::ncplane_highgradient_sized(self, ul, ur, ll, lr, y_stop as i32, x_stop as i32)
}

View File

@ -6,7 +6,7 @@ use libc::free;
use crate::ffi::__va_list_tag;
use crate::{
cell_release, cstring, ncplane_channels, NcAlign, NcAlphaBits, NcBoxMask, NcCell, NcChannel,
NcChannelPair, NcColor, NcDimension, NcEgc, NcPlane, NcResult, NcRgb, NcStyleMask,
NcChannelPair, NcColor, NcDimension, NcEgc, NcIntResult, NcPlane, NcRgb, NcStyleMask,
NCRESULT_ERR, NCRESULT_OK,
};
@ -118,7 +118,7 @@ pub fn ncplane_bg_default_p(plane: &NcPlane) -> bool {
///
/// *Method: NcPlane.[putc()][NcPlane#method.putc].*
#[inline]
pub fn ncplane_putc(plane: &mut NcPlane, cell: &NcCell) -> NcResult {
pub fn ncplane_putc(plane: &mut NcPlane, cell: &NcCell) -> NcIntResult {
unsafe { crate::ncplane_putc_yx(plane, -1, -1, cell) }
}
@ -126,7 +126,7 @@ pub fn ncplane_putc(plane: &mut NcPlane, cell: &NcCell) -> NcResult {
///
/// *Method: NcPlane.[putchar()][NcPlane#method.putchar].*
#[inline]
pub fn ncplane_putchar(plane: &mut NcPlane, ch: char) -> NcResult {
pub fn ncplane_putchar(plane: &mut NcPlane, ch: char) -> NcIntResult {
unsafe {
let cell = NcCell::with_char(ch, plane);
crate::ncplane_putc_yx(plane, -1, -1, &cell)
@ -143,7 +143,7 @@ pub fn ncplane_putchar_yx(
y: NcDimension,
x: NcDimension,
ch: char,
) -> NcResult {
) -> NcIntResult {
unsafe {
let cell = NcCell::with_char(ch, plane);
crate::ncplane_putc_yx(plane, y as i32, x as i32, &cell)
@ -154,7 +154,7 @@ pub fn ncplane_putchar_yx(
///
/// *Method: NcPlane.[putstr()][NcPlane#method.putstr].*
#[inline]
pub fn ncplane_putstr(plane: &mut NcPlane, string: &str) -> NcResult {
pub fn ncplane_putstr(plane: &mut NcPlane, string: &str) -> NcIntResult {
unsafe { crate::ncplane_putstr_yx(plane, -1, -1, cstring![string]) }
}
@ -162,7 +162,7 @@ pub fn ncplane_putstr(plane: &mut NcPlane, string: &str) -> NcResult {
///
/// *Method: NcPlane.[putnstr()][NcPlane#method.putnstr].*
#[inline]
pub fn ncplane_putnstr(plane: &mut NcPlane, size: u64, gclustarr: &[u8]) -> NcResult {
pub fn ncplane_putnstr(plane: &mut NcPlane, size: u64, gclustarr: &[u8]) -> NcIntResult {
unsafe { crate::ncplane_putnstr_yx(plane, -1, -1, size, cstring![gclustarr]) }
}
@ -170,7 +170,7 @@ pub fn ncplane_putnstr(plane: &mut NcPlane, size: u64, gclustarr: &[u8]) -> NcRe
///
/// *Method: NcPlane.[vprintf()][NcPlane#method.vprintf].*
#[inline]
pub fn ncplane_vprintf(plane: &mut NcPlane, format: &str, ap: &mut __va_list_tag) -> NcResult {
pub fn ncplane_vprintf(plane: &mut NcPlane, format: &str, ap: &mut __va_list_tag) -> NcIntResult {
unsafe { crate::ncplane_vprintf_yx(plane, -1, -1, cstring![format], ap) }
}
@ -182,13 +182,13 @@ pub fn ncplane_vprintf(plane: &mut NcPlane, format: &str, ap: &mut __va_list_tag
///
/// *Method: NcPlane.[at_cursor_cell()][NcPlane#method.at_cursor_cell].*
#[inline]
pub fn ncplane_at_cursor_cell(plane: &mut NcPlane, cell: &mut NcCell) -> NcResult {
pub fn ncplane_at_cursor_cell(plane: &mut NcPlane, cell: &mut NcCell) -> NcIntResult {
let mut egc =
unsafe { crate::ncplane_at_cursor(plane, &mut cell.stylemask, &mut cell.channels) };
if egc.is_null() {
return NCRESULT_ERR;
}
let result: NcResult = unsafe { crate::cell_load(plane, cell, egc) };
let result: NcIntResult = unsafe { crate::cell_load(plane, cell, egc) };
if result != NCRESULT_OK {
unsafe {
free(&mut egc as *mut _ as *mut c_void);
@ -207,7 +207,7 @@ pub fn ncplane_at_yx_cell(
y: NcDimension,
x: NcDimension,
cell: &mut NcCell,
) -> NcResult {
) -> NcIntResult {
let mut egc = unsafe {
crate::ncplane_at_yx(
plane,
@ -221,7 +221,7 @@ pub fn ncplane_at_yx_cell(
return NCRESULT_ERR;
}
let channels = cell.channels; // need to preserve wide flag
let result: NcResult = unsafe { crate::cell_load(plane, cell, egc) };
let result: NcIntResult = unsafe { crate::cell_load(plane, cell, egc) };
cell.channels = channels;
unsafe {
free(&mut egc as *mut _ as *mut c_void);
@ -265,7 +265,7 @@ pub fn ncplane_resize_simple(
plane: &mut NcPlane,
y_len: NcDimension,
x_len: NcDimension,
) -> NcResult {
) -> NcIntResult {
let (mut old_y, mut old_x) = (0, 0);
unsafe {
crate::ncplane_dim_yx(plane, &mut old_y, &mut old_x);
@ -307,7 +307,7 @@ pub fn ncplane_resize_simple(
///
/// *Method: NcPlane.[align()][NcPlane#method.align].*
#[inline]
pub fn ncplane_align(plane: &NcPlane, align: NcAlign, cols: NcDimension) -> NcResult {
pub fn ncplane_align(plane: &NcPlane, align: NcAlign, cols: NcDimension) -> NcIntResult {
crate::notcurses_align(ncplane_dim_x(plane), align, cols)
}
@ -324,7 +324,7 @@ pub fn ncplane_align(plane: &NcPlane, align: NcAlign, cols: NcDimension) -> NcRe
///
/// *Method: NcPlane.[hline()][NcPlane#method.hline].*
#[inline]
pub fn ncplane_hline(plane: &mut NcPlane, cell: &NcCell, len: NcDimension) -> NcResult {
pub fn ncplane_hline(plane: &mut NcPlane, cell: &NcCell, len: NcDimension) -> NcIntResult {
unsafe { crate::ncplane_hline_interp(plane, cell, len as i32, cell.channels, cell.channels) }
}
@ -339,7 +339,7 @@ pub fn ncplane_hline(plane: &mut NcPlane, cell: &NcCell, len: NcDimension) -> Nc
///
/// *Method: NcPlane.[vline()][NcPlane#method.vline].*
#[inline]
pub fn ncplane_vline(plane: &mut NcPlane, cell: &NcCell, len: NcDimension) -> NcResult {
pub fn ncplane_vline(plane: &mut NcPlane, cell: &NcCell, len: NcDimension) -> NcIntResult {
unsafe { crate::ncplane_vline_interp(plane, cell, len as i32, cell.channels, cell.channels) }
}
@ -358,7 +358,7 @@ pub fn ncplane_perimeter(
hline: &NcCell,
vline: &NcCell,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
unsafe {
crate::ncplane_cursor_move_yx(plane, 0, 0);
let (mut dimy, mut dimx) = (0, 0);
@ -387,7 +387,7 @@ pub fn ncplane_perimeter_double(
stylemask: NcStyleMask,
channels: NcChannelPair,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
if unsafe { crate::ncplane_cursor_move_yx(plane, 0, 0) } != NCRESULT_OK {
return NCRESULT_ERR;
}
@ -449,7 +449,7 @@ pub fn ncplane_perimeter_rounded(
stylemask: NcStyleMask,
channels: NcChannelPair,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
if unsafe { crate::ncplane_cursor_move_yx(plane, 0, 0) } != NCRESULT_OK {
return NCRESULT_ERR;
}
@ -524,7 +524,7 @@ pub fn ncplane_box_sized(
y_len: NcDimension,
x_len: NcDimension,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
let (mut y, mut x) = (0, 0);
unsafe {
crate::ncplane_cursor_yx(plane, &mut y, &mut x);
@ -554,7 +554,7 @@ pub fn ncplane_double_box(
y_stop: NcDimension,
x_stop: NcDimension,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
#[allow(unused_assignments)]
let mut ret = NCRESULT_OK;
@ -613,7 +613,7 @@ pub fn ncplane_double_box_sized(
y_len: NcDimension,
x_len: NcDimension,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
let (mut y, mut x) = (0, 0);
unsafe {
crate::ncplane_cursor_yx(plane, &mut y, &mut x);
@ -639,7 +639,7 @@ pub fn ncplane_rounded_box(
y_stop: NcDimension,
x_stop: NcDimension,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
#[allow(unused_assignments)]
let mut ret = NCRESULT_OK;
@ -697,7 +697,7 @@ pub fn ncplane_rounded_box_sized(
y_len: NcDimension,
x_len: NcDimension,
boxmask: NcBoxMask,
) -> NcResult {
) -> NcIntResult {
let (mut y, mut x) = (0, 0);
unsafe {
crate::ncplane_cursor_yx(plane, &mut y, &mut x);
@ -731,7 +731,7 @@ pub fn ncplane_gradient_sized(
lr: NcChannel,
y_len: NcDimension,
x_len: NcDimension,
) -> NcResult {
) -> NcIntResult {
if y_len < 1 || x_len < 1 {
return NCRESULT_ERR;
}

View File

@ -1,15 +1,15 @@
//! `NcResizeCb`
use crate::{NcPlane, NcResult};
use crate::{NcIntResult, NcPlane};
/// A callback function called when an [NcPlane] is resized.
///
/// See also [ncresizecb_to_rust] & [ncresizecb_to_c].
///
pub type NcResizeCb = fn(&mut NcPlane) -> NcResult;
pub type NcResizeCb = fn(&mut NcPlane) -> NcIntResult;
/// The unsafe version of [NcResizeCb] expected by the notcurses C API.
pub type NcResizeCbUnsafe = unsafe extern "C" fn(*mut NcPlane) -> NcResult;
pub type NcResizeCbUnsafe = unsafe extern "C" fn(*mut NcPlane) -> NcIntResult;
/// Converts [NcResizeCbUnsafe] to [NcResizeCb].
pub fn ncresizecb_to_rust(resizecb: Option<NcResizeCbUnsafe>) -> Option<NcResizeCb> {

View File

@ -1,6 +1,6 @@
//! `NcSigSet`
use crate::NcResult;
use crate::NcIntResult;
/// A wrapper over
/// [sigset_t](https://www.gnu.org/software/libc/manual/html_node/Signal-Sets.html).
@ -16,22 +16,22 @@ impl NcSignalSet {
}
/// Adds `signum` to this set.
pub fn addset(&mut self, signum: i32) -> NcResult {
pub fn addset(&mut self, signum: i32) -> NcIntResult {
unsafe { crate::bindings::ffi::sigaddset(self, signum) }
}
/// Removes `signum` from this set.
pub fn delset(&mut self, signum: i32) -> NcResult {
pub fn delset(&mut self, signum: i32) -> NcIntResult {
unsafe { crate::bindings::ffi::sigdelset(self, signum) }
}
/// Clears all signals from this set.
pub fn emptyset(&mut self) -> NcResult {
pub fn emptyset(&mut self) -> NcIntResult {
unsafe { crate::bindings::ffi::sigemptyset(self) }
}
/// Sets all signals in this set.
pub fn fillset(&mut self) -> NcResult {
pub fn fillset(&mut self) -> NcIntResult {
unsafe { crate::bindings::ffi::sigfillset(self) }
}
@ -44,20 +44,20 @@ impl NcSignalSet {
}
/// Puts in this set all signals that are blocked and waiting to be delivered.
pub fn pending(&mut self) -> NcResult {
pub fn pending(&mut self) -> NcIntResult {
unsafe { crate::bindings::ffi::sigpending(self) }
}
/// Gets and/or changes the set of blocked signals.
//
// https://linux.die.net/man/2/sigprocmask
pub fn procmask(how: i32, set: &NcSignalSet, old_set: &mut NcSignalSet) -> NcResult {
pub fn procmask(how: i32, set: &NcSignalSet, old_set: &mut NcSignalSet) -> NcIntResult {
unsafe { crate::bindings::ffi::sigprocmask(how, set, old_set) }
}
/// Changes the set of blocked signals to the ones in this set,
/// waits until a signal arrives, and restores the set of blocked signals.
pub fn suspend(&self) -> NcResult {
pub fn suspend(&self) -> NcIntResult {
unsafe { crate::bindings::ffi::sigsuspend(self) }
}
}

View File

@ -1,6 +1,6 @@
//! `NcProgBar` & `NcProgBarOptions` methods and associated functions.
use crate::{NcPlane, NcProgBar, NcProgBarOptions, NcResult};
use crate::{NcIntResult, NcPlane, NcProgBar, NcProgBarOptions};
/// # `NcProgBarOptions` Methods
impl NcProgBarOptions {
@ -61,7 +61,7 @@ impl NcProgBar {
/// Returns [NCRESULT_ERR][crate::NCRESULT_ERR] if progress is < 0 || > 1.
///
/// *C style function: [ncprogbar_set_progress()][crate::ncprogbar_set_progress].*
pub fn set_progress(&mut self, progress: f64) -> NcResult {
pub fn set_progress(&mut self, progress: f64) -> NcIntResult {
unsafe { crate::ncprogbar_set_progress(self, progress) }
}
}