Skip to content

Commit

Permalink
color files with lscolors
Browse files Browse the repository at this point in the history
  • Loading branch information
meain committed Feb 2, 2019
1 parent 0fa6809 commit 2bceb36
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 23 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ time = "0.1.40"
users = "0.8.0"
chrono-humanize = "0.0.11"
unicode-width = "0.1.5"
lscolors = "0.5.0"

[dependencies.clap]
features = ["suggestions", "color", "wrap_help"]
Expand Down
79 changes: 70 additions & 9 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use ansi_term::{ANSIString, Colour, Style};
use lscolors::{Indicator, LsColors};
use std::collections::HashMap;

#[allow(dead_code)]
Expand Down Expand Up @@ -62,6 +63,7 @@ pub enum Theme {

pub struct Colors {
colors: Option<HashMap<Elem, Colour>>,
lscolors: LsColors,
}

impl Colors {
Expand All @@ -70,24 +72,83 @@ impl Colors {
Theme::NoColor => None,
Theme::Default => Some(Self::get_light_theme_colour_map()),
};
let lscolors = LsColors::from_env().unwrap_or_default();

Self { colors }
Self { colors, lscolors }
}

pub fn colorize<'a>(&self, input: String, elem: &Elem) -> ColoredString<'a> {
self.style(elem).paint(input)
}

pub fn colorize_using_path<'a>(&self, input: String, path: &str) -> ColoredString<'a> {
self.style_from_path(path).paint(input)
}

fn style_from_path(&self, path: &str) -> Style {
let style = self.lscolors.style_for_path(path);
style
.map(lscolors::Style::to_ansi_term_style)
.unwrap_or_default()
}

fn style(&self, elem: &Elem) -> Style {
if let Some(ref colors) = self.colors {
let style_fg = Style::default().fg(colors[elem]);
if elem.has_suid() {
style_fg.on(Colour::Fixed(124)) // Red3
} else {
style_fg
let indicator = self.get_indicator_from_elem(elem);
match indicator {
Some(style) => {
let style = self.lscolors.style_for_indicator(style);
let ans = style
.map(lscolors::Style::to_ansi_term_style)
.unwrap_or_default();
ans
}
None => {
if let Some(ref colors) = self.colors {
let style_fg = Style::default().fg(colors[elem]);
if elem.has_suid() {
style_fg.on(Colour::Fixed(124)) // Red3
} else {
style_fg
}
} else {
Style::default()
}
}
}
}

fn get_indicator_from_elem(&self, elem: &Elem) -> Option<Indicator> {
let indicator_string = match elem {
Elem::File { exec, uid } => {
if !uid {
if !exec {
Some("fi")
} else {
Some("ex")
}
} else {
None
}
}
} else {
Style::default()
Elem::Dir { uid } => {
if !uid {
Some("di")
} else {
None
}
}
Elem::SymLink => Some("ln"),
Elem::Pipe => Some("pi"),
Elem::Socket => Some("so"),
Elem::BlockDevice => Some("bd"),
Elem::CharDevice => Some("cd"),
Elem::BrokenSymLink => Some("or"),
_ => None,
};

match indicator_string {
Some(ids) => Indicator::from(ids),
None => None,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern crate clap;
extern crate ansi_term;
extern crate chrono_humanize;
extern crate libc;
extern crate lscolors;
#[cfg(test)]
extern crate tempdir;
extern crate term_grid;
Expand Down
20 changes: 6 additions & 14 deletions src/meta/name.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use color::{ColoredString, Colors, Elem};
use color::{ColoredString, Colors};
use icon::Icons;
use meta::filetype::FileType;
use std::cmp::{Ordering, PartialOrd};
Expand All @@ -7,6 +7,7 @@ use std::path::Path;
#[derive(Debug, Eq)]
pub struct Name {
name: String,
path: String,
extension: Option<String>,
file_type: FileType,
}
Expand All @@ -27,8 +28,11 @@ impl Name {
);
}

let path_string = path.to_string_lossy().to_string();

Self {
name,
path:path_string,
extension,
file_type,
}
Expand All @@ -39,21 +43,9 @@ impl Name {
let mut content = String::with_capacity(icon.len() + self.name.len() + 3 /* spaces */);

content += icon.as_str();

let elem = match self.file_type {
FileType::CharDevice => Elem::CharDevice,
FileType::Directory { uid } => Elem::Dir { uid },
FileType::SymLink => Elem::SymLink,
FileType::File { uid, exec } => Elem::File { uid, exec },
_ => Elem::File {
exec: false,
uid: false,
},
};

content += &self.name;

colors.colorize(content, &elem)
colors.colorize_using_path(content, &self.path)
}

pub fn name(&self) -> String {
Expand Down

0 comments on commit 2bceb36

Please sign in to comment.