mirror of
https://github.com/uutils/coreutils
synced 2024-11-16 01:38:04 +00:00
ls: rustfmt the code
This commit is contained in:
parent
2713758b52
commit
0394d5398d
1 changed files with 52 additions and 33 deletions
85
src/ls/ls.rs
85
src/ls/ls.rs
|
@ -1,13 +1,12 @@
|
|||
#![crate_name = "uu_ls"]
|
||||
|
||||
/*
|
||||
* This file is part of the uutils coreutils package.
|
||||
*
|
||||
* (c) Jeremiah Peschka <jeremiah.peschka@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE file
|
||||
* that was distributed with this source code.
|
||||
*/
|
||||
// This file is part of the uutils coreutils package.
|
||||
//
|
||||
// (c) Jeremiah Peschka <jeremiah.peschka@gmail.com>
|
||||
//
|
||||
// For the full copyright and license information, please view the LICENSE file
|
||||
// that was distributed with this source code.
|
||||
//
|
||||
|
||||
extern crate getopts;
|
||||
extern crate pretty_bytes;
|
||||
|
@ -22,7 +21,7 @@ use self::libc::c_char;
|
|||
use getopts::Options;
|
||||
use std::fs;
|
||||
use std::fs::{ReadDir, DirEntry, FileType, Metadata};
|
||||
use std::ffi::{OsString,CStr};
|
||||
use std::ffi::{OsString, CStr};
|
||||
use std::path::Path;
|
||||
use std::io::Write;
|
||||
use std::ptr;
|
||||
|
@ -31,7 +30,7 @@ use std::ptr;
|
|||
enum Mode {
|
||||
Help,
|
||||
Version,
|
||||
List
|
||||
List,
|
||||
}
|
||||
|
||||
static NAME: &'static str = "ls";
|
||||
|
@ -43,19 +42,37 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
|||
opts.optflag("", "help", "display this help and exit");
|
||||
opts.optflag("", "version", "output version information and exit");
|
||||
|
||||
opts.optflag("a", "all", "Do not ignore hidden files (files with names that start with '.').");
|
||||
opts.optflag("A", "almost-all", "In a directory, do not ignore all file names that start with '.', only ignore '.' and '..'.");
|
||||
opts.optflag("B", "ignore-backups", "Ignore files that end with ~. Equivalent to using `--ignore='*~'` or `--ignore='.*~'.");
|
||||
opts.optflag("d", "directory", "Only list the names of directories, rather than listing directory contents. This will not follow symbolic links unless one of `--dereference-command-line (-H)`, `--dereference (-L)`, or `--dereference-command-line-symlink-to-dir` is specified.");
|
||||
opts.optflag("H", "dereference-command-line", "If a command line argument specifies a symbolic link, show information about the linked file rather than the link itself.");
|
||||
opts.optflag("h", "human-readable", "Print human readable file sizes (e.g. 1K 234M 56G).");
|
||||
opts.optflag("a",
|
||||
"all",
|
||||
"Do not ignore hidden files (files with names that start with '.').");
|
||||
opts.optflag("A",
|
||||
"almost-all",
|
||||
"In a directory, do not ignore all file names that start with '.', only ignore \
|
||||
'.' and '..'.");
|
||||
opts.optflag("B",
|
||||
"ignore-backups",
|
||||
"Ignore files that end with ~. Equivalent to using `--ignore='*~'` or \
|
||||
`--ignore='.*~'.");
|
||||
opts.optflag("d",
|
||||
"directory",
|
||||
"Only list the names of directories, rather than listing directory contents. \
|
||||
This will not follow symbolic links unless one of `--dereference-command-line \
|
||||
(-H)`, `--dereference (-L)`, or `--dereference-command-line-symlink-to-dir` is \
|
||||
specified.");
|
||||
opts.optflag("H",
|
||||
"dereference-command-line",
|
||||
"If a command line argument specifies a symbolic link, show information about \
|
||||
the linked file rather than the link itself.");
|
||||
opts.optflag("h",
|
||||
"human-readable",
|
||||
"Print human readable file sizes (e.g. 1K 234M 56G).");
|
||||
|
||||
let matches = match opts.parse(&args[1..]) {
|
||||
Ok(m) => m,
|
||||
Err(e) => {
|
||||
show_error!("{}", e);
|
||||
panic!()
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
let mode = if matches.opt_present("version") {
|
||||
|
@ -68,15 +85,15 @@ pub fn uumain(args: Vec<String>) -> i32 {
|
|||
|
||||
match mode {
|
||||
Mode::Version => version(),
|
||||
Mode::Help => help(),
|
||||
Mode::List => list(matches)
|
||||
Mode::Help => help(),
|
||||
Mode::List => list(matches),
|
||||
}
|
||||
|
||||
0
|
||||
}
|
||||
|
||||
fn version() {
|
||||
println!("{} {}", NAME, VERSION);
|
||||
println!("{} {}", NAME, VERSION);
|
||||
}
|
||||
|
||||
fn help() {
|
||||
|
@ -87,7 +104,9 @@ fn help() {
|
|||
By default, ls will list the files and contents of any directories on \
|
||||
the command line, expect that it will ignore files and directories \
|
||||
whose names start with '.'. \n\
|
||||
\n", NAME, VERSION);
|
||||
\n",
|
||||
NAME,
|
||||
VERSION);
|
||||
println!("{}", msg);
|
||||
}
|
||||
|
||||
|
@ -108,10 +127,10 @@ fn list(options: getopts::Matches) {
|
|||
|
||||
if p.is_dir() {
|
||||
match fs::read_dir(p) {
|
||||
Err(e) => {
|
||||
Err(e) => {
|
||||
show_error!("Cannot read directory '{}'. \n Reason: {}", loc, e);
|
||||
panic!();
|
||||
},
|
||||
}
|
||||
Ok(entries) => enter_directory(entries, &options),
|
||||
};
|
||||
}
|
||||
|
@ -128,8 +147,8 @@ fn enter_directory(contents: ReadDir, options: &getopts::Matches) {
|
|||
Err(err) => {
|
||||
show_error!("{}", err);
|
||||
panic!();
|
||||
},
|
||||
Ok(en) => en
|
||||
}
|
||||
Ok(en) => en,
|
||||
};
|
||||
|
||||
// Currently have a DirEntry that we can believe in.
|
||||
|
@ -141,21 +160,21 @@ fn display_dir_entry(entry: DirEntry, options: &getopts::Matches) {
|
|||
let md = match entry.metadata() {
|
||||
Err(e) => {
|
||||
show_error!("Unable to retrieve metadata for {}. \n Error: {}",
|
||||
display_file_name(entry.file_name()), e);
|
||||
display_file_name(entry.file_name()),
|
||||
e);
|
||||
panic!();
|
||||
},
|
||||
Ok(md) => md
|
||||
}
|
||||
Ok(md) => md,
|
||||
};
|
||||
|
||||
println!(" {}{} {} {} {} {: >9} {}",
|
||||
println!("{}{} {} {} {} {: >9} {}",
|
||||
display_file_type(entry.file_type()),
|
||||
display_permissions(&md),
|
||||
display_symlink_count(&md),
|
||||
display_uname(&md),
|
||||
display_group(&md),
|
||||
display_file_size(&md, options),
|
||||
display_file_name(entry.file_name())
|
||||
);
|
||||
display_file_name(entry.file_name()));
|
||||
}
|
||||
|
||||
fn cstr2string(cstr: *const c_char) -> String {
|
||||
|
@ -216,8 +235,8 @@ fn display_file_type(file_type: Result<FileType, std::io::Error>) -> String {
|
|||
Err(e) => {
|
||||
show_error!("{}", e);
|
||||
panic!()
|
||||
},
|
||||
Ok(ft) => ft
|
||||
}
|
||||
Ok(ft) => ft,
|
||||
};
|
||||
|
||||
if file_type.is_dir() {
|
||||
|
|
Loading…
Reference in a new issue