use std::sync::atomic::Ordering;
use anyhow::Result;
use crate::{Window, gen::HasHandle, ncurses::{INITSCR_CALLED, COLOR_STARTED}};
pub(in crate::ncurses) struct NCurses {
handle: ncursesw::WINDOW
}
impl NCurses {
pub fn new() -> Result<Self> {
if !INITSCR_CALLED.load(Ordering::SeqCst) {
let handle = ncursesw::initscr()?;
assert!(!handle.is_null(), "NCurses::new() : handle.is_null()");
COLOR_STARTED.store(false, Ordering::SeqCst);
INITSCR_CALLED.store(true, Ordering::SeqCst);
Ok(Self { handle })
} else {
panic!("NCurses already initialised!!!")
}
}
pub fn stdscr(&self) -> Window {
Window::_from(None, self.handle, true)
}
}
impl Drop for NCurses {
fn drop(&mut self) {
match ncursesw::endwin() {
Err(source) => panic!("{} @ ({:p})", source, self.handle),
_ => {
COLOR_STARTED.store(false, Ordering::SeqCst);
INITSCR_CALLED.store(false, Ordering::SeqCst);
}
}
}
}