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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
|
use std::time::{Duration, Instant};
const STARTUP_PARSE_DEADLINE_MS: u64 = 500;
use lazy_static::lazy_static;
use regex::Regex;
use zellij_utils::{
consts::ZELLIJ_STDIN_CACHE_FILE, ipc::PixelDimensions, pane_size::SizeInPixels,
};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
/// Describe the terminal implementation of synchronised output
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum SyncOutput {
DCS,
CSI,
}
impl SyncOutput {
pub fn start_seq(&self) -> &'static [u8] {
static CSI_BSU_SEQ: &'static [u8] = "\u{1b}[?2026h".as_bytes();
static DCS_BSU_SEQ: &'static [u8] = "\u{1b}P=1s\u{1b}".as_bytes();
match self {
SyncOutput::DCS => DCS_BSU_SEQ,
SyncOutput::CSI => CSI_BSU_SEQ,
}
}
pub fn end_seq(&self) -> &'static [u8] {
static CSI_ESU_SEQ: &'static [u8] = "\u{1b}[?2026l".as_bytes();
static DCS_ESU_SEQ: &'static [u8] = "\u{1b}P=2s\u{1b}".as_bytes();
match self {
SyncOutput::DCS => DCS_ESU_SEQ,
SyncOutput::CSI => CSI_ESU_SEQ,
}
}
}
#[derive(Debug)]
pub struct StdinAnsiParser {
raw_buffer: Vec<u8>,
pending_color_sequences: Vec<(usize, String)>,
pending_events: Vec<AnsiStdinInstruction>,
parse_deadline: Option<Instant>,
}
impl StdinAnsiParser {
pub fn new() -> Self {
StdinAnsiParser {
raw_buffer: vec![],
pending_color_sequences: vec![],
pending_events: vec![],
parse_deadline: None,
}
}
pub fn terminal_emulator_query_string(&mut self) -> String {
// note that this assumes the String will be sent to the terminal emulator and so starts a
// deadline timeout (self.parse_deadline)
// <ESC>[14t => get text area size in pixels,
// <ESC>[16t => get character cell size in pixels
// <ESC>]11;?<ESC>\ => get background color
// <ESC>]10;?<ESC>\ => get foreground color
// <ESC>[?2026$p => get synchronised output mode
let mut query_string = String::from(
"\u{1b}[14t\u{1b}[16t\u{1b}]11;?\u{1b}\u{5c}\u{1b}]10;?\u{1b}\u{5c}\u{1b}[?2026$p",
);
// query colors
// eg. <ESC>]4;5;?<ESC>\ => query color register number 5
for i in 0..256 {
query_string.push_str(&format!("\u{1b}]4;{};?\u{1b}\u{5c}", i));
}
self.parse_deadline =
Some(Instant::now() + Duration::from_millis(STARTUP_PARSE_DEADLINE_MS));
query_string
}
fn drain_pending_events(&mut self) -> Vec<AnsiStdinInstruction> {
let mut events = vec![];
events.append(&mut self.pending_events);
if let Some(color_registers) =
AnsiStdinInstruction::color_registers_from_bytes(&mut self.pending_color_sequences)
{
events.push(color_registers);
}
events
}
pub fn should_parse(&self) -> bool {
if let Some(parse_deadline) = self.parse_deadline {
if parse_deadline >= Instant::now() {
return true;
}
}
false
}
pub fn startup_query_duration(&self) -> u64 {
STARTUP_PARSE_DEADLINE_MS
}
pub fn parse(&mut self, mut raw_bytes: Vec<u8>) -> Vec<AnsiStdinInstruction> {
for byte in raw_bytes.drain(..) {
self.parse_byte(byte);
}
self.drain_pending_events()
}
pub fn read_cache(&self) -> Option<Vec<AnsiStdinInstruction>> {
match OpenOptions::new()
.read(true)
.open(ZELLIJ_STDIN_CACHE_FILE.as_path())
{
Ok(mut file) => {
let mut json_cache = String::new();
file.read_to_string(&mut json_cache).ok()?;
let instructions =
serde_json::from_str::<Vec<AnsiStdinInstruction>>(&json_cache).ok()?;
if instructions.is_empty() {
None
} else {
Some(instructions)
}
},
Err(e) => {
log::error!("Failed to open STDIN cache file: {:?}", e);
None
},
}
}
pub fn write_cache(&self, events: Vec<AnsiStdinInstruction>) {
if let Ok(serialized_events) = serde_json::to_string(&events) {
if let Some(parent) = ZELLIJ_STDIN_CACHE_FILE.parent() {
let _ = std::fs::create_dir_all(parent);
}
if let Ok(mut file) = File::create(ZELLIJ_STDIN_CACHE_FILE.as_path()) {
let _ = file.write_all(serialized_events.as_bytes());
}
};
}
fn parse_byte(&mut self, byte: u8) {
if byte == b't' {
self.raw_buffer.push(byte);
match AnsiStdinInstruction::pixel_dimensions_from_bytes(&self.raw_buffer) {
Ok(ansi_sequence) => {
self.pending_events.push(ansi_sequence);
self.raw_buffer.clear();
},
Err(_) => {
self.raw_buffer.clear();
},
}
} else if byte == b'\\' {
self.raw_buffer.push(byte);
if let Ok(ansi_sequence) = AnsiStdinInstruction::bg_or_fg_from_bytes(&self.raw_buffer) {
self.pending_events.push(ansi_sequence);
self.raw_buffer.clear();
} else if let Ok((color_register, color_sequence)) =
color_sequence_from_bytes(&self.raw_buffer)
{
self.raw_buffer.clear();
self.pending_color_sequences
.push((color_register, color_sequence));
} else {
self.raw_buffer.clear();
}
} else if byte == b'y' {
self.raw_buffer.push(byte);
if let Some(ansi_sequence) =
AnsiStdinInstruction::synchronized_output_from_bytes(&self.raw_buffer)
{
self.pending_events.push(ansi_sequence);
self.raw_buffer.clear();
}
} else {
self.raw_buffer.push(byte);
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AnsiStdinInstruction {
PixelDimensions(PixelDimensions),
BackgroundColor(String),
ForegroundColor(String),
ColorRegisters(Vec<(usize, String)>),
SynchronizedOutput(Option<SyncOutput>),
}
impl AnsiStdinInstruction {
pub fn pixel_dimensions_from_bytes(bytes: &[u8]) -> Result<Self, &'static str> {
// eg. <ESC>[4;21;8t
lazy_static! {
static ref RE: Regex = Regex::new(r"^\u{1b}\[(\d+);(\d+);(\d+)t$").unwrap();
}
let key_string = String::from_utf8_lossy(bytes); // TODO: handle error
let captures = RE
.captures_iter(&key_string)
.next()
.ok_or("invalid_instruction")?;
let csi_index = captures[1].parse::<usize>();
let first_field = captures[2].parse::<usize>();
let second_field = captures[3].parse::<usize>();
if csi_index.is_err() || first_field.is_err() || second_field.is_err() {
return Err("invalid_instruction");
}
match csi_index {
Ok(4) => {
// text area size
Ok(AnsiStdinInstruction::PixelDimensions(PixelDimensions {
character_cell_size: None,
text_area_size: Some(SizeInPixels {
height: first_field.unwrap(),
width: second_field.unwrap(),
}),
}))
},
Ok(6) => {
// character cell size
Ok(AnsiStdinInstruction::PixelDimensions(PixelDimensions {
character_cell_size: Some(SizeInPixels {
height: first_field.unwrap(),
width: second_field.unwrap(),
}),
text_area_size: None,
}))
},
_ => Err("invalid sequence"),
}
}
pub fn bg_or_fg_from_bytes(bytes: &[u8]) -> Result<Self, &'static str> {
// eg. <ESC>]11;rgb:0000/0000/0000\
lazy_static! {
static ref BACKGROUND_RE: Regex = Regex::new(r"\]11;(.*)\u{1b}\\$").unwrap();
}
// eg. <ESC>]10;rgb:ffff/ffff/ffff\
lazy_static! {
static ref FOREGROUND_RE: Regex = Regex::new(r"\]10;(.*)\u{1b}\\$").unwrap();
}
let key_string = String::from_utf8_lossy(bytes);
if let Some(captures) = BACKGROUND_RE.captures_iter(&key_string).next() {
let background_query_response = captures[1].parse::<String>();
match background_query_response {
Ok(background_query_response) => Ok(AnsiStdinInstruction::BackgroundColor(
background_query_response,
)),
_ => Err("invalid_instruction"),
}
} else if let Some(captures) = FOREGROUND_RE.captures_iter(&key_string).next() {
let foreground_query_response = captures[1].parse::<String>();
match foreground_query_response {
Ok(foreground_query_response) => Ok(AnsiStdinInstruction::ForegroundColor(
foreground_query_response,
)),
_ => Err("invalid_instruction"),
}
} else {
Err("invalid_instruction")
}
}
pub fn color_registers_from_bytes(color_sequences: &mut Vec<(usize, String)>) -> Option<Self> {
if color_sequences.is_empty() {
return None;
}
let mut registers = vec![];
for (color_register, color_sequence) in color_sequences.drain(..) {
registers.push((color_register, color_sequence));
}
Some(AnsiStdinInstruction::ColorRegisters(registers))
}
pub fn synchronized_output_from_bytes(bytes: &[u8]) -> Option<Self> {
lazy_static! {
static ref RE: Regex = Regex::new(r"^\u{1b}\[\?2026;([0|1|2|3|4])\$y$").unwrap();
}
let key_string = String::from_utf8_lossy(bytes);
if let Some(captures) = RE.captures_iter(&key_string).next() {
match captures[1].parse::<usize>().ok()? {
1 | 2 => Some(AnsiStdinInstruction::SynchronizedOutput(Some(
SyncOutput::CSI,
))),
0 | 4 => Some(AnsiStdinInstruction::SynchronizedOutput(None)),
_ => None,
}
} else {
None
}
}
}
fn color_sequence_from_bytes(bytes: &[u8]) -> Result<(usize, String), &'static str> {
lazy_static! {
static ref COLOR_REGISTER_RE: Regex = Regex::new(r"\]4;(.*);(.*)\u{1b}\\$").unwrap();
}
lazy_static! {
// this form is used by eg. Alacritty, where the leading 4 is dropped in the response
static ref ALTERNATIVE_COLOR_REGISTER_RE: Regex = Regex::new(r"\](.*);(.*)\u{1b}\\$").unwrap();
}
let key_string = String::from_utf8_lossy(bytes);
if let Some(captures) = COLOR_REGISTER_RE.captures_iter(&key_string).next() {
let color_register_response = captures[1].parse::<usize>();
let color_response = captures[2].parse::<String>();
match (color_register_response, color_response) {
(Ok(crr), Ok(cr)) => Ok((crr, cr)),
_ => Err("invalid_instruction"),
}
} else if let Some(captures) = ALTERNATIVE_COLOR_REGISTER_RE
.captures_iter(&key_string)
.next()
{
let color_register_response = captures[1].parse::<usize>();
let color_response = captures[2].parse::<String>();
match (color_register_response, color_response) {
(Ok(crr), Ok(cr)) => Ok((crr, cr)),
_ => Err("invalid_instruction"),
}
} else {
Err("invalid_instruction")
}
}
|