Skip to content

Commit 2bceb36

Browse files
committed
color files with lscolors
1 parent 0fa6809 commit 2bceb36

5 files changed

Lines changed: 88 additions & 23 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ time = "0.1.40"
2828
users = "0.8.0"
2929
chrono-humanize = "0.0.11"
3030
unicode-width = "0.1.5"
31+
lscolors = "0.5.0"
3132

3233
[dependencies.clap]
3334
features = ["suggestions", "color", "wrap_help"]

src/color.rs

Lines changed: 70 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use ansi_term::{ANSIString, Colour, Style};
2+
use lscolors::{Indicator, LsColors};
23
use std::collections::HashMap;
34

45
#[allow(dead_code)]
@@ -62,6 +63,7 @@ pub enum Theme {
6263

6364
pub struct Colors {
6465
colors: Option<HashMap<Elem, Colour>>,
66+
lscolors: LsColors,
6567
}
6668

6769
impl Colors {
@@ -70,24 +72,83 @@ impl Colors {
7072
Theme::NoColor => None,
7173
Theme::Default => Some(Self::get_light_theme_colour_map()),
7274
};
75+
let lscolors = LsColors::from_env().unwrap_or_default();
7376

74-
Self { colors }
77+
Self { colors, lscolors }
7578
}
7679

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

84+
pub fn colorize_using_path<'a>(&self, input: String, path: &str) -> ColoredString<'a> {
85+
self.style_from_path(path).paint(input)
86+
}
87+
88+
fn style_from_path(&self, path: &str) -> Style {
89+
let style = self.lscolors.style_for_path(path);
90+
style
91+
.map(lscolors::Style::to_ansi_term_style)
92+
.unwrap_or_default()
93+
}
94+
8195
fn style(&self, elem: &Elem) -> Style {
82-
if let Some(ref colors) = self.colors {
83-
let style_fg = Style::default().fg(colors[elem]);
84-
if elem.has_suid() {
85-
style_fg.on(Colour::Fixed(124)) // Red3
86-
} else {
87-
style_fg
96+
let indicator = self.get_indicator_from_elem(elem);
97+
match indicator {
98+
Some(style) => {
99+
let style = self.lscolors.style_for_indicator(style);
100+
let ans = style
101+
.map(lscolors::Style::to_ansi_term_style)
102+
.unwrap_or_default();
103+
ans
104+
}
105+
None => {
106+
if let Some(ref colors) = self.colors {
107+
let style_fg = Style::default().fg(colors[elem]);
108+
if elem.has_suid() {
109+
style_fg.on(Colour::Fixed(124)) // Red3
110+
} else {
111+
style_fg
112+
}
113+
} else {
114+
Style::default()
115+
}
116+
}
117+
}
118+
}
119+
120+
fn get_indicator_from_elem(&self, elem: &Elem) -> Option<Indicator> {
121+
let indicator_string = match elem {
122+
Elem::File { exec, uid } => {
123+
if !uid {
124+
if !exec {
125+
Some("fi")
126+
} else {
127+
Some("ex")
128+
}
129+
} else {
130+
None
131+
}
88132
}
89-
} else {
90-
Style::default()
133+
Elem::Dir { uid } => {
134+
if !uid {
135+
Some("di")
136+
} else {
137+
None
138+
}
139+
}
140+
Elem::SymLink => Some("ln"),
141+
Elem::Pipe => Some("pi"),
142+
Elem::Socket => Some("so"),
143+
Elem::BlockDevice => Some("bd"),
144+
Elem::CharDevice => Some("cd"),
145+
Elem::BrokenSymLink => Some("or"),
146+
_ => None,
147+
};
148+
149+
match indicator_string {
150+
Some(ids) => Indicator::from(ids),
151+
None => None,
91152
}
92153
}
93154

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extern crate clap;
1313
extern crate ansi_term;
1414
extern crate chrono_humanize;
1515
extern crate libc;
16+
extern crate lscolors;
1617
#[cfg(test)]
1718
extern crate tempdir;
1819
extern crate term_grid;

src/meta/name.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use color::{ColoredString, Colors, Elem};
1+
use color::{ColoredString, Colors};
22
use icon::Icons;
33
use meta::filetype::FileType;
44
use std::cmp::{Ordering, PartialOrd};
@@ -7,6 +7,7 @@ use std::path::Path;
77
#[derive(Debug, Eq)]
88
pub struct Name {
99
name: String,
10+
path: String,
1011
extension: Option<String>,
1112
file_type: FileType,
1213
}
@@ -27,8 +28,11 @@ impl Name {
2728
);
2829
}
2930

31+
let path_string = path.to_string_lossy().to_string();
32+
3033
Self {
3134
name,
35+
path:path_string,
3236
extension,
3337
file_type,
3438
}
@@ -39,21 +43,9 @@ impl Name {
3943
let mut content = String::with_capacity(icon.len() + self.name.len() + 3 /* spaces */);
4044

4145
content += icon.as_str();
42-
43-
let elem = match self.file_type {
44-
FileType::CharDevice => Elem::CharDevice,
45-
FileType::Directory { uid } => Elem::Dir { uid },
46-
FileType::SymLink => Elem::SymLink,
47-
FileType::File { uid, exec } => Elem::File { uid, exec },
48-
_ => Elem::File {
49-
exec: false,
50-
uid: false,
51-
},
52-
};
53-
5446
content += &self.name;
5547

56-
colors.colorize(content, &elem)
48+
colors.colorize_using_path(content, &self.path)
5749
}
5850

5951
pub fn name(&self) -> String {

0 commit comments

Comments
 (0)