Highlight executable files

This commit is contained in:
sharkdp 2017-06-05 21:50:46 +02:00
parent 8adb5b9f84
commit 4d950ae97c
4 changed files with 17 additions and 2 deletions

2
Cargo.lock generated
View file

@ -1,6 +1,6 @@
[root]
name = "fd"
version = "0.3.0"
version = "1.0.0"
dependencies = [
"ansi_term 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)",
"getopts 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -1,6 +1,6 @@
[package]
name = "fd"
version = "0.3.0"
version = "1.0.0"
authors = ["David Peter <mail@david-peter.de>"]
[dependencies]

View file

@ -24,6 +24,9 @@ pub struct LsColors {
/// ANSI style for symbolic links.
pub symlink: Style,
/// ANSI style for executable files.
pub executable: Style,
/// A map that defines ANSI styles for different file extensions.
pub extensions: ExtensionStyles,
@ -37,6 +40,7 @@ impl Default for LsColors {
LsColors {
directory: Colour::Blue.bold(),
symlink: Colour::Cyan.normal(),
executable: Colour::Red.bold(),
extensions: HashMap::new(),
filenames: HashMap::new()
}
@ -125,6 +129,7 @@ impl LsColors {
match code.as_ref() {
"di" => self.directory = style,
"ln" => self.symlink = style,
"ex" => self.executable = style,
_ => return
}
} else if pattern.starts_with("*.") {

View file

@ -12,6 +12,7 @@ use std::error::Error;
use std::ffi::OsStr;
use std::fs;
use std::io::Write;
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, Component};
use std::process;
@ -76,6 +77,13 @@ fn print_entry(base: &Path, entry: &Path, config: &FdOptions) {
None => return
};
let is_executable = |p: &std::path::PathBuf| {
p.metadata()
.ok()
.map(|f| f.permissions().mode() & 0o111 != 0)
.unwrap_or(false)
};
if let Some(ref ls_colors) = config.ls_colors {
let mut component_path = base.to_path_buf();
@ -100,6 +108,8 @@ fn print_entry(base: &Path, entry: &Path, config: &FdOptions) {
ls_colors.symlink
} else if component_path.is_dir() {
ls_colors.directory
} else if is_executable(&component_path) {
ls_colors.executable
} else {
// Look up file name
let o_style =