Skip to content

Commit a94c864

Browse files
committed
Color output based on LSCOLORS
1 parent 092c961 commit a94c864

6 files changed

Lines changed: 51 additions & 25 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.3.0"
3132

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

src/batch.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use color::Colors;
33
use flags::{DirOrderFlag, Flags, SortFlag, SortOrder};
44
use icon::Icons;
55
use meta::{FileType, Meta};
6+
use lscolors::LsColors;
67
use std::cmp::Ordering;
78
use std::iter::IntoIterator;
89
use std::vec::IntoIter;
@@ -33,12 +34,17 @@ impl Batch {
3334
self.0.sort_unstable_by(|a, b| sort_by_meta(a, b, flags));
3435
}
3536

36-
pub fn get_short_output(&self, colors: &Colors, icons: &Icons, flags: Flags) -> Vec<String> {
37+
pub fn get_short_output(
38+
&self,
39+
icons: &Icons,
40+
flags: Flags,
41+
lscolors: &LsColors,
42+
) -> Vec<String> {
3743
let mut res = Vec::with_capacity(self.0.len());
3844

3945
for meta in &self.0 {
4046
let strings: &[ANSIString] = &[
41-
meta.name.render(colors, icons),
47+
meta.name.render(icons, &lscolors),
4248
meta.indicator.render(flags),
4349
];
4450

@@ -48,7 +54,13 @@ impl Batch {
4854
res
4955
}
5056

51-
pub fn get_long_output(&self, colors: &Colors, icons: &Icons, flags: Flags) -> Vec<String> {
57+
pub fn get_long_output(
58+
&self,
59+
colors: &Colors,
60+
icons: &Icons,
61+
flags: Flags,
62+
lscolors: &LsColors,
63+
) -> Vec<String> {
5264
let mut res = Vec::with_capacity(self.0.len());
5365

5466
let max_user_length = self.detect_user_lenght();
@@ -70,7 +82,7 @@ impl Batch {
7082
ANSIString::from(" "),
7183
meta.date.render(colors, max_date_length, flags),
7284
ANSIString::from(" "),
73-
meta.name.render(colors, icons),
85+
meta.name.render(icons, &lscolors),
7486
meta.indicator.render(flags),
7587
meta.symlink.render(colors),
7688
];

src/core.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use color::{self, Colors};
33
use display::Display;
44
use flags::{Flags, WhenFlag};
55
use icon::{self, Icons};
6+
use lscolors::LsColors;
67
use meta::{FileType, Meta};
78
use std::path::{Path, PathBuf};
89
use terminal_size::terminal_size;
@@ -12,6 +13,7 @@ pub struct Core {
1213
icons: Icons,
1314
display: Display,
1415
colors: Colors,
16+
lscolors: LsColors,
1517
}
1618

1719
impl Core {
@@ -49,6 +51,7 @@ impl Core {
4951
display: Display::new(inner_flags),
5052
colors: Colors::new(color_theme),
5153
icons: Icons::new(icon_theme),
54+
lscolors: LsColors::from_env().unwrap_or_default(),
5255
}
5356
}
5457

@@ -124,14 +127,14 @@ impl Core {
124127

125128
if elem.file_type == FileType::Directory {
126129
output += &self.display.print_tree_row(
127-
&elem.name.render(&self.colors, &self.icons),
130+
&elem.name.render(&self.icons, &self.lscolors),
128131
depth,
129132
last,
130133
);
131134
self.run_inner(vec![elem.path], depth + 1);
132135
} else {
133136
output += &self.display.print_tree_row(
134-
&elem.name.render(&self.colors, &self.icons),
137+
&elem.name.render(&self.icons, &self.lscolors),
135138
depth,
136139
last,
137140
);
@@ -143,9 +146,9 @@ impl Core {
143146

144147
pub fn get_batch_outputs<'b>(&self, batch: &'b Batch) -> Vec<String> {
145148
if self.flags.display_long {
146-
batch.get_long_output(&self.colors, &self.icons, self.flags)
149+
batch.get_long_output(&self.colors, &self.icons, self.flags, &self.lscolors)
147150
} else {
148-
batch.get_short_output(&self.colors, &self.icons, self.flags)
151+
batch.get_short_output(&self.icons, self.flags, &self.lscolors)
149152
}
150153
}
151154

src/main.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
#![cfg_attr(feature = "cargo-clippy", allow(
2-
clippy::cast_precision_loss,
3-
clippy::cast_sign_loss,
4-
clippy::match_same_arms,
5-
clippy::cast_possible_wrap
6-
))]
1+
#![cfg_attr(
2+
feature = "cargo-clippy",
3+
allow(
4+
clippy::cast_precision_loss,
5+
clippy::cast_sign_loss,
6+
clippy::match_same_arms,
7+
clippy::cast_possible_wrap
8+
)
9+
)]
710

811
#[macro_use]
912
extern crate clap;
1013
extern crate ansi_term;
1114
extern crate chrono_humanize;
1215
extern crate libc;
16+
extern crate lscolors;
1317
#[cfg(test)]
1418
extern crate tempdir;
1519
extern crate term_grid;

src/meta/name.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
use color::{ColoredString, Colors, Elem};
1+
use color::ColoredString;
22
use icon::Icons;
33
use meta::filetype::FileType;
44
use std::cmp::{Ordering, PartialOrd};
55
use std::path::Path;
66

7+
use lscolors::{LsColors, Style};
8+
79
#[derive(Debug, Eq)]
810
pub struct Name {
911
name: String,
@@ -35,23 +37,17 @@ impl Name {
3537
}
3638
}
3739

38-
pub fn render(&self, colors: &Colors, icons: &Icons) -> ColoredString {
40+
pub fn render(&self, icons: &Icons, lscolors: &LsColors) -> ColoredString {
3941
let icon = icons.get(self);
4042
let mut content = String::with_capacity(icon.len() + self.name.len() + 3 /* spaces */);
4143

4244
content += icon.as_str();
4345

44-
let elem = match self.file_type {
45-
FileType::CharDevice => &Elem::CharDevice,
46-
FileType::Directory => &Elem::Dir,
47-
FileType::SymLink => &Elem::SymLink,
48-
FileType::ExecutableFile => &Elem::ExecutableFile,
49-
_ => &Elem::File,
50-
};
51-
5246
content += &self.name;
5347

54-
colors.colorize(content, elem)
48+
let style = lscolors.style_for_path(&self.name);
49+
let ansi_style = style.map(Style::to_ansi_term_style).unwrap_or_default();
50+
ansi_style.paint(content)
5551
}
5652

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

0 commit comments

Comments
 (0)