mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-11-13 07:56:29 +00:00
commit
9974d4d5a2
18
src/main.rs
18
src/main.rs
@ -1,7 +1,8 @@
|
|||||||
#![feature(collections, core, env, libc, old_io, old_path, plugin, std_misc)]
|
#![feature(collections, core, env, libc, old_io, old_path, plugin, std_misc)]
|
||||||
// Other platforms then macos don’t need std_misc but you can’t
|
|
||||||
|
// Other platforms than macos don't need std_misc but you can't
|
||||||
// use #[cfg] on features.
|
// use #[cfg] on features.
|
||||||
#![allow(unused_features)]
|
#![allow(unused_features)]
|
||||||
|
|
||||||
extern crate ansi_term;
|
extern crate ansi_term;
|
||||||
extern crate datetime;
|
extern crate datetime;
|
||||||
@ -20,7 +21,7 @@ use std::old_io::{fs, FileType};
|
|||||||
|
|
||||||
use dir::Dir;
|
use dir::Dir;
|
||||||
use file::File;
|
use file::File;
|
||||||
use options::{Options, View, DirAction};
|
use options::{Options, View};
|
||||||
use output::lines_view;
|
use output::lines_view;
|
||||||
|
|
||||||
pub mod column;
|
pub mod column;
|
||||||
@ -58,7 +59,7 @@ impl<'a> Exa<'a> {
|
|||||||
match fs::stat(&path) {
|
match fs::stat(&path) {
|
||||||
Ok(stat) => {
|
Ok(stat) => {
|
||||||
if stat.kind == FileType::Directory {
|
if stat.kind == FileType::Directory {
|
||||||
if self.options.dir_action == DirAction::Tree {
|
if self.options.dir_action.is_tree() {
|
||||||
self.files.push(File::with_stat(stat, &path, None, true));
|
self.files.push(File::with_stat(stat, &path, None, true));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -111,9 +112,12 @@ impl<'a> Exa<'a> {
|
|||||||
// backwards: the *last* element of the stack is used each
|
// backwards: the *last* element of the stack is used each
|
||||||
// time, so by inserting them backwards, they get displayed in
|
// time, so by inserting them backwards, they get displayed in
|
||||||
// the correct sort order.
|
// the correct sort order.
|
||||||
if self.options.dir_action == DirAction::Recurse {
|
if let Some(recurse_opts) = self.options.dir_action.recurse_options() {
|
||||||
for dir in files.iter().filter(|f| f.stat.kind == FileType::Directory).rev() {
|
let depth = dir_path.components().filter(|&c| c != b".").count() + 1;
|
||||||
self.dirs.push(dir.path.clone());
|
if !recurse_opts.tree && !recurse_opts.is_too_deep(depth) {
|
||||||
|
for dir in files.iter().filter(|f| f.stat.kind == FileType::Directory).rev() {
|
||||||
|
self.dirs.push(dir.path.clone());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
106
src/options.rs
106
src/options.rs
@ -8,6 +8,7 @@ use xattr;
|
|||||||
|
|
||||||
use std::cmp::Ordering;
|
use std::cmp::Ordering;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::num::ParseIntError;
|
||||||
|
|
||||||
use getopts;
|
use getopts;
|
||||||
use natord;
|
use natord;
|
||||||
@ -44,11 +45,6 @@ impl Options {
|
|||||||
/// Call getopts on the given slice of command-line strings.
|
/// Call getopts on the given slice of command-line strings.
|
||||||
pub fn getopts(args: &[String]) -> Result<(Options, Vec<String>), Misfire> {
|
pub fn getopts(args: &[String]) -> Result<(Options, Vec<String>), Misfire> {
|
||||||
let mut opts = getopts::Options::new();
|
let mut opts = getopts::Options::new();
|
||||||
if xattr::feature_implemented() {
|
|
||||||
opts.optflag("@", "extended",
|
|
||||||
"display extended attribute keys and sizes in long (-l) output"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
opts.optflag("1", "oneline", "display one entry per line");
|
opts.optflag("1", "oneline", "display one entry per line");
|
||||||
opts.optflag("a", "all", "show dot-files");
|
opts.optflag("a", "all", "show dot-files");
|
||||||
opts.optflag("b", "binary", "use binary prefixes in file sizes");
|
opts.optflag("b", "binary", "use binary prefixes in file sizes");
|
||||||
@ -59,6 +55,7 @@ impl Options {
|
|||||||
opts.optflag("H", "links", "show number of hard links");
|
opts.optflag("H", "links", "show number of hard links");
|
||||||
opts.optflag("i", "inode", "show each file's inode number");
|
opts.optflag("i", "inode", "show each file's inode number");
|
||||||
opts.optflag("l", "long", "display extended details and attributes");
|
opts.optflag("l", "long", "display extended details and attributes");
|
||||||
|
opts.optopt ("L", "level", "maximum depth of recursion", "DEPTH");
|
||||||
opts.optflag("m", "modified", "display timestamp of most recent modification");
|
opts.optflag("m", "modified", "display timestamp of most recent modification");
|
||||||
opts.optflag("r", "reverse", "reverse order of files");
|
opts.optflag("r", "reverse", "reverse order of files");
|
||||||
opts.optflag("R", "recurse", "recurse into directories");
|
opts.optflag("R", "recurse", "recurse into directories");
|
||||||
@ -71,6 +68,10 @@ impl Options {
|
|||||||
opts.optflag("x", "across", "sort multi-column view entries across");
|
opts.optflag("x", "across", "sort multi-column view entries across");
|
||||||
opts.optflag("?", "help", "show list of command-line options");
|
opts.optflag("?", "help", "show list of command-line options");
|
||||||
|
|
||||||
|
if xattr::feature_implemented() {
|
||||||
|
opts.optflag("@", "extended", "display extended attribute keys and sizes in long (-l) output");
|
||||||
|
}
|
||||||
|
|
||||||
let matches = match opts.parse(args) {
|
let matches = match opts.parse(args) {
|
||||||
Ok(m) => m,
|
Ok(m) => m,
|
||||||
Err(e) => return Err(Misfire::InvalidOptions(e)),
|
Err(e) => return Err(Misfire::InvalidOptions(e)),
|
||||||
@ -98,9 +99,12 @@ impl Options {
|
|||||||
matches.free.clone()
|
matches.free.clone()
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let dir_action = try!(DirAction::deduce(&matches));
|
||||||
|
let view = try!(View::deduce(&matches, filter, dir_action));
|
||||||
|
|
||||||
Ok((Options {
|
Ok((Options {
|
||||||
dir_action: try!(DirAction::deduce(&matches)),
|
dir_action: dir_action,
|
||||||
view: try!(View::deduce(&matches, filter)),
|
view: view,
|
||||||
filter: filter,
|
filter: filter,
|
||||||
}, path_strs))
|
}, path_strs))
|
||||||
}
|
}
|
||||||
@ -179,12 +183,19 @@ pub enum Misfire {
|
|||||||
/// this enum isn't named Error!
|
/// this enum isn't named Error!
|
||||||
Help(String),
|
Help(String),
|
||||||
|
|
||||||
/// Two options were given that conflict with one another
|
/// Two options were given that conflict with one another.
|
||||||
Conflict(&'static str, &'static str),
|
Conflict(&'static str, &'static str),
|
||||||
|
|
||||||
/// An option was given that does nothing when another one either is or
|
/// An option was given that does nothing when another one either is or
|
||||||
/// isn't present.
|
/// isn't present.
|
||||||
Useless(&'static str, bool, &'static str),
|
Useless(&'static str, bool, &'static str),
|
||||||
|
|
||||||
|
/// An option was given that does nothing when either of two other options
|
||||||
|
/// are not present.
|
||||||
|
Useless2(&'static str, &'static str, &'static str),
|
||||||
|
|
||||||
|
/// A numeric option was given that failed to be parsed as a number.
|
||||||
|
FailedParse(ParseIntError),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Misfire {
|
impl Misfire {
|
||||||
@ -203,12 +214,14 @@ impl fmt::Display for Misfire {
|
|||||||
Conflict(a, b) => write!(f, "Option --{} conflicts with option {}.", a, b),
|
Conflict(a, b) => write!(f, "Option --{} conflicts with option {}.", a, b),
|
||||||
Useless(a, false, b) => write!(f, "Option --{} is useless without option --{}.", a, b),
|
Useless(a, false, b) => write!(f, "Option --{} is useless without option --{}.", a, b),
|
||||||
Useless(a, true, b) => write!(f, "Option --{} is useless given option --{}.", a, b),
|
Useless(a, true, b) => write!(f, "Option --{} is useless given option --{}.", a, b),
|
||||||
|
Useless2(a, b1, b2) => write!(f, "Option --{} is useless without options --{} or --{}.", a, b1, b2),
|
||||||
|
FailedParse(ref e) => write!(f, "Failed to parse number: {}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl View {
|
impl View {
|
||||||
pub fn deduce(matches: &getopts::Matches, filter: FileFilter) -> Result<View, Misfire> {
|
pub fn deduce(matches: &getopts::Matches, filter: FileFilter, dir_action: DirAction) -> Result<View, Misfire> {
|
||||||
if matches.opt_present("long") {
|
if matches.opt_present("long") {
|
||||||
if matches.opt_present("across") {
|
if matches.opt_present("across") {
|
||||||
Err(Misfire::Useless("across", true, "long"))
|
Err(Misfire::Useless("across", true, "long"))
|
||||||
@ -220,7 +233,7 @@ impl View {
|
|||||||
let details = Details {
|
let details = Details {
|
||||||
columns: try!(Columns::deduce(matches)),
|
columns: try!(Columns::deduce(matches)),
|
||||||
header: matches.opt_present("header"),
|
header: matches.opt_present("header"),
|
||||||
tree: matches.opt_present("recurse") || matches.opt_present("tree"),
|
recurse: dir_action.recurse_options(),
|
||||||
xattr: xattr::feature_implemented() && matches.opt_present("extended"),
|
xattr: xattr::feature_implemented() && matches.opt_present("extended"),
|
||||||
filter: filter,
|
filter: filter,
|
||||||
};
|
};
|
||||||
@ -252,6 +265,9 @@ impl View {
|
|||||||
else if matches.opt_present("tree") {
|
else if matches.opt_present("tree") {
|
||||||
Err(Misfire::Useless("tree", false, "long"))
|
Err(Misfire::Useless("tree", false, "long"))
|
||||||
}
|
}
|
||||||
|
else if matches.opt_present("level") && !matches.opt_present("recurse") {
|
||||||
|
Err(Misfire::Useless2("level", "recurse", "tree"))
|
||||||
|
}
|
||||||
else if xattr::feature_implemented() && matches.opt_present("extended") {
|
else if xattr::feature_implemented() && matches.opt_present("extended") {
|
||||||
Err(Misfire::Useless("extended", false, "long"))
|
Err(Misfire::Useless("extended", false, "long"))
|
||||||
}
|
}
|
||||||
@ -373,7 +389,9 @@ impl TimeTypes {
|
|||||||
/// What to do when encountering a directory?
|
/// What to do when encountering a directory?
|
||||||
#[derive(PartialEq, Debug, Copy)]
|
#[derive(PartialEq, Debug, Copy)]
|
||||||
pub enum DirAction {
|
pub enum DirAction {
|
||||||
AsFile, List, Recurse, Tree
|
AsFile,
|
||||||
|
List,
|
||||||
|
Recurse(RecurseOptions),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl DirAction {
|
impl DirAction {
|
||||||
@ -385,12 +403,67 @@ impl DirAction {
|
|||||||
match (recurse, list, tree) {
|
match (recurse, list, tree) {
|
||||||
(true, true, _ ) => Err(Misfire::Conflict("recurse", "list-dirs")),
|
(true, true, _ ) => Err(Misfire::Conflict("recurse", "list-dirs")),
|
||||||
(_, true, true ) => Err(Misfire::Conflict("tree", "list-dirs")),
|
(_, true, true ) => Err(Misfire::Conflict("tree", "list-dirs")),
|
||||||
(true, false, false) => Ok(DirAction::Recurse),
|
(true, false, false) => Ok(DirAction::Recurse(try!(RecurseOptions::deduce(matches, false)))),
|
||||||
(_ , _, true ) => Ok(DirAction::Tree),
|
(_ , _, true ) => Ok(DirAction::Recurse(try!(RecurseOptions::deduce(matches, true)))),
|
||||||
(false, true, _ ) => Ok(DirAction::AsFile),
|
(false, true, _ ) => Ok(DirAction::AsFile),
|
||||||
(false, false, _ ) => Ok(DirAction::List),
|
(false, false, _ ) => Ok(DirAction::List),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn recurse_options(&self) -> Option<RecurseOptions> {
|
||||||
|
match *self {
|
||||||
|
DirAction::Recurse(opts) => Some(opts),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_tree(&self) -> bool {
|
||||||
|
match *self {
|
||||||
|
DirAction::Recurse(RecurseOptions { max_depth: _, tree }) => tree,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_recurse(&self) -> bool {
|
||||||
|
match *self {
|
||||||
|
DirAction::Recurse(RecurseOptions { max_depth: _, tree }) => !tree,
|
||||||
|
_ => false,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug, Copy)]
|
||||||
|
pub struct RecurseOptions {
|
||||||
|
pub tree: bool,
|
||||||
|
pub max_depth: Option<usize>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl RecurseOptions {
|
||||||
|
pub fn deduce(matches: &getopts::Matches, tree: bool) -> Result<RecurseOptions, Misfire> {
|
||||||
|
let max_depth = if let Some(level) = matches.opt_str("level") {
|
||||||
|
match level.parse() {
|
||||||
|
Ok(l) => Some(l),
|
||||||
|
Err(e) => return Err(Misfire::FailedParse(e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(RecurseOptions {
|
||||||
|
tree: tree,
|
||||||
|
max_depth: max_depth,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn is_too_deep(&self, depth: usize) -> bool {
|
||||||
|
match self.max_depth {
|
||||||
|
None => false,
|
||||||
|
Some(d) => {
|
||||||
|
d <= depth
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Copy, Debug)]
|
#[derive(PartialEq, Copy, Debug)]
|
||||||
@ -565,4 +638,11 @@ mod test {
|
|||||||
assert_eq!(opts.unwrap_err(), Misfire::Useless("extended", false, "long"))
|
assert_eq!(opts.unwrap_err(), Misfire::Useless("extended", false, "long"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn level_without_recurse_or_tree() {
|
||||||
|
let opts = Options::getopts(&[ "--level".to_string(), "69105".to_string() ]);
|
||||||
|
assert_eq!(opts.unwrap_err(), Misfire::Useless2("level", "recurse", "tree"))
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,7 @@ use column::{Alignment, Column, Cell};
|
|||||||
use xattr::Attribute;
|
use xattr::Attribute;
|
||||||
use dir::Dir;
|
use dir::Dir;
|
||||||
use file::{File, GREY};
|
use file::{File, GREY};
|
||||||
use options::{Columns, FileFilter};
|
use options::{Columns, FileFilter, RecurseOptions};
|
||||||
use users::OSUsers;
|
use users::OSUsers;
|
||||||
|
|
||||||
use locale;
|
use locale;
|
||||||
@ -12,7 +12,7 @@ use ansi_term::Style::Plain;
|
|||||||
pub struct Details {
|
pub struct Details {
|
||||||
pub columns: Columns,
|
pub columns: Columns,
|
||||||
pub header: bool,
|
pub header: bool,
|
||||||
pub tree: bool,
|
pub recurse: Option<RecurseOptions>,
|
||||||
pub xattr: bool,
|
pub xattr: bool,
|
||||||
pub filter: FileFilter,
|
pub filter: FileFilter,
|
||||||
}
|
}
|
||||||
@ -57,7 +57,7 @@ impl Details {
|
|||||||
print!("{} ", column.alignment().pad_string(&row.cells[num].text, padding));
|
print!("{} ", column.alignment().pad_string(&row.cells[num].text, padding));
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.tree {
|
if self.recurse.is_some() {
|
||||||
stack.resize(row.depth + 1, "├──");
|
stack.resize(row.depth + 1, "├──");
|
||||||
stack[row.depth] = if row.last { "└──" } else { "├──" };
|
stack[row.depth] = if row.last { "└──" } else { "├──" };
|
||||||
|
|
||||||
@ -75,7 +75,7 @@ impl Details {
|
|||||||
}
|
}
|
||||||
|
|
||||||
print!("{}\n", row.name);
|
print!("{}\n", row.name);
|
||||||
|
|
||||||
if self.xattr {
|
if self.xattr {
|
||||||
let width = row.attrs.iter().map(|a| a.name().len()).max().unwrap_or(0);
|
let width = row.attrs.iter().map(|a| a.name().len()).max().unwrap_or(0);
|
||||||
for attr in row.attrs.iter() {
|
for attr in row.attrs.iter() {
|
||||||
@ -103,7 +103,11 @@ impl Details {
|
|||||||
|
|
||||||
dest.push(row);
|
dest.push(row);
|
||||||
|
|
||||||
if self.tree {
|
if let Some(r) = self.recurse {
|
||||||
|
if r.tree == false || r.is_too_deep(depth) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if let Some(ref dir) = file.this {
|
if let Some(ref dir) = file.this {
|
||||||
let mut files = dir.files(true);
|
let mut files = dir.files(true);
|
||||||
self.filter.transform_files(&mut files);
|
self.filter.transform_files(&mut files);
|
||||||
|
Loading…
Reference in New Issue
Block a user