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
use crate::mode::Mode;
use crate::privmode::PrivateMode;
use crate::color::Color;
use crate::sgr::SGR;

#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum CSI<'a> {
    /// CUrsor Up
    CUU(u16),

    /// CUrsor Down
    CUD(u16),

    /// CUrsor Forward
    CUF(u16),

    /// CUrsor Backward
    CUB(u16),

    /// CUrsor Next line
    CNL(u16),

    /// CUrsor Preceding line
    CPL(u16),

    /// Cursor cHaracter Absolute
    CHA(u16),

    /// CUrsor Position
    CUP(u16, u16),

    /// Erase Display; 0: from cursor to end of display; 1: from start to cursor; 2: whole display;
    /// 3: whole display including scrollback
    ED(u16),

    /// Erase Line; 0: from cursor to end of line; 1: from start of line to cursor; 2: whole line
    EL(u16),

    /// Set Mode
    #[allow(dead_code)]
    SM(Mode),

    /// Reset Mode
    #[allow(dead_code)]
    RM(Mode),

    /// Private Set Mode
    #[allow(dead_code)]
    PSM(PrivateMode),

    /// Private Reset Mode
    #[allow(dead_code)]
    PRM(PrivateMode),

    /// Set Graphics Rendition
    SGR(&'a [SGR]),

    /// Set underline color
    LxULColor(Color),

    /// Set dim color
    LxDimColor(Color),

    /// Make the current color pair the default attributes
    LxDefColor,

    /// Set screen blank timeout to n minutes
    LxScreenBlankTimeout(u16),

    /// Set bell frequency in Hz
    LxBellFrequency(u16),

    /// Set bell duration in msec
    LxBellDuration(u16),

    /// Bring specified console to the front
    LxActivateConsole(u8),

    /// Unblank the screen
    LxUnblankScreen,

    /// Set VESA powerdown interval in minutes
    LxVESAPowerdownInterval(u16),

    /// Bring the previous console to the front
    LxActivatePreviousConsole,

    /// Set cursor blink interval in milliseconds
    LxSetCursorBlinkInterval(u16),
}

fn csi(attr: &[u16], ch: char) -> String {
    format!("\x1b[{}{}", attr.iter().map(|a| a.to_string()).collect::<Vec<_>>().join(";"), ch)
}

fn decpm(p: u16, ch: char) -> String {
    format!("\x1b[?{}{}", p, ch)
}

fn sgr(attr: &[SGR]) -> Vec<u16> {
    attr.iter()
        .flat_map::<Vec<u16>, _>(|a| (*a).into())
        .collect()
}

impl<'a> std::fmt::Display for CSI<'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", match *self {
            CSI::CUU(n)                         => csi(&[n], 'A'),
            CSI::CUD(n)                         => csi(&[n], 'B'),
            CSI::CUF(n)                         => csi(&[n], 'C'),
            CSI::CUB(n)                         => csi(&[n], 'D'),
            CSI::CNL(n)                         => csi(&[n], 'E'),
            CSI::CPL(n)                         => csi(&[n], 'F'),
            CSI::CHA(n)                         => csi(&[n], 'G'),
            CSI::CUP(r, c)                      => csi(&[r, c], 'H'),
            CSI::ED(m) if m <= 3                => csi(&[m], 'J'),
            CSI::ED(x)                          => panic!("illegal ED mode {}", x),
            CSI::EL(m) if m <= 2                => csi(&[m], 'K'),
            CSI::EL(x)                          => panic!("illegal EL mode {}", x),
            CSI::SM(m)                          => csi(&[m.into()], 'h'),
            CSI::RM(m)                          => csi(&[m.into()], 'l'),
            CSI::PSM(m)                         => decpm(m.into(), 'h'),
            CSI::PRM(m)                         => decpm(m.into(), 'l'),
            CSI::SGR(attr)                      => csi(&sgr(attr), 'm'),
            CSI::LxULColor(c)                   => csi(&[1, c.into()], ']'),
            CSI::LxDimColor(c)                  => csi(&[2, c.into()], ']'),
            CSI::LxDefColor                     => csi(&[8], ']'),
            CSI::LxScreenBlankTimeout(n)        => csi(&[9, n], ']'),
            CSI::LxBellFrequency(n)             => csi(&[10, n], ']'),
            CSI::LxBellDuration(t)              => csi(&[11, t], ']'),
            CSI::LxActivateConsole(n)           => csi(&[12, n as u16], ']'),
            CSI::LxUnblankScreen                => csi(&[13], ']'),
            CSI::LxVESAPowerdownInterval(n)     => csi(&[14, n], ']'),
            CSI::LxActivatePreviousConsole      => csi(&[15], ']'),
            CSI::LxSetCursorBlinkInterval(n)    => csi(&[16, n], ']'),
        })
    }
}

#[cfg(test)]
mod test {
    use super::CSI;
    use crate::sgr::SGR;
    use crate::color::Color;

    #[test]
    fn test_eq() {
        let a = CSI::CUU(3);
        let b = CSI::CUU(3);
        assert!(a == b);
    }

    #[test]
    fn test_neq() {
        let a = CSI::CUU(3);
        let b = CSI::CUD(3);
        assert!(a != b);
    }

    #[test]
    fn test_clone() {
        let a = CSI::CUU(3);
        let b = a.clone();
        assert!(a == b);
    }

    #[test]
    fn test_copy() {
        let a = CSI::SGR(&[SGR::FG(Color::Red)]);
        let b = a;
        assert!(a == b);
    }
}