use std::{convert::TryFrom, env, io};
use ncursesw::{*, shims::ncurses::{ACS_ULCORNER, ACS_LLCORNER, ACS_URCORNER, ACS_LRCORNER, ACS_HLINE, ACS_VLINE}};
macro_rules! result { ($type: ty) => { Result<$type, NCurseswError> } }
fn main() {
if let Err(source) = main_routine() {
eprintln!("error: {}", source);
}
}
fn main_routine() -> result!(()) {
let term = env::var("TERM").expect("$TERM is undefined!!!");
let screen = newterm(Some(term), &io::stdout().lock(), &io::stdin().lock())?;
curs_set_sp(screen, CursorType::Invisible)?;
noecho_sp(screen)?;
let window = newwin_sp(screen, Size::default(), Origin::default())?;
let left_side = ChtypeChar::from_chtype(ACS_VLINE());
let right_side = ChtypeChar::from_chtype(ACS_VLINE());
let top_side = ChtypeChar::from_chtype(ACS_HLINE());
let bottom_side = ChtypeChar::from_chtype(ACS_HLINE());
let upper_left = ChtypeChar::from_chtype(ACS_ULCORNER());
let upper_right = ChtypeChar::from_chtype(ACS_URCORNER());
let lower_left = ChtypeChar::from_chtype(ACS_LLCORNER());
let lower_right = ChtypeChar::from_chtype(ACS_LRCORNER());
wborder(window, left_side, right_side, top_side, bottom_side, upper_left, upper_right, lower_left, lower_right)?;
let line1 = "If the doors of perception were cleansed every thing would appear to man as it is: Infinite.";
let line2 = "For man has closed himself up, till he sees all things thro' narrow chinks of his cavern.";
let line3 = "Press any key to exit";
let window_size = getmaxyx(window)?;
let mut origin = Origin { y: (window_size.lines / 2) - 2, x: calc_x_axis(line1, window_size)? };
mvwaddstr(window, origin, line1)?;
origin.y += 1;
origin.x = calc_x_axis(line2, window_size)?;
mvwaddstr(window, origin, line2)?;
origin.y += 2;
origin.x = calc_x_axis(line3, window_size)?;
mvwaddstr(window, origin, line3)?;
wgetch(window)?;
delwin(window)?;
endwin_sp(screen)?;
delscreen(screen);
Ok(())
}
fn calc_x_axis(line: &str, window_size: Size) -> result!(i32) {
Ok((window_size.columns / 2) - (i32::try_from(line.len())? / 2))
}