Skip to content

Commit

Permalink
Support for colors. Using green, which might be invisible to some!
Browse files Browse the repository at this point in the history
Probably those who can't see red or green will have configured their
terminal to display different colors instead.
  • Loading branch information
Sebastian Thiel committed Jun 1, 2019
1 parent 498bcd0 commit 9d09499
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 2 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
5 changes: 4 additions & 1 deletion src/aggregate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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("")
Expand Down
31 changes: 31 additions & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use jwalk::WalkDir;
use std::fmt;
use std::path::Path;

pub enum ByteFormat {
Expand All @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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 {
Expand Down

0 comments on commit 9d09499

Please sign in to comment.