coded_chars/
area.rs

1//! Qualify and delimit areas.
2
3use std::fmt::{Display, Formatter};
4use crate::control::ControlSequence;
5use crate::escape::{escape, EscapeSequence};
6
7/// # DAQ - Define area qualification
8///
9/// DAQ is used to indicate that the active presentation position in the presentation component is the first
10/// character position of a qualified area. The last character position of the qualified area is the character
11/// position in the presentation component immediately preceding the first character position of the
12/// following qualified area.
13///
14/// This control function operates independently of the setting of the TABULATION STOP MODE (TSM).
15/// The character tabulation stop set by parameter value 7 applies to the active line only
16///
17/// ### Note
18/// The control functions for area definition (DAQ, EPA, ESA, SPA, SSA) should not be used within an SRS
19/// string or an SDS string.
20pub fn area_qualification(qualification: Qualification) -> ControlSequence {
21    ControlSequence::new(&[&qualification.to_string()], "o")
22}
23
24#[derive(Copy, Clone, Debug)]
25pub enum Qualification {
26    UnprotectNoGuard,
27    ProtectGuard,
28    Character,
29    Numeric,
30    Alphabet,
31    AlignLast,
32    FillZero,
33    SetTabStop,
34    Protect,
35    FillSpace,
36    AlignFirst,
37    Reverse,
38}
39
40impl Display for Qualification {
41    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42        write!(f, "{}", match self {
43            Qualification::UnprotectNoGuard => "0",
44            Qualification::ProtectGuard => "1",
45            Qualification::Character => "2",
46            Qualification::Numeric => "3",
47            Qualification::Alphabet => "4",
48            Qualification::AlignLast => "5",
49            Qualification::FillZero => "6",
50            Qualification::SetTabStop => "7",
51            Qualification::Protect => "8",
52            Qualification::FillSpace => "9",
53            Qualification::AlignFirst => "10",
54            Qualification::Reverse => "11",
55        })
56    }
57}
58
59/// # Start of selected area
60///
61/// SSA is used to indicate that the active presentation position is the first of a string of character positions
62/// in the presentation component, the contents of which are eligible to be transmitted in the form of a data
63/// stream or transferred to an auxiliary input/output device.
64///
65/// The end of this string is indicated by END OF SELECTED AREA (ESA). The string of characters
66/// actually transmitted or transferred depends on the setting of the GUARDED AREA TRANSFER MODE
67/// (GATM) and on any guarded areas established by DEFINE AREA QUALIFICATION (DAQ), or by
68/// START OF GUARDED AREA (SPA) and END OF GUARDED AREA (EPA).
69///
70/// ### Note
71///
72/// The control functions for area definition (DAQ, EPA, ESA, SPA, SSA) should not be used within an SRS
73/// string or an SDS string.
74pub const SSA: EscapeSequence = escape('F');
75
76/// # End of selected area
77///
78/// ESA is used to indicate that the active presentation position is the last of a string of character positions
79/// in the presentation component, the contents of which are eligible to be transmitted in the form of a data
80/// stream or transferred to an auxiliary input/output device. The beginning of this string is indicated by
81/// START OF SELECTED AREA (SSA).
82///
83/// ### Note
84///
85/// The control function for area definition (DAQ, EPA, ESA, SPA, SSA) should not be used within an SRS
86/// string or an SDS string.
87pub const ESA: EscapeSequence = escape('G');
88
89/// Start of protected area
90pub const SPA: EscapeSequence = escape('V');
91
92/// End of protected area
93pub const EPA: EscapeSequence = escape('W');
94
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn test_area_qualification() {
102        let qualification = Qualification::ProtectGuard;
103        let control_seq = area_qualification(qualification);
104
105        assert_eq!(
106            control_seq.to_string(),
107            format!("\x1b[{}o", qualification)
108        );
109    }
110
111    #[test]
112    fn test_qualification_display() {
113        assert_eq!(Qualification::UnprotectNoGuard.to_string(), "0");
114        assert_eq!(Qualification::ProtectGuard.to_string(), "1");
115        assert_eq!(Qualification::Character.to_string(), "2");
116        assert_eq!(Qualification::Numeric.to_string(), "3");
117        assert_eq!(Qualification::Alphabet.to_string(), "4");
118        assert_eq!(Qualification::AlignLast.to_string(), "5");
119        assert_eq!(Qualification::FillZero.to_string(), "6");
120        assert_eq!(Qualification::SetTabStop.to_string(), "7");
121        assert_eq!(Qualification::Protect.to_string(), "8");
122        assert_eq!(Qualification::FillSpace.to_string(), "9");
123        assert_eq!(Qualification::AlignFirst.to_string(), "10");
124        assert_eq!(Qualification::Reverse.to_string(), "11");
125    }
126
127    #[test]
128    fn test_escape_sequences() {
129        assert_eq!(SSA.to_string(), "\x1bF");
130        assert_eq!(ESA.to_string(), "\x1bG");
131        assert_eq!(SPA.to_string(), "\x1bV");
132        assert_eq!(EPA.to_string(), "\x1bW");
133    }
134}