3 config
::{ColoredConfig, Entity}
,
4 dimension
::CompleteDimensionVecRecords
,
5 records
::{ExactRecords, PeekableRecords, Records, RecordsMut}
,
6 util
::string
::{count_lines, get_lines}
,
8 settings
::{measurement::Measurement, peaker::Peaker, CellOption, Height, TableOption}
,
11 use super::table_height_limit
::TableHeightLimit
;
13 /// A modification for cell/table to increase its height.
15 /// If used for a [`Table`] [`PriorityNone`] is used.
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> {
24 impl<W
> CellHeightLimit
<W
> {
25 /// Constructs a new object.
26 pub fn new(height
: W
) -> Self
28 W
: Measurement
<Height
>,
33 /// Set's a priority by which the limit logic will be applied.
34 pub fn priority
<P
>(self) -> TableHeightLimit
<W
, P
>
37 W
: Measurement
<Height
>,
39 TableHeightLimit
::new(self.height
).priority
::<P
>()
43 impl<W
, R
> CellOption
<R
, ColoredConfig
> for CellHeightLimit
<W
>
45 W
: Measurement
<Height
>,
46 R
: Records
+ ExactRecords
+ PeekableRecords
+ RecordsMut
<String
>,
47 for<'a
> &'a R
: Records
,
49 fn change(self, records
: &mut R
, cfg
: &mut ColoredConfig
, entity
: Entity
) {
50 let height
= self.height
.measure(&*records
, cfg
);
52 let count_rows
= records
.count_rows();
53 let count_columns
= records
.count_columns();
55 for pos
in entity
.iter(count_rows
, count_columns
) {
56 let is_valid_pos
= pos
.0 < count_rows
&& pos
.1 < count_columns
;
61 let text
= records
.get_text(pos
);
62 let count_lines
= count_lines(text
);
64 if count_lines
<= height
{
68 let content
= limit_lines(text
, height
);
69 records
.set(pos
, content
);
74 impl<R
, W
> TableOption
<R
, CompleteDimensionVecRecords
<'
static>, ColoredConfig
>
75 for CellHeightLimit
<W
>
77 W
: Measurement
<Height
>,
78 R
: Records
+ ExactRecords
+ PeekableRecords
+ RecordsMut
<String
>,
79 for<'a
> &'a R
: Records
,
84 cfg
: &mut ColoredConfig
,
85 dims
: &mut CompleteDimensionVecRecords
<'
static>,
87 let height
= self.height
.measure(&*records
, cfg
);
88 TableHeightLimit
::new(height
).change(records
, cfg
, dims
)
92 fn limit_lines(s
: &str, n
: usize) -> String
{
93 let mut text
= String
::new();
94 for (i
, line
) in get_lines(s
).take(n
).enumerate() {