mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-12-24 17:40:25 +00:00
Cache readdir results in a Dir object
This new object is then passed down to the File, so it can see what files are around it. This means it doesn't need to re-query the filesystem several times per file, instead using the in-memory copy. Also, switch to using the built-in Path#with_extension method.
This commit is contained in:
parent
78771a3a3e
commit
a5e404da55
33
dir.rs
Normal file
33
dir.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use std::io::fs;
|
||||
use file::File;
|
||||
|
||||
// The purpose of a Dir is to provide a cached list of the file paths
|
||||
// in the directory being searched for. This object is then passed to
|
||||
// the Files themselves, which can then check the status of their
|
||||
// surrounding files, such as whether it needs to be coloured
|
||||
// differently if a certain other file exists.
|
||||
|
||||
pub struct Dir<'a> {
|
||||
contents: Vec<Path>,
|
||||
}
|
||||
|
||||
impl<'a> Dir<'a> {
|
||||
pub fn readdir(path: Path) -> Dir<'a> {
|
||||
match fs::readdir(&path) {
|
||||
Ok(paths) => Dir {
|
||||
contents: paths,
|
||||
},
|
||||
Err(e) => fail!("readdir: {}", e),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn files(&'a self) -> Vec<File<'a>> {
|
||||
self.contents.iter().map(|path| File::from_path(path, self)).collect()
|
||||
}
|
||||
|
||||
pub fn contains(&self, path: &Path) -> bool {
|
||||
self.contents.contains(path)
|
||||
}
|
||||
}
|
||||
|
||||
|
13
exa.rs
13
exa.rs
@ -3,13 +3,14 @@ extern crate regex;
|
||||
#[phase(syntax)] extern crate regex_macros;
|
||||
|
||||
use std::os;
|
||||
use std::io::fs;
|
||||
|
||||
use file::File;
|
||||
use dir::Dir;
|
||||
use options::Options;
|
||||
|
||||
pub mod colours;
|
||||
pub mod column;
|
||||
pub mod dir;
|
||||
pub mod format;
|
||||
pub mod file;
|
||||
pub mod unix;
|
||||
@ -40,13 +41,9 @@ fn main() {
|
||||
}
|
||||
|
||||
fn exa(options: &Options, path: Path) {
|
||||
let paths = match fs::readdir(&path) {
|
||||
Ok(paths) => paths,
|
||||
Err(e) => fail!("readdir: {}", e),
|
||||
};
|
||||
|
||||
let unordered_files: Vec<File> = paths.iter().map(|path| File::from_path(path)).collect();
|
||||
let files: Vec<&File> = options.transform_files(&unordered_files);
|
||||
let dir = Dir::readdir(path);
|
||||
let unsorted_files = dir.files();
|
||||
let files: Vec<&File> = options.transform_files(&unsorted_files);
|
||||
|
||||
// The output gets formatted into columns, which looks nicer. To
|
||||
// do this, we have to write the results into a table, instead of
|
||||
|
58
file.rs
58
file.rs
@ -6,6 +6,7 @@ use column::{Column, Permissions, FileName, FileSize, User, Group};
|
||||
use format::{format_metric_bytes, format_IEC_bytes};
|
||||
use unix::{get_user_name, get_group_name};
|
||||
use sort::SortPart;
|
||||
use dir::Dir;
|
||||
|
||||
static IMAGE_TYPES: &'static [&'static str] = &[
|
||||
"png", "jpeg", "jpg", "gif", "bmp", "tiff", "tif",
|
||||
@ -37,6 +38,7 @@ static TEMP_TYPES: &'static [&'static str] = &[
|
||||
static CRYPTO_TYPES: &'static [&'static str] = &[
|
||||
"asc", "gpg", "sig", "signature", "pgp" ];
|
||||
|
||||
|
||||
// Instead of working with Rust's Paths, we have our own File object
|
||||
// that holds the Path and various cached information. Each file is
|
||||
// definitely going to have its filename used at least once, its stat
|
||||
@ -46,6 +48,7 @@ static CRYPTO_TYPES: &'static [&'static str] = &[
|
||||
|
||||
pub struct File<'a> {
|
||||
pub name: &'a str,
|
||||
pub dir: &'a Dir<'a>,
|
||||
pub ext: Option<&'a str>,
|
||||
pub path: &'a Path,
|
||||
pub stat: io::FileStat,
|
||||
@ -53,7 +56,7 @@ pub struct File<'a> {
|
||||
}
|
||||
|
||||
impl<'a> File<'a> {
|
||||
pub fn from_path(path: &'a Path) -> File<'a> {
|
||||
pub fn from_path(path: &'a Path, parent: &'a Dir) -> File<'a> {
|
||||
// Getting the string from a filename fails whenever it's not
|
||||
// UTF-8 representable - just assume it is for now.
|
||||
let filename: &str = path.filename_str().unwrap();
|
||||
@ -68,6 +71,7 @@ impl<'a> File<'a> {
|
||||
|
||||
return File {
|
||||
path: path,
|
||||
dir: parent,
|
||||
stat: stat,
|
||||
name: filename,
|
||||
ext: File::ext(filename),
|
||||
@ -90,40 +94,36 @@ impl<'a> File<'a> {
|
||||
fn is_tmpfile(&self) -> bool {
|
||||
self.name.ends_with("~") || (self.name.starts_with("#") && self.name.ends_with("#"))
|
||||
}
|
||||
|
||||
fn with_extension(&self, newext: &'static str) -> String {
|
||||
format!("{}.{}", self.path.filestem_str().unwrap(), newext)
|
||||
}
|
||||
|
||||
|
||||
// Highlight the compiled versions of files. Some of them, like .o,
|
||||
// get special highlighting when they're alone because there's no
|
||||
// point in existing without their source. Others can be perfectly
|
||||
// content without their source files, such as how .js is valid
|
||||
// without a .coffee.
|
||||
|
||||
fn get_source_files(&self) -> Vec<String> {
|
||||
fn get_source_files(&self) -> Vec<Path> {
|
||||
match self.ext {
|
||||
Some("class") => vec![self.with_extension("java")], // Java
|
||||
Some("elc") => vec![self.name.chop()], // Emacs Lisp
|
||||
Some("hi") => vec![self.with_extension("hs")], // Haskell
|
||||
Some("o") => vec![self.with_extension("c"), self.with_extension("cpp")], // C, C++
|
||||
Some("pyc") => vec![self.name.chop()], // Python
|
||||
Some("class") => vec![self.path.with_extension("java")], // Java
|
||||
Some("elc") => vec![self.path.with_extension("el")], // Emacs Lisp
|
||||
Some("hi") => vec![self.path.with_extension("hs")], // Haskell
|
||||
Some("o") => vec![self.path.with_extension("c"), self.path.with_extension("cpp")], // C, C++
|
||||
Some("pyc") => vec![self.path.with_extension("py")], // Python
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
||||
fn get_source_files_usual(&self) -> Vec<String> {
|
||||
fn get_source_files_usual(&self) -> Vec<Path> {
|
||||
match self.ext {
|
||||
Some("js") => vec![self.with_extension("coffee"), self.with_extension("ts")], // CoffeeScript, TypeScript
|
||||
Some("css") => vec![self.with_extension("sass"), self.with_extension("less")], // SASS, Less
|
||||
Some("js") => vec![self.path.with_extension("coffee"), self.path.with_extension("ts")], // CoffeeScript, TypeScript
|
||||
Some("css") => vec![self.path.with_extension("sass"), self.path.with_extension("less")], // SASS, Less
|
||||
|
||||
Some("aux") => vec![self.with_extension("tex")], // TeX: auxiliary file
|
||||
Some("bbl") => vec![self.with_extension("tex")], // BibTeX bibliography file
|
||||
Some("blg") => vec![self.with_extension("tex")], // BibTeX log file
|
||||
Some("lof") => vec![self.with_extension("tex")], // list of figures
|
||||
Some("log") => vec![self.with_extension("tex")], // TeX log file
|
||||
Some("lot") => vec![self.with_extension("tex")], // list of tables
|
||||
Some("toc") => vec![self.with_extension("tex")], // table of contents
|
||||
Some("aux") => vec![self.path.with_extension("tex")], // TeX: auxiliary file
|
||||
Some("bbl") => vec![self.path.with_extension("tex")], // BibTeX bibliography file
|
||||
Some("blg") => vec![self.path.with_extension("tex")], // BibTeX log file
|
||||
Some("lof") => vec![self.path.with_extension("tex")], // list of figures
|
||||
Some("log") => vec![self.path.with_extension("tex")], // TeX log file
|
||||
Some("lot") => vec![self.path.with_extension("tex")], // list of tables
|
||||
Some("toc") => vec![self.path.with_extension("tex")], // table of contents
|
||||
|
||||
_ => vec![],
|
||||
}
|
||||
@ -213,14 +213,14 @@ impl<'a> File<'a> {
|
||||
let source_files = self.get_source_files();
|
||||
if source_files.len() == 0 {
|
||||
let source_files_usual = self.get_source_files_usual();
|
||||
if source_files_usual.iter().any(|filename| Path::new(format!("{}/{}", self.path.dirname_str().unwrap(), filename)).exists()) {
|
||||
if source_files_usual.iter().any(|path| self.dir.contains(path)) {
|
||||
Fixed(244).normal()
|
||||
}
|
||||
else {
|
||||
Plain
|
||||
}
|
||||
}
|
||||
else if source_files.iter().any(|filename| Path::new(format!("{}/{}", self.path.dirname_str().unwrap(), filename)).exists()) {
|
||||
else if source_files.iter().any(|path| self.dir.contains(path)) {
|
||||
Fixed(244).normal()
|
||||
}
|
||||
else {
|
||||
@ -256,13 +256,3 @@ impl<'a> File<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
trait Chop {
|
||||
fn chop(&self) -> String;
|
||||
}
|
||||
|
||||
impl<'a> Chop for &'a str {
|
||||
fn chop(&self) -> String {
|
||||
self.slice_to(self.len() - 1).to_string()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user