freya_terminal/
parser.rs

1use std::sync::Arc;
2
3use vt100::Parser;
4
5/// Check for terminal queries in PTY output and return appropriate responses.
6///
7/// This handles DSR, DA, and other queries that shells like nushell send.
8pub(crate) fn check_for_terminal_queries(
9    data: &[u8],
10    parser: &Arc<std::sync::RwLock<Parser>>,
11) -> Vec<Vec<u8>> {
12    let mut responses = Vec::new();
13
14    // DSR 6n - Cursor Position Report
15    if data.windows(4).any(|w| w == b"\x1b[6n")
16        && let Ok(p) = parser.read()
17    {
18        let (row, col) = p.screen().cursor_position();
19        let response = format!("\x1b[{};{}R", row + 1, col + 1);
20        responses.push(response.into_bytes());
21    }
22
23    // DSR ?6n - Extended Cursor Position Report
24    if data.windows(5).any(|w| w == b"\x1b[?6n")
25        && let Ok(p) = parser.read()
26    {
27        let (row, col) = p.screen().cursor_position();
28        let response = format!("\x1b[?{};{}R", row + 1, col + 1);
29        responses.push(response.into_bytes());
30    }
31
32    // DSR 5n - Device Status Report (terminal OK)
33    if data.windows(4).any(|w| w == b"\x1b[5n") {
34        responses.push(b"\x1b[0n".to_vec());
35    }
36
37    // DA1 - Primary Device Attributes
38    if data.windows(3).any(|w| w == b"\x1b[c") || data.windows(4).any(|w| w == b"\x1b[0c") {
39        responses.push(b"\x1b[?62;22c".to_vec());
40    }
41
42    // DA2 - Secondary Device Attributes
43    if data.windows(4).any(|w| w == b"\x1b>c") || data.windows(5).any(|w| w == b"\x1b>0c") {
44        responses.push(b"\x1b[>0;0;0c".to_vec());
45    }
46
47    responses
48}