grsh/src/ls.rs

45 lines
1.3 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;
2021-07-23 10:55:27 +00:00
let mut show_prev_and_current = 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;
2021-07-23 10:55:27 +00:00
show_prev_and_current = true;
},
2021-07-23 10:55:27 +00:00
"-A" | "--almost-all" => {
show_hidden = true;
show_prev_and_current = false;
}
arg => {
dir = Some(arg);
},
2021-07-21 16:23:55 +00:00
}
}
2021-07-23 10:55:27 +00:00
list_dir(dir.unwrap().to_string(), show_hidden, show_prev_and_current);
2021-07-21 16:23:55 +00:00
}
2021-07-23 10:55:27 +00:00
fn list_dir(arg: String, show_hidden: bool, show_prev_and_current: bool) {
2021-07-21 16:23:55 +00:00
let files = fs::read_dir(arg.clone()).unwrap();
2021-07-23 10:55:27 +00:00
if show_hidden && show_prev_and_current {
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());
}
}