]> git.proxmox.com Git - rustc.git/blob - vendor/tabled/src/settings/height/cell_height_limit.rs
New upstream version 1.75.0+dfsg1
[rustc.git] / vendor / tabled / src / settings / height / cell_height_limit.rs
1 use crate::{
2 grid::{
3 config::{ColoredConfig, Entity},
4 dimension::CompleteDimensionVecRecords,
5 records::{ExactRecords, 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 {
49 fn change(self, records: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
50 let height = self.height.measure(&*records, cfg);
51
52 let count_rows = records.count_rows();
53 let count_columns = records.count_columns();
54
55 for pos in entity.iter(count_rows, count_columns) {
56 let is_valid_pos = pos.0 < count_rows && pos.1 < count_columns;
57 if !is_valid_pos {
58 continue;
59 }
60
61 let text = records.get_text(pos);
62 let count_lines = count_lines(text);
63
64 if count_lines <= height {
65 continue;
66 }
67
68 let content = limit_lines(text, height);
69 records.set(pos, content);
70 }
71 }
72 }
73
74 impl<R, W> TableOption<R, CompleteDimensionVecRecords<'static>, ColoredConfig>
75 for CellHeightLimit<W>
76 where
77 W: Measurement<Height>,
78 R: Records + ExactRecords + PeekableRecords + RecordsMut<String>,
79 for<'a> &'a R: Records,
80 {
81 fn change(
82 self,
83 records: &mut R,
84 cfg: &mut ColoredConfig,
85 dims: &mut CompleteDimensionVecRecords<'static>,
86 ) {
87 let height = self.height.measure(&*records, cfg);
88 TableHeightLimit::new(height).change(records, cfg, dims)
89 }
90 }
91
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() {
95 if i > 0 {
96 text.push('\n');
97 }
98
99 text.push_str(&line);
100 }
101
102 text
103 }