Skip to content

Commit

Permalink
Color output based on LSCOLORS
Browse files Browse the repository at this point in the history
  • Loading branch information
meain committed Jan 2, 2019
1 parent d8d9105 commit 877104a
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
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.3.0"

[dependencies.clap]
features = ["suggestions", "color", "wrap_help"]
Expand Down
20 changes: 16 additions & 4 deletions src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use ansi_term::{ANSIString, ANSIStrings};
use color::Colors;
use flags::{Flags, SortFlag, SortOrder};
use icon::Icons;
use lscolors::LsColors;
use meta::FileType;
use meta::Meta;
use std::cmp::Ordering;
Expand Down Expand Up @@ -34,12 +35,17 @@ impl Batch {
self.0.sort_unstable_by(|a, b| sort_by_meta(a, b, flags));
}

pub fn get_short_output(&self, colors: &Colors, icons: &Icons, flags: Flags) -> Vec<String> {
pub fn get_short_output(
&self,
icons: &Icons,
flags: Flags,
lscolors: &LsColors,
) -> Vec<String> {
let mut res = Vec::with_capacity(self.0.len());

for meta in &self.0 {
let strings: &[ANSIString] = &[
meta.name.render(colors, icons),
meta.name.render(icons, &lscolors),
meta.indicator.render(flags),
];

Expand All @@ -49,7 +55,13 @@ impl Batch {
res
}

pub fn get_long_output(&self, colors: &Colors, icons: &Icons, flags: Flags) -> Vec<String> {
pub fn get_long_output(
&self,
colors: &Colors,
icons: &Icons,
flags: Flags,
lscolors: &LsColors,
) -> Vec<String> {
let mut res = Vec::with_capacity(self.0.len());

let max_user_length = self.detect_user_lenght();
Expand All @@ -71,7 +83,7 @@ impl Batch {
ANSIString::from(" "),
meta.date.render(colors, max_date_length, flags),
ANSIString::from(" "),
meta.name.render(colors, icons),
meta.name.render(icons, &lscolors),
meta.indicator.render(flags),
meta.symlink.render(colors),
];
Expand Down
11 changes: 7 additions & 4 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use color::{self, Colors};
use display::Display;
use flags::{Flags, WhenFlag};
use icon::{self, Icons};
use lscolors::LsColors;
use meta::{FileType, Meta};
use std::path::{Path, PathBuf};
use terminal_size::terminal_size;
Expand All @@ -12,6 +13,7 @@ pub struct Core {
icons: Icons,
display: Display,
colors: Colors,
lscolors: LsColors,
}

impl Core {
Expand Down Expand Up @@ -48,6 +50,7 @@ impl Core {
display: Display::new(inner_flags),
colors: Colors::new(color_theme),
icons: Icons::new(icon_theme),
lscolors: LsColors::from_env().unwrap_or_default(),
}
}

Expand Down Expand Up @@ -122,14 +125,14 @@ impl Core {

if elem.file_type == FileType::Directory {
self.display.print_tree_row(
&elem.name.render(&self.colors, &self.icons),
&elem.name.render(&self.icons, &self.lscolors),
depth,
last,
);
self.run_inner(vec![elem.path], depth + 1);
} else {
self.display.print_tree_row(
&elem.name.render(&self.colors, &self.icons),
&elem.name.render(&self.icons, &self.lscolors),
depth,
last,
);
Expand All @@ -139,9 +142,9 @@ impl Core {

pub fn get_batch_outputs<'b>(&self, batch: &'b Batch) -> Vec<String> {
if self.flags.display_long {
batch.get_long_output(&self.colors, &self.icons, self.flags)
batch.get_long_output(&self.colors, &self.icons, self.flags, &self.lscolors)
} else {
batch.get_short_output(&self.colors, &self.icons, self.flags)
batch.get_short_output(&self.icons, self.flags, &self.lscolors)
}
}

Expand Down
16 changes: 10 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
#![cfg_attr(feature = "cargo-clippy", allow(
clippy::cast_precision_loss,
clippy::cast_sign_loss,
clippy::match_same_arms,
clippy::cast_possible_wrap
))]
#![cfg_attr(
feature = "cargo-clippy",
allow(
clippy::cast_precision_loss,
clippy::cast_sign_loss,
clippy::match_same_arms,
clippy::cast_possible_wrap
)
)]

#[macro_use]
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
18 changes: 7 additions & 11 deletions src/meta/name.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use color::{ColoredString, Colors, Elem};
use color::ColoredString;
use icon::Icons;
use meta::filetype::FileType;
use std::cmp::{Ordering, PartialOrd};
use std::path::Path;

use lscolors::{LsColors, Style};

#[derive(Debug, Eq)]
pub struct Name {
name: String,
Expand Down Expand Up @@ -35,23 +37,17 @@ impl Name {
}
}

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

content += icon.as_str();

let elem = match self.file_type {
FileType::CharDevice => &Elem::CharDevice,
FileType::Directory => &Elem::Dir,
FileType::SymLink => &Elem::SymLink,
FileType::ExecutableFile => &Elem::ExecutableFile,
_ => &Elem::File,
};

content += &self.name;

colors.colorize(content, elem)
let style = lscolors.style_for_path(&self.name);
let ansi_style = style.map(Style::to_ansi_term_style).unwrap_or_default();
ansi_style.paint(content)
}

pub fn name(&self) -> String {
Expand Down

0 comments on commit 877104a

Please sign in to comment.