Skip to content

Commit 7a760a1

Browse files
committed
♻️ Provide separate _with_options overloads
1 parent 307173d commit 7a760a1

File tree

9 files changed

+76
-50
lines changed

9 files changed

+76
-50
lines changed

crates/benchmark/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::fs::OpenOptions;
55
use std::hint::black_box;
66
use std::io::{self, Write as _};
77
use std::time::{Duration, Instant};
8-
use terminal_colorsaurus::{color_palette, Error, QueryOptions, Result};
8+
use terminal_colorsaurus::{color_palette, Error, Result};
99

1010
#[derive(Parser, Debug)]
1111
struct Args {
@@ -40,7 +40,7 @@ fn main() -> Result<()> {
4040
.collect::<Result<Vec<_>>>()?;
4141
bar.finish();
4242

43-
let supported = match color_palette(QueryOptions::default()) {
43+
let supported = match color_palette() {
4444
Ok(_) => true,
4545
Err(Error::UnsupportedTerminal) => false,
4646
Err(e) => return Err(e),
@@ -53,7 +53,7 @@ fn main() -> Result<()> {
5353

5454
fn bench() -> Result<Duration> {
5555
let start = Instant::now();
56-
match black_box(color_palette(QueryOptions::default())) {
56+
match black_box(color_palette()) {
5757
Ok(_) | Err(Error::UnsupportedTerminal) => Ok(start.elapsed()),
5858
Err(err) => Err(err),
5959
}

crates/pycolorsaurus/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ create_exception!(colorsaurus, ColorsaurusError, PyException);
3131
#[pyfunction]
3232
#[pyo3(signature = (*, timeout=None))]
3333
fn color_scheme(timeout: Option<Timeout>) -> PyResult<ColorScheme> {
34-
imp::color_scheme(query_options(timeout))
34+
imp::color_scheme_with_options(query_options(timeout))
3535
.map(ColorScheme::from)
3636
.map_err(to_py_error)
3737
}
@@ -40,7 +40,7 @@ fn color_scheme(timeout: Option<Timeout>) -> PyResult<ColorScheme> {
4040
#[pyfunction]
4141
#[pyo3(signature = (*, timeout=None))]
4242
fn color_palette(timeout: Option<Timeout>) -> PyResult<ColorPalette> {
43-
imp::color_palette(query_options(timeout))
43+
imp::color_palette_with_options(query_options(timeout))
4444
.map(ColorPalette)
4545
.map_err(to_py_error)
4646
}
@@ -49,7 +49,7 @@ fn color_palette(timeout: Option<Timeout>) -> PyResult<ColorPalette> {
4949
#[pyfunction]
5050
#[pyo3(signature = (*, timeout=None))]
5151
fn foreground_color(timeout: Option<Timeout>) -> PyResult<Color> {
52-
imp::foreground_color(query_options(timeout))
52+
imp::foreground_color_with_options(query_options(timeout))
5353
.map(Color)
5454
.map_err(to_py_error)
5555
}
@@ -58,7 +58,7 @@ fn foreground_color(timeout: Option<Timeout>) -> PyResult<Color> {
5858
#[pyfunction]
5959
#[pyo3(signature = (*, timeout=None))]
6060
fn background_color(timeout: Option<Timeout>) -> PyResult<Color> {
61-
imp::background_color(query_options(timeout))
61+
imp::background_color_with_options(query_options(timeout))
6262
.map(Color)
6363
.map_err(to_py_error)
6464
}

crates/terminal-colorsaurus/examples/bg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! This example shows how to retrieve the terminal's background color.
22
3-
use terminal_colorsaurus::{background_color, Error, QueryOptions};
3+
use terminal_colorsaurus::{background_color, Error};
44

55
fn main() -> Result<(), display::DisplayAsDebug<Error>> {
6-
let bg = background_color(QueryOptions::default())?;
6+
let bg = background_color()?;
77
let bg_8bit = bg.scale_to_8bit();
88
println!("rgb16({}, {}, {})", bg.r, bg.g, bg.b);
99
println!("rgb8({}, {}, {})", bg_8bit.0, bg_8bit.1, bg_8bit.2);

crates/terminal-colorsaurus/examples/fg.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! This example shows how to retrieve the terminal's foreground color.
22
3-
use terminal_colorsaurus::{foreground_color, Error, QueryOptions};
3+
use terminal_colorsaurus::{foreground_color, Error};
44

55
fn main() -> Result<(), display::DisplayAsDebug<Error>> {
6-
let fg = foreground_color(QueryOptions::default())?;
6+
let fg = foreground_color()?;
77
let fg_8bit = fg.scale_to_8bit();
88
println!("rgb16({}, {}, {})", fg.r, fg.g, fg.b);
99
println!("rgb8({}, {}, {})", fg_8bit.0, fg_8bit.1, fg_8bit.2);

crates/terminal-colorsaurus/examples/pager.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,11 @@
2020
//! 4. `cargo run --example pager 2>&1 >/dev/tty | less`—should print the color scheme (or error). This is a false positive.
2121
2222
use std::io::{stdout, IsTerminal as _};
23-
use terminal_colorsaurus::{color_palette, Error, QueryOptions};
23+
use terminal_colorsaurus::{color_palette, Error};
2424

2525
fn main() -> Result<(), display::DisplayAsDebug<Error>> {
2626
if stdout().is_terminal() {
27-
eprintln!(
28-
"Here's the color scheme: {:#?}",
29-
color_palette(QueryOptions::default())?
30-
);
27+
eprintln!("Here's the color scheme: {:#?}", color_palette()?);
3128
} else {
3229
eprintln!("No color scheme for you today :/");
3330
}

crates/terminal-colorsaurus/examples/theme.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
//! This example shows how to detect if the terminal uses
22
//! a dark-on-light or a light-on-dark theme.
33
4-
use terminal_colorsaurus::{color_palette, ColorScheme, Error, QueryOptions};
4+
use terminal_colorsaurus::{color_palette, ColorScheme, Error};
55

66
fn main() -> Result<(), display::DisplayAsDebug<Error>> {
7-
let colors = color_palette(QueryOptions::default())?;
7+
let colors = color_palette()?;
88

99
let theme = match colors.color_scheme() {
1010
ColorScheme::Dark => "dark",

crates/terminal-colorsaurus/src/lib.rs

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@
2121
//!
2222
//! ## Example 1: Test If the Terminal Uses a Dark Background
2323
//! ```no_run
24-
//! use terminal_colorsaurus::{color_scheme, QueryOptions, ColorScheme};
24+
//! use terminal_colorsaurus::{color_scheme, ColorScheme};
2525
//!
26-
//! let color_scheme = color_scheme(QueryOptions::default()).unwrap();
26+
//! let color_scheme = color_scheme().unwrap();
2727
//! dbg!(color_scheme == ColorScheme::Dark);
2828
//! ```
2929
//!
3030
//! ## Example 2: Get the Terminal's Foreground Color
3131
//! ```no_run
32-
//! use terminal_colorsaurus::{foreground_color, QueryOptions};
32+
//! use terminal_colorsaurus::foreground_color;
3333
//!
34-
//! let fg = foreground_color(QueryOptions::default()).unwrap();
34+
//! let fg = foreground_color().unwrap();
3535
//! println!("rgb({}, {}, {})", fg.r, fg.g, fg.b);
3636
//! ```
3737
//!
@@ -132,7 +132,7 @@ impl ColorPalette {
132132
pub type Result<T> = std::result::Result<T, Error>;
133133
pub use error::Error;
134134

135-
/// Options to be used with [`foreground_color`] and [`background_color`].
135+
/// Options to be used with [`foreground_color_with_options`] and [`background_color_with_options`].
136136
/// You should almost always use the unchanged [`QueryOptions::default`] value.
137137
#[derive(Debug, Clone, PartialEq, Eq)]
138138
#[non_exhaustive]
@@ -157,33 +157,61 @@ impl Default for QueryOptions {
157157
}
158158
}
159159

160-
/// Detects if the terminal is dark or light.
161-
#[doc = include_str!("../doc/caveats.md")]
162-
#[doc(alias = "theme")]
163-
pub fn color_scheme(options: QueryOptions) -> Result<ColorScheme> {
164-
color_palette(options).map(|p| p.color_scheme())
160+
macro_rules! impl_query_fn {
161+
($(#[$($meta:meta)*])* $vis:vis fn $name:ident() -> $ret:ty; $vis_opt:vis fn $name_opt:ident($opt:ident : $opt_ty:ty) -> $ret_opt:ty $impl:block) => {
162+
$(#[$($meta)*])*
163+
#[doc = concat!("\n\nUse [`", stringify!($name_opt), "`] instead, if you want to provide custom query options.")]
164+
$vis fn $name() -> $ret {
165+
$name_opt(Default::default())
166+
}
167+
168+
$(#[$($meta)*])* $vis_opt fn $name_opt($opt:$opt_ty) -> $ret_opt $impl
169+
};
170+
}
171+
172+
impl_query_fn! {
173+
/// Detects if the terminal is dark or light.
174+
#[doc = include_str!("../doc/caveats.md")]
175+
#[doc(alias = "theme")]
176+
pub fn color_scheme() -> Result<ColorScheme>;
177+
178+
pub fn color_scheme_with_options(options: QueryOptions) -> Result<ColorScheme> {
179+
color_palette_with_options(options).map(|p| p.color_scheme())
180+
}
165181
}
166182

167-
/// Queries the terminal for it's color scheme (foreground and background color).
168-
#[doc = include_str!("../doc/caveats.md")]
169-
pub fn color_palette(options: QueryOptions) -> Result<ColorPalette> {
170-
imp::color_palette(options)
183+
impl_query_fn! {
184+
/// Queries the terminal for it's color scheme (foreground and background color).
185+
#[doc = include_str!("../doc/caveats.md")]
186+
pub fn color_palette() -> Result<ColorPalette>;
187+
188+
pub fn color_palette_with_options(options: QueryOptions) -> Result<ColorPalette> {
189+
imp::color_palette(options)
190+
}
171191
}
172192

173-
/// Queries the terminal for it's foreground color. \
174-
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
175-
#[doc = include_str!("../doc/caveats.md")]
176-
#[doc(alias = "fg")]
177-
pub fn foreground_color(options: QueryOptions) -> Result<Color> {
178-
imp::foreground_color(options)
193+
impl_query_fn! {
194+
/// Queries the terminal for it's foreground color. \
195+
/// If you also need the background color it is more efficient to use [`color_palette`] instead.
196+
#[doc = include_str!("../doc/caveats.md")]
197+
#[doc(alias = "fg")]
198+
pub fn foreground_color() -> Result<Color>;
199+
200+
pub fn foreground_color_with_options(options: QueryOptions) -> Result<Color> {
201+
imp::foreground_color(options)
202+
}
179203
}
180204

181-
/// Queries the terminal for it's background color. \
182-
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
183-
#[doc = include_str!("../doc/caveats.md")]
184-
#[doc(alias = "bg")]
185-
pub fn background_color(options: QueryOptions) -> Result<Color> {
186-
imp::background_color(options)
205+
impl_query_fn! {
206+
/// Queries the terminal for it's background color. \
207+
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
208+
#[doc = include_str!("../doc/caveats.md")]
209+
#[doc(alias = "fg")]
210+
pub fn background_color() -> Result<Color>;
211+
212+
pub fn background_color_with_options(options: QueryOptions) -> Result<Color> {
213+
imp::background_color(options)
214+
}
187215
}
188216

189217
#[cfg(test)]

crates/termtheme/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
io::{self, stdout, IsTerminal},
66
process::exit,
77
};
8-
use terminal_colorsaurus::{color_scheme, ColorScheme, QueryOptions};
8+
use terminal_colorsaurus::{color_scheme, ColorScheme};
99

1010
fn main() {
1111
let args = Args::parse();
@@ -16,7 +16,7 @@ fn main() {
1616
);
1717
exit(1);
1818
}
19-
match color_scheme(QueryOptions::default()) {
19+
match color_scheme() {
2020
Ok(s) => display_theme(s, !args.no_newline),
2121
Err(e) => {
2222
display_error(e);

readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ Windows is unfortunately [not supported](./doc/windows.md).
1313

1414
## Example
1515
```rust,no_run
16-
use terminal_colorsaurus::{color_scheme, QueryOptions, ColorScheme};
16+
use terminal_colorsaurus::{color_scheme, ColorScheme};
1717
18-
match color_scheme(QueryOptions::default()).unwrap() {
19-
ColorScheme::Dark => { /* ... */ },
20-
ColorScheme::Light => { /* ... */ },
18+
match color_scheme() {
19+
Ok(ColorScheme::Dark) => { /* ... */ },
20+
Ok(ColorScheme::Light) => { /* ... */ },
21+
Err(_e) => { /* something went wrong, fallback to something else */ },
2122
}
2223
```
2324

0 commit comments

Comments
 (0)