Skip to content

Commit c3f3cc3

Browse files
committed
feat: add support for NO_COLOR standard for printing console messages
Signed-off-by: reubenmiller <[email protected]>
1 parent a70f207 commit c3f3cc3

File tree

6 files changed

+23
-13
lines changed

6 files changed

+23
-13
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,10 @@ which = "4.2"
211211
whoami = "1.5.0"
212212
ws_stream_tungstenite = "0.14"
213213
x509-parser = "0.16"
214-
yansi = "1.0.1"
214+
yansi = { version = "1.0.1", default-features = false, features = [
215+
"detect-env",
216+
"detect-tty",
217+
] }
215218
zeroize = "1.5"
216219

217220

crates/common/axum_tls/src/config.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ use rustls::RootCertStore;
1111
use std::fmt::Debug;
1212
use std::fs::File;
1313
use std::io;
14-
use std::io::stderr;
1514
use std::io::Cursor;
16-
use std::io::IsTerminal;
1715
use std::path::Path;
1816
use tedge_config::OptionalConfig;
1917
use tracing::info;
@@ -68,11 +66,6 @@ pub fn load_ssl_config(
6866
ca_path: OptionalConfig<impl TrustStoreLoader>,
6967
service_name: &'static str,
7068
) -> anyhow::Result<Option<rustls::ServerConfig>> {
71-
// TODO this could be moved somewhere more generic (e.g. where we initialize tracing subscriber)
72-
if !stderr().is_terminal() {
73-
yansi::disable();
74-
}
75-
7669
let enabled = Paint::green("enabled").bold();
7770
let disabled = Paint::red("disabled").bold();
7871
let service_name = service_name.bold();

crates/core/tedge/src/main.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use clap::CommandFactory;
77
use clap::FromArgMatches;
88
use std::alloc;
99
use std::ffi::OsString;
10-
use std::io::IsTerminal;
1110
use std::path::PathBuf;
1211
use std::time::Duration;
1312
use tedge::command::BuildCommand;
@@ -23,6 +22,11 @@ use tedge_config::unconfigured_logger;
2322
use tedge_file_log_plugin::bin::TEdgeConfigView;
2423
use tracing::log;
2524

25+
// Control when to use console colors (`stdout` and `stderr` is a TTY, `NO_COLOR` is not set)
26+
static USE_COLOR: yansi::Condition = yansi::Condition::from(|| {
27+
yansi::Condition::stdouterr_are_tty() && yansi::Condition::no_color()
28+
});
29+
2630
#[global_allocator]
2731
static ALLOCATOR: Cap<alloc::System> = Cap::new(alloc::System, usize::MAX);
2832

@@ -35,6 +39,8 @@ async fn main() -> anyhow::Result<()> {
3539
})
3640
.unwrap_or_else(|code| std::process::exit(code));
3741

42+
yansi::whenever(USE_COLOR);
43+
3844
match opt {
3945
TEdgeOptMulticall::Component(Component::TedgeMapper(opt)) => {
4046
let tedge_config = tedge_config::TEdgeConfig::load(&opt.common.config_dir).await?;
@@ -87,10 +93,6 @@ async fn main() -> anyhow::Result<()> {
8793
.build_command(&tedge_config)
8894
.with_context(|| "missing configuration parameter")?;
8995

90-
if !std::io::stdout().is_terminal() {
91-
yansi::disable();
92-
}
93-
9496
match cmd.execute(tedge_config).await {
9597
Ok(()) => Ok(()),
9698
// If the command already prints its own nicely formatted errors

crates/extensions/tedge-p11-server/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ tokio = { workspace = true, features = [
3333
toml.workspace = true
3434
tracing.workspace = true
3535
tracing-subscriber.workspace = true
36+
yansi.workspace = true
3637

3738
[dev-dependencies]
3839
tempfile.workspace = true

crates/extensions/tedge-p11-server/src/main.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,11 @@ async fn try_read_config(args: Args) -> anyhow::Result<ValidConfig> {
218218
})
219219
}
220220

221+
// Control when to use console colors (`stdout` and `stderr` is a TTY, `NO_COLOR` is not set)
222+
static USE_COLOR: yansi::Condition = yansi::Condition::from(|| {
223+
yansi::Condition::stdouterr_are_tty() && yansi::Condition::no_color()
224+
});
225+
221226
#[tokio::main(flavor = "current_thread")]
222227
async fn main() -> anyhow::Result<()> {
223228
let args = Args::parse();
@@ -234,6 +239,8 @@ async fn main() -> anyhow::Result<()> {
234239
)
235240
.init();
236241

242+
yansi::whenever(USE_COLOR);
243+
237244
let args = Args::parse();
238245
let config = try_read_config(args).await?;
239246
let cryptoki_config = CryptokiConfigDirect {

0 commit comments

Comments
 (0)