Skip to content
Closed
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
78 changes: 37 additions & 41 deletions src/ls/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
//

extern crate getopts;
extern crate number_prefix;
extern crate term_grid;
extern crate termsize;
extern crate time;
extern crate unicode_width;
extern crate number_prefix;
use number_prefix::{Standalone, Prefixed, decimal_prefix};
use number_prefix::{decimal_prefix, Prefixed, Standalone};
use term_grid::{Cell, Direction, Filling, Grid, GridOptions};
use time::{strftime, Timespec};

Expand All @@ -25,20 +25,19 @@ extern crate lazy_static;
#[macro_use]
extern crate uucore;
#[cfg(unix)]
use uucore::libc::{mode_t, S_ISGID, S_ISUID, S_ISVTX, S_IWOTH,
S_IXGRP, S_IXOTH, S_IXUSR};
use uucore::libc::{mode_t, S_ISGID, S_ISUID, S_ISVTX, S_IWOTH, S_IXGRP, S_IXOTH, S_IXUSR};

use std::fs;
use std::fs::{DirEntry, FileType, Metadata};
use std::path::{Path, PathBuf};
use std::cmp::Reverse;
#[cfg(unix)]
use std::collections::HashMap;
use std::fs;
use std::fs::{DirEntry, FileType, Metadata};
use std::path::{Path, PathBuf};

#[cfg(any(unix, target_os = "redox"))]
use std::os::unix::fs::MetadataExt;
#[cfg(unix)]
use std::os::unix::fs::FileTypeExt;
#[cfg(any(unix, target_os = "redox"))]
use std::os::unix::fs::MetadataExt;
#[cfg(unix)]
use unicode_width::UnicodeWidthStr;

Expand All @@ -58,12 +57,13 @@ static DEFAULT_COLORS: &str = "rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do

#[cfg(unix)]
lazy_static! {
static ref LS_COLORS: String = std::env::var("LS_COLORS").unwrap_or(DEFAULT_COLORS.to_string());
static ref LS_COLORS: String =
std::env::var("LS_COLORS").unwrap_or_else(|_| DEFAULT_COLORS.to_string());
static ref COLOR_MAP: HashMap<&'static str, &'static str> = {
let codes = LS_COLORS.split(":");
let codes = LS_COLORS.split(':');
let mut map = HashMap::new();
for c in codes {
let p: Vec<_> = c.split("=").collect();
let p: Vec<_> = c.split('=').collect();
if p.len() == 2 {
map.insert(p[0], p[1]);
}
Expand Down Expand Up @@ -169,7 +169,7 @@ fn list(options: getopts::Matches) {
let locs: Vec<String> = if options.free.is_empty() {
vec![String::from(".")]
} else {
options.free.iter().cloned().collect()
options.free.to_vec()
};

let mut files = Vec::<PathBuf>::new();
Expand Down Expand Up @@ -275,20 +275,18 @@ fn max(lhs: usize, rhs: usize) -> usize {
fn should_display(entry: &DirEntry, options: &getopts::Matches) -> bool {
let ffi_name = entry.file_name();
let name = ffi_name.to_string_lossy();
if !options.opt_present("a") && !options.opt_present("A") {
if name.starts_with('.') {
return false;
}
if !options.opt_present("a") && !options.opt_present("A") && name.starts_with('.') {
return false;
}
if options.opt_present("B") && name.ends_with('~') {
return false;
}
return true;
true
}

fn enter_directory(dir: &PathBuf, options: &getopts::Matches) {
let mut entries =
safe_unwrap!(fs::read_dir(dir).and_then(|e| e.collect::<Result<Vec<_>, _>>()));
let mut entries: std::vec::Vec<std::fs::DirEntry> =
safe_unwrap!(fs::read_dir(dir).and_then(std::iter::Iterator::collect));

entries.retain(|e| should_display(e, options));

Expand All @@ -314,7 +312,7 @@ fn enter_directory(dir: &PathBuf, options: &getopts::Matches) {

fn get_metadata(entry: &PathBuf, options: &getopts::Matches) -> std::io::Result<Metadata> {
if options.opt_present("L") {
entry.metadata().or(entry.symlink_metadata())
entry.metadata().or_else(|_| entry.symlink_metadata())
} else {
entry.symlink_metadata()
}
Expand All @@ -341,7 +339,7 @@ fn pad_left(string: String, count: usize) -> String {
}
}

fn display_items(items: &Vec<PathBuf>, strip: Option<&Path>, options: &getopts::Matches) {
fn display_items(items: &[PathBuf], strip: Option<&Path>, options: &getopts::Matches) {
if options.opt_present("long") || options.opt_present("numeric-uid-gid") {
let (mut max_links, mut max_size) = (1, 1);
for item in items {
Expand All @@ -354,19 +352,17 @@ fn display_items(items: &Vec<PathBuf>, strip: Option<&Path>, options: &getopts::
}
} else {
if !options.opt_present("1") {
let names = items
.iter()
.filter_map(|i| {
let md = get_metadata(i, options);
match md {
Err(e) => {
let filename = get_file_name(i, strip);
show_error!("{}: {}", filename, e);
None
}
Ok(md) => Some(display_file_name(&i, strip, &md, options)),
let names = items.iter().filter_map(|i| {
let md = get_metadata(i, options);
match md {
Err(e) => {
let filename = get_file_name(i, strip);
show_error!("{}: {}", filename, e);
None
}
});
Ok(md) => Some(display_file_name(&i, strip, &md, options)),
}
});

if let Some(size) = termsize::get() {
let mut grid = Grid::new(GridOptions {
Expand Down Expand Up @@ -452,7 +448,7 @@ fn display_uname(metadata: &Metadata, options: &getopts::Matches) -> String {
if options.opt_present("numeric-uid-gid") {
metadata.uid().to_string()
} else {
entries::uid2usr(metadata.uid()).unwrap_or(metadata.uid().to_string())
entries::uid2usr(metadata.uid()).unwrap_or_else(|_| metadata.uid().to_string())
}
}

Expand All @@ -461,7 +457,7 @@ fn display_group(metadata: &Metadata, options: &getopts::Matches) -> String {
if options.opt_present("numeric-uid-gid") {
metadata.gid().to_string()
} else {
entries::gid2grp(metadata.gid()).unwrap_or(metadata.gid().to_string())
entries::gid2grp(metadata.gid()).unwrap_or_else(|_| metadata.gid().to_string())
}
}

Expand Down Expand Up @@ -509,7 +505,7 @@ fn display_file_size(metadata: &Metadata, options: &getopts::Matches) -> String
if options.opt_present("human-readable") {
match decimal_prefix(metadata.len() as f64) {
Standalone(bytes) => bytes.to_string(),
Prefixed(prefix, bytes) => format!("{:.2}{}", bytes, prefix).to_uppercase()
Prefixed(prefix, bytes) => format!("{:.2}{}", bytes, prefix).to_uppercase(),
}
} else {
metadata.len().to_string()
Expand All @@ -531,7 +527,7 @@ fn get_file_name(name: &Path, strip: Option<&Path>) -> String {
Some(prefix) => name.strip_prefix(prefix).unwrap_or(name),
None => name,
};
if name.as_os_str().len() == 0 {
if name.as_os_str().is_empty() {
name = Path::new(".");
}
name.to_string_lossy().into_owned()
Expand Down Expand Up @@ -593,9 +589,9 @@ fn color_name(name: String, typ: &str) -> String {

#[cfg(unix)]
macro_rules! has {
($mode:expr, $perm:expr) => (
($mode:expr, $perm:expr) => {
$mode & ($perm as mode_t) != 0
)
};
}

#[cfg(unix)]
Expand Down Expand Up @@ -688,7 +684,7 @@ fn display_file_name(

Cell {
contents: name,
width: width,
width,
}
}

Expand Down