Skip to content

Commit e67ea4c

Browse files
committed
add more automatic error handling
1 parent c8709ff commit e67ea4c

6 files changed

Lines changed: 48 additions & 50 deletions

File tree

src/commands/bulk_rename.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,7 @@ const ENV_EDITOR: &str = "EDITOR";
1414

1515
pub fn _bulk_rename(context: &mut AppContext) -> JoshutoResult<()> {
1616
const PREFIX: &str = "joshuto-";
17-
let editor = match std::env::var(ENV_EDITOR) {
18-
Ok(s) => s,
19-
Err(_) => {
20-
return Err(JoshutoError::new(
21-
JoshutoErrorKind::EnvVarNotPresent,
22-
format!("{} environment variable not set", ENV_EDITOR),
23-
));
24-
}
25-
};
17+
let editor = std::env::var(ENV_EDITOR)?;
2618

2719
/* generate a random file name to write to */
2820
let mut rand_str = String::with_capacity(PREFIX.len() + 10);
@@ -36,13 +28,11 @@ pub fn _bulk_rename(context: &mut AppContext) -> JoshutoResult<()> {
3628
let mut file_path = path::PathBuf::from("/tmp");
3729
file_path.push(rand_str);
3830

39-
let paths = {
40-
let curr_tab = context.tab_context_ref().curr_tab_ref();
41-
match curr_tab.curr_list_ref() {
42-
Some(s) => s.get_selected_paths(),
43-
None => vec![],
44-
}
45-
};
31+
let paths = context
32+
.tab_context_ref()
33+
.curr_tab_ref()
34+
.curr_list_ref()
35+
.map_or(vec![], |s| s.get_selected_paths());
4636

4737
{
4838
let mut file = std::fs::File::create(&file_path)?;
@@ -57,13 +47,14 @@ pub fn _bulk_rename(context: &mut AppContext) -> JoshutoResult<()> {
5747
let mut command = process::Command::new(editor);
5848
command.arg(&file_path);
5949

60-
let time = std::time::SystemTime::now();
50+
let last_modified = std::fs::metadata(&file_path)?.modified()?;
6151
{
6252
let mut handle = command.spawn()?;
6353
handle.wait()?;
6454
}
55+
// check if the file was modified since it was created
6556
let metadata = std::fs::metadata(&file_path)?;
66-
if time >= metadata.modified()? {
57+
if metadata.modified()? <= last_modified {
6758
return Ok(());
6859
}
6960

src/commands/open_file.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ pub fn open(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult
3535
change_directory::cd(path.as_path(), context)?;
3636
LoadChild::load_child(context)?;
3737
} else {
38-
let paths: Vec<path::PathBuf> =
39-
match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
40-
Some(a) => a.get_selected_paths(),
41-
None => vec![],
42-
};
38+
let paths = context
39+
.tab_context_ref()
40+
.curr_tab_ref()
41+
.curr_list_ref()
42+
.map_or(vec![], |s| s.get_selected_paths());
43+
4344
if paths.is_empty() {
4445
return Err(JoshutoError::new(
4546
JoshutoErrorKind::Io(io::ErrorKind::NotFound),
@@ -133,10 +134,12 @@ where
133134
}
134135

135136
pub fn open_with(context: &mut AppContext, backend: &mut TuiBackend) -> JoshutoResult<()> {
136-
let paths: Vec<path::PathBuf> = match context.tab_context_ref().curr_tab_ref().curr_list_ref() {
137-
Some(a) => a.get_selected_paths(),
138-
None => vec![],
139-
};
137+
let paths = context
138+
.tab_context_ref()
139+
.curr_tab_ref()
140+
.curr_list_ref()
141+
.map_or(vec![], |s| s.get_selected_paths());
142+
140143
if paths.is_empty() {
141144
return Err(JoshutoError::new(
142145
JoshutoErrorKind::Io(io::ErrorKind::NotFound),

src/commands/rename_file.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@ pub fn _rename_file(
2626
}
2727

2828
pub fn rename_file(context: &mut AppContext, dest: &path::Path) -> JoshutoResult<()> {
29-
let mut path: Option<path::PathBuf> = None;
30-
31-
if let Some(s) = context
29+
let path: Option<path::PathBuf> = context
3230
.tab_context_ref()
3331
.curr_tab_ref()
3432
.curr_list_ref()
3533
.and_then(|s| s.curr_entry_ref())
36-
{
37-
path = Some(s.file_path().to_path_buf());
38-
}
34+
.and_then(|s| Some(s.file_path().to_path_buf()));
3935

4036
if let Some(path) = path {
4137
_rename_file(context, path.as_path(), dest)?;
@@ -49,15 +45,13 @@ pub fn _rename_file_append(
4945
backend: &mut TuiBackend,
5046
file_name: &str,
5147
) -> JoshutoResult<()> {
52-
let prefix;
53-
let suffix;
54-
if let Some(ext) = file_name.rfind('.') {
55-
prefix = format!("rename {}", &file_name[0..ext]);
56-
suffix = String::from(&file_name[ext..]);
57-
} else {
58-
prefix = format!("rename {}", file_name);
59-
suffix = String::new();
60-
}
48+
let (prefix, suffix): (String, String) = match file_name.rfind('.') {
49+
Some(ext) => (
50+
format!("rename {}", &file_name[0..ext]),
51+
file_name[ext..].to_string(),
52+
),
53+
None => (format!("rename {}", file_name), "".to_string()),
54+
};
6155
command_line::readline(context, backend, &prefix, &suffix)
6256
}
6357

src/error/error_kind.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,9 @@ impl std::convert::From<&globset::ErrorKind> for JoshutoErrorKind {
3131
Self::Glob
3232
}
3333
}
34+
35+
impl std::convert::From<std::env::VarError> for JoshutoErrorKind {
36+
fn from(_: std::env::VarError) -> Self {
37+
Self::EnvVarNotPresent
38+
}
39+
}

src/error/error_type.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,15 @@ impl std::convert::From<globset::Error> for JoshutoError {
4242
}
4343
}
4444

45+
impl std::convert::From<std::env::VarError> for JoshutoError {
46+
fn from(err: std::env::VarError) -> Self {
47+
Self {
48+
_kind: JoshutoErrorKind::from(err),
49+
_cause: "Environment variable not found".to_string(),
50+
}
51+
}
52+
}
53+
4554
impl std::convert::From<trash::Error> for JoshutoError {
4655
fn from(err: trash::Error) -> Self {
4756
let err = match err {

src/main.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use crate::config::{
2121
AppConfig, AppKeyMapping, AppMimetypeRegistry, AppTheme, ConfigStructure, JoshutoPreview,
2222
};
2323
use crate::context::AppContext;
24-
use crate::error::{JoshutoError, JoshutoErrorKind};
24+
use crate::error::JoshutoError;
2525
use crate::run::run;
2626

2727
const PROGRAM_NAME: &str = "joshuto";
@@ -67,12 +67,8 @@ pub struct Args {
6767
fn run_joshuto(args: Args) -> Result<(), JoshutoError> {
6868
if args.version {
6969
let version = env!("CARGO_PKG_VERSION");
70-
println!("{}", version);
71-
let err = JoshutoError::new(
72-
JoshutoErrorKind::EnvVarNotPresent,
73-
"CARGO_PKG_VERSION variable not found".to_string(),
74-
);
75-
return Err(err);
70+
println!("{}-{}", PROGRAM_NAME, version);
71+
return Ok(());
7672
}
7773
if let Some(p) = args.path.as_ref() {
7874
match std::env::set_current_dir(p.as_path()) {
@@ -106,7 +102,6 @@ fn run_joshuto(args: Args) -> Result<(), JoshutoError> {
106102
)?;
107103
file.write_all("\n".as_bytes())?;
108104
}
109-
110105
Ok(())
111106
}
112107

0 commit comments

Comments
 (0)