notcurses/rust/examples/poc-direct.rs
joseLuís 6049e07d27 rust: continue refactoring the error system.
- made const NotcursesOptions constructors.
- refactor Notcurses constructors to return NcResult.
- pass NotcursesOptions by value, since it's Copy.
- update tests.
- add helper modules for Notcurses & NcPlane.
  - new initialization functions to be used in tests.
- BONUS:
  - refactor NcDirect
  - update examples.
2020-12-25 19:35:21 +01:00

65 lines
1.5 KiB
Rust

//! based on the proof of concept at ../../src/poc/direct.c
use libnotcurses_sys::*;
fn main() -> NcResult<()> {
let ncd = NcDirect::new()?;
let dimy = ncd.dim_y();
let dimx = ncd.dim_x();
for _ in 0..dimy {
for _ in 0..dimx {
printf!("X");
}
}
ncd.flush()?;
ncd.fg_rgb(0xff8080)?;
ncd.styles_on(NCSTYLE_STANDOUT)?;
printf!(" erp erp \n");
ncd.fg_rgb(0x80ff80)?;
printf!(" erp erp \n");
ncd.styles_off(NCSTYLE_STANDOUT)?;
printf!(" erp erp \n");
ncd.fg_rgb(0xff8080)?;
printf!(" erp erp \n");
ncd.cursor_right(dimx / 2)?;
ncd.cursor_up(dimy / 2)?;
printf!(" erperperp! \n");
let (mut y, x);
if let Ok((_y, _x)) = ncd.cursor_yx() {
y = _y;
x = _x;
printf!("\n\tRead cursor position: y: %d x: %d\n", y, x);
y += 2;
while y > 3 {
let up = if y >= 3 { 3 } else { y };
ncd.cursor_up(up)?;
ncd.flush()?;
y -= up;
let newy;
if let Ok((_y, _)) = ncd.cursor_yx() {
newy = _y;
} 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;
}
} else {
return Err(NcError::with_msg(-10, "Couldn't read cursor position."));
}
ncd.stop()?;
Ok(())
}