made util function for finding files with index
This commit is contained in:
21
src/utils.rs
21
src/utils.rs
@@ -3,6 +3,7 @@ use std::{
|
||||
fs,
|
||||
io::{self, Write},
|
||||
path::Path,
|
||||
process,
|
||||
};
|
||||
use xdg::BaseDirectories;
|
||||
|
||||
@@ -118,3 +119,23 @@ pub fn match_file_name(file_name: &str, shelf: &mut Shelf) -> Result<usize, std:
|
||||
format!("File not Found in Shelf \"{}\": {file_name}", shelf.name),
|
||||
))
|
||||
}
|
||||
|
||||
pub fn match_file_name_or_index(file: &str, shelf: &mut Shelf) -> Result<usize, std::io::Error> {
|
||||
let index: usize = match file.parse::<usize>() {
|
||||
Ok(index) => {
|
||||
println!("path is: {}", &shelf.content[*&index]);
|
||||
index
|
||||
}
|
||||
Err(_) => match match_file_name(&file, shelf) {
|
||||
Ok(index) => {
|
||||
println!("path is: {}", &shelf.content[*&index as usize]);
|
||||
index
|
||||
}
|
||||
Err(e) => {
|
||||
eprintln!("{e}");
|
||||
process::exit(1)
|
||||
}
|
||||
},
|
||||
};
|
||||
Ok(index)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user