Skip to content

Commit

Permalink
Highlight executable files
Browse files Browse the repository at this point in the history
  • Loading branch information
sharkdp committed Jun 5, 2017
1 parent 8adb5b9 commit 4d950ae
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fd"
version = "0.3.0"
version = "1.0.0"
authors = ["David Peter <[email protected]>"]

[dependencies]
Expand Down
5 changes: 5 additions & 0 deletions src/lscolors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,

Expand All @@ -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()
}
Expand Down Expand Up @@ -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("*.") {
Expand Down
10 changes: 10 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();

Expand All @@ -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 =
Expand Down

0 comments on commit 4d950ae

Please sign in to comment.