From 9d0949933cb46d2e73c047b5f06201dbd75bca1d Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Sat, 1 Jun 2019 15:27:17 +0530 Subject: 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. --- src/aggregate.rs | 5 ++++- src/common.rs | 31 +++++++++++++++++++++++++++++++ src/main.rs | 7 ++++++- 3 files changed, 41 insertions(+), 2 deletions(-) (limited to 'src') 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 { + kind: Color, + color: C, +} + +impl Color { + pub fn display(&self, color: C) -> DisplayColor { + DisplayColor { kind: *self, color } + } +} + +impl fmt::Display for DisplayColor +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 { -- cgit v1.2.3