Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Color output based on LSCOLORS #84

Merged
merged 3 commits into from
Feb 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
81 changes: 80 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 @@ -54,31 +55,80 @@ impl Elem {

pub type ColoredString<'a> = ANSIString<'a>;


#[allow(dead_code)]
#[derive(Debug, Copy, Clone)]
pub enum Theme {
NoColor,
Default,
NoLscolors,
}

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

impl Colors {
pub fn new(theme: Theme) -> Self {
let colors = match theme {
Theme::NoColor => None,
Theme::Default => Some(Self::get_light_theme_colour_map()),
Theme::NoLscolors => Some(Self::get_light_theme_colour_map()),
};
let lscolors = match theme {
Theme::NoColor => None,
Theme::Default => LsColors::from_env(),
Theme::NoLscolors => None,
};

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 +141,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
2 changes: 1 addition & 1 deletion src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ mod tests {
);
let output = name
.render(
&Colors::new(color::Theme::Default),
&Colors::new(color::Theme::NoLscolors),
&Icons::new(icon::Theme::NoIcon),
)
.to_string();
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
12 changes: 6 additions & 6 deletions src/meta/filetype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ mod test {
File::create(&file_path).expect("failed to create file");
let meta = file_path.metadata().expect("failed to get metas");

let colors = Colors::new(Theme::Default);
let colors = Colors::new(Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));

assert_eq!(Colour::Fixed(184).paint("."), file_type.render(&colors));
Expand All @@ -95,7 +95,7 @@ mod test {
let tmp_dir = TempDir::new("test_dir_type").expect("failed to create temp dir");
let meta = tmp_dir.path().metadata().expect("failed to get metas");

let colors = Colors::new(Theme::Default);
let colors = Colors::new(Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));

assert_eq!(Colour::Fixed(33).paint("d"), file_type.render(&colors));
Expand All @@ -116,7 +116,7 @@ mod test {
.symlink_metadata()
.expect("failed to get metas");

let colors = Colors::new(Theme::Default);
let colors = Colors::new(Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));

assert_eq!(Colour::Fixed(44).paint("l"), file_type.render(&colors));
Expand All @@ -136,7 +136,7 @@ mod test {
assert_eq!(true, success, "failed to exec mkfifo");
let meta = pipe_path.metadata().expect("failed to get metas");

let colors = Colors::new(Theme::Default);
let colors = Colors::new(Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));

assert_eq!(Colour::Fixed(44).paint("|"), file_type.render(&colors));
Expand All @@ -161,7 +161,7 @@ mod test {
assert_eq!(true, success, "failed to exec mknod");
let meta = char_device_path.metadata().expect("failed to get metas");

let colors = Colors::new(Theme::Default);
let colors = Colors::new(Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));

assert_eq!(Colour::Fixed(44).paint("c"), file_type.render(&colors));
Expand All @@ -176,7 +176,7 @@ mod test {
UnixListener::bind(&socket_path).expect("failed to create the socket");
let meta = socket_path.metadata().expect("failed to get metas");

let colors = Colors::new(Theme::Default);
let colors = Colors::new(Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));

assert_eq!(Colour::Fixed(44).paint("s"), file_type.render(&colors));
Expand Down
17 changes: 10 additions & 7 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 Expand Up @@ -112,7 +115,7 @@ mod test {
File::create(&file_path).expect("failed to create file");
let meta = file_path.metadata().expect("failed to get metas");

let colors = Colors::new(color::Theme::Default);
let colors = Colors::new(color::Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));
let name = Name::new(&file_path, file_type);

Expand All @@ -132,7 +135,7 @@ mod test {
fs::create_dir(&dir_path).expect("failed to create the dir");
let meta = dir_path.metadata().expect("failed to get metas");

let colors = Colors::new(color::Theme::Default);
let colors = Colors::new(color::Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));
let name = Name::new(&dir_path, file_type);

Expand All @@ -158,7 +161,7 @@ mod test {
.symlink_metadata()
.expect("failed to get metas");

let colors = Colors::new(color::Theme::Default);
let colors = Colors::new(color::Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));
let name = Name::new(&symlink_path, file_type);

Expand All @@ -183,7 +186,7 @@ mod test {
assert_eq!(true, success, "failed to exec mkfifo");
let meta = pipe_path.metadata().expect("failed to get metas");

let colors = Colors::new(color::Theme::Default);
let colors = Colors::new(color::Theme::NoLscolors);
let file_type = FileType::new(&meta, &Permissions::from(&meta));
let name = Name::new(&pipe_path, file_type);

Expand Down