#![allow(non_snake_case)]
use std::{sync::atomic::Ordering, convert::TryFrom};
use ncursesw::{ColorsType, ColorType, ColorAttributeTypes, CursorType};
use crate::{
InputMode, Origin, Size, Window, NCurseswWinError,
gen::HasHandle, ncurses::{INITSCR_CALLED, COLOR_STARTED}
};
pub fn LINES() -> result!(u16) {
Ok(u16::try_from(ncursesw::LINES())?)
}
pub fn COLS() -> result!(u16) {
Ok(u16::try_from(ncursesw::COLS())?)
}
pub fn curscr() -> Window {
Window::_from(None, ncursesw::curscr(), false)
}
pub fn newscr() -> Window {
Window::_from(None, ncursesw::newscr(), false)
}
pub fn stdscr() -> Window {
Window::_from(None, ncursesw::stdscr(), false)
}
pub fn set_input_mode(mode: InputMode) -> result!(()) {
check_initscr_called()?;
match match mode {
InputMode::Character => ncursesw::cbreak(),
InputMode::Cooked => ncursesw::nocbreak(),
InputMode::RawCharacter => ncursesw::raw(),
InputMode::RawCooked => ncursesw::noraw()
} {
Err(source) => Err(NCurseswWinError::NCurseswError { source }),
Ok(_) => Ok(())
}
}
pub fn set_echo(flag: bool) -> result!(()) {
check_initscr_called()?;
match if flag {
ncursesw::echo()
} else {
ncursesw::noecho()
} {
Err(source) => Err(NCurseswWinError::NCurseswError { source }),
Ok(_) => Ok(())
}
}
pub fn set_newline(flag: bool) -> result!(()) {
check_initscr_called()?;
match if flag {
ncursesw::nl()
} else {
ncursesw::nonl()
} {
Err(source) => Err(NCurseswWinError::NCurseswError { source }),
Ok(_) => Ok(())
}
}
pub fn start_color() -> result!(()) {
check_initscr_called()?;
if color_started() {
Err(NCurseswWinError::StartColorAlreadyCalled)
} else {
ncursesw::start_color()?;
COLOR_STARTED.store(true, Ordering::SeqCst);
Ok(())
}
}
pub fn use_default_colors() -> result!(()) {
check_initscr_called()?;
if !color_started() {
Err(NCurseswWinError::StartColorNotCalled)
} else {
Ok(ncursesw::use_default_colors()?)
}
}
pub fn assume_default_colors<S, C, T>(colors: S) -> result!(())
where S: ColorsType<C, T>,
C: ColorType<T>,
T: ColorAttributeTypes
{
check_initscr_called()?;
if !color_started() {
Err(NCurseswWinError::StartColorNotCalled)
} else {
Ok(ncursesw::assume_default_colors(colors)?)
}
}
pub fn cursor_set(cursor: CursorType) -> result!(CursorType) {
check_initscr_called()?;
Ok(ncursesw::curs_set(cursor)?)
}
#[deprecated(since = "0.6.0", note = "Use cursor_set() instead")]
pub fn curs_set(cursor: CursorType) -> result!(CursorType) {
cursor_set(cursor)
}
pub fn intrflush(flag: bool) -> result!(()) {
check_initscr_called()?;
Ok(ncursesw::intrflush(flag)?)
}
pub fn terminal_size() -> result!(Size) {
Ok(Size { lines: LINES()? - 1, columns: COLS()? - 1 })
}
pub fn terminal_bottom_right_origin() -> result!(Origin) {
Ok(Origin { y: LINES()? - 1, x: COLS()? - 1 })
}
fn check_initscr_called() -> result!(()) {
if INITSCR_CALLED.load(Ordering::SeqCst) {
Ok(())
} else {
Err(NCurseswWinError::InitscrNotCalled)
}
}
fn color_started() -> bool {
COLOR_STARTED.load(Ordering::SeqCst)
}