@@ -19,6 +19,7 @@ use path::Path;
19
19
pub fn main ( args : Vec < String > ) -> isize {
20
20
let mut opts = Options :: new ( ) ;
21
21
opts. optflag ( "h" , "help" , "print this help menu" ) ;
22
+ opts. optflag ( "s" , "size" , "print the size of each file in directory" ) ;
22
23
23
24
let matches = match opts. parse ( args) {
24
25
Ok ( m) => m,
@@ -34,13 +35,16 @@ pub fn main(args: Vec<String>) -> isize {
34
35
return 0 ;
35
36
}
36
37
38
+ let size_option = matches. opt_present ( "s" ) ;
39
+
37
40
let Ok ( curr_wd) = task:: with_current_task ( |t| t. get_env ( ) . lock ( ) . working_dir . clone ( ) ) else {
38
41
println ! ( "failed to get current task" ) ;
39
42
return -1 ;
40
43
} ;
44
+
41
45
// print children of working directory if no child is specified
42
46
if matches. free . is_empty ( ) {
43
- print_children ( & curr_wd) ;
47
+ print_children ( & curr_wd, size_option ) ;
44
48
return 0 ;
45
49
}
46
50
@@ -49,7 +53,7 @@ pub fn main(args: Vec<String>) -> isize {
49
53
// Navigate to the path specified by first argument
50
54
match path. get ( & curr_wd) {
51
55
Some ( FileOrDir :: Dir ( dir) ) => {
52
- print_children ( & dir) ;
56
+ print_children ( & dir, size_option ) ;
53
57
0
54
58
}
55
59
Some ( FileOrDir :: File ( file) ) => {
@@ -63,12 +67,25 @@ pub fn main(args: Vec<String>) -> isize {
63
67
}
64
68
}
65
69
66
- fn print_children ( dir : & DirRef ) {
70
+ fn print_children ( dir : & DirRef , print_size : bool ) {
67
71
let mut child_string = String :: new ( ) ;
68
72
let mut child_list = dir. lock ( ) . list ( ) ;
69
73
child_list. reverse ( ) ;
70
74
for child in child_list. iter ( ) {
71
- writeln ! ( child_string, "{child}" ) . expect ( "Failed to write child_string" ) ;
75
+ let child_path = dir. lock ( ) . get ( child) . expect ( "Failed to get child path" ) ;
76
+ if print_size {
77
+ match & child_path {
78
+ FileOrDir :: File ( file_ref) => {
79
+ let file = file_ref. lock ( ) ;
80
+ writeln ! ( child_string, " {} {}" , file. len( ) , child) . expect ( "Failed to write child_string" ) ;
81
+ } ,
82
+ FileOrDir :: Dir ( _) => {
83
+ writeln ! ( child_string, " -- {}" , child) . expect ( "Failed to write child_string" ) ;
84
+ } ,
85
+ } ;
86
+ } else {
87
+ writeln ! ( child_string, "{}" , child) . expect ( "Failed to write child_string" ) ;
88
+ }
72
89
}
73
90
println ! ( "{}" , child_string) ;
74
91
}
@@ -80,4 +97,4 @@ fn print_usage(opts: Options) {
80
97
81
98
const USAGE : & str = "Usage: ls [DIR | FILE]
82
99
List the contents of the given directory or info about the given file.
83
- If no arguments are provided, it lists the contents of the current directory." ;
100
+ If no arguments are provided, it lists the contents of the current directory." ;
0 commit comments