extern crate startrust;
use std::io::{stdin, Write};
use clap::{crate_authors, crate_description, crate_version, Clap};
use log::{debug, LevelFilter};
use termcolor::{ColorChoice, StandardStream, WriteColor};
use startrust::{
clrscr, show_instructions, show_title, yesno, StResult, StarTrustError, TheGame, TheGameDefs,
TheGameDefsBuilder,
};
#[derive(Clap)]
#[clap(version = crate_version!(), author = crate_authors!(), about = crate_description!())]
struct GetOpts {
#[clap(long)]
no_color: bool,
#[clap(long)]
force_color: bool,
#[clap(short, long)]
debug: bool,
}
fn get_game_config() -> StResult<TheGameDefs> {
let the_game_defs = TheGameDefsBuilder::default()
.build()
.map_err(StarTrustError::GeneralError)?;
Ok(the_game_defs)
}
fn init_logger(get_opts: &GetOpts) {
let mut builder = pretty_env_logger::formatted_builder();
if let Ok(s) = ::std::env::var("RUST_LOG") {
builder.parse_filters(&s);
}
if get_opts.debug {
builder.filter_level(LevelFilter::Debug);
debug!("Debug is *ON*");
}
builder.init();
}
fn main() -> Result<(), StarTrustError> {
let get_opts = GetOpts::parse();
init_logger(&get_opts);
let sin = stdin();
let choice = if !atty::is(atty::Stream::Stdout) || get_opts.no_color {
ColorChoice::Never
} else if get_opts.force_color {
ColorChoice::Always
} else {
ColorChoice::Auto
};
let mut sout = StandardStream::stdout(choice);
show_title(&mut sout)?;
show_instructions(&mut sin.lock(), &mut sout)?;
let the_game_config = get_game_config()?;
loop {
let mut the_game = TheGame::new(&the_game_config);
debug!("About to print title");
clrscr(&mut sout)?;
show_title(&mut sout)?;
let _game = the_game.play(&mut sin.lock(), &mut sout)?;
let _ = write!(sout, "\nTRY AGAIN? ")?;
sout.flush()?;
let ans = yesno(&mut sin.lock())?;
if ans != 'Y' {
sout.reset()?;
writeln!(sout)?;
return Ok(());
}
}
}