grsh/src/ls.rs

39 lines
1.0 KiB
Rust
Raw Normal View History

2021-07-21 16:23:55 +00:00
use std::env;
use std::fs;
fn main(){
let args: Vec<String> = env::args().collect();
let pwd = env::current_dir().unwrap();
let mut show_hidden = false;
let mut dir = pwd.to_str();
for arg in 1..args.len() {
match args[arg].as_str() {
"--help" => {
println!("List files in directory");
},
"-a" | "--all" => {
show_hidden = true;
},
arg => {
dir = Some(arg);
},
2021-07-21 16:23:55 +00:00
}
}
list_dir(dir.unwrap().to_string(), show_hidden);
2021-07-21 16:23:55 +00:00
}
fn list_dir(arg: String, show_hidden: bool) {
let files = fs::read_dir(arg.clone()).unwrap();
if show_hidden {
println!(".");
println!("..");
}
2021-07-21 16:23:55 +00:00
for entry in files {
let path = entry.unwrap().path();
let file = path.strip_prefix(arg.clone());
if file.clone().unwrap().display().to_string().starts_with('.') && !show_hidden {
2021-07-21 16:23:55 +00:00
continue;
}
println!("{}", file.unwrap().display());
}
}