summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Thiel <sebastian.thiel@icloud.com>2020-07-06 13:35:40 +0800
committerSebastian Thiel <sebastian.thiel@icloud.com>2020-07-06 13:35:40 +0800
commit40e9eb1d0e548dac3ec896d293291d1e439ba976 (patch)
tree2a0418932cada86adc439a4ebd865c733defcf27
parente867e58ebd2febc66342f0337f08b75574b24e02 (diff)
Replace termion::color with colored
-rw-r--r--Cargo.lock31
-rw-r--r--Cargo.toml3
-rw-r--r--src/aggregate.rs48
3 files changed, 40 insertions, 42 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 6ca456f..68c0401 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -87,6 +87,17 @@ dependencies = [
]
[[package]]
+name = "colored"
+version = "1.9.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f4ffc801dacf156c5854b9df4f425a626539c3a6ef7893cc0c5084a23f0b6c59"
+dependencies = [
+ "atty",
+ "lazy_static",
+ "winapi 0.3.9",
+]
+
+[[package]]
name = "crossbeam"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -219,6 +230,7 @@ dependencies = [
"anyhow",
"atty",
"byte-unit",
+ "colored",
"crosstermion",
"filesize",
"flume",
@@ -230,7 +242,6 @@ dependencies = [
"petgraph",
"pretty_assertions",
"structopt",
- "termcolor",
"termion",
"tui",
"tui-react 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -697,15 +708,6 @@ dependencies = [
]
[[package]]
-name = "termcolor"
-version = "1.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
-dependencies = [
- "winapi-util",
-]
-
-[[package]]
name = "termion"
version = "1.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -823,15 +825,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
-name = "winapi-util"
-version = "0.1.5"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
-dependencies = [
- "winapi 0.3.9",
-]
-
-[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
diff --git a/Cargo.toml b/Cargo.toml
index 842a349..f1dece0 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -13,7 +13,6 @@ include = ["src/**/*", "Cargo.*", "LICENSE", "README.md", "CHANGELOG.md", "!**/*
structopt = "0.3.15"
jwalk = "0.5.0"
byte-unit = "4"
-termion = "1.5.2"
atty = "0.2.11"
tui = "0.9.1"
petgraph = "0.5"
@@ -27,7 +26,7 @@ filesize = "0.2.0"
flume = {version = "0.7.1", default-features = false}
anyhow = "1.0.31"
crosstermion = { version = "0.1.3", default-features = false, features = ["tui-react-crossterm", "input-thread-flume"]}
-termcolor = "1.1.0"
+colored = "1.9.3"
[[bin]]
name="dua"
diff --git a/src/aggregate.rs b/src/aggregate.rs
index a216089..a754c98 100644
--- a/src/aggregate.rs
+++ b/src/aggregate.rs
@@ -1,10 +1,9 @@
use crate::{crossdev, InodeFilter, WalkOptions, WalkResult};
use anyhow::Result;
+use colored::{Color, Colorize};
use filesize::PathExt;
use std::borrow::Cow;
-use std::{fmt, io, path::Path};
-use termcolor;
-use termion::color;
+use std::{io, path::Path};
/// Aggregate the given `paths` and write information about them to `out` in a human-readable format.
/// If `compute_total` is set, it will write an additional line with the total size across all given `paths`.
@@ -67,13 +66,13 @@ pub fn aggregate(
if sort_by_size_in_bytes {
aggregates.push((path.as_ref().to_owned(), num_bytes, num_errors));
} else {
- write_path(
+ output_colored_path(
&mut out,
&walk_options,
&path,
num_bytes,
num_errors,
- path_color(&path),
+ path_color_of(&path),
)?;
}
total += num_bytes;
@@ -87,51 +86,62 @@ pub fn aggregate(
if sort_by_size_in_bytes {
aggregates.sort_by_key(|&(_, num_bytes, _)| num_bytes);
for (path, num_bytes, num_errors) in aggregates.into_iter() {
- write_path(
+ output_colored_path(
&mut out,
&walk_options,
&path,
num_bytes,
num_errors,
- path_color(&path),
+ path_color_of(&path),
)?;
}
}
if num_roots > 1 && compute_total {
- write_path(
+ output_colored_path(
&mut out,
&walk_options,
Path::new("total"),
total,
res.num_errors,
- color::Fg(color::Reset),
+ None,
)?;
}
Ok((res, stats))
}
-fn path_color(path: impl AsRef<Path>) -> Box<dyn fmt::Display> {
+fn path_color_of(path: impl AsRef<Path>) -> Option<Color> {
if path.as_ref().is_file() {
- Box::new(color::Fg(color::LightBlack))
+ Some(Color::BrightBlack)
} else {
- Box::new(color::Fg(color::Reset))
+ None
}
}
-fn write_path<C: fmt::Display>(
+fn output_colored_path(
out: &mut impl io::Write,
options: &WalkOptions,
path: impl AsRef<Path>,
num_bytes: u128,
num_errors: u64,
- path_color: C,
+ path_color: Option<colored::Color>,
) -> std::result::Result<(), io::Error> {
writeln!(
out,
- "{byte_color}{:>byte_column_width$}{byte_color_reset} {path_color}{}{path_color_reset}{}",
- options.byte_format.display(num_bytes),
- path.as_ref().display(),
+ "{:>byte_column_width$} {}{}",
+ options
+ .byte_format
+ .display(num_bytes)
+ .to_string()
+ .as_str()
+ .green(),
+ options.color.display(
+ path.as_ref()
+ .display()
+ .to_string()
+ .as_str()
+ .color(path_color.unwrap_or(Color::White))
+ ),
if num_errors == 0 {
Cow::Borrowed("")
} else {
@@ -141,10 +151,6 @@ fn write_path<C: fmt::Display>(
if num_errors > 1 { "s" } else { "" }
))
},
- byte_color = options.color.display(color::Fg(color::Green)),
- byte_color_reset = options.color.display(color::Fg(color::Reset)),
- path_color = options.color.display(path_color),
- path_color_reset = options.color.display(color::Fg(color::Reset)),
byte_column_width = options.byte_format.width()
)
}