summaryrefslogtreecommitdiffstats
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
parent2c73b4d59603c12d31ded1a2f2ca9ef97a5ff0b3 (diff)
Fix color handling (causing the text to disappear); fix tty detection
Crossterm works differently from termion, I might say: more correct!
-rw-r--r--Cargo.lock16
-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
-rw-r--r--tests/snapshots/failure-interactive-without-tty5
6 files changed, 30 insertions, 55 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 07191b1..c338bf9 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -247,6 +247,7 @@ dependencies = [
"tui",
"tui-react 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation",
+ "wild",
]
[[package]]
@@ -296,6 +297,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7"
[[package]]
+name = "glob"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
+
+[[package]]
name = "heck"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -804,6 +811,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
+name = "wild"
+version = "2.0.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "035793abb854745033f01a07647a79831eba29ec0be377205f2a25b0aa830020"
+dependencies = [
+ "glob",
+]
+
+[[package]]
name = "winapi"
version = "0.2.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
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(
diff --git a/tests/snapshots/failure-interactive-without-tty b/tests/snapshots/failure-interactive-without-tty
index d5d704b..741f6a0 100644
--- a/tests/snapshots/failure-interactive-without-tty
+++ b/tests/snapshots/failure-interactive-without-tty
@@ -1,4 +1 @@
-Error: Interactive mode requires a connected terminal
-
-Caused by:
- Inappropriate ioctl for device (os error 25) \ No newline at end of file
+Error: Interactive mode requires a connected terminal \ No newline at end of file