Skip to content

Commit

Permalink
Add detection of stdin input with is_terminal
Browse files Browse the repository at this point in the history
* remove (hidden) flag `--stdin`
* remove ability to detect stdin with "-"
* refactor move stdin detection to Context::init
  • Loading branch information
jhscheer committed Apr 7, 2023
1 parent 23070d8 commit ebd67e8
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 70 deletions.
44 changes: 38 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 2 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -47,17 +44,7 @@ fn main() -> ExitCode {
}

fn run() -> Result<(), Box<dyn std::error::Error>> {
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::<Vec<String>>();
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());
Expand Down
20 changes: 15 additions & 5 deletions src/render/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -128,18 +130,26 @@ 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 {
/// Initializes [Context], optionally reading in the configuration file to override defaults.
/// Arguments provided will take precedence over config.
pub fn init() -> Result<Self, Error> {
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)
Expand Down
5 changes: 4 additions & 1 deletion src/render/tree/node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,10 @@ impl Node {

match size_loc {
SizeLocation::Right => {
write!(f, "{prefix}{icon:<icon_padding$}{styled_name}{size_padding}{size}")
write!(
f,
"{prefix}{icon:<icon_padding$}{styled_name}{size_padding}{size}"
)
}
SizeLocation::Left => {
write!(f, "{size} {prefix}{icon:<icon_padding$}{styled_name}")
Expand Down
18 changes: 0 additions & 18 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,3 @@ where
.filter(|item| set.insert(item.to_owned()))
.collect::<Vec<T>>()
}

/// 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<String>) {
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)
}
}
39 changes: 14 additions & 25 deletions tests/glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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());
}
1 change: 1 addition & 0 deletions tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub fn run_cmd(args: &[&str]) -> String {
}

let output = cmd
.stdin(Stdio::null())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()
Expand Down

0 comments on commit ebd67e8

Please sign in to comment.