libsixel_rs/device_control_string/
scrolling_mode.rs

1use super::constants::ESC;
2use crate::std::fmt;
3
4/// Sixel scrolling mode
5///
6/// You can set the sixel scrolling mode by using the Sixel Scrolling feature in the Graphics Set-Up screen. You can also select this mode by using the sixel display mode (DECSDM) control function.
7///
8/// When sixel display mode is set, the Sixel Scrolling feature is enabled. When sixel display mode is reset, the Sixel Scrolling feature is disabled.
9///
10/// To set DECSDM, the control function is.
11///
12/// | CSI  | ?    | 8   | 0   | h   |
13/// |------|------|-----|-----|-----|
14/// | 9/11 | 3/15 | 3/8 | 3/0 | 6/8 |
15///
16/// To reset DECSDM, the control function is.
17///
18/// | CSI  | ?    | 8    | 0     | 1      |
19/// |------|------|------|-------|--------|
20/// | 9/11 | 3/15 | 3/8  | 3/0   | 6/12   |
21#[repr(u8)]
22#[derive(Clone, Copy, Debug, Default, PartialEq)]
23pub enum ScrollingMode {
24    Enable = b'h',
25    #[default]
26    Disable = b'l',
27}
28
29impl From<ScrollingMode> for u8 {
30    fn from(val: ScrollingMode) -> Self {
31        val as u8
32    }
33}
34
35impl From<&ScrollingMode> for u8 {
36    fn from(val: &ScrollingMode) -> Self {
37        (*val).into()
38    }
39}
40
41impl From<ScrollingMode> for char {
42    fn from(val: ScrollingMode) -> Self {
43        (val as u8) as char
44    }
45}
46
47impl From<&ScrollingMode> for char {
48    fn from(val: &ScrollingMode) -> Self {
49        (*val).into()
50    }
51}
52
53impl fmt::Display for ScrollingMode {
54    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55        write!(f, "{ESC} [ ? 8 0 {}", char::from(self))
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn test_display() {
65        let enable = ScrollingMode::Enable;
66        let disable = ScrollingMode::Disable;
67
68        let exp_enable = [
69            ESC as u8, b' ', b'[', b' ', b'?', b' ', b'8', b' ', b'0', b' ', b'h',
70        ];
71        let exp_disable = [
72            ESC as u8, b' ', b'[', b' ', b'?', b' ', b'8', b' ', b'0', b' ', b'l',
73        ];
74
75        assert_eq!(format!("{enable}").as_bytes(), exp_enable.as_ref());
76        assert_eq!(format!("{disable}").as_bytes(), exp_disable.as_ref());
77    }
78}