aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe-external-packages/src/symfony/console/helper/table_style.rs
blob: 5820f15ae608d6981464fa5963711c6bf6dc720c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
use crate::symfony::console::exception::invalid_argument_exception::InvalidArgumentException;
use crate::symfony::console::exception::logic_exception::LogicException;

/// Defines the styles for a Table.
#[derive(Debug)]
pub struct TableStyle {
    padding_char: String,
    horizontal_outside_border_char: String,
    horizontal_inside_border_char: String,
    vertical_outside_border_char: String,
    vertical_inside_border_char: String,
    crossing_char: String,
    crossing_top_right_char: String,
    crossing_top_mid_char: String,
    crossing_top_left_char: String,
    crossing_mid_right_char: String,
    crossing_bottom_right_char: String,
    crossing_bottom_mid_char: String,
    crossing_bottom_left_char: String,
    crossing_mid_left_char: String,
    crossing_top_left_bottom_char: String,
    crossing_top_mid_bottom_char: String,
    crossing_top_right_bottom_char: String,
    header_title_format: String,
    footer_title_format: String,
    cell_header_format: String,
    cell_row_format: String,
    cell_row_content_format: String,
    border_format: String,
    pad_type: i64,
}

impl Default for TableStyle {
    fn default() -> Self {
        Self {
            padding_char: " ".to_string(),
            horizontal_outside_border_char: "-".to_string(),
            horizontal_inside_border_char: "-".to_string(),
            vertical_outside_border_char: "|".to_string(),
            vertical_inside_border_char: "|".to_string(),
            crossing_char: "+".to_string(),
            crossing_top_right_char: "+".to_string(),
            crossing_top_mid_char: "+".to_string(),
            crossing_top_left_char: "+".to_string(),
            crossing_mid_right_char: "+".to_string(),
            crossing_bottom_right_char: "+".to_string(),
            crossing_bottom_mid_char: "+".to_string(),
            crossing_bottom_left_char: "+".to_string(),
            crossing_mid_left_char: "+".to_string(),
            crossing_top_left_bottom_char: "+".to_string(),
            crossing_top_mid_bottom_char: "+".to_string(),
            crossing_top_right_bottom_char: "+".to_string(),
            header_title_format: "<fg=black;bg=white;options=bold> %s </>".to_string(),
            footer_title_format: "<fg=black;bg=white;options=bold> %s </>".to_string(),
            cell_header_format: "<info>%s</info>".to_string(),
            cell_row_format: "%s".to_string(),
            cell_row_content_format: " %s ".to_string(),
            border_format: "%s".to_string(),
            pad_type: shirabe_php_shim::STR_PAD_RIGHT,
        }
    }
}

impl TableStyle {
    /// Sets padding character, used for cell padding.
    pub fn set_padding_char(
        &mut self,
        padding_char: String,
    ) -> anyhow::Result<Result<&mut Self, LogicException>> {
        if padding_char.is_empty() {
            return Ok(Err(LogicException(shirabe_php_shim::LogicException {
                message: "The padding char must not be empty.".to_string(),
                code: 0,
            })));
        }

        self.padding_char = padding_char;

        Ok(Ok(self))
    }

    /// Gets padding character, used for cell padding.
    pub fn get_padding_char(&self) -> String {
        self.padding_char.clone()
    }

    /// Sets horizontal border characters.
    pub fn set_horizontal_border_chars(
        &mut self,
        outside: String,
        inside: Option<String>,
    ) -> &mut Self {
        self.horizontal_outside_border_char = outside.clone();
        self.horizontal_inside_border_char = inside.unwrap_or(outside);

        self
    }

    /// Sets vertical border characters.
    pub fn set_vertical_border_chars(
        &mut self,
        outside: String,
        inside: Option<String>,
    ) -> &mut Self {
        self.vertical_outside_border_char = outside.clone();
        self.vertical_inside_border_char = inside.unwrap_or(outside);

        self
    }

    /// Gets border characters.
    pub fn get_border_chars(&self) -> Vec<String> {
        vec![
            self.horizontal_outside_border_char.clone(),
            self.vertical_outside_border_char.clone(),
            self.horizontal_inside_border_char.clone(),
            self.vertical_inside_border_char.clone(),
        ]
    }

    /// Sets crossing characters.
    #[allow(clippy::too_many_arguments)]
    pub fn set_crossing_chars(
        &mut self,
        cross: String,
        top_left: String,
        top_mid: String,
        top_right: String,
        mid_right: String,
        bottom_right: String,
        bottom_mid: String,
        bottom_left: String,
        mid_left: String,
        top_left_bottom: Option<String>,
        top_mid_bottom: Option<String>,
        top_right_bottom: Option<String>,
    ) -> &mut Self {
        self.crossing_char = cross.clone();
        self.crossing_top_left_char = top_left;
        self.crossing_top_mid_char = top_mid;
        self.crossing_top_right_char = top_right;
        self.crossing_mid_right_char = mid_right.clone();
        self.crossing_bottom_right_char = bottom_right;
        self.crossing_bottom_mid_char = bottom_mid;
        self.crossing_bottom_left_char = bottom_left;
        self.crossing_mid_left_char = mid_left.clone();
        self.crossing_top_left_bottom_char = top_left_bottom.unwrap_or(mid_left);
        self.crossing_top_mid_bottom_char = top_mid_bottom.unwrap_or(cross);
        self.crossing_top_right_bottom_char = top_right_bottom.unwrap_or(mid_right);

        self
    }

    /// Sets default crossing character used for each cross.
    pub fn set_default_crossing_char(&mut self, char: String) -> &mut Self {
        self.set_crossing_chars(
            char.clone(),
            char.clone(),
            char.clone(),
            char.clone(),
            char.clone(),
            char.clone(),
            char.clone(),
            char.clone(),
            char,
            None,
            None,
            None,
        )
    }

    /// Gets crossing character.
    pub fn get_crossing_char(&self) -> String {
        self.crossing_char.clone()
    }

    /// Gets crossing characters.
    pub fn get_crossing_chars(&self) -> Vec<String> {
        vec![
            self.crossing_char.clone(),
            self.crossing_top_left_char.clone(),
            self.crossing_top_mid_char.clone(),
            self.crossing_top_right_char.clone(),
            self.crossing_mid_right_char.clone(),
            self.crossing_bottom_right_char.clone(),
            self.crossing_bottom_mid_char.clone(),
            self.crossing_bottom_left_char.clone(),
            self.crossing_mid_left_char.clone(),
            self.crossing_top_left_bottom_char.clone(),
            self.crossing_top_mid_bottom_char.clone(),
            self.crossing_top_right_bottom_char.clone(),
        ]
    }

    /// Sets header cell format.
    pub fn set_cell_header_format(&mut self, cell_header_format: String) -> &mut Self {
        self.cell_header_format = cell_header_format;

        self
    }

    /// Gets header cell format.
    pub fn get_cell_header_format(&self) -> String {
        self.cell_header_format.clone()
    }

    /// Sets row cell format.
    pub fn set_cell_row_format(&mut self, cell_row_format: String) -> &mut Self {
        self.cell_row_format = cell_row_format;

        self
    }

    /// Gets row cell format.
    pub fn get_cell_row_format(&self) -> String {
        self.cell_row_format.clone()
    }

    /// Sets row cell content format.
    pub fn set_cell_row_content_format(&mut self, cell_row_content_format: String) -> &mut Self {
        self.cell_row_content_format = cell_row_content_format;

        self
    }

    /// Gets row cell content format.
    pub fn get_cell_row_content_format(&self) -> String {
        self.cell_row_content_format.clone()
    }

    /// Sets table border format.
    pub fn set_border_format(&mut self, border_format: String) -> &mut Self {
        self.border_format = border_format;

        self
    }

    /// Gets table border format.
    pub fn get_border_format(&self) -> String {
        self.border_format.clone()
    }

    /// Sets cell padding type.
    pub fn set_pad_type(
        &mut self,
        pad_type: i64,
    ) -> anyhow::Result<Result<&mut Self, InvalidArgumentException>> {
        if ![
            shirabe_php_shim::STR_PAD_LEFT,
            shirabe_php_shim::STR_PAD_RIGHT,
            shirabe_php_shim::STR_PAD_BOTH,
        ]
        .contains(&pad_type)
        {
            return Ok(Err(InvalidArgumentException(
                shirabe_php_shim::InvalidArgumentException {
                    message: "Invalid padding type. Expected one of (STR_PAD_LEFT, STR_PAD_RIGHT, STR_PAD_BOTH)."
                        .to_string(),
                    code: 0,
                },
            )));
        }

        self.pad_type = pad_type;

        Ok(Ok(self))
    }

    /// Gets cell padding type.
    pub fn get_pad_type(&self) -> i64 {
        self.pad_type
    }

    pub fn get_header_title_format(&self) -> String {
        self.header_title_format.clone()
    }

    pub fn set_header_title_format(&mut self, format: String) -> &mut Self {
        self.header_title_format = format;

        self
    }

    pub fn get_footer_title_format(&self) -> String {
        self.footer_title_format.clone()
    }

    pub fn set_footer_title_format(&mut self, format: String) -> &mut Self {
        self.footer_title_format = format;

        self
    }
}