notcurses/rust
joseLuís 3d830f5e73 [rust] updates against 2.3.4
- remove ncvisual_inflate and add ncvisual_resize_noninterpolative & fix example.
- add notcurses_detected_terminal & ncdirect_detected_terminal.
- take into account new static functions pending to implement:
  ncdirect_light_box, ncdirect_heavy_box, nccells_light_box, nccells_heavy_box, nccells_ascii_box.
- update module info headers notation.
2021-06-13 13:07:00 +02:00
..
.cargo rust: continue NcMenu PoC example; fixes 2020-12-27 19:00:32 +01:00
build v2.3.4 2021-06-12 17:27:28 -04:00
examples [rust] updates against 2.3.4 2021-06-13 13:07:00 +02:00
src [rust] updates against 2.3.4 2021-06-13 13:07:00 +02:00
Cargo.toml v2.3.4 2021-06-12 17:27:28 -04:00
LICENSE-APACHE [rust] re-license with dual MIT OR Apache 2.0 2021-04-21 18:09:44 +02:00
LICENSE-MIT [rust] re-license with dual MIT OR Apache 2.0 2021-04-21 18:09:44 +02:00
README.md [rust] fix docs 2021-05-06 13:08:21 +02:00

Crate API MSRV: 1.48.0

libnotcurses-sys is a Rust wrapper for the notcurses C library

This library is built with several layers of zero-overhead abstractions over the C functions and pointers accessed through FFI.

How to use this library

There are basically two ways: The Rust way, and the C way. (Or a mix of both).

1. The Rust way

Where you use the more safely wrapped types, with its methods and constructors, and painless error handling, like this:

use libnotcurses_sys::*;

fn main() -> NcResult<()> {
    let mut nc = Notcurses::with_flags(NCOPTION_NO_ALTERNATE_SCREEN)?;
    let plane = nc.stdplane();
    plane.putstr("hello world")?;
    nc.render()?;
    nc.stop()?;
    Ok(())
}

Although you still have to manually call the stop() method for Notcurses and NcDirect objects, and the destroy() method for the rest of types that allocate, (like NcPlane, NcMenu…) at the end of their scope, since the Drop trait is not implemented for any wrapping type in libnotcurses-sys.

But they do implement methods and use NcResult as the return type, for handling errors in the way we are used to in Rust.

For the types that don't allocate, most are based on primitives like i32, u32, u64… without a name in the C library. In Rust they are type aliased (e.g.: NcChannel, NcChannelPair, NcRgb, NcColor…), to leverage type checking, and they implement methods through traits (e.g. NcChannelMethods must be in scope to use the NcChannel methods.

2. The C way

You can always use the C API functions directly if you prefer, in a very similar way as the C library is used.

It requires the use of unsafe, since most functions are wrapped directly by bindgen marked as such.

Error handling is done this way by checking the returned NcIntResult, or in case of receiving a pointer, by comparing it to null_mut().

Example

use core::ptr::{null, null_mut};
use std::process::exit;

use libnotcurses_sys::*;

fn main() {
    let options = ffi::notcurses_options {
        termtype: null(),
        renderfp: null_mut(),
        loglevel: 0,
        margin_t: 0,
        margin_r: 0,
        margin_b: 0,
        margin_l: 0,
        flags: NCOPTION_NO_ALTERNATE_SCREEN,
    };
    unsafe {
        let nc = notcurses_init(&options, null_mut());
        if nc == null_mut() {
            exit(1);
        }
        let plane = notcurses_stdplane(nc);
        let cols = ncplane_putstr(&mut *plane, "hello world");
        if cols < NCRESULT_OK {
            notcurses_stop(nc);
            exit(cols.abs());
        }
        if notcurses_stop(nc) < NCRESULT_OK {
            exit(2);
        }
    }
}

The notcurses C API docs