(timeout: Duration)
| 145 | } |
| 146 | |
| 147 | fn detect_terminal_appearance(timeout: Duration) -> Option<Appearance> { |
| 148 | if std::env::var_os("NO_COLOR").is_some() { |
| 149 | return None; |
| 150 | } |
| 151 | if !std::io::stdout().is_terminal() || !std::io::stdin().is_terminal() { |
| 152 | return None; |
| 153 | } |
| 154 | |
| 155 | // OSC 11 query: request default background color. |
| 156 | // Response typically looks like: |
| 157 | // ESC ] 11 ; rgb:RRRR/GGGG/BBBB BEL |
| 158 | // or: |
| 159 | // ESC ] 11 ; #RRGGBB BEL |
| 160 | use std::io::Write; |
| 161 | |
| 162 | let mut stdout = std::io::stdout().lock(); |
| 163 | let _ = stdout.write_all(b"\x1b]11;?\x07"); |
| 164 | let _ = stdout.flush(); |
| 165 | |
| 166 | let start = Instant::now(); |
| 167 | |
| 168 | // Read from stdin in non-blocking mode (Unix-only best-effort). |
| 169 | #[cfg(unix)] |
| 170 | { |
| 171 | let mut buf = Vec::with_capacity(256); |
| 172 | use std::os::fd::AsRawFd; |
| 173 | |
| 174 | let fd = std::io::stdin().as_raw_fd(); |
| 175 | unsafe { |
| 176 | let flags = libc::fcntl(fd, libc::F_GETFL); |
| 177 | if flags < 0 { |
| 178 | return None; |
| 179 | } |
| 180 | if libc::fcntl(fd, libc::F_SETFL, flags | libc::O_NONBLOCK) < 0 { |
| 181 | return None; |
| 182 | } |
| 183 | |
| 184 | let mut stdin = std::io::stdin().lock(); |
| 185 | let mut tmp = [0u8; 256]; |
| 186 | while start.elapsed() < timeout { |
| 187 | match stdin.read(&mut tmp) { |
| 188 | Ok(0) => { |
| 189 | std::thread::sleep(Duration::from_millis(5)); |
| 190 | } |
| 191 | Ok(n) => { |
| 192 | buf.extend_from_slice(&tmp[..n]); |
| 193 | if buf.contains(&b'\x07') { |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | Err(e) if e.kind() == std::io::ErrorKind::WouldBlock => { |
| 198 | std::thread::sleep(Duration::from_millis(5)); |
| 199 | } |
| 200 | Err(_) => break, |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | let _ = libc::fcntl(fd, libc::F_SETFL, flags); |
no test coverage detected