nu_command/platform/term/
term_query.rs

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
use std::{
    io::{Read, Write},
    time::Duration,
};

use nu_engine::command_prelude::*;
use nu_protocol::shell_error::io::IoError;

const CTRL_C: u8 = 3;

#[derive(Clone)]
pub struct TermQuery;

impl Command for TermQuery {
    fn name(&self) -> &str {
        "term query"
    }

    fn description(&self) -> &str {
        "Query the terminal for information."
    }

    fn extra_description(&self) -> &str {
        "Print the given query, and read the immediate result from stdin.

The standard input will be read right after `query` is printed, and consumed until the `terminator`
sequence is encountered. The `terminator` is not included in the output.

If `terminator` is not supplied, input will be read until Ctrl-C is pressed.

If `prefix` is supplied, input's initial bytes will be validated against it.
The `prefix` is not included in the output."
    }

    fn signature(&self) -> Signature {
        Signature::build("term query")
            .category(Category::Platform)
            .input_output_types(vec![(Type::Nothing, Type::Binary)])
            .allow_variants_without_examples(true)
            .required(
                "query",
                SyntaxShape::OneOf(vec![SyntaxShape::Binary, SyntaxShape::String]),
                "The query that will be printed to stdout.",
            )
            .named(
                "prefix",
                SyntaxShape::OneOf(vec![SyntaxShape::Binary, SyntaxShape::String]),
                "Prefix sequence for the expected reply.",
                Some('p'),
            )
            .named(
                "terminator",
                SyntaxShape::OneOf(vec![SyntaxShape::Binary, SyntaxShape::String]),
                "Terminator sequence for the expected reply.",
                Some('t'),
            )
            .switch(
                "keep",
                "Include prefix and terminator in the output.",
                Some('k'),
            )
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Get cursor position.",
                example: r#"term query (ansi cursor_position) --prefix (ansi csi) --terminator 'R'"#,
                result: None,
            },
            Example {
                description: "Get terminal background color.",
                example: r#"term query $'(ansi osc)10;?(ansi st)' --prefix $'(ansi osc)10;' --terminator (ansi st)"#,
                result: None,
            },
            Example {
                description: "Get terminal background color. (some terminals prefer `char bel` rather than `ansi st` as string terminator)",
                example: r#"term query $'(ansi osc)10;?(char bel)' --prefix $'(ansi osc)10;' --terminator (char bel)"#,
                result: None,
            },
            Example {
                description: "Read clipboard content on terminals supporting OSC-52.",
                example: r#"term query $'(ansi osc)52;c;?(ansi st)' --prefix $'(ansi osc)52;c;' --terminator (ansi st)"#,
                result: None,
            },
        ]
    }

    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        _input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let query: Vec<u8> = call.req(engine_state, stack, 0)?;
        let keep = call.has_flag(engine_state, stack, "keep")?;
        let prefix: Option<Vec<u8>> = call.get_flag(engine_state, stack, "prefix")?;
        let prefix = prefix.unwrap_or_default();
        let terminator: Option<Vec<u8>> = call.get_flag(engine_state, stack, "terminator")?;

        crossterm::terminal::enable_raw_mode()
            .map_err(|err| IoError::new(err.kind(), call.head, None))?;
        scopeguard::defer! {
            let _ = crossterm::terminal::disable_raw_mode();
        }

        // clear terminal events
        while crossterm::event::poll(Duration::from_secs(0))
            .map_err(|err| IoError::new(err.kind(), call.head, None))?
        {
            // If there's an event, read it to remove it from the queue
            let _ = crossterm::event::read()
                .map_err(|err| IoError::new(err.kind(), call.head, None))?;
        }

        let mut b = [0u8; 1];
        let mut buf = vec![];
        let mut stdin = std::io::stdin().lock();

        {
            let mut stdout = std::io::stdout().lock();
            stdout
                .write_all(&query)
                .map_err(|err| IoError::new(err.kind(), call.head, None))?;
            stdout
                .flush()
                .map_err(|err| IoError::new(err.kind(), call.head, None))?;
        }

        // Validate and skip prefix
        for bc in prefix {
            stdin
                .read_exact(&mut b)
                .map_err(|err| IoError::new(err.kind(), call.head, None))?;
            if b[0] != bc {
                return Err(ShellError::GenericError {
                    error: "Input did not begin with expected sequence".into(),
                    msg: "".into(),
                    span: None,
                    help: Some("Try running without `--prefix` and inspecting the output.".into()),
                    inner: vec![],
                });
            }
            if keep {
                buf.push(b[0]);
            }
        }

        if let Some(terminator) = terminator {
            loop {
                stdin
                    .read_exact(&mut b)
                    .map_err(|err| IoError::new(err.kind(), call.head, None))?;

                if b[0] == CTRL_C {
                    return Err(ShellError::InterruptedByUser {
                        span: Some(call.head),
                    });
                }

                buf.push(b[0]);

                if buf.ends_with(&terminator) {
                    if !keep {
                        // Remove terminator
                        buf.drain((buf.len() - terminator.len())..);
                    }
                    break;
                }
            }
        } else {
            loop {
                stdin
                    .read_exact(&mut b)
                    .map_err(|err| IoError::new(err.kind(), call.head, None))?;

                if b[0] == CTRL_C {
                    break;
                }

                buf.push(b[0]);
            }
        };

        Ok(Value::binary(buf, call.head).into_pipeline_data())
    }
}