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
use {
nix::sys::epoll::*,
std::io::{self, Read, Write},
};
#[derive(thiserror::Error, Debug)]
pub enum TlError {
#[error("IO error: {0}")]
IO(#[from] std::io::Error),
#[error("UTF8 error: {0}")]
Utf8(#[from] std::str::Utf8Error),
#[error("Parse Int error: {0}")]
ParseInt(#[from] std::num::ParseIntError),
#[error("Wrong answer format: {0}")]
WrongFormat(String),
#[error("Timeout waiting for xterm")]
Timeout,
#[error("Terminal error code: {0}")]
TerminalError(i64),
#[error("Nix error: {0}")]
NixError(#[from] nix::errno::Errno),
#[error("Unsupported platform")]
Unsupported,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Rgb {
pub r: u16,
pub g: u16,
pub b: u16,
}
impl Rgb {
pub fn new(r: u16, g: u16, b: u16) -> Self {
Self { r, g, b }
}
pub fn rp(self) -> f32 {
self.r as f32 / 65535f32
}
pub fn gp(self) -> f32 {
self.g as f32 / 65535f32
}
pub fn bp(self) -> f32 {
self.b as f32 / 65535f32
}
pub fn luma(self) -> f32 {
0.2627 * self.rp() + 0.6780 * self.gp() + 0.0593 * self.bp()
}
}
#[cfg(not(unix))]
fn query_xterm(query: &str, response: &mut[u8], timeout_ms: isize) -> Result<usize, TlError> {
Err(TlError::Unsupported)
}
#[cfg(unix)]
fn query_xterm(query: &str, response: &mut[u8], timeout_ms: isize) -> Result<usize, TlError> {
let stdin = io::stdin();
let mut stdin = stdin.lock();
let stdout = io::stdout();
let mut stdout = stdout.lock();
write!(stdout, "{}", query)?;
stdout.flush()?;
let poll_fd = epoll_create1(EpollCreateFlags::empty())?;
let mut event = EpollEvent::new(EpollFlags::EPOLLIN, 0);
epoll_ctl(
poll_fd,
EpollOp::EpollCtlAdd,
0,
Some(& mut event),
)?;
let mut events = [EpollEvent::empty(); 1];
let n = epoll_wait(poll_fd, &mut events, timeout_ms)?;
if n == 0 {
Err(TlError::Timeout)
} else {
let n = stdin.read(&mut response[..])?;
Ok(n)
}
}
fn query_bg_color() -> Result<Rgb, TlError> {
let query = "\x1b]11;?\x07";
let mut buffer = [0;50];
let n = query_xterm(query, &mut buffer, 20)?;
let s = std::str::from_utf8(&buffer[..n])?;
match s.strip_prefix("\x1b]11;rgb:") {
Some(raw_color) if raw_color.len() >= 14 => {
Ok(Rgb::new(
u16::from_str_radix(&raw_color[0..4], 16)?,
u16::from_str_radix(&raw_color[5..9], 16)?,
u16::from_str_radix(&raw_color[10..14], 16)?,
))
}
_ => Err(TlError::WrongFormat(s.to_string()))
}
}
pub fn bg_color() -> Result<Rgb, TlError> {
crossterm::terminal::enable_raw_mode()?;
let bg_color = query_bg_color();
crossterm::terminal::disable_raw_mode()?;
bg_color
}
pub fn luma() -> Result<f32, TlError> {
crossterm::terminal::enable_raw_mode()?;
let bg_color = query_bg_color();
crossterm::terminal::disable_raw_mode()?;
bg_color.map(|c| c.luma())
}