1 | use crate::{ |
2 | grid::config::ColoredConfig, |
3 | grid::config::Entity, |
4 | grid::dimension::CompleteDimensionVecRecords, |
5 | grid::records::{ExactRecords, IntoRecords, PeekableRecords, Records, RecordsMut}, |
6 | grid::util::string::count_lines, |
7 | settings::{measurement::Measurement, peaker::Peaker, CellOption, Height, TableOption}, |
8 | }; |
9 | |
10 | use super::TableHeightIncrease; |
11 | |
12 | /// A modification for cell/table to increase its height. |
13 | /// |
14 | /// If used for a [`Table`] [`PriorityNone`] is used. |
15 | /// |
16 | /// [`PriorityNone`]: crate::settings::peaker::PriorityNone |
17 | /// [`Table`]: crate::Table |
18 | #[derive (Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
19 | pub struct CellHeightIncrease<W = usize> { |
20 | height: W, |
21 | } |
22 | |
23 | impl<W> CellHeightIncrease<W> { |
24 | /// Creates a new object of the structure. |
25 | pub fn new(height: W) -> Self |
26 | where |
27 | W: Measurement<Height>, |
28 | { |
29 | Self { height } |
30 | } |
31 | |
32 | /// The priority makes scence only for table, so the function |
33 | /// converts it to [`TableHeightIncrease`] with a given priority. |
34 | pub fn priority<P>(self) -> TableHeightIncrease<W, P> |
35 | where |
36 | P: Peaker, |
37 | W: Measurement<Height>, |
38 | { |
39 | TableHeightIncrease::new(self.height).priority::<P>() |
40 | } |
41 | } |
42 | |
43 | impl<W, R> CellOption<R, ColoredConfig> for CellHeightIncrease<W> |
44 | where |
45 | W: Measurement<Height>, |
46 | R: Records + ExactRecords + PeekableRecords + RecordsMut<String>, |
47 | for<'a> &'a R: Records, |
48 | for<'a> <<&'a R as Records>::Iter as IntoRecords>::Cell: AsRef<str>, |
49 | { |
50 | fn change(self, records: &mut R, cfg: &mut ColoredConfig, entity: Entity) { |
51 | let height = self.height.measure(&*records, cfg); |
52 | |
53 | let count_rows = records.count_rows(); |
54 | let count_columns = records.count_columns(); |
55 | |
56 | for pos in entity.iter(count_rows, count_columns) { |
57 | let is_valid_pos = pos.0 < count_rows && pos.1 < count_columns; |
58 | if !is_valid_pos { |
59 | continue; |
60 | } |
61 | |
62 | let text = records.get_text(pos); |
63 | |
64 | let cell_height = count_lines(text); |
65 | if cell_height >= height { |
66 | continue; |
67 | } |
68 | |
69 | let content = add_lines(text, height - cell_height); |
70 | records.set(pos, content); |
71 | } |
72 | } |
73 | } |
74 | |
75 | impl<R, W> TableOption<R, ColoredConfig, CompleteDimensionVecRecords<'_>> for CellHeightIncrease<W> |
76 | where |
77 | W: Measurement<Height>, |
78 | R: Records + ExactRecords + PeekableRecords, |
79 | for<'a> &'a R: Records, |
80 | for<'a> <<&'a R as Records>::Iter as IntoRecords>::Cell: AsRef<str>, |
81 | { |
82 | fn change( |
83 | self, |
84 | records: &mut R, |
85 | cfg: &mut ColoredConfig, |
86 | dims: &mut CompleteDimensionVecRecords<'_>, |
87 | ) { |
88 | let height: usize = self.height.measure(&*records, cfg); |
89 | TableHeightIncrease::new(height).change(records, cfg, dimension:dims) |
90 | } |
91 | |
92 | fn hint_change(&self) -> Option<Entity> { |
93 | Some(Entity::Row(0)) |
94 | } |
95 | } |
96 | |
97 | fn add_lines(s: &str, n: usize) -> String { |
98 | let mut text: String = String::with_capacity(s.len() + n); |
99 | text.push_str(string:s); |
100 | text.extend(iter:std::iter::repeat(elt:' \n' ).take(n)); |
101 | |
102 | text |
103 | } |
104 | |