Skip to content

Commit 6b7f8d0

Browse files
committed
✨ Expose xparsecolor
1 parent 8d79f60 commit 6b7f8d0

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

crates/terminal-colorsaurus/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ mod color;
4646
mod error;
4747
mod fmt;
4848

49+
/// Low-level utilities for parsing responses to `OSC 10` / `OSC 11` queries.
50+
pub mod parse {
51+
pub use crate::xparsecolor::xparsecolor;
52+
}
53+
4954
cfg_if! {
5055
if #[cfg(all(any(unix, windows), not(terminal_colorsaurus_test_unsupported)))] {
5156
mod io;

crates/terminal-colorsaurus/src/xparsecolor.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
use crate::Color;
22
use std::str::from_utf8;
33

4-
/// Parses a color value that follows the `XParseColor` format.
5-
/// See <https://www.x.org/releases/current/doc/libX11/libX11/libX11.html#Color_Strings>
6-
/// for a reference of what `XParseColor` supports.
4+
/// Parses the subset of X11 [Color Strings](https://www.x.org/releases/current/doc/libX11/libX11/libX11.html#Color_Strings)
5+
/// emitted by terminals in response to `OSC 10` / `OSC 11` queries.
76
///
8-
/// Not all formats are supported, just the ones that are returned
9-
/// by the tested terminals. Feel free to open a PR if you encounter
10-
/// a terminal that returns a different format.
11-
pub(crate) fn xparsecolor(input: &[u8]) -> Option<Color> {
7+
/// ## Accepted Formats
8+
/// * `#<red><green><blue>`
9+
/// * `rgb:<red>/<green>/<blue>`
10+
/// * `rgba:<red>/<green>/<blue>/<alpha>` (rxvt-unicode extension)
11+
///
12+
/// where `<red>`, `<green>` and `<blue` are hexadecimal numbers with 1-4 digits.
13+
pub fn xparsecolor(input: &[u8]) -> Option<Color> {
1214
if let Some(stripped) = input.strip_prefix(b"#") {
1315
parse_sharp(from_utf8(stripped).ok()?)
1416
} else if let Some(stripped) = input.strip_prefix(b"rgb:") {

0 commit comments

Comments
 (0)