1 | use crate::{ |
2 | grid::{ |
3 | config::{ColoredConfig, Entity}, |
4 | dimension::CompleteDimensionVecRecords, |
5 | records::{ExactRecords, IntoRecords, PeekableRecords, Records, RecordsMut}, |
6 | util::string::{count_lines, get_lines}, |
7 | }, |
8 | settings::{measurement::Measurement, peaker::Peaker, CellOption, Height, TableOption}, |
9 | }; |
10 | |
11 | use super::table_height_limit::TableHeightLimit; |
12 | |
13 | /// A modification for cell/table to increase its height. |
14 | /// |
15 | /// If used for a [`Table`] [`PriorityNone`] is used. |
16 | /// |
17 | /// [`PriorityNone`]: crate::settings::peaker::PriorityNone |
18 | /// [`Table`]: crate::Table |
19 | #[derive (Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] |
20 | pub struct CellHeightLimit<W = usize> { |
21 | height: W, |
22 | } |
23 | |
24 | impl<W> CellHeightLimit<W> { |
25 | /// Constructs a new object. |
26 | pub fn new(height: W) -> Self |
27 | where |
28 | W: Measurement<Height>, |
29 | { |
30 | Self { height } |
31 | } |
32 | |
33 | /// Set's a priority by which the limit logic will be applied. |
34 | pub fn priority<P>(self) -> TableHeightLimit<W, P> |
35 | where |
36 | P: Peaker, |
37 | W: Measurement<Height>, |
38 | { |
39 | TableHeightLimit::new(self.height).priority::<P>() |
40 | } |
41 | } |
42 | |
43 | impl<W, R> CellOption<R, ColoredConfig> for CellHeightLimit<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 | let count_lines = count_lines(text); |
64 | |
65 | if count_lines <= height { |
66 | continue; |
67 | } |
68 | |
69 | let content = limit_lines(text, height); |
70 | records.set(pos, content); |
71 | } |
72 | } |
73 | } |
74 | |
75 | impl<R, W> TableOption<R, ColoredConfig, CompleteDimensionVecRecords<'_>> for CellHeightLimit<W> |
76 | where |
77 | W: Measurement<Height>, |
78 | R: Records + ExactRecords + PeekableRecords + RecordsMut<String>, |
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 | TableHeightLimit::new(height).change(records, cfg, dimension:dims) |
90 | } |
91 | } |
92 | |
93 | fn limit_lines(s: &str, n: usize) -> String { |
94 | let mut text: String = String::new(); |
95 | for (i: usize, line: Cow<'_, str>) in get_lines(text:s).take(n).enumerate() { |
96 | if i > 0 { |
97 | text.push(ch:' \n' ); |
98 | } |
99 | |
100 | text.push_str(&line); |
101 | } |
102 | |
103 | text |
104 | } |
105 | |