extern crate ncurseswwin;
use std::{convert::TryFrom, env, io, process::exit};
use anyhow::{Result, Context};
use ncurseswwin::*;
fn main() {
if let Err(source) = main_routine() {
if let Some(err) = source.downcast_ref::<NCurseswWinError>() {
match err {
NCurseswWinError::Panic { message } => eprintln!("panic: {}", message),
_ => eprintln!("error: {}", err)
}
} else {
eprintln!("error: {}", source);
}
source.chain().skip(1).for_each(|cause| eprintln!("cause: {}", cause));
exit(1);
}
exit(0);
}
fn main_routine() -> Result<()> {
safe_entry(|| {
let term = &env::var("TERM").with_context(|| "$TERM is invalid!!!")?;
let screen = &Screen::new(Some(term), &io::stdout().lock(), &io::stdin().lock())?;
assert!(&screen.termname()? == term);
screen.set_input_mode(InputMode::Character)?;
screen.set_echo(false)?;
screen.set_newline(false)?;
screen.intrflush(false)?;
screen.cursor_set(CursorType::Invisible)?;
screen_test(screen)
})
}
fn screen_test(screen: &Screen) -> Result<()> {
let window = &Window::new_sp(screen, Size::default(), Origin::default())?;
let left_side = chtype_box_graphic(BoxDrawingGraphic::LeftVerticalLine);
let right_side = chtype_box_graphic(BoxDrawingGraphic::RightVerticalLine);
let top_side = chtype_box_graphic(BoxDrawingGraphic::UpperHorizontalLine);
let bottom_side = chtype_box_graphic(BoxDrawingGraphic::LowerHorizontalLine);
let upper_left = chtype_box_graphic(BoxDrawingGraphic::UpperLeftCorner);
let upper_right = chtype_box_graphic(BoxDrawingGraphic::UpperRightCorner);
let lower_left = chtype_box_graphic(BoxDrawingGraphic::LowerLeftCorner);
let lower_right = chtype_box_graphic(BoxDrawingGraphic::LowerRightCorner);
window.border(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 = window.size()?;
let mut origin = Origin { y: (window_size.lines / 2) - 2, x: calc_x_axis(line1, window_size)? };
window.mvaddstr(origin, line1)?;
origin.y += 1;
origin.x = calc_x_axis(line2, window_size)?;
window.mvaddstr(origin, line2)?;
origin.y += 2;
origin.x = calc_x_axis(line3, window_size)?;
window.mvaddstr(origin, line3)?;
window.getch()?;
Ok(())
}
fn calc_x_axis(line: &str, window_size: Size) -> Result<u16> {
Ok((window_size.columns / 2) - (u16::try_from(line.len())? / 2))
}