From 6f50873592e18132cbbd5e45a346c55c7aa500e6 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Fri, 19 Jul 2024 06:08:17 +0000 Subject: [PATCH] Bug fix --- src/cfg.rs | 14 ++++++++++++++ src/main.rs | 18 ++++++++++-------- src/utils.rs | 10 ++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/src/cfg.rs b/src/cfg.rs index 26f8263..4730dcd 100644 --- a/src/cfg.rs +++ b/src/cfg.rs @@ -42,6 +42,16 @@ impl Config { self.obj.get(&k) } + pub fn get_bool + ?Sized>(&self, s: &S) -> Option<&bool> { + match self.get(s) { + Some(y) => match y { + Yaml::Boolean(i) => Some(i), + _ => None, + }, + None => None, + } + } + pub fn get_str + ?Sized>(&self, s: &S) -> Option<&str> { match self.get(s) { Some(y) => match y { @@ -98,4 +108,8 @@ impl Config { pub fn restore_command(&self) -> Option> { self.get_str_vec("restore_command") } + + pub fn pause_at_exit(&self) -> bool { + self.get_bool("pause_at_exit").map(|s| s.to_owned()).unwrap_or(false) + } } diff --git a/src/main.rs b/src/main.rs index 07c2d90..fb8ef5f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,7 +52,7 @@ impl Main { } else { let e = Self::call(cml)?; let ok = match &e { - ExitStatus::Exited(c) => *c != 0, + ExitStatus::Exited(c) => *c == 0, _ => false, }; if !ok { @@ -87,7 +87,7 @@ impl Main { } else { let e = Self::call(cml)?; let ok = match &e { - ExitStatus::Exited(c) => *c != 0, + ExitStatus::Exited(c) => *c == 0, _ => false, }; if !ok { @@ -115,7 +115,7 @@ impl Main { } else { let e = Self::call(cml)?; let ok = match &e { - ExitStatus::Exited(c) => *c != 0, + ExitStatus::Exited(c) => *c == 0, _ => false, }; if !ok { @@ -130,7 +130,6 @@ impl Main { } fn main() -> ExitCode { - println!("Hello, world!"); let argv: Vec = std::env::args().collect(); let mut opts = Options::new(); opts.optflag("h", "help", "Print help message."); @@ -164,12 +163,15 @@ fn main() -> ExitCode { return ExitCode::from(1); } let m = Main::new(cfg, result.opt_present("d")); - match m.run() { - Ok(_) => {} + let e = match m.run() { + Ok(_) => 0, Err(e) => { println!("{}", e); - return ExitCode::from(1); + 1 } + }; + if m._cfg.pause_at_exit() { + utils::enter_continue(); } - return ExitCode::from(0); + return ExitCode::from(e); } diff --git a/src/utils.rs b/src/utils.rs index ac540da..7dd5893 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,5 @@ use std::env; +use std::io::Read; use std::io::Write; use std::path::Path; use std::path::PathBuf; @@ -36,3 +37,12 @@ pub fn ask_continue() -> bool { } } } + +pub fn enter_continue() { + print!("Press enter to continue."); + std::io::stdout().flush().unwrap(); + let mut f = [0u8; 1]; + match std::io::stdin().read_exact(&mut f) { + _ => {} + } +}