From ff80a1953ed2a159562595bf5375db071fff013f Mon Sep 17 00:00:00 2001 From: nwrenger Date: Wed, 27 Mar 2024 02:49:03 +0100 Subject: [PATCH] :wrench: Refactor && Fixes --- src/error.rs | 51 ++++++++++++++++++++++++++++++++++----------------- src/events.rs | 18 +++--------------- src/main.rs | 13 +++++++++++-- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/src/error.rs b/src/error.rs index 61cfa23..4a435d9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -6,7 +6,7 @@ use cursive::{reexports::log::error, view::Nameable, views::Dialog, Cursive}; #[repr(i64)] #[derive(Debug, Clone, Copy)] pub enum Error { - /// This File already exists + /// This File already exists, this can be seen more than a warning than a real error AlreadyExists, /// The user provided arguments are malformed Arguments, @@ -29,13 +29,6 @@ impl fmt::Display for Error { } } -impl From for Error { - fn from(value: clippers::Error) -> Self { - error!("clippers::Error: {value:?}"); - Self::Clipboard - } -} - impl From for Error { fn from(e: std::convert::Infallible) -> Self { error!("convert::Infallible: {e:?}"); @@ -43,6 +36,13 @@ impl From for Error { } } +impl From for Error { + fn from(value: clippers::Error) -> Self { + error!("clippers::Error: {value:?}"); + Self::Clipboard + } +} + impl From for Error { fn from(e: std::io::Error) -> Self { error!("File Error: {e}"); @@ -57,15 +57,32 @@ impl Error { siv.screen_mut().remove_layer(pos); } let error_message = self.to_string(); - siv.add_layer( - Dialog::text(error_message) - .title("Error") - .padding_lrtb(1, 1, 1, 0) - .button("OK", |s| { - s.pop_layer(); - }) - .with_name("error"), - ); + match self { + // more a warn + Error::AlreadyExists => { + siv.add_layer( + Dialog::text(error_message) + .title("Warning") + .padding_lrtb(1, 1, 1, 0) + .button("Ok", |s| { + s.pop_layer(); + }) + .with_name("error"), + ); + } + // real errors + Error::Arguments | Error::FileOpen | Error::Clipboard => { + siv.add_layer( + Dialog::text(error_message) + .title("Error") + .padding_lrtb(1, 1, 1, 0) + .button("Ok", |s| { + s.pop_layer(); + }) + .with_name("error"), + ); + } + } } } diff --git a/src/events.rs b/src/events.rs index 126d4d0..a617562 100644 --- a/src/events.rs +++ b/src/events.rs @@ -100,16 +100,8 @@ pub fn save(s: &mut Cursive) -> Result { }) .unwrap(); - if !new_path.exists() { - match fs::write(new_path.clone(), content) { - Ok(_) => {} - Err(e) => { - Into::::into(e).to_dialog(s); - return; - } - } - } else { - Error::AlreadyExists.to_dialog(s); + if let Err(e) = fs::write(new_path.clone(), content) { + Into::::into(e).to_dialog(s); return; } @@ -138,11 +130,7 @@ pub fn save(s: &mut Cursive) -> Result { }) .unwrap(); - if file_path.is_file() { - fs::write(file_path.clone(), content)?; - } else { - return Err(Error::FileOpen); - } + fs::write(file_path.clone(), content)?; s.call_on_name("title_text", |view: &mut MainPanel| { view.set_title(file_path.to_string_lossy()) diff --git a/src/main.rs b/src/main.rs index ec528c0..e5dc60a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,9 @@ use cursive::backends; use cursive::event::Event; use cursive::event::Key; -use cursive::logger; +use cursive::logger::reserve_logs; +use cursive::logger::CursiveLogger; +use cursive::reexports::log; use cursive::theme::BaseColor; use cursive::theme::BorderStyle; use cursive::theme::Color; @@ -47,8 +49,15 @@ fn backend() -> Box { /// Helper type of the main panel type MainPanel = Panel>>>>>; +fn logging() { + reserve_logs(1_000); + log::set_logger(&CursiveLogger).unwrap(); + log::set_max_level(log::LevelFilter::Error); +} + fn main() { - logger::init(); + logging(); + let mut siv = cursive::default(); let args: Vec = env::args().collect();