use clap::Parser;
use gitu::{
Res,
cli::{self, Args, Commands},
config::{self, Config},
error::Error,
term::{self, Term},
};
use log::LevelFilter;
use std::{backtrace::Backtrace, fmt::Display, panic, process, sync::Arc};
pub fn main() -> Res<()> {
let args = Args::parse();
if args.version {
println!("gitu {}", git_version::git_version!(cargo_suffix = ""));
return Ok(());
}
if let Some(Commands::Completion { shell }) = args.command {
cli::completions(shell, &mut std::io::stdout());
return Ok(());
}
if args.log {
simple_logging::log_to_file(gitu::LOG_FILE_NAME, LevelFilter::Debug)
.map_err(Error::OpenLogFile)?;
}
let config = Arc::new(config::init_config(args.config.clone())?);
let config_ref = config.clone();
panic::set_hook(Box::new(move |panic_info| {
print_err(term::backend().reset_term(&config));
eprintln!("{}", panic_info);
eprintln!("trace: \n{}", Backtrace::force_capture());
}));
log::debug!("Initializing terminal backend");
let mut term = term::backend();
if !args.print {
term.setup_term(&config_ref).map_err(Error::Term)?;
}
let result = setup_term_and_run(&mut term, config_ref.clone(), &args);
term.reset_term(&config_ref).map_err(Error::Term)?;
process::exit(result?);
}
fn setup_term_and_run(term: &mut Term, config: Arc<Config>, args: &Args) -> Res<i32> {
log::debug!("Starting app");
gitu::run(config, args, term)
}
fn print_err<T, E: Display>(result: Result<T, E>) {
match result {
Ok(_) => (),
Err(error) => eprintln!("Error: {}", error),
};
}