coded_chars/display.rs
1//! This module provides control function that change the display.
2
3use std::fmt::{Display, Formatter};
4use crate::control::ControlSequence;
5
6/// # PP - Preceding page
7///
8/// PP causes the n-th preceding page in the presentation component to be displayed, where n equals the
9/// value of `n`. The effect of this control function on the active presentation position is not defined by this
10/// Standard.
11pub fn previous_page(n: usize) -> ControlSequence {
12 ControlSequence::new(&[&n.to_string()], "V")
13}
14
15/// # NP - Next page
16///
17/// NP causes the n-th following page in the presentation component to be displayed, where n equals the
18/// value of `n`. The effect of this control function on the active presentation position is not defined by this Standard.
19pub fn next_page(n: usize) -> ControlSequence {
20 ControlSequence::new(&[&n.to_string()], "U")
21}
22
23
24/// Use this function to call the control functions `SD`, `SL`, `ST` and `SR`.
25pub fn scroll(n: usize, scroll_direction: ScrollDirection) -> ControlSequence {
26 ControlSequence::new(&[&n.to_string()], &scroll_direction.to_string())
27}
28
29#[derive(Copy, Clone, Debug)]
30pub enum ScrollDirection {
31 /// # SD - Scroll down
32 ///
33 /// SD causes the data in the presentation component to be moved by n line positions if the line orientation
34 /// is horizontal, or by `n` character positions if the line orientation is vertical, such that the data appear to
35 /// move down.
36 ///
37 /// The active presentation position is not affected by this control function.
38 Down,
39
40 /// # SL - Scroll left
41 ///
42 /// SL causes the data in the presentation component to be moved by n character positions if the line
43 /// orientation is horizontal, or by `n` line positions if the line orientation is vertical, such that the data appear
44 /// to move to the left.
45 ///
46 /// The active presentation position is not affected by this control function.
47 Left,
48
49 /// # SR - Scroll right
50 ///
51 /// SR causes the data in the presentation component to be moved by n character positions if the line
52 /// orientation is horizontal, or by `n` line positions if the line orientation is vertical, such that the data appear
53 /// to move to the right.
54 ///
55 /// The active presentation position is not affected by this control function.
56 Right,
57
58 /// # SU - Scroll up
59 ///
60 /// SU causes the data in the presentation component to be moved by n line positions if the line orientation
61 /// is horizontal, or by `n` character positions if the line orientation is vertical, such that the data appear to
62 /// move up.
63 ///
64 /// The active presentation position is not affected by this control function.
65 Up,
66}
67
68impl Display for ScrollDirection {
69 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
70 write!(f, "{}", match self {
71 ScrollDirection::Down => "T",
72 ScrollDirection::Left => " @",
73 ScrollDirection::Right => " A",
74 ScrollDirection::Up => "S",
75 })
76 }
77}
78
79
80#[cfg(test)]
81mod tests {
82 use crate::introducers::CSI;
83 use super::*;
84
85 #[test]
86 fn test_previous_page() {
87 let sequence = previous_page(4);
88 assert_eq!(sequence.to_string(), format!("{}4V", CSI));
89 }
90
91 #[test]
92 fn test_next_page() {
93 let sequence = next_page(3);
94 assert_eq!(sequence.to_string(), format!("{}3U", CSI));
95 }
96
97 #[test]
98 fn test_scroll_down() {
99 let sequence = scroll(5, ScrollDirection::Down);
100 assert_eq!(sequence.to_string(), format!("{}5T", CSI));
101 }
102
103 #[test]
104 fn test_scroll_left() {
105 let sequence = scroll(2, ScrollDirection::Left);
106 assert_eq!(sequence.to_string(), format!("{}2 @", CSI));
107 }
108
109 #[test]
110 fn test_scroll_right() {
111 let sequence = scroll(7, ScrollDirection::Right);
112 assert_eq!(sequence.to_string(), format!("{}7 A", CSI));
113 }
114
115 #[test]
116 fn test_scroll_up() {
117 let sequence = scroll(1, ScrollDirection::Up);
118 assert_eq!(sequence.to_string(), format!("{}1S", CSI));
119 }
120
121 #[test]
122 fn test_scroll_direction_display() {
123 assert_eq!(ScrollDirection::Down.to_string(), "T");
124 assert_eq!(ScrollDirection::Left.to_string(), " @");
125 assert_eq!(ScrollDirection::Right.to_string(), " A");
126 assert_eq!(ScrollDirection::Up.to_string(), "S");
127 }
128}