Skip to content

Commit 2d18a21

Browse files
committed
added actual error when path is not found
1 parent de10c8b commit 2d18a21

File tree

3 files changed

+8
-32
lines changed

3 files changed

+8
-32
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "natls"
3-
version = "2.1.9"
3+
version = "2.1.10"
44
authors = ["Will Lane <[email protected]>"]
55
description = "nat - the 'ls' replacement you never knew you needed"
66
license = "MIT"

src/main.rs

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,10 @@ fn get_sort_type(sort_t: [bool; 4]) -> DirSortType {
124124

125125
impl Directory {
126126
fn new(dir: std::path::PathBuf) -> Result<Self, Box<dyn std::error::Error>> {
127+
if !std::path::Path::new(&dir).exists() {
128+
println!("{}OS Error (2): Path does not exist.", termion::color::Fg(termion::color::Red));
129+
std::process::exit(1);
130+
}
127131
if !std::path::Path::new(&dir).is_dir() {
128132
let f = File::new(dir.clone());
129133
match input::Cli::from_args().long {
@@ -132,40 +136,11 @@ impl Directory {
132136
}
133137
std::process::exit(0)
134138
}
135-
if !std::path::Path::new(&dir).exists() {
136-
let mut new_paths = Vec::new();
137-
let paths = std::fs::read_dir(".")?
138-
.map(|res| res.map(|e| e.path()))
139-
.collect::<Result<Vec<std::path::PathBuf>, std::io::Error>>()?;
140-
141-
for p in paths {
142-
if p
143-
.file_name()
144-
.unwrap()
145-
.to_str()
146-
.unwrap()
147-
.to_lowercase()
148-
.contains(&dir.display().to_string().to_lowercase())
149-
{
150-
new_paths.push(File::new(p))
151-
}
152-
}
153139

154-
if new_paths.is_empty() {
155-
println!(
156-
"{}Path could not be found",
157-
termion::color::Fg(termion::color::Red)
158-
);
159-
std::process::exit(1)
160-
}
161-
162-
Ok(Self { paths: new_paths })
163-
} else {
164-
let paths = std::fs::read_dir(dir)?
140+
let paths = std::fs::read_dir(dir)?
165141
.map(|res| res.map(|e| File::new(e.path())))
166142
.collect::<Result<Vec<File>, std::io::Error>>()?;
167143
Ok(Self { paths })
168-
}
169144
}
170145

171146
fn self_name_sort(&mut self) {
@@ -431,6 +406,7 @@ impl std::fmt::Display for Directory {
431406

432407
fn main() {
433408
let mut dir = Directory::new(input::Cli::from_args().dir).expect("Failed to run natls");
409+
// let mut dir = Directory::new(std::path::PathBuf::from("wrong path")).expect("Failed to run natls");
434410
dir.setup();
435411
println!("{}", dir);
436412
}

0 commit comments

Comments
 (0)