Skip to content

Commit

Permalink
Factor out ansi_style_for_path helper
Browse files Browse the repository at this point in the history
  • Loading branch information
jez committed May 13, 2020
1 parent 9c8ab4d commit 9d92289
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 12 deletions.
1 change: 1 addition & 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 @@ -11,6 +11,7 @@ name = "as-tree"
path = "src/main.rs"

[dependencies]
ansi_term = "0.12"
lscolors = "0.7"

[raze]
Expand Down
1 change: 1 addition & 0 deletions src/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ rust_binary(
name = "as-tree",
srcs = ["main.rs"],
deps = [
"//third_party/cargo:ansi_term",
"//third_party/cargo:lscolors",
],
visibility = ["//visibility:public"],
Expand Down
23 changes: 11 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
extern crate ansi_term;
extern crate lscolors;

use std::collections::BTreeMap;
Expand All @@ -16,6 +17,13 @@ pub struct PathTrie {
trie: BTreeMap<PathBuf, PathTrie>,
}

fn ansi_style_for_path(lscolors: &LsColors, path: &Path) -> ansi_term::Style {
lscolors
.style_for_path(&path)
.map(Style::to_ansi_term_style)
.unwrap_or_default()
}

impl PathTrie {
pub fn insert(&mut self, path: &Path) {
let mut cur = self;
Expand All @@ -40,10 +48,7 @@ impl PathTrie {

for (idx, (path, it)) in self.trie.iter().enumerate() {
let current_path = parent_path.join(path);
let ansi_style = lscolors
.style_for_path(&current_path)
.map(Style::to_ansi_term_style)
.unwrap_or_default();
let ansi_style = ansi_style_for_path(&lscolors, &current_path);
if idx != self.trie.len() - 1 {
write!(
out,
Expand Down Expand Up @@ -76,20 +81,14 @@ impl fmt::Display for PathTrie {
let lscolors = LsColors::from_env().unwrap_or_default();

if let Some((path, it)) = self.trie.iter().next() {
let ansi_style = lscolors
.style_for_path(path)
.map(Style::to_ansi_term_style)
.unwrap_or_default();
let ansi_style = ansi_style_for_path(&lscolors, path);
if path.is_absolute() || path == &PathBuf::from(".") {
write!(out, "{}\n", ansi_style.paint(path.to_string_lossy()))?;
return it._fmt(out, "", &lscolors, path.to_owned());
}
}

let ansi_style = lscolors
.style_for_path(".")
.map(Style::to_ansi_term_style)
.unwrap_or_default();
let ansi_style = ansi_style_for_path(&lscolors, Path::new("."));
write!(out, "{}\n", ansi_style.paint("."))?;
self._fmt(out, "", &lscolors, PathBuf::from("."))
}
Expand Down
4 changes: 4 additions & 0 deletions third_party/cargo/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ package(default_visibility = ["//visibility:public"])
licenses([
"notice" # See individual crates for specific licenses
])
alias(
name = "ansi_term",
actual = "@raze__ansi_term__0_12_1//:ansi_term",
)
alias(
name = "lscolors",
actual = "@raze__lscolors__0_7_0//:lscolors",
Expand Down

0 comments on commit 9d92289

Please sign in to comment.