Browse Source

feat: add ls command

main
fdai7375 2 years ago
committed by fdai7451
parent
commit
893280cfbe
  1. 28
      src/builtins/ls.rs
  2. 2
      src/builtins/mod.rs

28
src/builtins/ls.rs

@ -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(())
}
}

2
src/builtins/mod.rs

@ -6,6 +6,7 @@ mod cd;
mod change_prompt; mod change_prompt;
mod exit; mod exit;
mod fetch; mod fetch;
mod ls;
mod open; mod open;
mod pwd; mod pwd;
@ -33,6 +34,7 @@ const BUILTINS: Lazy<Vec<(&str, Box<dyn Builtin>)>> = Lazy::new(|| {
("pwd", Box::new(pwd::Pwd)), ("pwd", Box::new(pwd::Pwd)),
("open", Box::new(open::Open)), ("open", Box::new(open::Open)),
("fetch", Box::new(fetch::Fetch)), ("fetch", Box::new(fetch::Fetch)),
("ls", Box::new(ls::Ls)),
] ]
}); });

Loading…
Cancel
Save