1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//! OSC 11 terminal background detection — auto dark/light theme selection.
//!
//! # Core API (always available, dep-free, WASM-safe)
//!
//! - [`parse_osc11_response`] — parse a terminal's `\x1b]11;rgb:RRRR/GGGG/BBBB\x07` reply.
//! - [`is_dark_background`] — decide whether a colour is perceived as dark (luminance < 0.5).
//! - [`ConsoleBackground`] — `Dark / Light / Unknown` enum.
//!
//! # Interactive probe (feature `terminal-query`, native only)
//!
//! When the `terminal-query` feature is enabled **and** the build target is not
//! `wasm32`, the function [`query_terminal_background`] is available. It writes
//! an OSC 11 query to the controlling TTY, waits up to 200 ms for the response,
//! and returns the parsed `ConsoleBackground`. Any I/O error or timeout yields
//! `ConsoleBackground::Unknown`.
//!
//! # Example
//!
//! ```
//! use gilt::terminal_bg::{parse_osc11_response, is_dark_background, ConsoleBackground};
//! use gilt::color::color_triplet::ColorTriplet;
//!
//! // Parse a typical Xterm OSC 11 reply for a dark background.
//! let triplet = parse_osc11_response("\x1b]11;rgb:1a1a/1a1a/1a1a\x07").unwrap();
//! assert!(is_dark_background(triplet));
//!
//! // Parse a light background reply.
//! let white = parse_osc11_response("\x1b]11;rgb:ffff/ffff/ffff\x07").unwrap();
//! assert!(!is_dark_background(white));
//! ```
use crateColorTriplet;
// ---------------------------------------------------------------------------
// ConsoleBackground
// ---------------------------------------------------------------------------
/// Whether the terminal background is perceived as dark, light, or unknown.
///
/// Returned by [`Console::detect_background`](crate::console::Console::detect_background)
/// and (on native targets with the `terminal-query` feature) by
/// [`query_terminal_background`].
// ---------------------------------------------------------------------------
// Core parsing (dep-free, WASM-safe)
// ---------------------------------------------------------------------------
/// Parse a terminal's OSC 11 background-colour response.
///
/// Accepts replies terminated by either BEL (`\x07`) or String Terminator
/// (`\x1b\\`). Channels may be 1, 2, or 4 hex digits; they are scaled to
/// 8-bit by taking the high byte:
///
/// | Digits | Example | Scaled 8-bit |
/// |--------|-----------|--------------|
/// | 1 | `f` | `0xff` → 255 |
/// | 2 | `ff` | `0xff` → 255 |
/// | 4 | `ffff` | high byte `0xff` → 255 |
/// | 4 | `8080` | high byte `0x80` → 128 |
///
/// Returns `None` on any parse error (missing prefix, wrong OSC number,
/// non-hex digits, wrong channel count, etc.).
///
/// # Examples
///
/// ```
/// use gilt::terminal_bg::parse_osc11_response;
///
/// // Black background
/// assert_eq!(
/// parse_osc11_response("\x1b]11;rgb:0000/0000/0000\x07"),
/// Some(gilt::color::color_triplet::ColorTriplet::new(0, 0, 0))
/// );
///
/// // White background
/// assert_eq!(
/// parse_osc11_response("\x1b]11;rgb:ffff/ffff/ffff\x07"),
/// Some(gilt::color::color_triplet::ColorTriplet::new(255, 255, 255))
/// );
///
/// // Malformed → None
/// assert_eq!(parse_osc11_response("not-an-osc-reply"), None);
/// ```
/// Scale a 1/2/4-digit hex channel string to an 8-bit value.
///
/// - 4 digits: take the high byte (first 2 digits).
/// - 2 digits: direct 8-bit value.
/// - 1 digit: replicate to both nibbles (e.g. `f` → `ff`).
///
/// Returns `None` for empty, non-hex, or unsupported lengths.
// ---------------------------------------------------------------------------
// Luminance check (reuses the WCAG formula in color/accessibility.rs)
// ---------------------------------------------------------------------------
/// Return `true` if the colour is perceived as dark (WCAG relative luminance < 0.5).
///
/// Uses the same linearisation formula as [`crate::accessibility::contrast_ratio`].
///
/// # Examples
///
/// ```
/// use gilt::terminal_bg::is_dark_background;
/// use gilt::color::color_triplet::ColorTriplet;
///
/// assert!(is_dark_background(ColorTriplet::new(0, 0, 0))); // black → dark
/// assert!(!is_dark_background(ColorTriplet::new(255, 255, 255))); // white → light
/// ```
/// WCAG 2.1 relative luminance for a single colour.
///
/// Duplicated here (rather than re-exported from `accessibility`) so
/// `terminal_bg` remains dep-free and can be compiled without pulling in the
/// rest of `color::accessibility`.
// ---------------------------------------------------------------------------
// Interactive TTY probe (feature = "terminal-query", native only)
// ---------------------------------------------------------------------------
/// Query the controlling terminal for its background colour using OSC 11.
///
/// Writes `\x1b]11;?\x07` to the TTY, then reads back the reply with a
/// 200 ms timeout in raw mode. On success the response is parsed via
/// [`parse_osc11_response`] and converted to a [`ConsoleBackground`] via
/// [`is_dark_background`].
///
/// Returns [`ConsoleBackground::Unknown`] when:
/// - No controlling TTY is available.
/// - The terminal does not support OSC 11 queries.
/// - The timeout expires before a complete reply arrives.
/// - Any I/O error occurs.
///
/// Requires the `terminal-query` feature and is excluded from WASM builds.