diff --git a/src/dir.rs b/src/dir.rs index 8a567fd6e0a5ae87847b23a15f83f9fc26e00d53..e0a2f25eb844c9bdddad043c260f9c3ab8a90f00 100644 --- a/src/dir.rs +++ b/src/dir.rs @@ -31,11 +31,14 @@ impl Dir { /// Produce a vector of File objects from an initialised directory, /// printing out an error if any of the Files fail to be created. - pub fn files(&self) -> Vec { + /// + /// Passing in `recurse` means that any directories will be scanned for + /// their contents, as well. + pub fn files(&self, recurse: bool) -> Vec { let mut files = vec![]; for path in self.contents.iter() { - match File::from_path(path, Some(self)) { + match File::from_path(path, Some(self), recurse) { Ok(file) => files.push(file), Err(e) => println!("{}: {}", path.display(), e), } diff --git a/src/file.rs b/src/file.rs index 63d50ece01ac7451eb6a780af481324047560fad..850a066146a47b650ad3c7d94cb8baf3ea7bed9f 100644 --- a/src/file.rs +++ b/src/file.rs @@ -32,6 +32,7 @@ pub struct File<'a> { pub ext: Option, pub path: Path, pub stat: io::FileStat, + pub this: Option, } impl<'a> File<'a> { @@ -39,12 +40,12 @@ impl<'a> File<'a> { /// appropriate. Paths specified directly on the command-line have no Dirs. /// /// This uses lstat instead of stat, which doesn't follow symbolic links. - pub fn from_path(path: &Path, parent: Option<&'a Dir>) -> IoResult> { - fs::lstat(path).map(|stat| File::with_stat(stat, path, parent)) + pub fn from_path(path: &Path, parent: Option<&'a Dir>, recurse: bool) -> IoResult> { + fs::lstat(path).map(|stat| File::with_stat(stat, path, parent, recurse)) } /// Create a new File object from the given Stat result, and other data. - pub fn with_stat(stat: io::FileStat, path: &Path, parent: Option<&'a Dir>) -> File<'a> { + pub fn with_stat(stat: io::FileStat, path: &Path, parent: Option<&'a Dir>, recurse: bool) -> File<'a> { // The filename to display is the last component of the path. However, // the path has no components for `.`, `..`, and `/`, so in these @@ -58,12 +59,23 @@ impl<'a> File<'a> { // replacement characters. let filename = String::from_utf8_lossy(bytes); + // If we are recursing, then the `this` field contains a Dir object + // that represents the current File as a directory, if it is a + // directory. This is used for the --tree option. + let this = if recurse && stat.kind == io::FileType::Directory { + Dir::readdir(path).ok() + } + else { + None + }; + File { path: path.clone(), dir: parent, stat: stat, name: filename.to_string(), ext: ext(filename.as_slice()), + this: this, } } @@ -184,6 +196,7 @@ impl<'a> File<'a> { stat: stat, name: filename.to_string(), ext: ext(filename.as_slice()), + this: None, }) } else { diff --git a/src/main.rs b/src/main.rs index c31c6cba90d2f71292bf98f193c9bae2070cdfe5..facf3c126ae8e251e8ceb3786d1ed970cd489b53 100644 --- a/src/main.rs +++ b/src/main.rs @@ -39,11 +39,14 @@ fn exa(options: &Options) { let path = Path::new(file); match fs::stat(&path) { Ok(stat) => { - if stat.kind == FileType::Directory && options.dir_action != DirAction::AsFile { + if stat.kind == FileType::Directory && options.dir_action == DirAction::Tree { + files.push(File::with_stat(stat, &path, None, true)); + } + else if stat.kind == FileType::Directory && options.dir_action != DirAction::AsFile { dirs.push(path); } else { - files.push(File::with_stat(stat, &path, None)); + files.push(File::with_stat(stat, &path, None, false)); } } Err(e) => println!("{}: {}", file, e), @@ -77,7 +80,7 @@ fn exa(options: &Options) { match Dir::readdir(&dir_path) { Ok(ref dir) => { - let unsorted_files = dir.files(); + let unsorted_files = dir.files(false); let files: Vec = options.transform_files(unsorted_files); // When recursing, add any directories to the dirs stack diff --git a/src/options.rs b/src/options.rs index 8d1f35544b242288257d6850c3c2514c89bad9bb..d9d819e4579f5103bed72064eff96e0d516c8f29 100644 --- a/src/options.rs +++ b/src/options.rs @@ -45,6 +45,7 @@ impl Options { getopts::optflag("R", "recurse", "recurse into directories"), getopts::optopt ("s", "sort", "field to sort by", "WORD"), getopts::optflag("S", "blocks", "show number of file system blocks"), + getopts::optflag("T", "tree", "recurse into subdirectories in a tree view"), getopts::optflag("x", "across", "sort multi-column view entries across"), getopts::optflag("?", "help", "show list of command-line options"), ]; @@ -111,7 +112,7 @@ impl Options { /// What to do when encountering a directory? #[derive(PartialEq, Debug, Copy)] pub enum DirAction { - AsFile, List, Recurse + AsFile, List, Recurse, Tree } /// User-supplied field to sort by. @@ -189,7 +190,7 @@ fn view(matches: &getopts::Matches) -> Result { Err(Misfire::Useless("oneline", true, "long")) } else { - Ok(View::Details(try!(Columns::new(matches)), matches.opt_present("header"))) + Ok(View::Details(try!(Columns::new(matches)), matches.opt_present("header"), matches.opt_present("tree"))) } } else if matches.opt_present("binary") { @@ -242,12 +243,14 @@ fn file_size(matches: &getopts::Matches) -> Result { fn dir_action(matches: &getopts::Matches) -> Result { let recurse = matches.opt_present("recurse"); let list = matches.opt_present("list-dirs"); - - match (recurse, list) { - (true, true ) => Err(Misfire::Conflict("recurse", "list-dirs")), - (true, false) => Ok(DirAction::Recurse), - (false, true ) => Ok(DirAction::AsFile), - (false, false) => Ok(DirAction::List), + let tree = matches.opt_present("tree"); + + match (recurse, list, tree) { + (true, true, _ ) => Err(Misfire::Conflict("recurse", "list-dirs")), + (true, false, false) => Ok(DirAction::Recurse), + (true, false, true ) => Ok(DirAction::Tree), + (false, true, _ ) => Ok(DirAction::AsFile), + (false, false, _ ) => Ok(DirAction::List), } } diff --git a/src/output.rs b/src/output.rs index 52251d4ce64cf3af115950208988c9a4d8d3f75c..f022fb629c0f8dd0539b14885f4b485a2f0b770a 100644 --- a/src/output.rs +++ b/src/output.rs @@ -12,7 +12,7 @@ use ansi_term::Style::Plain; #[derive(PartialEq, Copy, Debug)] pub enum View { - Details(Columns, bool), + Details(Columns, bool, bool), Lines, Grid(bool, usize), } @@ -21,7 +21,7 @@ impl View { pub fn view(&self, dir: Option<&Dir>, files: &[File]) { match *self { View::Grid(across, width) => grid_view(across, width, files), - View::Details(ref cols, header) => details_view(&*cols.for_dir(dir), files, header), + View::Details(ref cols, header, tree) => details_view(&*cols.for_dir(dir), files, header, tree), View::Lines => lines_view(files), } } @@ -122,7 +122,7 @@ fn grid_view(across: bool, console_width: usize, files: &[File]) { } } -fn details_view(columns: &[Column], files: &[File], header: bool) { +fn details_view(columns: &[Column], files: &[File], header: bool, tree: bool) { // The output gets formatted into columns, which looks nicer. To // do this, we have to write the results into a table, instead of // displaying each file immediately, then calculating the maximum @@ -131,19 +131,22 @@ fn details_view(columns: &[Column], files: &[File], header: bool) { let mut cache = OSUsers::empty_cache(); - let mut table: Vec> = files.iter() - .map(|f| columns.iter().map(|c| f.display(c, &mut cache)).collect()) - .collect(); + let mut table = Vec::new(); + get_files(columns, &mut cache, tree, &mut table, files, 0); if header { - table.insert(0, columns.iter().map(|c| Cell::paint(Plain.underline(), c.header())).collect()); + table.insert(0, (0, columns.iter().map(|c| Cell::paint(Plain.underline(), c.header())).collect())); } let column_widths: Vec = range(0, columns.len()) - .map(|n| table.iter().map(|row| row[n].length).max().unwrap_or(0)) + .map(|n| table.iter().map(|row| row.1[n].length).max().unwrap_or(0)) .collect(); - for row in table.iter() { + for &(depth, ref row) in table.iter() { + for _ in range(0, depth) { + print!("#"); + } + for (num, column) in columns.iter().enumerate() { if num != 0 { print!(" "); // Separator @@ -161,3 +164,16 @@ fn details_view(columns: &[Column], files: &[File], header: bool) { print!("\n"); } } + +fn get_files(columns: &[Column], cache: &mut OSUsers, recurse: bool, dest: &mut Vec<(usize, Vec)>, src: &[File], depth: usize) { + for file in src.iter() { + let cols = columns.iter().map(|c| file.display(c, cache)).collect(); + dest.push((depth, cols)); + + if recurse { + if let Some(ref dir) = file.this { + get_files(columns, cache, recurse, dest, dir.files(true).as_slice(), depth + 1); + } + } + } +}