use std::sync::atomic::Ordering;
use crate::inputmode::InputMode;
use ncursesw::{LcCategory, SoftLabelType, NCurseswError};
use ncursesw::gen::{ColorsType, ColorType, ColorAttributeTypes};
use crate::ncurses::{INITSCR_CALLED, COLOR_STARTED, INITSCR_NOT_CALLED, INITSCR_ALREADY_CALLED};
lazy_static! {
static ref START_COLOR_ALREADY_CALLED: &'static str = "ncursesw::start_color() has already been called!";
static ref START_COLOR_NOT_CALLED: &'static str = "ncursesw::start_color() has not been called!";
}
pub fn setlocale(lc: LcCategory, locale: &str) -> String {
if INITSCR_CALLED.load(Ordering::SeqCst) {
panic!(INITSCR_ALREADY_CALLED.to_string());
};
ncursesw::setlocale(lc, locale)
}
pub fn set_input_mode(mode: InputMode) -> result!(()) {
if !INITSCR_CALLED.load(Ordering::SeqCst) {
panic!(INITSCR_NOT_CALLED.to_string());
};
match mode {
InputMode::Character => ncursesw::cbreak(),
InputMode::Cooked => ncursesw::nocbreak(),
InputMode::RawCharacter => ncursesw::raw(),
InputMode::RawCooked => ncursesw::noraw()
}
}
pub fn set_echo(echoing: bool) -> result!(()) {
if !INITSCR_CALLED.load(Ordering::SeqCst) {
panic!(INITSCR_NOT_CALLED.to_string());
} else if echoing {
ncursesw::echo()
} else {
ncursesw::noecho()
}
}
pub fn set_newline(newline: bool) -> result!(()) {
if !INITSCR_CALLED.load(Ordering::SeqCst) {
panic!(INITSCR_NOT_CALLED.to_string());
} else if newline {
ncursesw::nl()
} else {
ncursesw::nonl()
}
}
pub fn start_color() -> result!(()) {
if !INITSCR_CALLED.load(Ordering::SeqCst) {
panic!(INITSCR_NOT_CALLED.to_string());
} else if COLOR_STARTED.load(Ordering::SeqCst) {
panic!(START_COLOR_ALREADY_CALLED.to_string());
}
match ncursesw::start_color() {
Err(e) => Err(e),
Ok(_) => {
COLOR_STARTED.store(true, Ordering::SeqCst);
Ok(())
}
}
}
pub fn use_default_colors() -> result!(()) {
if !INITSCR_CALLED.load(Ordering::SeqCst) {
panic!(INITSCR_NOT_CALLED.to_string());
} else if !COLOR_STARTED.load(Ordering::SeqCst) {
panic!(START_COLOR_NOT_CALLED.to_string());
};
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 {
if !INITSCR_CALLED.load(Ordering::SeqCst) {
panic!(INITSCR_NOT_CALLED.to_string());
} else if !COLOR_STARTED.load(Ordering::SeqCst) {
panic!(START_COLOR_NOT_CALLED.to_string());
};
ncursesw::assume_default_colors(colors)
}
pub fn slk_init(fmt: SoftLabelType) -> result!(()) {
if INITSCR_CALLED.load(Ordering::SeqCst) {
panic!(INITSCR_ALREADY_CALLED.to_string());
};
ncursesw::slk_init(fmt)
}