Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement ls #104

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ members = [
"groups",
"head",
"id",
"ls",
"link",
"logname",
"mktemp",
Expand Down
12 changes: 12 additions & 0 deletions ls/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "ls"
version = "0.1.0"
authors = ["Lucas Leonardo <lucasleonardo@protonmail.com>"]
build = "build.rs"
edition = "2018"

[dependencies]
clap = { version = "^2.33.0", features = ["yaml", "wrap_help"] }

[build-dependencies]
clap = { version = "^2.33.0", features = ["yaml"] }
19 changes: 19 additions & 0 deletions ls/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use std::env;

use clap::{load_yaml, App, Shell};

fn main() {
let yaml = load_yaml!("src/ls.yml");
let mut app = App::from_yaml(yaml);

let out_dir = match env::var("OUT_DIR") {
Ok(dir) => dir,
_ => return,
};

app.gen_completions("ls", Shell::Zsh, out_dir.clone());
app.gen_completions("ls", Shell::Fish, out_dir.clone());
app.gen_completions("ls", Shell::Bash, out_dir.clone());
app.gen_completions("ls", Shell::PowerShell, out_dir.clone());
app.gen_completions("ls", Shell::Elvish, out_dir);
}
16 changes: 16 additions & 0 deletions ls/src/ls.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: ls
version: "0.1"
author: "Lucas Leonardo <lucasleonardo@protonmail.com>"
about: List files and directories
args:
- all:
help: Show hidden
short: a
long: all
- long:
help: Show long, detailed form
short: l
- FILE:
long_help: "Directory to be shown"
required: false
multiple: true
80 changes: 80 additions & 0 deletions ls/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
use std::{
env::current_dir,
fs::{self, DirEntry},
io, process,
};

use clap::{load_yaml, App, ArgMatches};

#[derive(Debug, Clone, Copy)]
struct LsFlags {
pub all: bool,
}

impl LsFlags {
pub fn from_matches(matches: &ArgMatches) -> Self {
let flags = LsFlags { all: matches.is_present("all") };

return flags;
}
}

fn main() {
let yaml = load_yaml!("ls.yml");
let matches = App::from_yaml(yaml).get_matches();
let flags = LsFlags::from_matches(&matches);

let cwd = match current_dir() {
Ok(path) => path.into_os_string().into_string().unwrap(),
Err(err) => {
eprintln!("ls: error reading current working directory: {}", err);
process::exit(1);
},
};

let mut dirs: Vec<String> = match matches.values_of("FILE") {
Some(dirs) => dirs.map(String::from).collect(),
None => vec![],
};

if dirs.is_empty() {
dirs.push(cwd);
}


match ls(dirs, flags) {
Ok(()) => {},
Err(msg) => {
eprintln!("ls: {}", msg);
process::exit(1);
},
};
}

fn ls(dirs: Vec<String>, flags: LsFlags) -> io::Result<()> {
for (_, dir) in dirs.iter().enumerate() {
let paths = match fs::read_dir(dir) {
Ok(paths) => paths,
Err(err) => {
eprintln!("ls: couldn't read {}: {}", dir, err);
process::exit(1);
},
};

let mut files: Vec<DirEntry> = paths.map(|f| f.unwrap()).collect();
files.sort_by_key(|dir| dir.path());

for entry in files {
// let metadata = entry.metadata()?;
// let permissions = metadata.permissions();
let name = entry.file_name().into_string().unwrap();

if name.starts_with(".") && !flags.all {
continue;
}

println!("{}", name);
}
}
Ok(())
}