aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/console/cursor.rs
blob: 7b93f28137532ac9ed4f363eadd52e569d3ddbd2 (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
//! ref: composer/vendor/symfony/console/Cursor.php

use crate::symfony::console::output::OutputInterface;
use crate::symfony::console::output::output_interface;

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

impl Cursor {
    pub fn new(
        output: std::rc::Rc<std::cell::RefCell<dyn OutputInterface>>,
        input: Option<shirabe_php_shim::PhpResource>,
    ) -> Self {
        let input = input.unwrap_or(shirabe_php_shim::STDIN);

        Self { output, input }
    }

    pub fn move_up(&self, lines: i64) -> &Self {
        self.output.borrow().write(
            &[format!("\x1b[{}A", lines)],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    pub fn move_left(&self, columns: i64) -> &Self {
        self.output.borrow().write(
            &[format!("\x1b[{}D", columns)],
            false,
            output_interface::OUTPUT_NORMAL,
        );

        self
    }

    pub fn move_to_column(&self, column: i64) -> &Self {
        self.output.borrow().write(
            &[format!("\x1b[{}G", 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
    }

    /// 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
    }
}