Skip to content

Commit

Permalink
refactor(api): use helper method to detect color in stdx
Browse files Browse the repository at this point in the history
  • Loading branch information
ymgyt committed Sep 2, 2024
1 parent a1fc295 commit 5be8288
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 11 deletions.
13 changes: 11 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crates/synd_api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ path = "src/main.rs"
synd-auth = { path = "../synd_auth", version = "0.2.4" }
synd-feed = { path = "../synd_feed", version = "0.3.4", features = ["graphql"] }
synd-o11y = { path = "../synd_o11y", version = "0.1.8" }
synd-stdx = { path = "../synd_stdx", version = "0.1.0", features = ["color"] }

anyhow = { workspace = true }
async-graphql = { workspace = true, features = ["tracing"] }
Expand All @@ -40,7 +41,6 @@ pin-project = "1.1.4"
reqwest = { workspace = true }
serde = { workspace = true }
serde_json = "1.0.127"
supports-color = { version = "3.0.0" }
thiserror = { workspace = true }
tokio = { workspace = true, features = ["macros", "rt-multi-thread", "sync"] }
tokio-metrics = { version = "0.3.1", default-features = false, features = ["rt"] }
Expand Down
15 changes: 7 additions & 8 deletions crates/synd_api/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use fdlimit::Outcome;
use synd_o11y::{
opentelemetry::OpenTelemetryGuard, tracing_subscriber::otel_metrics::metrics_event_filter,
};
use synd_stdx::color::{is_color_supported, ColorSupport};
use tracing::{error, info};

use synd_api::{
Expand All @@ -22,12 +23,6 @@ fn init_tracing(options: &ObservabilityOptions) -> Option<OpenTelemetryGuard> {
Registry,
};

let color = {
use supports_color::Stream;
supports_color::on(Stream::Stdout).is_some()
};
let show_src = options.show_code_location;
let show_target = options.show_target;
let (otel_layer, otel_guard) = match options
.otlp_endpoint
.as_deref()
Expand All @@ -44,11 +39,15 @@ fn init_tracing(options: &ObservabilityOptions) -> Option<OpenTelemetryGuard> {
_ => (None, None),
};

let ansi = is_color_supported() == ColorSupport::Supported;
let show_src = options.show_code_location;
let show_target = options.show_target;

Registry::default()
.with(
fmt::Layer::new()
.with_ansi(color)
.with_timer(fmt::time::UtcTime::rfc_3339())
.with_ansi(ansi)
.with_timer(fmt::time::ChronoLocal::rfc_3339())
.with_file(show_src)
.with_line_number(show_src)
.with_target(show_target)
Expand Down
32 changes: 32 additions & 0 deletions crates/synd_stdx/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[package]
authors.workspace = true
categories.workspace = true
edition.workspace = true
homepage.workspace = true
keywords.workspace = true
license.workspace = true
readme.workspace = true
repository.workspace = true

description = "syndicationd lib"
include = ["src/**/*", "CHANGELOG.md"]
name = "synd-stdx"
version = "0.1.0"

[dependencies]
supports-color = { version = "3.0.0", optional = true }

[features]
color = ["dep:supports-color"]

[lints]
workspace = true

[package.metadata.release]
pre-release-hook = ["just", "changelog", "{{version}}"]
pre-release-replacements = [
{ file = "CHANGELOG.md", search = "unreleased", replace = "v{{version}}", min = 0 },
{ file = "CHANGELOG.md", search = "__release_date__", replace = "{{date}}", min = 0 },
{ file = "README.md", search = "/synd-stdx-v.*/", replace = "/{{crate_name}}-v{{version}}/", min = 0 },
]
tag-message = "chore: release {{crate_name}} version {{version}}"
2 changes: 2 additions & 0 deletions crates/synd_stdx/src/color/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod support;
pub use support::{is_color_supported, ColorSupport};
15 changes: 15 additions & 0 deletions crates/synd_stdx/src/color/support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#[derive(PartialEq, Eq)]
pub enum ColorSupport {
Supported,
NotSupported,
}

/// Return whether or not the current environment supports ANSI color output.
pub fn is_color_supported() -> ColorSupport {
use supports_color::Stream;
if supports_color::on(Stream::Stdout).is_some() {
ColorSupport::Supported
} else {
ColorSupport::NotSupported
}
}
2 changes: 2 additions & 0 deletions crates/synd_stdx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#[cfg(feature = "color")]
pub mod color;

0 comments on commit 5be8288

Please sign in to comment.