Highlight compiled files if their source is present

Compiled files with a source present (such as code.o when code.c is present) are now classified as temporary files. If the source isn't present, they're highlighted in a kind of drab colour (using the new 255-colour ability, yay!)

The code does do exists() checks on the filesystem when it could be possible to compare the files to the list of files we got from the call to readdir(), but it doesn't.
This commit is contained in:
Ben S 2014-06-16 21:20:09 +01:00
parent b9fd420be2
commit cce20d5953

34
file.rs
View File

@ -70,6 +70,21 @@ impl<'a> File<'a> {
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)
}
fn get_source_files(&self) -> Vec<String> {
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
_ => vec![],
}
}
pub fn display(&self, column: &Column) -> String {
match *column {
Permissions => self.permissions_string(),
@ -134,8 +149,17 @@ impl<'a> File<'a> {
Red.normal()
}
else {
let source_files = self.get_source_files();
if source_files.len() == 0 {
Plain
}
else if source_files.iter().any(|filename| Path::new(format!("{}/{}", self.path.dirname_str().unwrap(), filename)).exists()) {
Fixed(244).normal()
}
else {
Fixed(137).normal()
}
}
}
fn permissions_string(&self) -> String {
@ -165,3 +189,13 @@ 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()
}
}