Skip to content

Commit 2d48998

Browse files
authored
feat: hide progress bar if input is stdin only (#1938)
1 parent caa7d5c commit 2d48998

File tree

4 files changed

+25
-3
lines changed

4 files changed

+25
-3
lines changed

lychee-bin/src/commands/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ where
6161
accept,
6262
));
6363

64-
let hide_bar = params.cfg.no_progress;
64+
let hide_bar = params.cfg.no_progress || params.is_stdin_input;
6565
let level = params.cfg.verbose().log_level();
6666

6767
let progress = Progress::new("Extracting links", hide_bar, level, &params.cfg.mode());

lychee-bin/src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub(crate) struct CommandParams<S: futures::Stream<Item = Result<Request, Reques
2222
pub(crate) cache: Cache,
2323
pub(crate) requests: S,
2424
pub(crate) cfg: Config,
25+
pub(crate) is_stdin_input: bool,
2526
}
2627

2728
/// Creates a writer that outputs to a file or stdout.

lychee-bin/src/main.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
#![deny(missing_docs)]
6060

6161
use std::fs::{self, File};
62-
use std::io::{self, BufRead, BufReader, ErrorKind};
62+
use std::io::{self, BufRead, BufReader, ErrorKind, IsTerminal, stdin};
6363
use std::path::PathBuf;
6464

6565
use anyhow::{Context, Error, Result};
@@ -301,6 +301,22 @@ fn underlying_io_error_kind(error: &Error) -> Option<io::ErrorKind> {
301301
async fn run(opts: &LycheeOptions) -> Result<i32> {
302302
let inputs = opts.inputs()?;
303303

304+
// Hide the progress bar only when stdin is the sole input and it is
305+
// interactive (TTY).
306+
//
307+
// We restrict this to the sole-input case because with mixed inputs like
308+
// `lychee - README.md` the file is processed concurrently with stdin, so
309+
// the order of completion is non-deterministic. Hiding the bar there would
310+
// be confusing rather than helpful.
311+
//
312+
// When stdin is piped (`cat links.txt | lychee -`), `is_terminal()` returns
313+
// false, so the progress bar is shown normally.
314+
let is_stdin_input = inputs.len() == 1
315+
&& inputs
316+
.iter()
317+
.any(|input| matches!(input.source, lychee_lib::InputSource::Stdin))
318+
&& stdin().is_terminal();
319+
304320
// TODO: Remove this section after `--base` got removed with 1.0
305321
let base = match (opts.config.base.clone(), opts.config.base_url.clone()) {
306322
(None, base_url) => base_url,
@@ -367,6 +383,7 @@ async fn run(opts: &LycheeOptions) -> Result<i32> {
367383
cache,
368384
requests,
369385
cfg: opts.config.clone(),
386+
is_stdin_input,
370387
};
371388

372389
let exit_code = if opts.config.dump {

lychee-lib/src/types/input/input.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{ErrorKind, LycheeResult};
1313
use async_stream::try_stream;
1414
use futures::stream::{Stream, StreamExt};
1515
use log::debug;
16+
use std::io::IsTerminal;
1617
use std::path::{Path, PathBuf};
1718
use tokio::io::{AsyncReadExt, stdin};
1819

@@ -271,7 +272,10 @@ impl Input {
271272
let mut content = String::new();
272273
let mut stdin = stdin();
273274

274-
debug!("Reading content from stdin"); // useful info when nothing piped and process blocks
275+
if std::io::stdin().is_terminal() {
276+
// useful info when nothing piped and process blocks
277+
debug!("Reading content from stdin");
278+
}
275279
stdin.read_to_string(&mut content).await?;
276280

277281
let input_content = InputContent {

0 commit comments

Comments
 (0)