aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/console/cursor.rs
blob: 9016fab381d716d96d60635d4960973da8e58b33 (plain)
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
use crate::symfony::console::output::OutputInterface;
use crate::symfony::console::output::output_interface;
use std::cell::RefCell;
use std::rc::Rc;

#[derive(Debug)]
pub struct Cursor {
    output: Rc<RefCell<dyn OutputInterface>>,
    input: shirabe_php_shim::PhpMixed,
}

impl Cursor {
    pub fn new(
        output: Rc<RefCell<dyn OutputInterface>>,
        input: Option<shirabe_php_shim::PhpMixed>,
    ) -> Self {
        let input = input.unwrap_or_else(|| {
            if shirabe_php_shim::defined("STDIN") {
                // PHP uses the `STDIN` resource constant here, but the shim models it as
                // `PhpResource` while this field is `PhpMixed` (which has no resource
                // variant). Fall back to opening `php://input` so the value stays a
                // PhpMixed stream handle.
                shirabe_php_shim::fopen("php://input", "r+")
            } else {
                shirabe_php_shim::fopen("php://input", "r+")
            }
        });

        Self { output, input }
    }

    pub fn move_up(&self, lines: i64) -> &Self {
        self.output.borrow().write(
            &[shirabe_php_shim::sprintf(
                "\x1b[%dA",
                &[shirabe_php_shim::PhpMixed::Int(lines)],
            )],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    pub fn move_down(&self, lines: i64) -> &Self {
        self.output.borrow().write(
            &[shirabe_php_shim::sprintf(
                "\x1b[%dB",
                &[shirabe_php_shim::PhpMixed::Int(lines)],
            )],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    pub fn move_right(&self, columns: i64) -> &Self {
        self.output.borrow().write(
            &[shirabe_php_shim::sprintf(
                "\x1b[%dC",
                &[shirabe_php_shim::PhpMixed::Int(columns)],
            )],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    pub fn move_left(&self, columns: i64) -> &Self {
        self.output.borrow().write(
            &[shirabe_php_shim::sprintf(
                "\x1b[%dD",
                &[shirabe_php_shim::PhpMixed::Int(columns)],
            )],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    pub fn move_to_column(&self, column: i64) -> &Self {
        self.output.borrow().write(
            &[shirabe_php_shim::sprintf(
                "\x1b[%dG",
                &[shirabe_php_shim::PhpMixed::Int(column)],
            )],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    pub fn move_to_position(&self, column: i64, row: i64) -> &Self {
        self.output.borrow().write(
            &[shirabe_php_shim::sprintf(
                "\x1b[%d;%dH",
                &[
                    shirabe_php_shim::PhpMixed::Int(row + 1),
                    shirabe_php_shim::PhpMixed::Int(column),
                ],
            )],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    pub fn save_position(&self) -> &Self {
        self.output.borrow().write(
            &["\x1b7".to_string()],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    pub fn restore_position(&self) -> &Self {
        self.output.borrow().write(
            &["\x1b8".to_string()],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    pub fn hide(&self) -> &Self {
        self.output.borrow().write(
            &["\x1b[?25l".to_string()],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    pub fn show(&self) -> &Self {
        self.output.borrow().write(
            &["\x1b[?25h\x1b[?0c".to_string()],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    /// Clears all the output from the current line.
    pub fn clear_line(&self) -> &Self {
        self.output.borrow().write(
            &["\x1b[2K".to_string()],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    /// Clears all the output from the current line after the current position.
    pub fn clear_line_after(&self) -> &Self {
        self.output.borrow().write(
            &["\x1b[K".to_string()],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    /// Clears all the output from the cursors' current position to the end of the screen.
    pub fn clear_output(&self) -> &Self {
        self.output.borrow().write(
            &["\x1b[0J".to_string()],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    /// Clears the entire screen.
    pub fn clear_screen(&self) -> &Self {
        self.output.borrow().write(
            &["\x1b[2J".to_string()],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    /// Returns the current cursor position as x,y coordinates.
    pub fn get_current_position(&self) -> Vec<i64> {
        static IS_TTY_SUPPORTED: std::sync::OnceLock<bool> = std::sync::OnceLock::new();

        let is_tty_supported = if shirabe_php_shim::function_exists("proc_open") {
            *IS_TTY_SUPPORTED.get_or_init(|| {
                let mut pipes = shirabe_php_shim::PhpMixed::Null;
                shirabe_php_shim::proc_open(
                    "echo 1 >/dev/null",
                    &vec![
                        shirabe_php_shim::PhpMixed::List(vec![
                            Box::new(shirabe_php_shim::PhpMixed::String("file".to_string())),
                            Box::new(shirabe_php_shim::PhpMixed::String("/dev/tty".to_string())),
                            Box::new(shirabe_php_shim::PhpMixed::String("r".to_string())),
                        ]),
                        shirabe_php_shim::PhpMixed::List(vec![
                            Box::new(shirabe_php_shim::PhpMixed::String("file".to_string())),
                            Box::new(shirabe_php_shim::PhpMixed::String("/dev/tty".to_string())),
                            Box::new(shirabe_php_shim::PhpMixed::String("w".to_string())),
                        ]),
                        shirabe_php_shim::PhpMixed::List(vec![
                            Box::new(shirabe_php_shim::PhpMixed::String("file".to_string())),
                            Box::new(shirabe_php_shim::PhpMixed::String("/dev/tty".to_string())),
                            Box::new(shirabe_php_shim::PhpMixed::String("w".to_string())),
                        ]),
                    ],
                    &mut pipes,
                )
            })
        } else {
            false
        };

        if !is_tty_supported {
            return vec![1, 1];
        }

        let stty_mode = shirabe_php_shim::shell_exec("stty -g");
        shirabe_php_shim::shell_exec("stty -icanon -echo");

        shirabe_php_shim::fwrite(self.input.clone(), "\x1b[6n", 0);

        let code = shirabe_php_shim::trim(
            shirabe_php_shim::fread(self.input.clone(), 1024)
                .as_deref()
                .unwrap_or(""),
            None,
        );

        shirabe_php_shim::shell_exec(&shirabe_php_shim::sprintf(
            "stty %s",
            &[shirabe_php_shim::PhpMixed::String(
                stty_mode.unwrap_or_default(),
            )],
        ));

        let mut row: i64 = 0;
        let mut col: i64 = 0;
        shirabe_php_shim::sscanf(&code, "\x1b[%d;%dR", &mut row, &mut col);

        vec![col, row]
    }
}