You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/main.rs
+33-12
Original file line number
Diff line number
Diff line change
@@ -88,7 +88,7 @@ enum Commands {
88
88
},
89
89
/// Display file or file system status.
90
90
Stat{
91
-
/// File or file system to display status.
91
+
/// File or file system to display status.
92
92
#[arg(required = true)]
93
93
file:Vec<path::PathBuf>,
94
94
},
@@ -111,11 +111,12 @@ fn main() -> Result<()> {
111
111
Some(Commands::Create{ file }) => {
112
112
// Create files.
113
113
for f in file {
114
-
fs::File::create_new(f).with_context(|| format!("Failed to create file: {:?}", f))?;
114
+
fs::File::create_new(f)
115
+
.with_context(|| format!("Failed to create file: {:?}", f))?;
115
116
}
116
117
Ok(())
117
118
}
118
-
Some(Commands::Echo{ string, n }) => {
119
+
Some(Commands::Echo{ string, n }) => {
119
120
// Echo strings
120
121
if*n {
121
122
for s in string {
@@ -128,50 +129,70 @@ fn main() -> Result<()> {
128
129
println!();
129
130
}
130
131
Ok(())
131
-
}
132
-
Some(Commands::Grep{ patterns, file }) => {
133
-
// Search for patterns
132
+
}// TODO: Add color to patterns that get matched and craete a recursive function to search directories for patterns and display the name of each file that matched.
133
+
Some(Commands::Grep{ patterns, file }) => {
134
134
for f in file {
135
135
if f.is_file(){
136
-
let contents = fs::read_to_string(f)?;
136
+
let contents = fs::read_to_string(f).with_context(|| format!("Failed to read the contents of {:?}", f))?;
137
137
for(number, line)in contents.lines().enumerate(){
138
138
if line.contains(patterns){
139
139
println!("{}: {}", number + 1, line);
140
140
}
141
141
}
142
+
}elseif f.is_dir(){
143
+
ifletOk(entries) = fs::read_dir(f){
144
+
for entry in entries {
145
+
ifletOk(entry) = entry {
146
+
if entry.path().is_file(){
147
+
let contents = fs::read_to_string(entry.path()).with_context(|| format!("Failed to read the contents of {:?}", entry.path()))?;
148
+
for(number, line)in contents.lines().enumerate(){
149
+
if line.contains(patterns){
150
+
println!("{}: {}", number + 1, line);
151
+
}
152
+
}
153
+
}
154
+
}
155
+
}
156
+
}
142
157
}
143
158
}
144
159
Ok(())
145
160
}
146
161
Some(Commands::Mkdir{ dir }) => {
147
162
// Create directories
148
163
for d in dir {
149
-
fs::create_dir_all(d).with_context(|| format!("Failed to create directory: {:?}", d))?;
164
+
fs::create_dir_all(d)
165
+
.with_context(|| format!("Failed to create directory: {:?}", d))?;
150
166
}
151
167
Ok(())
152
168
}
153
169
Some(Commands::Rm{ file, recursive }) => {
154
170
// Remove files.
155
171
for f in file {
156
172
if*recursive && fs::metadata(f)?.is_dir(){
157
-
fs::remove_dir_all(f).with_context(|| format!("Failed to remove directory recursively: {:?}", f))?;
173
+
fs::remove_dir_all(f).with_context(|| {
174
+
format!("Failed to remove directory recursively: {:?}", f)
175
+
})?;
158
176
}else{
159
-
fs::remove_file(f).with_context(|| format!("Failed to remove file: {:?}", f))?;
177
+
fs::remove_file(f)
178
+
.with_context(|| format!("Failed to remove file: {:?}", f))?;
160
179
}
161
180
}
162
181
Ok(())
163
182
}
164
183
Some(Commands::Rmdir{ dir }) => {
165
184
// Remove directories.
166
185
for d in dir {
167
-
fs::remove_dir(d).with_context(|| format!("Failed to remove directory: {:?}", d))?;
186
+
fs::remove_dir(d)
187
+
.with_context(|| format!("Failed to remove directory: {:?}", d))?;
168
188
}
169
189
Ok(())
170
190
}
171
191
Some(Commands::Stat{ file }) => {
172
192
// Display the stats for a given file or directory
173
193
for f in file {
174
-
let stats = fs::metadata(f).with_context(|| format!("Cannot get stats for {:?}", f))?;
194
+
let stats =
195
+
fs::metadata(f).with_context(|| format!("Cannot get stats for {:?}", f))?;
0 commit comments