summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSebastian Thiel <sthiel@thoughtworks.com>2019-06-01 15:27:17 +0530
committerSebastian Thiel <sthiel@thoughtworks.com>2019-06-01 15:27:44 +0530
commit9d0949933cb46d2e73c047b5f06201dbd75bca1d (patch)
tree16b9341106abc36e72a9e5e0f0f354611af51e42
parent498bcd0da4dc44d04634f2cabc245f4c46d2c46a (diff)
Support for colors. Using green, which might be invisible to some!
Probably those who can't see red or green will have configured their terminal to display different colors instead.
-rw-r--r--Cargo.toml2
-rw-r--r--src/aggregate.rs5
-rw-r--r--src/common.rs31
-rw-r--r--src/main.rs7
4 files changed, 43 insertions, 2 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 8c57062..e5e8d63 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -11,6 +11,8 @@ failure-tools = "4.0.2"
structopt = "0.2.14"
jwalk = "0.4.0"
byte-unit = "2.1.0"
+termion = "1.5.2"
+atty = "0.2.11"
[[bin]]
name="dua"
diff --git a/src/aggregate.rs b/src/aggregate.rs
index 2f36171..6079e04 100644
--- a/src/aggregate.rs
+++ b/src/aggregate.rs
@@ -83,10 +83,13 @@ fn write_path(
num_bytes: u64,
num_errors: u64,
) -> Result<(), io::Error> {
+ use termion::color;
writeln!(
out,
- "{:>10}\t{}{}",
+ "{}{:>10}{}\t{}{}",
+ options.color.display(color::Fg(color::Green)),
options.format_bytes(num_bytes),
+ options.color.display(color::Fg(color::Reset)),
path.as_ref().display(),
if num_errors == 0 {
Cow::Borrowed("")
diff --git a/src/common.rs b/src/common.rs
index 658a404..95b6967 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -1,4 +1,5 @@
use jwalk::WalkDir;
+use std::fmt;
use std::path::Path;
pub enum ByteFormat {
@@ -12,9 +13,39 @@ pub enum Sorting {
Alphabetical,
}
+#[derive(Clone, Copy)]
+pub enum Color {
+ None,
+ Terminal,
+}
+
+pub struct DisplayColor<C> {
+ kind: Color,
+ color: C,
+}
+
+impl Color {
+ pub fn display<C>(&self, color: C) -> DisplayColor<C> {
+ DisplayColor { kind: *self, color }
+ }
+}
+
+impl<C> fmt::Display for DisplayColor<C>
+where
+ C: fmt::Display,
+{
+ fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
+ match self.kind {
+ Color::None => Ok(()),
+ Color::Terminal => self.color.fmt(f),
+ }
+ }
+}
+
pub struct WalkOptions {
pub threads: usize,
pub format: ByteFormat,
+ pub color: Color,
}
impl WalkOptions {
diff --git a/src/main.rs b/src/main.rs
index 490d860..a687030 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,7 +4,7 @@ extern crate structopt;
use structopt::StructOpt;
-use dua::ByteFormat;
+use dua::{ByteFormat, Color};
use failure::Error;
use failure_tools::ok_or_exit;
use std::{io, io::Write, path::PathBuf, process};
@@ -20,6 +20,11 @@ fn run() -> Result<(), Error> {
let walk_options = dua::WalkOptions {
threads: opt.threads.unwrap_or(0),
format: opt.format.map(Into::into).unwrap_or(ByteFormat::Metric),
+ color: if atty::is(atty::Stream::Stdout) {
+ Color::Terminal
+ } else {
+ Color::None
+ },
};
let (show_statistics, res) = match opt.command {
Some(Aggregate {