termal_core 5.0.0

This library contains implementation for the termal library
Documentation
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use base64::Engine;

use crate::{FromAnsiColorStr, Rgb, codes, raw::events::csi::Csi};

use super::{
    Key, KeyCode, Modifiers, Status, TermAttr, mouse::Mouse, osc::Osc,
    state_change::StateChange,
};

/// Possibly ambiguous terminal event.
///
/// Some terminal events are amiguous. This will contain all sensible
/// possibilities.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AmbiguousEvent {
    /// The main (most propable) event.
    pub event: AnyEvent,
    /// Other amiguous events.
    pub other: Vec<Event>,
}

/// Either known or unknown event.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum AnyEvent {
    /// Known parsed event.
    Known(Event),
    /// Unknown unparsed event.
    Unknown(Vec<u8>),
}

/// Known terminal event.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
    /// Key was pressed.
    KeyPress(Key),
    /// Mouse event.
    ///
    /// Mouse events are sent only if enabled with one of:
    ///
    /// - [`codes::ENABLE_MOUSE_XY_PR_TRACKING`]: enables events only on mouse
    ///   press, release and scroll.
    /// - [`codes::ENABLE_MOUSE_XY_DRAG_TRACKING`]: also enables drag events.
    /// - [`codes::ENABLE_MOUSE_XY_ALL_TRACKING`]: also enables move events.
    ///
    /// It is recommended to use an extension for better encoding of the
    /// events. The recommended extension is [`codes::ENABLE_MOUSE_XY_EXT`].
    Mouse(Mouse),
    /// Received terminal attributes.
    ///
    /// Usually received after request.
    Status(Status),
    /// The terminal has gained focus.
    ///
    /// The event may be received only if enabled with
    /// [`codes::ENABLE_FOCUS_EVENT`].
    Focus,
    /// The terminal has lost focus.
    ///
    /// The event may be received only if enabled with
    /// [`codes::ENABLE_FOCUS_EVENT`].
    FocusLost,
    /// The input state has changed.
    ///
    /// Usually requires to be enabled with feature.
    StateChange(StateChange),
}

impl AmbiguousEvent {
    /// Create unknown event from the given data.
    pub fn unknown<B>(data: B) -> Self
    where
        B: Into<Vec<u8>>,
    {
        AmbiguousEvent {
            event: AnyEvent::Unknown(data.into()),
            other: vec![],
        }
    }

    /// Create unambiguous event.
    pub fn event(evt: Event) -> Self {
        Self {
            event: AnyEvent::Known(evt),
            other: vec![],
        }
    }

    /// Create unambiguous key event.
    pub fn key(key: Key) -> Self {
        Self::event(Event::KeyPress(key))
    }

    /// Create unambiguous mouse event.
    pub fn mouse(mouse: Mouse) -> Self {
        Self::event(Event::Mouse(mouse))
    }

    /// Create unambiguous status event.
    pub fn status(status: Status) -> Self {
        Self::event(Event::Status(status))
    }

    /// Create unambiguous state change event.
    pub fn state_change(state: StateChange) -> Self {
        Self::event(Event::StateChange(state))
    }

    /// Parse single char event.
    pub fn from_char_code(code: char) -> Self {
        Self::char_key(code)
    }

    /// Parse the code into event.
    pub fn from_code(code: &[u8]) -> Self {
        if (6..=9).contains(&code.len()) && code.starts_with(b"\x1b[M") {
            return Self::mouse_code(code);
        }

        std::str::from_utf8(code)
            .ok()
            .and_then(Self::from_code_str)
            .unwrap_or_else(|| Self::unknown(code))
    }

    /// Create verbatim key code. (key code with the given char value)
    pub fn verbatim(c: char) -> Self {
        Self::key(Key::verbatim(c))
    }

    fn mouse_code(code: &[u8]) -> Self {
        if code.len() == 6 && code.starts_with(b"\x1b[M") {
            return AmbiguousEvent::mouse(Mouse::from_data(
                code[3] as u32 - 32,
                code[4] as usize - 32,
                code[5] as usize - 32,
                None,
            ));
        }
        let utf_code = std::str::from_utf8(&code[3..])
            .map(|s| s.chars().collect::<Box<[char]>>());
        let Ok([s, x, y]) = utf_code.as_deref() else {
            return Self::unknown(code);
        };
        AmbiguousEvent::mouse(Mouse::from_data(
            *s as u32 - 32,
            *x as usize - 32,
            *y as usize - 32,
            None,
        ))
    }

    fn from_code_str(code: &str) -> Option<Self> {
        if code.is_empty() {
            return None;
        }

        let cnt = (code.len() <= 8).then(|| code.chars().count());

        // shouldn't really happen
        if cnt == Some(1) {
            return code.chars().next().map(Self::char_key);
        }

        // ALT code
        if cnt == Some(2) && code.starts_with('\x1b') {
            let chr = code.chars().last().unwrap();

            let mut res = Self::from_char_code(chr);
            if let AnyEvent::Known(Event::KeyPress(ref mut k)) = res.event {
                k.modifiers |= Modifiers::ALT;
                k.key_char = None;
            }

            for k in res.other.iter_mut() {
                let Event::KeyPress(k) = k else {
                    return None;
                };
                k.modifiers |= Modifiers::ALT;
                k.key_char = None;
            }

            if chr == 'd' {
                res.other.push(Event::KeyPress(Key::mcode(
                    KeyCode::Delete,
                    Modifiers::CONTROL,
                )));
            }

            return Some(res);
        }

        // check for code type
        if let Some(code) = code.strip_prefix(codes::CSI) {
            Self::csi(code)
        } else if let Some(code) = code.strip_prefix(codes::DCS) {
            Self::dcs(code)
        } else if let Some(code) = code.strip_prefix(codes::OSC) {
            Self::osc(code)
        } else {
            code.strip_prefix(codes::SS3).and_then(|cscode| {
                let csi = Csi::parse(cscode);
                matches!(csi.postfix.as_str(), "P" | "Q" | "R" | "S")
                    .then(|| Self::csi_xterm(csi))
                    .flatten()
            })
        }
    }

    fn csi(code: &str) -> Option<Self> {
        match code {
            "I" => return Some(Self::event(Event::Focus)),
            "O" => return Some(Self::event(Event::FocusLost)),
            "0n" => return Some(Self::status(Status::Ok)),
            _ => {}
        }

        let csi = Csi::parse(code);

        match (csi.prefix.as_str(), &csi.args[..], csi.postfix.as_str()) {
            // Ambiguous (F3 with modifiers or specific cursor position)
            ("", [1, x], "R") if *x < 16 => Some(Self {
                event: AnyEvent::Known(Event::KeyPress(Key::mcode(
                    KeyCode::F3,
                    Modifiers::from_id(*x),
                ))),
                other: vec![Event::Status(Status::CursorPosition {
                    x: *x as usize,
                    y: 1,
                })],
            }),
            // Terminal attributes
            ("?", _, "c") => {
                Some(Self::status(Status::Attributes(TermAttr::parse(csi))))
            }
            // Mouse event with the SGR extension
            ("<", [s, x, y], d @ ("M" | "m")) => Some(Self::mouse(
                Mouse::from_data(*s, *x as usize, *y as usize, Some(d == "M")),
            )),
            // Mouse event with the URXVT extension
            ("", [s, x, y], "M") => Some(Self::mouse(Mouse::from_data(
                s.checked_sub(32)?,
                *x as usize,
                *y as usize,
                None,
            ))),
            // Cursor position
            ("" | "?", [y, x], "R") => {
                Some(Self::status(Status::CursorPosition {
                    x: *x as usize,
                    y: *y as usize,
                }))
            }
            // Size of buffer in pixels
            ("", [4, h, w], "t") => {
                Some(Self::status(Status::TextAreaSizePx {
                    w: *w as usize,
                    h: *h as usize,
                }))
            }
            // Size of single character
            ("", [6, h, w], "t") => Some(Self::status(Status::CharSize {
                w: *w as usize,
                h: *h as usize,
            })),
            // Size of terminal in characters
            ("", [8, h, w], "t") => Some(Self::status(Status::TextAreaSize {
                w: *w as usize,
                h: *h as usize,
            })),
            // Sixel color register count
            ("?", [1, 0, v], "S") => {
                Some(Self::status(Status::SixelColors(*v as usize)))
            }
            // Max sixel image size
            ("?", [2, 0, w, h], "S") => {
                Some(Self::status(Status::SixelSize {
                    w: *w as usize,
                    h: *h as usize,
                }))
            }
            ("", [200], "~") => {
                Some(Self::state_change(StateChange::BracketedPasteStart))
            }
            ("", [201], "~") => {
                Some(Self::state_change(StateChange::BracketedPasteEnd))
            }
            // Possibly VT key press
            ("", _, "~") => Self::csi_vt(csi),
            // Possibly xterm key press
            ("", _, post) if post.len() == 1 => Self::csi_xterm(csi),
            _ => None,
        }
    }

    fn dcs(code: &str) -> Option<Self> {
        let code = code.strip_suffix(codes::ST)?;

        code.strip_prefix(">|")
            .map(|name| Self::status(Status::TerminalName(name.into())))
    }

    fn osc(code: &str) -> Option<Self> {
        let code = code
            .strip_suffix(codes::ST)
            .or_else(|| code.strip_suffix(codes::BELL))?;
        let osc = Osc::parse(code);

        match (&osc.args[..], osc.data) {
            ([4, code], color) => Some(Self::status(Status::ColorCodeColor {
                code: *code as u8,
                color: Rgb::<u16>::from_ansi_color_str(color).ok()?,
            })),
            ([10], color) => Some(Self::status(Status::DefaultFgColor(
                Rgb::<u16>::from_ansi_color_str(color).ok()?,
            ))),
            ([11], color) => Some(Self::status(Status::DefaultBgColor(
                Rgb::<u16>::from_ansi_color_str(color).ok()?,
            ))),
            ([12], color) => Some(Self::status(Status::CursorColor(
                Rgb::<u16>::from_ansi_color_str(color).ok()?,
            ))),
            ([52], selection) => Some(Self::status(Status::SelectionData(
                base64::prelude::BASE64_STANDARD
                    .decode(selection.split_once(';')?.1)
                    .ok()?,
            ))),
            ([52, _], selection) => Some(Self::status(Status::SelectionData(
                base64::prelude::BASE64_STANDARD.decode(selection).ok()?,
            ))),
            _ => None,
        }
    }

    /// # Prerequisities
    /// - `csi.postfix.chars().count() == 1`
    fn csi_xterm(csi: Csi) -> Option<Self> {
        let pchr = csi.postfix.chars().next()?;
        match csi.args.as_slice() {
            [] | [1] => {
                KeyCode::from_xterm_id(pchr).map(Key::code).map(Self::key)
            }
            [1, m] => KeyCode::from_xterm_id(pchr)
                .map(|k| Self::key(Key::mcode(k, Modifiers::from_id(*m)))),
            _ => None,
        }
    }

    fn csi_vt(csi: Csi) -> Option<Self> {
        match csi.args.as_slice() {
            [k] => KeyCode::from_vt_id(*k).map(Key::code).map(Self::key),
            [k, m] => KeyCode::from_vt_id(*k)
                .map(|k| Self::key(Key::mcode(k, Modifiers::from_id(*m)))),
            _ => None,
        }
    }

    fn char_key(chr: char) -> Self {
        let mut key = Key {
            key_char: Some(chr),
            code: KeyCode::from_char(chr),
            modifiers: Modifiers::NONE,
        };

        if chr.is_uppercase() {
            key.modifiers |= Modifiers::SHIFT;
        }

        if ('\0'..='\x1A').contains(&chr) && chr != '\x09' && chr != '\x0d' {
            key.modifiers |= Modifiers::CONTROL;
        }

        if chr.is_ascii_control() {
            key.key_char = None;
        }

        if chr == '\r' {
            key.key_char = Some('\n');
        }

        let event = Event::KeyPress(key);
        let mut amb = vec![];

        match chr {
            '\x08' => amb.push(Event::KeyPress(Key {
                key_char: None,
                code: KeyCode::Backspace,
                modifiers: Modifiers::CONTROL,
            })),
            '\x09' => amb.push(Event::KeyPress(Key {
                key_char: None,
                code: KeyCode::Char('i'),
                modifiers: Modifiers::CONTROL,
            })),
            '\x0d' => amb.push(Event::KeyPress(Key {
                key_char: None,
                code: KeyCode::Char('i'),
                modifiers: Modifiers::CONTROL,
            })),
            '\x17' => amb.push(Event::KeyPress(Key {
                key_char: None,
                code: KeyCode::Backspace,
                modifiers: Modifiers::CONTROL,
            })),
            _ => {}
        }

        AmbiguousEvent {
            event: AnyEvent::Known(event),
            other: amb,
        }
    }
}