Skip to content

Commit

Permalink
feat: disable progress in non tty (prefix-dev#2308)
Browse files Browse the repository at this point in the history
  • Loading branch information
ruben-arts authored Oct 26, 2024
1 parent c79cb70 commit 48b521e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
13 changes: 11 additions & 2 deletions install/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,23 @@ cleanup() {

trap cleanup EXIT

# Test if stdout is a terminal before showing progress
if [[ ! -t 1 ]]; then
CURL_OPTIONS="--no-progress-meter"
WGET_OPTIONS="--no-verbose"
else
CURL_OPTIONS="--progress-bar"
WGET_OPTIONS="--show-progress"
fi

if hash curl 2> /dev/null; then
HTTP_CODE="$(curl -SL --progress-bar "$DOWNLOAD_URL" --output "$TEMP_FILE" --write-out "%{http_code}")"
HTTP_CODE="$(curl -SL $CURL_OPTIONS "$DOWNLOAD_URL" --output "$TEMP_FILE" --write-out "%{http_code}")"
if [[ "${HTTP_CODE}" -lt 200 || "${HTTP_CODE}" -gt 299 ]]; then
echo "error: '${DOWNLOAD_URL}' is not available"
exit 1
fi
elif hash wget 2> /dev/null; then
if ! wget -q --show-progress --output-document="$TEMP_FILE" "$DOWNLOAD_URL"; then
if ! wget $WGET_OPTIONS --output-document="$TEMP_FILE" "$DOWNLOAD_URL"; then
echo "error: '${DOWNLOAD_URL}' is not available"
exit 1
fi
Expand Down
17 changes: 13 additions & 4 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use std::{env, io::IsTerminal};

use clap::Parser;
use clap_verbosity_flag::Verbosity;
use indicatif::ProgressDrawTarget;
use miette::IntoDiagnostic;
use pixi_consts::consts;
use std::{env, io::IsTerminal};
use tracing_subscriber::{
filter::LevelFilter, prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt,
EnvFilter,
Expand Down Expand Up @@ -80,10 +79,20 @@ struct Args {
#[clap(long, default_value = "auto", global = true, env = "PIXI_COLOR")]
color: ColorOutput,

/// Hide all progress bars
/// Hide all progress bars, always turned on if stderr is not a terminal.
#[clap(long, default_value = "false", global = true, env = "PIXI_NO_PROGRESS")]
no_progress: bool,
}
impl Args {
/// Whether to show progress bars or not, based on the terminal and the user's preference.
fn no_progress(&self) -> bool {
if !std::io::stderr().is_terminal() {
true
} else {
self.no_progress
}
}
}

#[derive(Parser, Debug)]
pub enum Command {
Expand Down Expand Up @@ -184,7 +193,7 @@ pub async fn execute() -> miette::Result<()> {
console::set_colors_enabled_stderr(use_colors);

// Hide all progress bars if the user requested it.
if args.no_progress {
if args.no_progress() {
global_multi_progress().set_draw_target(ProgressDrawTarget::hidden());
}

Expand Down

0 comments on commit 48b521e

Please sign in to comment.