|
|
@ -0,0 +1,28 @@ |
|
|
|
use std::{fs::File, io::stdout, path::PathBuf, str::FromStr};
|
|
|
|
|
|
|
|
use crate::error::ShellError;
|
|
|
|
|
|
|
|
use super::{Builtin, BuiltinConfig};
|
|
|
|
|
|
|
|
pub struct Ls;
|
|
|
|
|
|
|
|
impl Builtin for Ls {
|
|
|
|
fn execute(&mut self, _: &mut BuiltinConfig, args: Vec<String>) -> Result<(), ShellError> {
|
|
|
|
let dir = std::env::current_dir().unwrap_or(PathBuf::from_str("/").unwrap());
|
|
|
|
let entries = match std::fs::read_dir(dir) {
|
|
|
|
Ok(e) => e,
|
|
|
|
Err(e) => return Err(ShellError::ExecuteFailure(e.to_string())),
|
|
|
|
};
|
|
|
|
for entry in entries {
|
|
|
|
let entry = match entry {
|
|
|
|
Ok(e) => e,
|
|
|
|
Err(e) => {
|
|
|
|
println!("Couldn't get directory entry");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
println!("{}", entry.file_name().to_string_lossy())
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|