[go: up one dir, main page]

  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
//!
//! TODO - top/left is 0,0
//!

impl_esc_sequence!(
    "A control sequence to save the cursor position.",
    SavePosition,
    "7"
);
impl_esc_sequence!(
    "A control sequence to restore the cursor position.",
    RestorePosition,
    "8"
);

impl_csi_sequence!("A control sequence to hide the cursor.", Hide, "?25l");
impl_csi_sequence!("A control sequence to show the cursor.", Show, "?25h");

impl_csi_sequence!(
    "A control sequence to enable the cursor blinking.",
    EnableBlinking,
    "?12h"
);
impl_csi_sequence!(
    "A control sequence to disable the cursor blinking.",
    DisableBlinking,
    "?12l"
);

/// A control sequence to move the cursor to the given location (column, row).
#[derive(Copy, Clone, Debug)]
pub struct MoveTo(pub u16, pub u16);

impl ::std::fmt::Display for MoveTo {
    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, csi!("{};{}H"), self.0 + 1, self.1 + 1)
    }
}

/// A control sequence to move the cursor by the given number of rows up.
#[derive(Copy, Clone, Debug)]
pub struct MoveUp(pub u16);

impl ::std::fmt::Display for MoveUp {
    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, csi!("{}A"), self.0)
    }
}

/// A control sequence to move the cursor by the given number of rows down.
#[derive(Copy, Clone, Debug)]
pub struct MoveDown(pub u16);

impl ::std::fmt::Display for MoveDown {
    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, csi!("{}B"), self.0)
    }
}

/// A control sequence to move the cursor by the given number of columns right.
#[derive(Copy, Clone, Debug)]
pub struct MoveRight(pub u16);

impl ::std::fmt::Display for MoveRight {
    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, csi!("{}C"), self.0)
    }
}

/// A control sequence to move the cursor by the given number of columns right.
#[derive(Copy, Clone, Debug)]
pub struct MoveLeft(pub u16);

impl ::std::fmt::Display for MoveLeft {
    fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, csi!("{}D"), self.0)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_save_position() {
        assert_eq!(&format!("{}", SavePosition), "\x1B7");
    }

    #[test]
    fn test_restore_position() {
        assert_eq!(&format!("{}", RestorePosition), "\x1B8");
    }

    #[test]
    fn test_hide() {
        assert_eq!(&format!("{}", Hide), "\x1B[?25l");
    }

    #[test]
    fn test_show() {
        assert_eq!(&format!("{}", Show), "\x1B[?25h");
    }

    #[test]
    fn test_disable_blinking() {
        assert_eq!(&format!("{}", DisableBlinking), "\x1B[?12l");
    }

    #[test]
    fn test_enable_blinking() {
        assert_eq!(&format!("{}", EnableBlinking), "\x1B[?12h");
    }

    #[test]
    fn test_move_up() {
        assert_eq!(&format!("{}", MoveUp(10)), "\x1B[10A");
    }

    #[test]
    fn test_move_down() {
        assert_eq!(&format!("{}", MoveDown(10)), "\x1B[10B");
    }

    #[test]
    fn test_move_right() {
        assert_eq!(&format!("{}", MoveRight(10)), "\x1B[10C");
    }

    #[test]
    fn test_move_left() {
        assert_eq!(&format!("{}", MoveLeft(10)), "\x1B[10D");
    }

    #[test]
    fn test_move_to() {
        assert_eq!(&format!("{}", MoveTo(5, 10)), "\x1B[6;11H");
    }
}