@@ -124,6 +124,10 @@ fn get_sort_type(sort_t: [bool; 4]) -> DirSortType {
124124
125125impl 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
432407fn 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