Skip to content

Commit

Permalink
Provide separate _with_options overloads
Browse files Browse the repository at this point in the history
  • Loading branch information
bash committed Sep 26, 2024
1 parent adb1ac2 commit 384ead1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 37 deletions.
8 changes: 4 additions & 4 deletions crates/pycolorsaurus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ create_exception!(colorsaurus, ColorsaurusError, PyException);
#[pyfunction]
#[pyo3(signature = (*, timeout=None))]
fn color_scheme(timeout: Option<Timeout>) -> PyResult<ColorScheme> {
imp::color_scheme(query_options(timeout))
imp::color_scheme_with_options(query_options(timeout))
.map(ColorScheme::from)
.map_err(to_py_error)
}
Expand All @@ -40,7 +40,7 @@ fn color_scheme(timeout: Option<Timeout>) -> PyResult<ColorScheme> {
#[pyfunction]
#[pyo3(signature = (*, timeout=None))]
fn color_palette(timeout: Option<Timeout>) -> PyResult<ColorPalette> {
imp::color_palette(query_options(timeout))
imp::color_palette_with_options(query_options(timeout))
.map(ColorPalette)
.map_err(to_py_error)
}
Expand All @@ -49,7 +49,7 @@ fn color_palette(timeout: Option<Timeout>) -> PyResult<ColorPalette> {
#[pyfunction]
#[pyo3(signature = (*, timeout=None))]
fn foreground_color(timeout: Option<Timeout>) -> PyResult<Color> {
imp::foreground_color(query_options(timeout))
imp::foreground_color_with_options(query_options(timeout))
.map(Color)
.map_err(to_py_error)
}
Expand All @@ -58,7 +58,7 @@ fn foreground_color(timeout: Option<Timeout>) -> PyResult<Color> {
#[pyfunction]
#[pyo3(signature = (*, timeout=None))]
fn background_color(timeout: Option<Timeout>) -> PyResult<Color> {
imp::background_color(query_options(timeout))
imp::background_color_with_options(query_options(timeout))
.map(Color)
.map_err(to_py_error)
}
Expand Down
4 changes: 2 additions & 2 deletions crates/termtheme/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::{
io::{self, stdout, IsTerminal},
process::exit,
};
use terminal_colorsaurus::{color_scheme, ColorScheme, QueryOptions};
use terminal_colorsaurus::{color_scheme, ColorScheme};

fn main() {
let args = Args::parse();
Expand All @@ -16,7 +16,7 @@ fn main() {
);
exit(1);
}
match color_scheme(QueryOptions::default()) {
match color_scheme() {
Ok(s) => display_theme(s, !args.no_newline),
Err(e) => {
display_error(e);
Expand Down
6 changes: 3 additions & 3 deletions examples/benchmark/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::fs::OpenOptions;
use std::hint::black_box;
use std::io::{self, Write as _};
use std::time::{Duration, Instant};
use terminal_colorsaurus::{color_palette, Error, QueryOptions, Result};
use terminal_colorsaurus::{color_palette, Error, Result};

#[derive(Parser, Debug)]
struct Args {
Expand Down Expand Up @@ -40,7 +40,7 @@ fn main() -> Result<()> {
.collect::<Result<Vec<_>>>()?;
bar.finish();

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

fn bench() -> Result<Duration> {
let start = Instant::now();
match black_box(color_palette(QueryOptions::default())) {
match black_box(color_palette()) {
Ok(_) | Err(Error::UnsupportedTerminal) => Ok(start.elapsed()),
Err(err) => Err(err),
}
Expand Down
9 changes: 5 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ Windows is unfortunately [not supported](./doc/windows.md).

## Example
```rust,no_run
use terminal_colorsaurus::{color_scheme, QueryOptions, ColorScheme};
use terminal_colorsaurus::{color_scheme, ColorScheme};
match color_scheme(QueryOptions::default()).unwrap() {
ColorScheme::Dark => { /* ... */ },
ColorScheme::Light => { /* ... */ },
match color_scheme() {
Ok(ColorScheme::Dark) => { /* ... */ },
Ok(ColorScheme::Light) => { /* ... */ },
Err(_e) => { /* something went wrong, fallback to something else */ },
}
```

Expand Down
76 changes: 52 additions & 24 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
//! ```no_run
//! use terminal_colorsaurus::{color_scheme, QueryOptions, ColorScheme};
//!
//! let color_scheme = color_scheme(QueryOptions::default()).unwrap();
//! let color_scheme = color_scheme().unwrap();
//! dbg!(color_scheme == ColorScheme::Dark);
//! ```
//!
//! ## Example 2: Query for the Terminal's Foreground Color
//! ```no_run
//! use terminal_colorsaurus::{foreground_color, QueryOptions};
//!
//! let fg = foreground_color(QueryOptions::default()).unwrap();
//! let fg = foreground_color().unwrap();
//! println!("rgb({}, {}, {})", fg.r, fg.g, fg.b);
//! ```
//!
Expand Down Expand Up @@ -183,7 +183,7 @@ impl ColorPalette {
pub type Result<T> = std::result::Result<T, Error>;
pub use error::Error;

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

/// Detects if the terminal is dark or light.
#[doc = include_str!("../doc/caveats.md")]
#[doc(alias = "theme")]
pub fn color_scheme(options: QueryOptions) -> Result<ColorScheme> {
color_palette(options).map(|p| p.color_scheme())
macro_rules! impl_query_fn {
($(#[$($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) => {
$(#[$($meta)*])*
#[doc = concat!("\n\nUse [`", stringify!($name_opt), "`] instead, if you want to provide custom query options.")]
$vis fn $name() -> $ret {
$name_opt(Default::default())
}

$(#[$($meta)*])* $vis_opt fn $name_opt($opt:$opt_ty) -> $ret_opt $impl
};
}

impl_query_fn! {
/// Detects if the terminal is dark or light.
#[doc = include_str!("../doc/caveats.md")]
#[doc(alias = "theme")]
pub fn color_scheme() -> Result<ColorScheme>;

pub fn color_scheme_with_options(options: QueryOptions) -> Result<ColorScheme> {
color_palette_with_options(options).map(|p| p.color_scheme())
}
}

/// Queries the terminal for it's color scheme (foreground and background color).
#[doc = include_str!("../doc/caveats.md")]
pub fn color_palette(options: QueryOptions) -> Result<ColorPalette> {
imp::color_palette(options)
impl_query_fn! {
/// Queries the terminal for it's color scheme (foreground and background color).
#[doc = include_str!("../doc/caveats.md")]
pub fn color_palette() -> Result<ColorPalette>;

pub fn color_palette_with_options(options: QueryOptions) -> Result<ColorPalette> {
imp::color_palette(options)
}
}

/// Queries the terminal for it's foreground color. \
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
#[doc = include_str!("../doc/caveats.md")]
#[doc(alias = "fg")]
pub fn foreground_color(options: QueryOptions) -> Result<Color> {
imp::foreground_color(options)
impl_query_fn! {
/// Queries the terminal for it's foreground color. \
/// If you also need the background color it is more efficient to use [`color_palette`] instead.
#[doc = include_str!("../doc/caveats.md")]
#[doc(alias = "fg")]
pub fn foreground_color() -> Result<Color>;

pub fn foreground_color_with_options(options: QueryOptions) -> Result<Color> {
imp::foreground_color(options)
}
}

/// Queries the terminal for it's background color. \
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
#[doc = include_str!("../doc/caveats.md")]
#[doc(alias = "bg")]
pub fn background_color(options: QueryOptions) -> Result<Color> {
imp::background_color(options)
impl_query_fn! {
/// Queries the terminal for it's background color. \
/// If you also need the foreground color it is more efficient to use [`color_palette`] instead.
#[doc = include_str!("../doc/caveats.md")]
#[doc(alias = "fg")]
pub fn background_color() -> Result<Color>;

pub fn background_color_with_options(options: QueryOptions) -> Result<Color> {
imp::background_color(options)
}
}

#[cfg(test)]
Expand Down

0 comments on commit 384ead1

Please sign in to comment.