Skip to content

Commit

Permalink
🔧 Refactor && Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nwrenger committed Mar 27, 2024
1 parent 82d9839 commit ff80a19
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 34 deletions.
51 changes: 34 additions & 17 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -29,20 +29,20 @@ impl fmt::Display for Error {
}
}

impl From<clippers::Error> for Error {
fn from(value: clippers::Error) -> Self {
error!("clippers::Error: {value:?}");
Self::Clipboard
}
}

impl From<std::convert::Infallible> for Error {
fn from(e: std::convert::Infallible) -> Self {
error!("convert::Infallible: {e:?}");
Self::Arguments
}
}

impl From<clippers::Error> for Error {
fn from(value: clippers::Error) -> Self {
error!("clippers::Error: {value:?}");
Self::Clipboard
}
}

impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
error!("File Error: {e}");
Expand All @@ -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"),
);
}
}
}
}

Expand Down
18 changes: 3 additions & 15 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,8 @@ pub fn save(s: &mut Cursive) -> Result<bool> {
})
.unwrap();

if !new_path.exists() {
match fs::write(new_path.clone(), content) {
Ok(_) => {}
Err(e) => {
Into::<Error>::into(e).to_dialog(s);
return;
}
}
} else {
Error::AlreadyExists.to_dialog(s);
if let Err(e) = fs::write(new_path.clone(), content) {
Into::<Error>::into(e).to_dialog(s);
return;
}

Expand Down Expand Up @@ -138,11 +130,7 @@ pub fn save(s: &mut Cursive) -> Result<bool> {
})
.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())
Expand Down
13 changes: 11 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -47,8 +49,15 @@ fn backend() -> Box<BufferedBackend> {
/// Helper type of the main panel
type MainPanel = Panel<OnEventView<ResizedView<NamedView<ScrollView<NamedView<TextArea>>>>>>;

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<String> = env::args().collect();

Expand Down

0 comments on commit ff80a19

Please sign in to comment.