Skip to content

Commit 0911e92

Browse files
committed
Color output based on LSCOLORS
1 parent 072757b commit 0911e92

7 files changed

Lines changed: 52 additions & 26 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
@@ -27,6 +27,7 @@ terminal_size = "0.1.8"
2727
time = "0.1.40"
2828
users = "0.8.0"
2929
chrono-humanize = "0.0.11"
30+
lscolors = "0.3.0"
3031

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

src/batch.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use ansi_term::{ANSIString, ANSIStrings};
22
use color::Colors;
33
use flags::{Flags, SortFlag, SortOrder};
44
use icon::Icons;
5+
use lscolors::LsColors;
56
use meta::FileType;
67
use meta::Meta;
78
use std::cmp::Ordering;
@@ -34,12 +35,17 @@ impl Batch {
3435
self.0.sort_unstable_by(|a, b| sort_by_meta(a, b, flags));
3536
}
3637

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

4046
for meta in &self.0 {
4147
let strings: &[ANSIString] = &[
42-
meta.name.render(colors, icons),
48+
meta.name.render(icons, &lscolors),
4349
meta.indicator.render(flags),
4450
];
4551

@@ -49,7 +55,13 @@ impl Batch {
4955
res
5056
}
5157

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

5567
let max_user_length = self.detect_user_lenght();
@@ -71,7 +83,7 @@ impl Batch {
7183
ANSIString::from(" "),
7284
meta.date.render(colors, max_date_length, flags),
7385
ANSIString::from(" "),
74-
meta.name.render(colors, icons),
86+
meta.name.render(icons, &lscolors),
7587
meta.indicator.render(flags),
7688
meta.symlink.render(colors),
7789
];

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 {
@@ -48,6 +50,7 @@ impl Core {
4850
display: Display::new(inner_flags),
4951
colors: Colors::new(color_theme),
5052
icons: Icons::new(icon_theme),
53+
lscolors: LsColors::from_env().unwrap_or_default(),
5154
}
5255
}
5356

@@ -118,14 +121,14 @@ impl Core {
118121

119122
if elem.file_type == FileType::Directory {
120123
self.display.print_tree_row(
121-
&elem.name.render(&self.colors, &self.icons),
124+
&elem.name.render(&self.icons, &self.lscolors),
122125
depth,
123126
last,
124127
);
125128
self.run_inner(vec![elem.path], depth + 1);
126129
} else {
127130
self.display.print_tree_row(
128-
&elem.name.render(&self.colors, &self.icons),
131+
&elem.name.render(&self.icons, &self.lscolors),
129132
depth,
130133
last,
131134
);
@@ -135,9 +138,9 @@ impl Core {
135138

136139
pub fn get_batch_outputs<'b>(&self, batch: &'b Batch) -> Vec<String> {
137140
if self.flags.display_long {
138-
batch.get_long_output(&self.colors, &self.icons, self.flags)
141+
batch.get_long_output(&self.colors, &self.icons, self.flags, &self.lscolors)
139142
} else {
140-
batch.get_short_output(&self.colors, &self.icons, self.flags)
143+
batch.get_short_output(&self.icons, self.flags, &self.lscolors)
141144
}
142145
}
143146

src/icon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl Icons {
4242
return res;
4343
}
4444

45-
// Check the known extensions.
45+
// Check the known extensions.
4646
if let Some(extension) = name.extension() {
4747
if let Some(icon) = self.icons_by_extension.get(extension.as_str()) {
4848
res += icon;

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)