libsixel_rs/device_control_string/
repeat.rs

1use super::SixelChar;
2use crate::std::fmt;
3
4/// Graphics repeat introducer sequence.
5///
6/// The `! (2/1)` character introduces a repeat sequence. A repeat sequence lets you repeat a graphic character a specified number of times. You use the following format for the repeat sequence.
7///
8/// | !    | Pn   | character |
9/// |------|------|-----------|
10/// | 2/1  | `**` | `****`    |
11///
12/// where:
13///
14/// - **Pn** is the repeat count. The repeat count can be any decimal value. For example, if you use a repeat count of 23, the next character repeats 23 times.
15/// - **character** is the character to repeat. You can use any character in the range of `? (hex 3F)` to `~ (hex 7E)`.
16#[repr(C)]
17#[derive(Clone, Copy, Debug, Default, PartialEq)]
18pub struct Repeat {
19    number: usize,
20    character: SixelChar,
21}
22
23impl Repeat {
24    /// Creates a new [Repeat].
25    pub const fn new() -> Self {
26        Self {
27            number: 0,
28            character: SixelChar::new(),
29        }
30    }
31
32    /// Creates a new [Repeat] with the provided parameters.
33    pub const fn create(number: usize, character: SixelChar) -> Self {
34        Self { number, character }
35    }
36
37    /// Gets the number of [SixelChar]s to repeat.
38    pub const fn number(&self) -> usize {
39        self.number
40    }
41
42    /// Sets the number of [SixelChar]s to repeat.
43    pub fn set_number(&mut self, val: usize) {
44        self.number = val;
45    }
46
47    /// Builder function that sets the number of [SixelChar]s to repeat.
48    pub fn with_number(mut self, val: usize) -> Self {
49        self.set_number(val);
50        self
51    }
52
53    /// Gets the [SixelChar] to repeat.
54    pub const fn character(&self) -> SixelChar {
55        self.character
56    }
57
58    /// Sets the [SixelChar] to repeat.
59    pub fn set_character(&mut self, val: SixelChar) {
60        self.character = val;
61    }
62
63    /// Builder function that sets the [SixelChar] to repeat.
64    pub fn with_character(mut self, val: SixelChar) -> Self {
65        self.set_character(val);
66        self
67    }
68}
69
70impl fmt::Display for Repeat {
71    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72        write!(f, "!{}{}", self.number, self.character)
73    }
74}