mirror of
https://github.com/Llewellynvdm/exa.git
synced 2025-01-11 16:34:26 +00:00
Make Dir return an Iterator of files, not Vec
This is part of work to make the flow of files more iterator-able, rather than going in and out of vectors. Here, a Dir returns an iterator of files, rather than a pre-filled vector. For now, this removes the ability for error messages to be displayed. Will be added in later though!
This commit is contained in:
parent
d547c3f5d7
commit
5d0bd37168
31
src/dir.rs
31
src/dir.rs
@ -1,6 +1,7 @@
|
||||
use std::io;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::slice::Iter as SliceIter;
|
||||
|
||||
use feature::Git;
|
||||
use file::{File, fields};
|
||||
@ -36,17 +37,12 @@ impl Dir {
|
||||
///
|
||||
/// Passing in `recurse` means that any directories will be scanned for
|
||||
/// their contents, as well.
|
||||
pub fn files(&self, recurse: bool) -> Vec<File> {
|
||||
let mut files = vec![];
|
||||
|
||||
for path in self.contents.iter() {
|
||||
match File::from_path(path, Some(self), recurse) {
|
||||
Ok(file) => files.push(file),
|
||||
Err(e) => println!("{}: {}", path.display(), e),
|
||||
}
|
||||
pub fn files<'dir>(&'dir self, recurse: bool) -> Files<'dir> {
|
||||
Files {
|
||||
inner: self.contents.iter(),
|
||||
recurse: recurse,
|
||||
dir: &self,
|
||||
}
|
||||
|
||||
files
|
||||
}
|
||||
|
||||
/// Whether this directory contains a file with the given path.
|
||||
@ -73,3 +69,18 @@ impl Dir {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
pub struct Files<'dir> {
|
||||
inner: SliceIter<'dir, PathBuf>,
|
||||
recurse: bool,
|
||||
dir: &'dir Dir,
|
||||
}
|
||||
|
||||
impl<'dir> Iterator for Files<'dir> {
|
||||
type Item = io::Result<File<'dir>>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
self.inner.next().map(|path| File::from_path(path, Some(self.dir), self.recurse))
|
||||
}
|
||||
}
|
@ -146,7 +146,7 @@ impl<'dir> Exa<'dir> {
|
||||
|
||||
match Dir::readdir(&dir_path, self.options.should_scan_for_git()) {
|
||||
Ok(ref dir) => {
|
||||
let mut files = dir.files(false);
|
||||
let mut files = dir.files(false).flat_map(|f| f).collect();
|
||||
self.options.transform_files(&mut files);
|
||||
|
||||
// When recursing, add any directories to the dirs stack
|
||||
|
@ -99,7 +99,8 @@ impl Details {
|
||||
// them, so we don't examine any directories that wouldn't
|
||||
// have their contents listed anyway.
|
||||
if let Some(ref dir) = file.this {
|
||||
let mut files = dir.files(true);
|
||||
let mut files = dir.files(true).flat_map(|f| f).collect();
|
||||
|
||||
filter.transform_files(&mut files);
|
||||
self.add_files_to_table(table, &files, depth + 1);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user