Skip to content

Commit 4bf1614

Browse files
committed
✨ Gate querying behind a feature
1 parent 6b7f8d0 commit 4bf1614

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ jobs:
4040
run: rustup override set '${{matrix.rust-version}}'
4141
- name: Build
4242
run: cargo build --workspace --all-features
43-
- name: Check fallback implementation
44-
run: just check-unsupported
43+
- name: Check
44+
run: just check
4545
docs:
4646
name: Docs
4747
runs-on: ubuntu-latest

Justfile

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,16 @@ test-package name *args:
99
cargo package -p "{{name}}" {{args}}
1010
(cd $CARGO_TARGET_DIR/package/{{name}}-*/ && cargo test)
1111

12+
check: clippy check-no-default-features check-unsupported
13+
14+
clippy:
15+
cargo clippy --workspace --tests --all-features --all-targets
16+
17+
check-no-default-features:
18+
cargo clippy -p terminal-colorsaurus --no-default-features
19+
1220
check-unsupported:
13-
RUSTFLAGS='--cfg terminal_colorsaurus_test_unsupported -Dwarnings' cargo check --workspace
21+
RUSTFLAGS='--cfg terminal_colorsaurus_test_unsupported -Dwarnings' cargo clippy --workspace
1422

1523
doc:
1624
cargo +nightly docs-rs -p terminal-colorsaurus

crates/terminal-colorsaurus/Cargo.toml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,27 @@ edition = "2021"
1111
rust-version = "1.70.0" # Search for `FIXME(msrv)` when bumping.
1212
exclude = [".github", ".gitignore", "*.sh", "benchmark/**/*", "doc/issues.md", "deny.toml"]
1313

14+
[features]
15+
default = ["query"]
16+
query = ["dep:memchr", "dep:mio", "dep:terminal-trx", "dep:libc", "dep:windows-sys"]
17+
1418
[dependencies]
1519
rgb = { version = "0.8.37", optional = true }
1620
anstyle = { version = "1.0.7", optional = true }
1721
cfg-if = "1.0.0"
1822

23+
[target.'cfg(any(unix, windows))'.dependencies]
24+
memchr = { version = "2.7.1", optional = true }
25+
terminal-trx = { version = "0.2.3", optional = true }
26+
1927
[target.'cfg(unix)'.dependencies]
20-
memchr = "2.7.1"
21-
mio = { version = "1", features = ["os-ext"], default-features = false }
22-
terminal-trx = "0.2.3"
28+
mio = { version = "1", features = ["os-ext"], default-features = false, optional = true }
2329

2430
[target.'cfg(target_os = "macos")'.dependencies]
25-
libc = "0.2.151"
31+
libc = { version = "0.2.151", optional = true }
2632

2733
[target.'cfg(windows)'.dependencies]
28-
memchr = "2.7.1"
29-
terminal-trx = "0.2.3"
30-
windows-sys = { version = "0.59.0", features = ["Win32_System_Threading"] } # Keep this in sync with terminal-trx's version to avoid duplicate deps.
34+
windows-sys = { version = "0.59.0", features = ["Win32_System_Threading"], optional = true } # Keep this in sync with terminal-trx's version to avoid duplicate deps.
3135

3236
[lints.rust]
3337
missing_debug_implementations = "warn"

crates/terminal-colorsaurus/src/lib.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
//! * [`anstyle`] — Enable this feature to convert [`Color`] to [`anstyle::RgbColor`].
4141
4242
use cfg_if::cfg_if;
43-
use std::time::Duration;
4443

4544
mod color;
4645
mod error;
@@ -51,11 +50,13 @@ pub mod parse {
5150
pub use crate::xparsecolor::xparsecolor;
5251
}
5352

53+
mod xparsecolor;
54+
55+
#[cfg(feature = "query")]
5456
cfg_if! {
5557
if #[cfg(all(any(unix, windows), not(terminal_colorsaurus_test_unsupported)))] {
5658
mod io;
5759
mod quirks;
58-
mod xparsecolor;
5960
mod xterm;
6061
use xterm as imp;
6162
} else {
@@ -92,6 +93,7 @@ pub use color::*;
9293

9394
/// The color palette i.e. foreground and background colors of the terminal.
9495
/// Retrieved by calling [`color_palette`].
96+
#[cfg(feature = "query")]
9597
#[derive(Debug, Clone, PartialEq, Eq)]
9698
#[non_exhaustive]
9799
pub struct ColorPalette {
@@ -105,6 +107,7 @@ pub struct ColorPalette {
105107
///
106108
/// The easiest way to retrieve the color scheme
107109
/// is by calling [`color_scheme`].
110+
#[cfg(feature = "query")]
108111
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
109112
#[allow(clippy::exhaustive_enums)]
110113
#[doc(alias = "Theme")]
@@ -116,8 +119,10 @@ pub enum ColorScheme {
116119
Light,
117120
}
118121

122+
#[cfg(feature = "query")]
119123
const PERCEPTUAL_MIDDLE_GRAY: u8 = 50;
120124

125+
#[cfg(feature = "query")]
121126
impl ColorPalette {
122127
/// Determines if the terminal uses a dark or light background.
123128
pub fn color_scheme(&self) -> ColorScheme {
@@ -139,6 +144,7 @@ pub use error::Error;
139144

140145
/// Options to be used with [`foreground_color`] and [`background_color`].
141146
/// You should almost always use the unchanged [`QueryOptions::default`] value.
147+
#[cfg(feature = "query")]
142148
#[derive(Debug, Clone, PartialEq, Eq)]
143149
#[non_exhaustive]
144150
pub struct QueryOptions {
@@ -151,32 +157,36 @@ pub struct QueryOptions {
151157
/// almost always be detected as such before this timeout elapses.
152158
///
153159
/// See [Feature Detection](`feature_detection`) for details on how this works.
154-
pub timeout: Duration,
160+
pub timeout: std::time::Duration,
155161
}
156162

163+
#[cfg(feature = "query")]
157164
impl Default for QueryOptions {
158165
fn default() -> Self {
159166
Self {
160-
timeout: Duration::from_secs(1),
167+
timeout: std::time::Duration::from_secs(1),
161168
}
162169
}
163170
}
164171

165172
/// Detects if the terminal is dark or light.
173+
#[cfg(feature = "query")]
166174
#[doc = include_str!("../doc/caveats.md")]
167175
#[doc(alias = "theme")]
168176
pub fn color_scheme(options: QueryOptions) -> Result<ColorScheme> {
169177
color_palette(options).map(|p| p.color_scheme())
170178
}
171179

172180
/// Queries the terminal for it's color scheme (foreground and background color).
181+
#[cfg(feature = "query")]
173182
#[doc = include_str!("../doc/caveats.md")]
174183
pub fn color_palette(options: QueryOptions) -> Result<ColorPalette> {
175184
imp::color_palette(options)
176185
}
177186

178187
/// Queries the terminal for it's foreground color. \
179188
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
189+
#[cfg(feature = "query")]
180190
#[doc = include_str!("../doc/caveats.md")]
181191
#[doc(alias = "fg")]
182192
pub fn foreground_color(options: QueryOptions) -> Result<Color> {
@@ -185,6 +195,7 @@ pub fn foreground_color(options: QueryOptions) -> Result<Color> {
185195

186196
/// Queries the terminal for it's background color. \
187197
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
198+
#[cfg(feature = "query")]
188199
#[doc = include_str!("../doc/caveats.md")]
189200
#[doc(alias = "bg")]
190201
pub fn background_color(options: QueryOptions) -> Result<Color> {

0 commit comments

Comments
 (0)