tabled/features/height/
cell_height_limit.rs

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
use papergrid::{
    records::{Records, RecordsMut},
    util::get_lines,
    width::CfgWidthFunction,
    Entity,
};

use crate::{measurment::Measurment, peaker::Peaker, CellOption, Height, Table, TableOption};

use super::table_height_limit::TableHeightLimit;

/// A modification for cell/table to increase its height.
///
/// If used for a [`Table`] [`PriorityNone`] is used.
///
/// [`PriorityNone`]: crate::peaker::PriorityNone
#[derive(Debug)]
pub struct CellHeightLimit<W = usize> {
    height: W,
}

impl<W> CellHeightLimit<W> {
    /// Constructs a new object.
    pub fn new(height: W) -> Self
    where
        W: Measurment<Height>,
    {
        Self { height }
    }

    /// Set's a priority by which the limit logic will be applied.
    pub fn priority<P>(self) -> TableHeightLimit<W, P>
    where
        P: Peaker,
        W: Measurment<Height>,
    {
        TableHeightLimit::new(self.height).priority::<P>()
    }
}

impl<W, R> CellOption<R> for CellHeightLimit<W>
where
    W: Measurment<Height>,
    R: Records + RecordsMut<String>,
{
    fn change_cell(&mut self, table: &mut Table<R>, entity: Entity) {
        let height = self.height.measure(table.get_records(), table.get_config());

        let (count_rows, count_cols) = table.shape();
        for pos in entity.iter(count_rows, count_cols) {
            let records = table.get_records();
            let cell_height = records.count_lines(pos);
            if cell_height <= height {
                continue;
            }

            let content = records.get_text(pos);
            let content = limit_lines(content, height);
            let ctrl = CfgWidthFunction::from_cfg(table.get_config());
            table.get_records_mut().set(pos, content, &ctrl);
        }

        table.destroy_height_cache();
        table.destroy_width_cache();
    }
}

impl<W, R> TableOption<R> for CellHeightLimit<W>
where
    W: Measurment<Height>,
    R: Records,
{
    fn change(&mut self, table: &mut Table<R>) {
        TableHeightLimit::new(self.height.measure(table.get_records(), table.get_config()))
            .change(table)
    }
}

fn limit_lines(s: &str, n: usize) -> String {
    let mut text = String::new();
    for (i, line) in get_lines(s).take(n).enumerate() {
        if i > 0 {
            text.push('\n');
        }

        text.push_str(&line);
    }

    text
}