Skip to content

Commit 6dd49aa

Browse files
committed
Respect terminal color capabilities in forc output
Detect NO_COLOR, FORC_TERM_COLOR, and non-interactive streams before emitting ANSI Refactor formatting logic into a pure helper and cover it with unit tests Drop the unused tracing-test dev dependency now that log capture isn’t needed
1 parent 547b978 commit 6dd49aa

File tree

9 files changed

+171
-119
lines changed

9 files changed

+171
-119
lines changed

Cargo.lock

Lines changed: 0 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

forc-pkg/src/lock.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -352,10 +352,8 @@ where
352352
true => format!(" {}", pkg.source),
353353
false => String::new(),
354354
};
355-
println_action_red(
356-
"Removing",
357-
&format!("{}{src}", ansiterm::Style::new().bold().paint(&pkg.name)),
358-
);
355+
let pkg_name = format_pkg_name(&pkg.name);
356+
println_action_red("Removing", &format!("{pkg_name}{src}"));
359357
}
360358
}
361359
}
@@ -370,14 +368,20 @@ where
370368
true => format!(" {}", pkg.source),
371369
false => "".to_string(),
372370
};
373-
println_action_green(
374-
"Adding",
375-
&format!("{}{src}", ansiterm::Style::new().bold().paint(&pkg.name)),
376-
);
371+
let pkg_name = format_pkg_name(&pkg.name);
372+
println_action_green("Adding", &format!("{pkg_name}{src}"));
377373
}
378374
}
379375
}
380376

377+
fn format_pkg_name(name: &str) -> String {
378+
if forc_tracing::ansi_output_enabled() {
379+
format!("{}", ansiterm::Style::new().bold().paint(name))
380+
} else {
381+
name.to_owned()
382+
}
383+
}
384+
381385
#[cfg(test)]
382386
mod tests {
383387
use sway_core::fuel_prelude::fuel_tx;

forc-pkg/src/pkg.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,11 +2334,23 @@ fn print_pkg_summary_header(built_pkg: &BuiltPackage) {
23342334
// type and name around the 10th column ourselves.
23352335
let padded_ty_str = format!("{prog_ty_str:>10}");
23362336
let padding = &padded_ty_str[..padded_ty_str.len() - prog_ty_str.len()];
2337-
let ty_ansi = ansiterm::Colour::Green.bold().paint(prog_ty_str);
2338-
let name_ansi = ansiterm::Style::new()
2339-
.bold()
2340-
.paint(&built_pkg.descriptor.name);
2341-
debug!("{padding}{ty_ansi} {name_ansi}");
2337+
let colors_enabled = forc_tracing::ansi_output_enabled();
2338+
let ty_fmt = if colors_enabled {
2339+
format!("{}", ansiterm::Colour::Green.bold().paint(prog_ty_str))
2340+
} else {
2341+
prog_ty_str.to_string()
2342+
};
2343+
let name_fmt = if colors_enabled {
2344+
format!(
2345+
"{}",
2346+
ansiterm::Style::new()
2347+
.bold()
2348+
.paint(&built_pkg.descriptor.name)
2349+
)
2350+
} else {
2351+
built_pkg.descriptor.name.clone()
2352+
};
2353+
debug!("{padding}{ty_fmt} {name_fmt}");
23422354
}
23432355

23442356
/// Returns the ContractId of a built_package contract with specified `salt`.

forc-pkg/src/source/git/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,10 +205,12 @@ impl source::Fetch for Pinned {
205205
{
206206
let _guard = lock.write()?;
207207
if !repo_path.exists() {
208-
println_action_green(
209-
"Fetching",
210-
&format!("{} {}", ansiterm::Style::new().bold().paint(ctx.name), self),
211-
);
208+
let pkg_name = if forc_tracing::ansi_output_enabled() {
209+
format!("{}", ansiterm::Style::new().bold().paint(ctx.name))
210+
} else {
211+
ctx.name.to_string()
212+
};
213+
println_action_green("Fetching", &format!("{pkg_name} {}", self));
212214
fetch(ctx.fetch_id(), ctx.name(), self)?;
213215
}
214216
}

forc-pkg/src/source/ipfs.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,12 @@ impl source::Fetch for Pinned {
7474
{
7575
let _guard = lock.write()?;
7676
if !repo_path.exists() {
77-
println_action_green(
78-
"Fetching",
79-
&format!("{} {}", ansiterm::Style::new().bold().paint(ctx.name), self),
80-
);
77+
let pkg_name = if forc_tracing::ansi_output_enabled() {
78+
format!("{}", ansiterm::Style::new().bold().paint(ctx.name))
79+
} else {
80+
ctx.name.to_string()
81+
};
82+
println_action_green("Fetching", &format!("{pkg_name} {}", self));
8183
let cid = self.0.clone();
8284
let ipfs_node = ctx.ipfs_node().clone();
8385
let ipfs_client = ipfs_client();

forc-pkg/src/source/reg/mod.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,14 +381,12 @@ impl source::Fetch for Pinned {
381381
{
382382
let _guard = lock.write()?;
383383
if !path.exists() {
384-
println_action_green(
385-
"Fetching",
386-
&format!(
387-
"{} {}",
388-
ansiterm::Style::new().bold().paint(ctx.name),
389-
self.source.version
390-
),
391-
);
384+
let pkg_name = if forc_tracing::ansi_output_enabled() {
385+
format!("{}", ansiterm::Style::new().bold().paint(ctx.name))
386+
} else {
387+
ctx.name.to_string()
388+
};
389+
println_action_green("Fetching", &format!("{pkg_name} {}", self.source.version));
392390
let pinned = self.clone();
393391
let fetch_id = ctx.fetch_id();
394392
let ipfs_node = ctx.ipfs_node().clone();

forc-tracing/Cargo.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,5 @@ regex.workspace = true
1414
tracing.workspace = true
1515
tracing-subscriber = { workspace = true, features = ["ansi", "env-filter", "json"] }
1616

17-
[dev-dependencies]
18-
tracing-test.workspace = true
19-
2017
[lints]
2118
workspace = true

0 commit comments

Comments
 (0)