Crate libnotcurses_sys
source · [−]Expand description
libnotcurses-sys
is a low-level Rust wrapper for the
notcurses C library
It is built with several layers of zero-overhead abstractions over the C functions and pointers accessed through FFI.
It adds greater safety and type correctness over the underlying C library API, while trying to remain very close to it.
It offers the choice of using it more like Rust and/or more like C.
Like Rust
Where you use the more safely wrapped types, with its methods and
constructors, and error handling with the NcResult
enum:
Example
use libnotcurses_sys::*;
fn main() -> NcResult<()> {
let nc = unsafe { Nc::new_cli()? };
let stdplane = unsafe { nc.stdplane() };
stdplane.putstr("\nhello world!\n")?;
nc.render()?;
unsafe { nc.stop()? }; // always stop before exiting
Ok(())
}
Notes on the Rust API
The Drop
trait is not implemented for any wrapping type in this library
over structures created by the underlying C library.
This means you still have to manually call the stop()
method for Nc
and NcDirect
objects, and the destroy()
method for the rest of types that
allocate, (like NcPlane
, NcMenu
…) at the end of their scope.
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
for the C API (e.g.: NcChannel_u32
, NcLogLevel_i32
, NcStyle_u16
…), to
leverage type checking. And for the Rust API they are wrapped as unit structs
or enums, with associated methods and constants (e.g. NcChannel
,
NcLogLevel
, NcStyle
…).
Several methods are declared unsafe when they have addittional contracts to manually upheld in order to avoid UB.
even more like Rust
The WIP sister crate
notcurses
will eventually
offer a closer to Rust, higher-level, safer, and simpler API, and make it
easier to interoperate with the rest of the Rust ecosystem.
Like C
You can access the imported, or reimplemented C API functions directly, and use it in a very similar way as the C library is used.
It requires more use of unsafe, since it has less safer abstractions.
Error handling is done this way by checking the returned NcResult_i32
,
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::c_api::*;
fn main() {
let options = ffi::notcurses_options {
termtype: null(),
loglevel: 0,
margin_t: 0,
margin_r: 0,
margin_b: 0,
margin_l: 0,
flags: NCOPTION_CLI_MODE,
};
unsafe {
let nc = notcurses_init(&options, null_mut());
if nc.is_null() {
exit(1);
}
let plane = notcurses_stdplane(nc);
let cols = ncplane_putstr(&mut *plane, "\nhello world!\n");
if cols < NCRESULT_OK {
notcurses_stop(nc);
exit(cols.abs());
}
if notcurses_render(&mut *nc) < NCRESULT_OK {
exit(2);
}
if notcurses_stop(nc) < NCRESULT_OK {
exit(3);
}
}
}
The notcurses
C API docs
Modules
The C API
, including structs, constants, functions and type aliases.
The notcurses widgets.
Macros
Nc::render
($nc
)? + sleep!
[$sleep_args]
.
NcPlane::render
($p
)? +
NcPlane::rasterize
($p
)? +
sleep!
[$sleep_args]
.
Wrapper around NcPlane.putstr
,
rendering and rasterizing the plane afterwards.
Wrapper around NcPlane.putstrln
.
rendering and rasterizing the plane afterwards.
Sleeps for $s
seconds + $ms
milliseconds
NcVisual::blit
($v
, $nc
, $vo
)? +
Nc::render
($nc
)? + sleep!
[$sleep_args]
.
Structs
A bitmask for drawing borders, gradients and corners.
64 bits containing a foreground and background NcChannel
A bitmask of NcDirect
flags.
The error type for the Rust methods API.
A wrapper struct around
libc::FILE
A synthesized input event.
A bitmask of mice input events.
An ABGR pixel.
Contains the pixel geometry information as returned by the
NcPlane.
pixel_geom
method.
A bitmask of flags for NcPlaneOptions
.
Builder object for NcPlaneOptions
.
24 bits broken into 3x RGB components.
32 bits broken into 3x RGB components + alpha component.
A bitmask of styles.
A bitmask of flags for NcVisualOptions
.
Describes all the geometries of an NcVisual
.
Builder object for NcVisualOptions
.
Enums
Alignment within a plane or terminal.
The type of the NcInput
event.
Log level for NcOptions
.
Pixel blitting implementations, informative only.
A received character or event.
Type Definitions
Capabilities, derived from terminfo, environment variables, and queries.
Minimal notcurses instance for styling text.
Context for a palette fade operation
A raw file descriptor, as returned by Nc.inputready_fd
and
NcDirect.inputready_fd
.
Options struct for NcFdPlane
.
Reads and decodes input events.
Used for indexing into a NcPalette
(alias of u8
).
Options struct for NcPlane
.
A callback function called when an NcPlane
is resized.
The result type for the Rust methods API.
notcurses runtime statistics
Options struct for NcSubproc
A visual bit of multimedia.
Options struct for NcVisual
.