Skip to content

Commit 877104a

Browse files
committed
Color output based on LSCOLORS
1 parent d8d9105 commit 877104a

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
@@ -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

@@ -122,14 +125,14 @@ impl Core {
122125

123126
if elem.file_type == FileType::Directory {
124127
self.display.print_tree_row(
125-
&elem.name.render(&self.colors, &self.icons),
128+
&elem.name.render(&self.icons, &self.lscolors),
126129
depth,
127130
last,
128131
);
129132
self.run_inner(vec![elem.path], depth + 1);
130133
} else {
131134
self.display.print_tree_row(
132-
&elem.name.render(&self.colors, &self.icons),
135+
&elem.name.render(&self.icons, &self.lscolors),
133136
depth,
134137
last,
135138
);
@@ -139,9 +142,9 @@ impl Core {
139142

140143
pub fn get_batch_outputs<'b>(&self, batch: &'b Batch) -> Vec<String> {
141144
if self.flags.display_long {
142-
batch.get_long_output(&self.colors, &self.icons, self.flags)
145+
batch.get_long_output(&self.colors, &self.icons, self.flags, &self.lscolors)
143146
} else {
144-
batch.get_short_output(&self.colors, &self.icons, self.flags)
147+
batch.get_short_output(&self.icons, self.flags, &self.lscolors)
145148
}
146149
}
147150

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)