1 | use crate::{ |
2 | grid::{ |
3 | config::ColoredConfig, |
4 | dimension::CompleteDimensionVecRecords, |
5 | records::{ExactRecords, PeekableRecords, Records}, |
6 | }, |
7 | settings::{ |
8 | measurement::Measurement, |
9 | peaker::{Peaker, PriorityNone}, |
10 | Height, TableOption, |
11 | }, |
12 | }; |
13 | |
14 | use super::util::get_table_height; |
15 | |
16 | /// A modification of a table to increase the table height. |
17 | #[derive (Debug, Clone)] |
18 | pub struct TableHeightIncrease<W = usize, P = PriorityNone> { |
19 | height: W, |
20 | priority: P, |
21 | } |
22 | |
23 | impl<W> TableHeightIncrease<W, PriorityNone> { |
24 | /// Creates a new object. |
25 | pub fn new(height: W) -> Self |
26 | where |
27 | W: Measurement<Height>, |
28 | { |
29 | Self { |
30 | height, |
31 | priority: PriorityNone::default(), |
32 | } |
33 | } |
34 | |
35 | /// Sets a different priority logic. |
36 | pub fn priority<P>(self) -> TableHeightIncrease<W, P> |
37 | where |
38 | P: Peaker, |
39 | { |
40 | TableHeightIncrease { |
41 | priority: P::create(), |
42 | height: self.height, |
43 | } |
44 | } |
45 | } |
46 | |
47 | impl<R, W, P> TableOption<R, CompleteDimensionVecRecords<'static>, ColoredConfig> |
48 | for TableHeightIncrease<W, P> |
49 | where |
50 | W: Measurement<Height>, |
51 | P: Peaker + Clone, |
52 | R: Records + ExactRecords + PeekableRecords, |
53 | for<'a> &'a R: Records, |
54 | { |
55 | fn change( |
56 | self, |
57 | records: &mut R, |
58 | cfg: &mut ColoredConfig, |
59 | dims: &mut CompleteDimensionVecRecords<'static>, |
60 | ) { |
61 | if records.count_rows() == 0 || records.count_columns() == 0 { |
62 | return; |
63 | } |
64 | |
65 | let height: usize = self.height.measure(&*records, cfg); |
66 | let (total: usize, mut heights: Vec) = get_table_height(&*records, cfg); |
67 | if total >= height { |
68 | return; |
69 | } |
70 | |
71 | get_increase_list(&mut heights, total:height, current:total, self.priority); |
72 | |
73 | let _ = dims.set_heights(rows:heights); |
74 | } |
75 | } |
76 | |
77 | fn get_increase_list<P>(list: &mut [usize], total: usize, mut current: usize, mut peaker: P) |
78 | where |
79 | P: Peaker, |
80 | { |
81 | while current != total { |
82 | let col: usize = match peaker.peak(&[], widths:list) { |
83 | Some(col: usize) => col, |
84 | None => break, |
85 | }; |
86 | |
87 | list[col] += 1; |
88 | current += 1; |
89 | } |
90 | } |
91 | |