3
3
mod input;
4
4
mod text_effects;
5
5
mod utils;
6
- use std:: os:: unix:: fs:: { FileTypeExt , MetadataExt } ;
7
- use structopt:: StructOpt ;
6
+ use std:: os:: unix:: fs:: { FileTypeExt , MetadataExt } ; use structopt:: StructOpt ;
8
7
use std:: cmp:: Ordering ;
9
8
10
9
struct Directory {
11
10
paths : Vec < File > ,
11
+ args : input:: Cli ,
12
12
}
13
13
14
14
#[ derive( Clone ) ]
@@ -104,12 +104,12 @@ impl PathType {
104
104
}
105
105
106
106
impl File {
107
- fn new ( file : std:: path:: PathBuf ) -> Self {
107
+ fn new ( file : std:: path:: PathBuf , time_format : String ) -> Self {
108
108
Self {
109
109
group : utils:: group ( file. to_path_buf ( ) ) ,
110
110
user : utils:: user ( file. to_path_buf ( ) ) ,
111
- modified : utils:: file_times:: modified ( file. to_path_buf ( ) , input :: Cli :: from_args ( ) . time_format ) ,
112
- created : utils:: file_times:: created ( file. to_path_buf ( ) , input :: Cli :: from_args ( ) . time_format ) ,
111
+ modified : utils:: file_times:: modified ( file. to_path_buf ( ) , time_format . to_owned ( ) ) ,
112
+ created : utils:: file_times:: created ( file. to_path_buf ( ) , time_format) ,
113
113
size : utils:: size:: size ( file. to_path_buf ( ) ) ,
114
114
perms : utils:: perms:: perms ( file. to_path_buf ( ) ) ,
115
115
file_type : PathType :: new ( & file) . unwrap ( ) ,
@@ -134,7 +134,9 @@ fn get_sort_type(sort_t: [bool; 4]) -> DirSortType {
134
134
}
135
135
136
136
impl Directory {
137
- fn new ( dir : std:: path:: PathBuf ) -> Result < Self , Box < dyn std:: error:: Error > > {
137
+ fn new ( args : input:: Cli ) -> Result < Self , Box < dyn std:: error:: Error > > {
138
+ let dir = & args. dir ;
139
+
138
140
if !std:: path:: Path :: new ( & dir) . exists ( ) {
139
141
return Err (
140
142
Box :: new (
@@ -144,18 +146,22 @@ impl Directory {
144
146
}
145
147
146
148
if !std:: path:: Path :: new ( & dir) . is_dir ( ) {
147
- let f = File :: new ( dir. clone ( ) ) ;
148
- match input :: Cli :: from_args ( ) . long {
149
+ let f = File :: new ( dir. to_owned ( ) , args . time_format ) ;
150
+ match args . long {
149
151
true => print ! ( "{:?}" , f) ,
150
152
_ => print ! ( "{}" , f)
151
153
}
152
154
std:: process:: exit ( 0 )
153
155
}
154
156
155
157
let paths = std:: fs:: read_dir ( dir) ?
156
- . map ( |res| res. map ( |e| File :: new ( e. path ( ) ) ) )
158
+ . map ( |res| res. map ( |e| File :: new (
159
+ e. path ( ) , args. time_format . to_owned ( )
160
+ )
161
+ )
162
+ )
157
163
. collect :: < Result < Vec < File > , std:: io:: Error > > ( ) ?;
158
- Ok ( Self { paths } )
164
+ Ok ( Self { paths, args } )
159
165
}
160
166
161
167
@@ -165,17 +171,17 @@ impl Directory {
165
171
let mut directories = Vec :: new ( ) ;
166
172
for ( i, f) in new. iter ( ) . enumerate ( ) {
167
173
if f. path . symlink_metadata ( ) . unwrap ( ) . is_dir ( ) {
168
- directories. push ( new[ i] . clone ( ) ) ;
174
+ directories. push ( new[ i] . to_owned ( ) ) ;
169
175
} else {
170
- newer. push ( new[ i] . clone ( ) )
176
+ newer. push ( new[ i] . to_owned ( ) )
171
177
}
172
178
}
173
179
174
180
match get_sort_type ( [
175
- input :: Cli :: from_args ( ) . name ,
176
- input :: Cli :: from_args ( ) . created ,
177
- input :: Cli :: from_args ( ) . modified ,
178
- input :: Cli :: from_args ( ) . size ,
181
+ self . args . name ,
182
+ self . args . created ,
183
+ self . args . modified ,
184
+ self . args . size ,
179
185
] ) {
180
186
DirSortType :: Name => {
181
187
name_sort ( & mut directories) ;
@@ -202,10 +208,10 @@ impl Directory {
202
208
203
209
fn sort_paths ( & mut self ) {
204
210
match get_sort_type ( [
205
- input :: Cli :: from_args ( ) . name ,
206
- input :: Cli :: from_args ( ) . created ,
207
- input :: Cli :: from_args ( ) . modified ,
208
- input :: Cli :: from_args ( ) . size ,
211
+ self . args . name ,
212
+ self . args . created ,
213
+ self . args . modified ,
214
+ self . args . size ,
209
215
] ) {
210
216
DirSortType :: Name => sort_as ( & mut self . paths , |a, b| {
211
217
a. path
@@ -265,7 +271,7 @@ impl Directory {
265
271
266
272
267
273
fn sort ( & mut self ) {
268
- match input :: Cli :: from_args ( ) . gdf {
274
+ match self . args . gdf {
269
275
true => self . sort_directory_then_path ( ) ,
270
276
false => self . sort_paths ( ) ,
271
277
}
@@ -288,9 +294,9 @@ impl Directory {
288
294
}
289
295
290
296
for p in 0 ..self . paths . iter ( ) . len ( ) {
291
- let ghold = self . paths [ p] . group . clone ( ) ;
292
- let uhold = self . paths [ p] . user . clone ( ) ;
293
- let shold = self . paths [ p] . size . clone ( ) ;
297
+ let ghold = self . paths [ p] . group . to_owned ( ) ;
298
+ let uhold = self . paths [ p] . user . to_owned ( ) ;
299
+ let shold = self . paths [ p] . size . to_owned ( ) ;
294
300
let mut gwidth = String :: new ( ) ;
295
301
for _ in 0 ..( gs - ghold. len ( ) ) {
296
302
gwidth. push ( ' ' )
@@ -433,7 +439,7 @@ impl std::fmt::Debug for File {
433
439
impl std:: fmt:: Display for Directory {
434
440
fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
435
441
Ok ( for i in self . paths . iter ( ) {
436
- match input :: Cli :: from_args ( ) . long {
442
+ match self . args . long {
437
443
true => write ! ( f, "{:?}" , i) ?,
438
444
_ => write ! ( f, "{} " , i) ?,
439
445
}
@@ -443,7 +449,7 @@ impl std::fmt::Display for Directory {
443
449
444
450
fn main ( ) {
445
451
println ! ( "{}" ,
446
- match Directory :: new( input:: Cli :: from_args( ) . dir ) {
452
+ match Directory :: new( input:: Cli :: from_args( ) ) {
447
453
Ok ( mut res) => format!( "{}" , res. setup( ) ) ,
448
454
Err ( err) => format!( "{}{}" , termion:: color:: Fg ( termion:: color:: Red ) , err)
449
455
}
0 commit comments