Skip to content

Commit 0306133

Browse files
committed
Provide separate _with_options overloads
1 parent adb1ac2 commit 0306133

File tree

9 files changed

+76
-50
lines changed

9 files changed

+76
-50
lines changed

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/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);

examples/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
}

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);

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);

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
}

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",

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

src/lib.rs

Lines changed: 54 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@
1616
//!
1717
//! ## Example 1: Test If the Terminal Uses a Dark Background
1818
//! ```no_run
19-
//! use terminal_colorsaurus::{color_scheme, QueryOptions, ColorScheme};
19+
//! use terminal_colorsaurus::{color_scheme, ColorScheme};
2020
//!
21-
//! let color_scheme = color_scheme(QueryOptions::default()).unwrap();
21+
//! let color_scheme = color_scheme().unwrap();
2222
//! dbg!(color_scheme == ColorScheme::Dark);
2323
//! ```
2424
//!
2525
//! ## Example 2: Query for the Terminal's Foreground Color
2626
//! ```no_run
27-
//! use terminal_colorsaurus::{foreground_color, QueryOptions};
27+
//! use terminal_colorsaurus::foreground_color;
2828
//!
29-
//! let fg = foreground_color(QueryOptions::default()).unwrap();
29+
//! let fg = foreground_color().unwrap();
3030
//! println!("rgb({}, {}, {})", fg.r, fg.g, fg.b);
3131
//! ```
3232
//!
@@ -183,7 +183,7 @@ impl ColorPalette {
183183
pub type Result<T> = std::result::Result<T, Error>;
184184
pub use error::Error;
185185

186-
/// Options to be used with [`foreground_color`] and [`background_color`].
186+
/// Options to be used with [`foreground_color_with_options`] and [`background_color_with_options`].
187187
/// You should almost always use the unchanged [`QueryOptions::default`] value.
188188
#[derive(Debug, Clone, PartialEq, Eq)]
189189
#[non_exhaustive]
@@ -208,33 +208,61 @@ impl Default for QueryOptions {
208208
}
209209
}
210210

211-
/// Detects if the terminal is dark or light.
212-
#[doc = include_str!("../doc/caveats.md")]
213-
#[doc(alias = "theme")]
214-
pub fn color_scheme(options: QueryOptions) -> Result<ColorScheme> {
215-
color_palette(options).map(|p| p.color_scheme())
211+
macro_rules! impl_query_fn {
212+
($(#[$($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) => {
213+
$(#[$($meta)*])*
214+
#[doc = concat!("\n\nUse [`", stringify!($name_opt), "`] instead, if you want to provide custom query options.")]
215+
$vis fn $name() -> $ret {
216+
$name_opt(Default::default())
217+
}
218+
219+
$(#[$($meta)*])* $vis_opt fn $name_opt($opt:$opt_ty) -> $ret_opt $impl
220+
};
221+
}
222+
223+
impl_query_fn! {
224+
/// Detects if the terminal is dark or light.
225+
#[doc = include_str!("../doc/caveats.md")]
226+
#[doc(alias = "theme")]
227+
pub fn color_scheme() -> Result<ColorScheme>;
228+
229+
pub fn color_scheme_with_options(options: QueryOptions) -> Result<ColorScheme> {
230+
color_palette_with_options(options).map(|p| p.color_scheme())
231+
}
216232
}
217233

218-
/// Queries the terminal for it's color scheme (foreground and background color).
219-
#[doc = include_str!("../doc/caveats.md")]
220-
pub fn color_palette(options: QueryOptions) -> Result<ColorPalette> {
221-
imp::color_palette(options)
234+
impl_query_fn! {
235+
/// Queries the terminal for it's color scheme (foreground and background color).
236+
#[doc = include_str!("../doc/caveats.md")]
237+
pub fn color_palette() -> Result<ColorPalette>;
238+
239+
pub fn color_palette_with_options(options: QueryOptions) -> Result<ColorPalette> {
240+
imp::color_palette(options)
241+
}
222242
}
223243

224-
/// Queries the terminal for it's foreground color. \
225-
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
226-
#[doc = include_str!("../doc/caveats.md")]
227-
#[doc(alias = "fg")]
228-
pub fn foreground_color(options: QueryOptions) -> Result<Color> {
229-
imp::foreground_color(options)
244+
impl_query_fn! {
245+
/// Queries the terminal for it's foreground color. \
246+
/// If you also need the background color it is more efficient to use [`color_palette`] instead.
247+
#[doc = include_str!("../doc/caveats.md")]
248+
#[doc(alias = "fg")]
249+
pub fn foreground_color() -> Result<Color>;
250+
251+
pub fn foreground_color_with_options(options: QueryOptions) -> Result<Color> {
252+
imp::foreground_color(options)
253+
}
230254
}
231255

232-
/// Queries the terminal for it's background color. \
233-
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
234-
#[doc = include_str!("../doc/caveats.md")]
235-
#[doc(alias = "bg")]
236-
pub fn background_color(options: QueryOptions) -> Result<Color> {
237-
imp::background_color(options)
256+
impl_query_fn! {
257+
/// Queries the terminal for it's background color. \
258+
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
259+
#[doc = include_str!("../doc/caveats.md")]
260+
#[doc(alias = "fg")]
261+
pub fn background_color() -> Result<Color>;
262+
263+
pub fn background_color_with_options(options: QueryOptions) -> Result<Color> {
264+
imp::background_color(options)
265+
}
238266
}
239267

240268
#[cfg(test)]

0 commit comments

Comments
 (0)