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 16, 2019
1 parent e8015eb commit 5bc5c3f
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 4 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
73 changes: 72 additions & 1 deletion 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: Option<LsColors>,
}

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

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,
elem: &Elem,
) -> ColoredString<'a> {
let style_from_path = self.style_from_path(path);
match style_from_path {
Some(style_from_path) => style_from_path.paint(input),
None => self.colorize(input, elem),
}
}

fn style_from_path(&self, path: &str) -> Option<Style> {
match &self.lscolors {
Some(lscolors) => lscolors
.style_for_path(path)
.map(lscolors::Style::to_ansi_term_style),
None => None,
}
}

fn style(&self, elem: &Elem) -> Style {
match &self.lscolors {
Some(lscolors) => {
match self.get_indicator_from_elem(elem) {
Some(style) => {
let style = lscolors.style_for_indicator(style);
style
.map(lscolors::Style::to_ansi_term_style)
.unwrap_or_default()
}
None => self.style_default(elem),
}
}
None => self.style_default(elem),
}
}

fn style_default(&self, elem: &Elem) -> Style {
if let Some(ref colors) = self.colors {
let style_fg = Style::default().fg(colors[elem]);
if elem.has_suid() {
Expand All @@ -91,6 +133,35 @@ impl Colors {
}
}

fn get_indicator_from_elem(&self, elem: &Elem) -> Option<Indicator> {
let indicator_string = match elem {
Elem::File { exec, uid } => match (exec, uid) {
(_, true) => None,
(true, false) => Some("ex"),
(false, false) => Some("fi"),
},
Elem::Dir { uid } => {
if *uid {
None
} else {
Some("di")
}
}
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,
}
}

// You can find the table for each color, code, and display at:
//
//https://jonasjacek.github.io/colors/
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,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
9 changes: 6 additions & 3 deletions src/meta/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,6 +43,7 @@ impl Name {
let mut content = String::with_capacity(icon.len() + self.name.len() + 3 /* spaces */);

content += icon.as_str();
content += &self.name;

let elem = match self.file_type {
FileType::CharDevice => Elem::CharDevice,
Expand All @@ -51,9 +56,7 @@ impl Name {
},
};

content += &self.name;

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

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

0 comments on commit 5bc5c3f

Please sign in to comment.