summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSebastian Thiel <sebastian.thiel@icloud.com>2020-07-07 08:23:23 +0800
committerSebastian Thiel <sebastian.thiel@icloud.com>2020-07-07 08:23:23 +0800
commit82d005b9e3ed9ce8d4441c607ec160f2f0a48b1c (patch)
tree8e8266faa454065f15c11871a745af96f64b9ce9 /src
parent2c73b4d59603c12d31ded1a2f2ca9ef97a5ff0b3 (diff)
Fix color handling (causing the text to disappear); fix tty detection
Crossterm works differently from termion, I might say: more correct!
Diffstat (limited to 'src')
-rw-r--r--src/aggregate.rs12
-rw-r--r--src/common.rs33
-rw-r--r--src/interactive/app_test/utils.rs3
-rw-r--r--src/main.rs16
4 files changed, 13 insertions, 51 deletions
diff --git a/src/aggregate.rs b/src/aggregate.rs
index a754c98..8d16047 100644
--- a/src/aggregate.rs
+++ b/src/aggregate.rs
@@ -135,13 +135,11 @@ fn output_colored_path(
.to_string()
.as_str()
.green(),
- options.color.display(
- path.as_ref()
- .display()
- .to_string()
- .as_str()
- .color(path_color.unwrap_or(Color::White))
- ),
+ path.as_ref()
+ .display()
+ .to_string()
+ .as_str()
+ .color(path_color.unwrap_or(Color::White)),
if num_errors == 0 {
Cow::Borrowed("")
} else {
diff --git a/src/common.rs b/src/common.rs
index d297c45..eb1bb03 100644
--- a/src/common.rs
+++ b/src/common.rs
@@ -112,38 +112,6 @@ pub enum TraversalSorting {
AlphabeticalByFileName,
}
-/// Specify the kind of color to use
-#[derive(Clone, Copy)]
-pub enum Color {
- /// Use no color
- None,
- /// Use terminal colors
- Terminal,
-}
-
-pub(crate) struct DisplayColor<C> {
- kind: Color,
- color: C,
-}
-
-impl Color {
- pub(crate) 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),
- }
- }
-}
-
/// Configures a filesystem walk, including output and formatting options.
#[derive(Clone)]
pub struct WalkOptions {
@@ -153,7 +121,6 @@ pub struct WalkOptions {
pub byte_format: ByteFormat,
pub count_hard_links: bool,
pub apparent_size: bool,
- pub color: Color,
pub sorting: TraversalSorting,
pub cross_filesystems: bool,
}
diff --git a/src/interactive/app_test/utils.rs b/src/interactive/app_test/utils.rs
index 3d1bbb5..a42f36b 100644
--- a/src/interactive/app_test/utils.rs
+++ b/src/interactive/app_test/utils.rs
@@ -2,7 +2,7 @@ use crate::interactive::{app_test::FIXTURE_PATH, Interaction, TerminalApp};
use anyhow::{Context, Error, Result};
use dua::{
traverse::{EntryData, Tree, TreeIndex},
- ByteFormat, Color, TraversalSorting, WalkOptions,
+ ByteFormat, TraversalSorting, WalkOptions,
};
use itertools::Itertools;
use jwalk::{DirEntry, WalkDir};
@@ -175,7 +175,6 @@ pub fn initialized_app_and_terminal_with_closure<P: AsRef<Path>>(
byte_format: ByteFormat::Metric,
apparent_size: true,
count_hard_links: false,
- color: Color::None,
sorting: TraversalSorting::AlphabeticalByFileName,
cross_filesystems: false,
},
diff --git a/src/main.rs b/src/main.rs
index 9c680a3..4c0edc2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1,7 +1,7 @@
#![forbid(unsafe_code)]
#![allow(clippy::match_bool)]
-use anyhow::Result;
-use dua::{ByteFormat, Color, TraversalSorting};
+use anyhow::{anyhow, Result};
+use dua::{ByteFormat, TraversalSorting};
use std::{fs, io, io::Write, path::PathBuf, process};
use structopt::StructOpt;
use wild;
@@ -17,11 +17,6 @@ fn main() -> Result<()> {
let walk_options = dua::WalkOptions {
threads: opt.threads.unwrap_or(0),
byte_format: opt.format.map(Into::into).unwrap_or(ByteFormat::Metric),
- color: if atty::is(atty::Stream::Stdout) {
- Color::Terminal
- } else {
- Color::None
- },
apparent_size: opt.apparent_size,
count_hard_links: opt.count_hard_links,
sorting: TraversalSorting::None,
@@ -33,10 +28,13 @@ fn main() -> Result<()> {
use crate::interactive::{Interaction, TerminalApp};
use anyhow::Context;
use crosstermion::terminal::{tui::new_terminal, AlternateRawScreen};
+ let no_tty_msg = "Interactive mode requires a connected terminal";
+ if atty::isnt(atty::Stream::Stdout) {
+ return Err(anyhow!(no_tty_msg));
+ }
let mut terminal = new_terminal(
- AlternateRawScreen::try_from(io::stdout())
- .with_context(|| "Interactive mode requires a connected terminal")?,
+ AlternateRawScreen::try_from(io::stdout()).with_context(|| no_tty_msg)?,
)
.with_context(|| "Could not instantiate terminal")?;
let res = TerminalApp::initialize(