1 | use crate::{ |
2 | grid::records::{Records, RecordsMut}, |
3 | settings::TableOption, |
4 | }; |
5 | |
6 | /// Set a tab size. |
7 | /// |
8 | /// The size is used in order to calculate width correctly. |
9 | /// |
10 | /// Default value is 4 (basically 1 '\t' equals 4 spaces). |
11 | /// |
12 | /// IMPORTANT: The tab character might be not present in output, |
13 | /// it might be replaced by spaces. |
14 | /// |
15 | /// # Example |
16 | /// |
17 | /// ``` |
18 | /// use tabled::{Table, settings::formatting::TabSize}; |
19 | /// |
20 | /// let text = "Some \ttext \t\twith \\tabs" ; |
21 | /// |
22 | /// let mut table = Table::new([text]); |
23 | /// table.with(TabSize::new(4)); |
24 | /// |
25 | /// assert_eq!( |
26 | /// table.to_string(), |
27 | /// "+--------------------------------+ \n\ |
28 | /// | &str | \n\ |
29 | /// +--------------------------------+ \n\ |
30 | /// | Some text with \\tabs | \n\ |
31 | /// +--------------------------------+" |
32 | /// ) |
33 | /// ``` |
34 | #[derive (Debug, Default, Clone)] |
35 | pub struct TabSize(usize); |
36 | |
37 | impl TabSize { |
38 | /// Creates new [`TabSize`] object. |
39 | pub fn new(size: usize) -> Self { |
40 | Self(size) |
41 | } |
42 | } |
43 | |
44 | impl<R, D, C> TableOption<R, D, C> for TabSize |
45 | where |
46 | for<'a> &'a R: Records, |
47 | R: RecordsMut<String>, |
48 | { |
49 | fn change(self, records: &mut R, _: &mut C, _: &mut D) { |
50 | let mut list: Vec<(({unknown}, {unknown}), …)> = vec![]; |
51 | for (row, cells) in records.iter_rows().into_iter().enumerate() { |
52 | for (col, text) in cells.into_iter().enumerate() { |
53 | let text = text.as_ref().replace(' \t' , &" " .repeat(self.0)); |
54 | list.push(((row, col), text)); |
55 | } |
56 | } |
57 | |
58 | for (pos: (usize, usize), text: String) in list { |
59 | records.set(pos, text); |
60 | } |
61 | } |
62 | } |
63 | |