diff options
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/aggregate.rs | 5 | ||||
-rw-r--r-- | src/common.rs | 31 | ||||
-rw-r--r-- | src/main.rs | 7 |
4 files changed, 43 insertions, 2 deletions
@@ -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 { |