Skip to content

Commit

Permalink
color optional, contrast fix
Browse files Browse the repository at this point in the history
  • Loading branch information
kernelPanic0x committed Dec 13, 2024
1 parent 4002c74 commit 50ac57c
Showing 1 changed file with 70 additions and 18 deletions.
88 changes: 70 additions & 18 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,18 @@ struct WormholeCli {
display_order = 100
)]
log: bool,

#[clap(subcommand)]
command: WormholeCommand,

/// Disable colored output
#[arg(
long = "no-color",
global = true,
help = "Disable colored output",
display_order = 101
)]
no_color: bool,
}

#[async_std::main]
Expand All @@ -272,6 +282,11 @@ async fn main() -> eyre::Result<()> {

let mut term = Term::stdout();

// Set NO_COLOR environment variable if --no-color flag is used
if app.no_color {
std::env::set_var("NO_COLOR", "1");
}

if app.log {
tracing_subscriber::fmt()
.with_max_level(tracing::Level::TRACE)
Expand Down Expand Up @@ -733,11 +748,16 @@ async fn make_send_offer(
fn create_progress_bar(file_size: u64) -> ProgressBar {
use indicatif::ProgressStyle;

let template = match should_use_color() {
true => "[{elapsed_precise:.yellow.dim}] [{wide_bar}] {bytes:.blue}/{total_bytes:.blue} | {decimal_bytes_per_sec:.green} | ETA: {eta:.yellow.dim}",
false => "[{elapsed_precise}] [{wide_bar}] {bytes}/{total_bytes} | {decimal_bytes_per_sec} | ETA: {eta}",
};

let pb = ProgressBar::new(file_size);
pb.set_style(
ProgressStyle::default_bar()
// .template("[{elapsed_precise}] [{wide_bar:.cyan/blue}] {bytes}/{total_bytes} ({eta})")
.template("[{elapsed_precise:.yellow}] [{wide_bar}] {bytes:.blue}/{total_bytes:.blue} {decimal_bytes_per_sec:.cyan} ({eta:.yellow})")
.template(&template)
.unwrap()
.progress_chars("#>-"),
);
Expand Down Expand Up @@ -1022,15 +1042,27 @@ async fn receive_inner_v1(
use number_prefix::NumberPrefix;
if !(noconfirm
|| util::ask_user(
format!(
"Receive file '{}' ({})?",
req.file_name().green(),
match NumberPrefix::binary(req.file_size() as f64) {
NumberPrefix::Standalone(bytes) => format!("{} bytes", bytes),
NumberPrefix::Prefixed(prefix, n) => format!("{:.1} {}B", n, prefix.symbol()),
}
.blue(),
),
match should_use_color() {
true => format!(
"Receive file '{}' ({})?",
req.file_name().green(),
match NumberPrefix::binary(req.file_size() as f64) {
NumberPrefix::Standalone(bytes) => format!("{} bytes", bytes),
NumberPrefix::Prefixed(prefix, n) =>
format!("{:.1} {}B", n, prefix.symbol()),
}
.blue(),
),
false => format!(
"Receive file '{}' ({})?",
req.file_name(),
match NumberPrefix::binary(req.file_size() as f64) {
NumberPrefix::Standalone(bytes) => format!("{} bytes", bytes),
NumberPrefix::Prefixed(prefix, n) =>
format!("{:.1} {}B", n, prefix.symbol()),
},
),
},
true,
)
.await)
Expand Down Expand Up @@ -1064,7 +1096,10 @@ async fn receive_inner_v1(

/* If there is a collision, ask whether to overwrite */
if !util::ask_user(
format!("Override existing file {}?", file_path.display()).red(),
match should_use_color() {
true => format!("Override existing file {}?", file_path.display().red()),
false => format!("Override existing file {}?", file_path.display()),
},
false,
)
.await
Expand Down Expand Up @@ -1188,14 +1223,31 @@ async fn receive_inner_v2(

fn transit_handler(info: TransitInfo) {
tracing::info!("{info}");
let mut term = Term::stdout();
let mut term = Term::stderr();
let use_color = should_use_color();

let _ = writeln!(
term,
"Connecting {} to {}",
info.conn_type.bright_magenta(),
info.peer_addr.cyan()
);
let conn_type = if use_color {
info.conn_type.bright_magenta().to_string()
} else {
info.conn_type.to_string()
};

let peer_addr = if use_color {
info.peer_addr.cyan().to_string()
} else {
info.peer_addr.to_string()
};

let _ = writeln!(term, "Connecting {} to {}", conn_type, peer_addr);
}

fn should_use_color() -> bool {
// Don't use colors if NO_COLOR is set (regardless of value)
if std::env::var_os("NO_COLOR").is_some() {
return false;
}
// Don't use colors when stdout is not a terminal
std::io::stdout().is_terminal()
}

#[cfg(test)]
Expand Down

0 comments on commit 50ac57c

Please sign in to comment.