diff --git a/Cargo.lock b/Cargo.lock index 22ea314c..3ba72a6e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -111,6 +111,7 @@ dependencies = [ "ignore", "indextree", "indoc", + "is-terminal", "lscolors", "once_cell", "strip-ansi-escapes", @@ -129,6 +130,17 @@ dependencies = [ "winapi", ] +[[package]] +name = "errno" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50d6a0976c999d473fe89ad888d5a284e55366d9dc9038b1ba2aa15128c4afa0" +dependencies = [ + "errno-dragonfly", + "libc", + "windows-sys 0.45.0", +] + [[package]] name = "errno-dragonfly" version = "0.1.2" @@ -239,13 +251,13 @@ dependencies = [ [[package]] name = "is-terminal" -version = "0.4.4" +version = "0.4.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "21b6b32576413a8e69b90e952e4a026476040d81017b80445deda5f2d3921857" +checksum = "256017f749ab3117e93acb91063009e1f1bb56d03965b14c2c8df4eb02c524d8" dependencies = [ "hermit-abi", "io-lifetimes", - "rustix", + "rustix 0.37.7", "windows-sys 0.45.0", ] @@ -267,6 +279,12 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f051f77a7c8e6957c0696eac88f26b0117e54f52d3fc682ab19397a8812846a4" +[[package]] +name = "linux-raw-sys" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d59d8c75012853d2e872fb56bc8a2e53718e2cafe1a4c823143141c6d90c322f" + [[package]] name = "log" version = "0.4.17" @@ -395,10 +413,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd5c6ff11fecd55b40746d1995a02f2eb375bf8c00d192d521ee09f42bef37bc" dependencies = [ "bitflags", - "errno", + "errno 0.2.8", + "io-lifetimes", + "libc", + "linux-raw-sys 0.1.4", + "windows-sys 0.45.0", +] + +[[package]] +name = "rustix" +version = "0.37.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2aae838e49b3d63e9274e1c01833cc8139d3fec468c3b84688c628f44b1ae11d" +dependencies = [ + "bitflags", + "errno 0.3.0", "io-lifetimes", "libc", - "linux-raw-sys", + "linux-raw-sys 0.3.1", "windows-sys 0.45.0", ] @@ -463,7 +495,7 @@ dependencies = [ "cfg-if", "fastrand", "redox_syscall", - "rustix", + "rustix 0.36.9", "windows-sys 0.42.0", ] diff --git a/Cargo.toml b/Cargo.toml index fd4753df..072b1e22 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,6 +30,7 @@ clap_complete = "4.1.1" filesize = "0.2.0" ignore = "0.4.2" indextree = "4.6.0" +is-terminal = "0.4.6" lscolors = { version = "0.13.0", features = ["ansi_term"] } once_cell = "1.17.0" thiserror = "1.0.40" diff --git a/src/main.rs b/src/main.rs index a395b16e..6e93c972 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,10 +20,7 @@ )] use clap::CommandFactory; use render::{context::Context, tree::Tree}; -use std::{ - io::{stdout, BufRead}, - process::ExitCode, -}; +use std::{io::stdout, process::ExitCode}; /// Filesystem operations. mod fs; @@ -47,17 +44,7 @@ fn main() -> ExitCode { } fn run() -> Result<(), Box> { - let mut ctx = Context::init()?; - - if ctx.stdin { - let mut stdin_lines = std::io::stdin() - .lock() - .lines() - .filter_map(|s| s.ok()) - .filter(|l| !l.is_empty()) - .collect::>(); - ctx.glob.append(&mut stdin_lines); - } + let ctx = Context::init()?; if let Some(shell) = ctx.completions { clap_complete::generate(shell, &mut Context::command(), "et", &mut stdout().lock()); diff --git a/src/render/context/mod.rs b/src/render/context/mod.rs index 42ce53ad..931c3ff1 100644 --- a/src/render/context/mod.rs +++ b/src/render/context/mod.rs @@ -6,9 +6,11 @@ use clap::{ parser::ValueSource, ArgMatches, CommandFactory, Error as ClapError, FromArgMatches, Id, Parser, }; use ignore::overrides::{Override, OverrideBuilder}; +use is_terminal::IsTerminal; use std::{ convert::From, ffi::{OsStr, OsString}, + io::{stdin, BufRead}, path::{Path, PathBuf}, }; @@ -128,10 +130,6 @@ pub struct Context { /// Don't read configuration file #[arg(long)] pub no_config: bool, - - /// Take input from Stdin - #[arg(long, hide = true)] - pub stdin: bool, } impl Context { @@ -139,7 +137,19 @@ impl Context { /// Arguments provided will take precedence over config. pub fn init() -> Result { let mut args: Vec<_> = std::env::args().collect(); - crate::utils::detect_stdin(&mut args); + + // If there's input on stdin we add each line as a separate glob pattern + if !stdin().is_terminal() { + stdin() + .lock() + .lines() + .filter_map(|s| s.ok()) + .filter(|l| !l.is_empty()) + .for_each(|line| { + args.push("--glob".into()); + args.push(line); + }); + } let user_args = Self::command() .args_override_self(true) diff --git a/src/render/tree/node/mod.rs b/src/render/tree/node/mod.rs index cdf060b4..6d26b075 100644 --- a/src/render/tree/node/mod.rs +++ b/src/render/tree/node/mod.rs @@ -234,7 +234,10 @@ impl Node { match size_loc { SizeLocation::Right => { - write!(f, "{prefix}{icon: { write!(f, "{size} {prefix}{icon:>() } - -/// Follow the naming convention and use "-" to specify a Standard Input. -/// Retain "-" from [`args`] and add "--stdin" if necessary. -pub fn detect_stdin(args: &mut Vec) { - let dash = String::from("-"); - let stdin_flag = String::from("--stdin"); - - let mut is_stdin = false; - args.retain(|e| { - if *e == dash { - is_stdin = true - }; - *e != dash - }); - if is_stdin && !args.contains(&stdin_flag) { - args.push(stdin_flag) - } -} diff --git a/tests/glob.rs b/tests/glob.rs index 47329192..8c51d835 100644 --- a/tests/glob.rs +++ b/tests/glob.rs @@ -86,6 +86,18 @@ fn glob_stdin() { use std::io::Write; use std::process::{Command, Stdio}; use strip_ansi_escapes::strip as strip_ansi_escapes; + let expected = indoc!( + " + data (304 B) + ├─ dream_cycle + ├─ lipsum + ├─ nemesis.txt (161 B) + └─ the_yellow_king (143 B) + └─ cassildas_song.md (143 B) + + " + ); + let stdin = String::from("cassildas_song.md\nnemesis.txt\n"); let cmd = Command::new("cargo") .args([ @@ -96,42 +108,19 @@ fn glob_stdin() { "--no-config", "--sort", "name", - "-", + "tests/data", ]) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn() .unwrap(); - - let stdin = String::from("cassildas_song.md\nnemesis.txt\n"); write!(cmd.stdin.as_ref().unwrap(), "{}", stdin).unwrap(); let output = cmd.wait_with_output().unwrap(); assert_eq!( String::from_utf8(strip_ansi_escapes(output.stdout).unwrap()).unwrap(), - indoc!( - " - erdtree (304 B) - ├─ assets - ├─ src - │ ├─ fs - │ └─ render - │ ├─ context - │ ├─ disk_usage - │ └─ tree - │ └─ node - └─ tests (304 B) - ├─ data (304 B) - │ ├─ dream_cycle - │ ├─ lipsum - │ ├─ nemesis.txt (161 B) - │ └─ the_yellow_king (143 B) - │ └─ cassildas_song.md (143 B) - └─ utils - - " - ) + expected ); assert!(output.status.success()); } diff --git a/tests/utils/mod.rs b/tests/utils/mod.rs index 7426d162..2f9119cf 100644 --- a/tests/utils/mod.rs +++ b/tests/utils/mod.rs @@ -11,6 +11,7 @@ pub fn run_cmd(args: &[&str]) -> String { } let output = cmd + .stdin(Stdio::null()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) .spawn()