From cce20d59539c6cadb6df8760f9c59a6c8f8fcbdc Mon Sep 17 00:00:00 2001 From: Ben S Date: Mon, 16 Jun 2014 21:20:09 +0100 Subject: [PATCH] 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. --- file.rs | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/file.rs b/file.rs index 901ae1b..d5c09e5 100644 --- a/file.rs +++ b/file.rs @@ -69,6 +69,21 @@ 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) + } + + fn get_source_files(&self) -> Vec { + 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 { @@ -134,7 +149,16 @@ impl<'a> File<'a> { Red.normal() } else { - Plain + 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() + } } } @@ -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() + } +} \ No newline at end of file -- GitLab