libsixel_rs/device_control_string/
selector.rs

1use super::constants::ESC;
2use crate::std::fmt;
3
4/// Bitness encoding for the [DeviceControlString](super::DeviceControlString).
5#[repr(u8)]
6#[derive(Clone, Copy, Debug, Default, PartialEq)]
7pub enum DcsMode {
8    #[default]
9    SevenBit = 0,
10    EightBit,
11}
12
13impl DcsMode {
14    /// Creates a new [DcsMode] selector.
15    pub const fn new() -> Self {
16        Self::SevenBit
17    }
18}
19
20/// DCS escape sequence function
21#[repr(u8)]
22#[derive(Clone, Copy, Debug, Default, PartialEq)]
23pub enum DcsFunction {
24    #[default]
25    SevenBit = b'P',
26    EightBit = 0x90,
27}
28
29impl DcsFunction {
30    /// Creates a new [DcsFunction].
31    pub const fn new() -> Self {
32        Self::SevenBit
33    }
34}
35
36impl From<DcsFunction> for u8 {
37    fn from(val: DcsFunction) -> Self {
38        val as u8
39    }
40}
41
42impl From<&DcsFunction> for u8 {
43    fn from(val: &DcsFunction) -> Self {
44        (*val).into()
45    }
46}
47
48impl From<DcsFunction> for char {
49    fn from(val: DcsFunction) -> Self {
50        (val as u8) as char
51    }
52}
53
54impl From<&DcsFunction> for char {
55    fn from(val: &DcsFunction) -> Self {
56        (*val).into()
57    }
58}
59
60impl From<&DcsMode> for DcsFunction {
61    fn from(val: &DcsMode) -> Self {
62        match val {
63            DcsMode::SevenBit => Self::SevenBit,
64            DcsMode::EightBit => Self::EightBit,
65        }
66    }
67}
68
69impl From<DcsMode> for DcsFunction {
70    fn from(val: DcsMode) -> Self {
71        (&val).into()
72    }
73}
74
75impl From<&DcsFunction> for DcsMode {
76    fn from(val: &DcsFunction) -> Self {
77        match val {
78            DcsFunction::SevenBit => Self::SevenBit,
79            DcsFunction::EightBit => Self::EightBit,
80        }
81    }
82}
83
84impl From<DcsFunction> for DcsMode {
85    fn from(val: DcsFunction) -> Self {
86        (&val).into()
87    }
88}
89
90impl fmt::Display for DcsFunction {
91    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92        if self == &Self::SevenBit {
93            write!(f, "{ESC}")?;
94        }
95        write!(f, "{}", char::from(self))
96    }
97}
98
99/// ST escape sequence function
100#[repr(u8)]
101#[derive(Clone, Copy, Debug, Default, PartialEq)]
102pub enum StFunction {
103    #[default]
104    SevenBit = b'\\',
105    EightBit = 0x9c,
106}
107
108impl StFunction {
109    /// Creates a new [StFunction].
110    pub const fn new() -> Self {
111        Self::SevenBit
112    }
113}
114
115impl From<StFunction> for u8 {
116    fn from(val: StFunction) -> Self {
117        val as u8
118    }
119}
120
121impl From<&StFunction> for u8 {
122    fn from(val: &StFunction) -> Self {
123        (*val).into()
124    }
125}
126
127impl From<StFunction> for char {
128    fn from(val: StFunction) -> Self {
129        (val as u8) as char
130    }
131}
132
133impl From<&StFunction> for char {
134    fn from(val: &StFunction) -> Self {
135        (*val).into()
136    }
137}
138
139impl From<&DcsMode> for StFunction {
140    fn from(val: &DcsMode) -> Self {
141        match val {
142            DcsMode::SevenBit => Self::SevenBit,
143            DcsMode::EightBit => Self::EightBit,
144        }
145    }
146}
147
148impl From<DcsMode> for StFunction {
149    fn from(val: DcsMode) -> Self {
150        (&val).into()
151    }
152}
153
154impl From<&StFunction> for DcsMode {
155    fn from(val: &StFunction) -> Self {
156        match val {
157            StFunction::SevenBit => Self::SevenBit,
158            StFunction::EightBit => Self::EightBit,
159        }
160    }
161}
162
163impl From<StFunction> for DcsMode {
164    fn from(val: StFunction) -> Self {
165        (&val).into()
166    }
167}
168
169impl fmt::Display for StFunction {
170    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
171        if self == &Self::SevenBit {
172            write!(f, "{ESC}")?;
173        }
174        write!(f, "{}", char::from(self))
175    }
176}
177
178/// Selects how the terminal draws the background color. You can use one of three values.
179///
180/// | P2 ([`DcsBackground`]) | Meaning                                                                 |
181/// |------------------------|-------------------------------------------------------------------------|
182/// | **0** or 2 (default)   | Pixel positions specified as 0 are set to the current background color. |
183/// | 1                      | Pixel positions specified as 0 remain at their current color.           |
184#[repr(u8)]
185#[derive(Clone, Copy, Debug, Default, PartialEq)]
186pub enum DcsBackground {
187    Background = 0,
188    Current = 1,
189    BackgroundAlt = 2,
190    #[default]
191    None = 0xff,
192}
193
194impl DcsBackground {
195    /// Creates a new [DcsBackground] selector.
196    pub const fn new() -> Self {
197        Self::None
198    }
199}
200
201impl From<u8> for DcsBackground {
202    fn from(val: u8) -> Self {
203        match val {
204            0 => Self::Background,
205            1 => Self::Current,
206            2 => Self::BackgroundAlt,
207            _ => Self::None,
208        }
209    }
210}
211
212impl From<DcsBackground> for u8 {
213    fn from(val: DcsBackground) -> Self {
214        val as u8
215    }
216}
217
218impl From<&DcsBackground> for u8 {
219    fn from(val: &DcsBackground) -> Self {
220        (*val).into()
221    }
222}
223
224impl From<DcsBackground> for char {
225    fn from(val: DcsBackground) -> Self {
226        (val as u8) as char
227    }
228}
229
230impl From<&DcsBackground> for char {
231    fn from(val: &DcsBackground) -> Self {
232        (*val).into()
233    }
234}
235
236impl fmt::Display for DcsBackground {
237    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
238        write!(f, "{}", char::from(self))
239    }
240}
241
242/// Represents the height-to-width pixel ratio.
243///
244/// | P1          | Pixel Aspect Ratio (Vertical:Horizontal) |
245/// |-------------|------------------------------------------|
246/// | **Omitted** | **2:1** (default)                        |
247/// | 0, 1        | 2:1                                      |
248/// | 2           | 5:1                                      |
249/// | 3, 4        | 3:1                                      |
250/// | 5, 6        | 2:1                                      |
251/// | 7, 8, 9     | 1:1                                      |
252#[repr(u8)]
253#[derive(Clone, Copy, Debug, Default, PartialEq)]
254pub enum PixelAspectRatio {
255    TwoOneA = 0,
256    TwoOneB = 1,
257    TwoOneC = 5,
258    TwoOneD = 6,
259    FiveOne = 2,
260    ThreeOneA = 3,
261    ThreeOneB = 4,
262    OneOneA = 7,
263    OneOneB = 8,
264    OneOneC = 9,
265    #[default]
266    None = 0xff,
267}
268
269impl PixelAspectRatio {
270    /// Creates a new [PixelAspectRatio] selector.
271    pub const fn new() -> Self {
272        Self::None
273    }
274}
275
276impl From<PixelAspectRatio> for u8 {
277    fn from(val: PixelAspectRatio) -> Self {
278        val as u8
279    }
280}
281
282impl From<&PixelAspectRatio> for u8 {
283    fn from(val: &PixelAspectRatio) -> Self {
284        (*val).into()
285    }
286}
287
288impl From<PixelAspectRatio> for char {
289    fn from(val: PixelAspectRatio) -> Self {
290        (val as u8) as char
291    }
292}
293
294impl From<&PixelAspectRatio> for char {
295    fn from(val: &PixelAspectRatio) -> Self {
296        (*val).into()
297    }
298}
299
300impl fmt::Display for PixelAspectRatio {
301    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
302        write!(f, "{}", char::from(self))
303    }
304}