Debian Codegraph @

src/utils/arrangement/constraint.rs

raw symbols SCIP
1 use super::{ColumnDisplayInfo, DisplayInfos, helper::*};
2 use crate::{
3 Column, Table,
4 style::{ColumnConstraint, ColumnConstraint::*, Width},
5 };
6
7 /// Look at given constraints of a column and check if some of them can be resolved at the very
8 /// beginning.
9 ///
10 /// For example:
11 /// - We get an absolute width.
12 /// - MinWidth constraints on columns, whose content is garantueed to be smaller than the specified
13 /// minimal width.
14 /// - The Column is supposed to be hidden.
15 pub fn evaluate(
16 table: &Table,
17 visible_columns: usize,
18 infos: &mut DisplayInfos,
19 column: &Column,
20 max_content_width: u16,
21 ) {
22 match &column.constraint {
23 Some(ContentWidth) => {
24 let info = ColumnDisplayInfo::new(column, max_content_width);
25 infos.insert(column.index, info);
26 }
27 Some(Absolute(width)) => {
28 if let Some(width) = absolute_value_from_width(table, width, visible_columns) {
29 // The column should get always get a fixed width.
30 let width = absolute_width_with_padding(column, width);
31 let info = ColumnDisplayInfo::new(column, width);
32 infos.insert(column.index, info);
33 }
34 }
35 Some(Hidden) => {
36 let mut info = ColumnDisplayInfo::new(column, max_content_width);
37 info.is_hidden = true;
38 infos.insert(column.index, info);
39 }
40 _ => {}
41 }
42
43 if let Some(min_width) = min(table, &column.constraint, visible_columns) {
44 // In case a min_width is specified, we may already fix the size of the column.
45 // We do this, if we know that the content is smaller than the min size.
46 let max_width = max_content_width + column.padding_width();
47 if max_width <= min_width {
48 let width = absolute_width_with_padding(column, min_width);
49 let info = ColumnDisplayInfo::new(column, width);
50 infos.insert(column.index, info);
51 }
52 }
53 }
54
55 /// A little wrapper, which resolves possible lower boundary constraints to their actual value for
56 /// the current table and terminal width.
57 ///
58 /// This returns the value of absolute characters that are allowed to be in this column. \
59 /// Lower boundaries with [Width::Fixed] just return their internal value. \
60 /// Lower boundaries with [Width::Percentage] return the percental amount of the current table
61 /// width.
62 pub fn min(
63 table: &Table,
64 constraint: &Option<ColumnConstraint>,
65 visible_columns: usize,
66 ) -> Option<u16> {
67 let constraint = if let Some(constraint) = constraint {
68 constraint
69 } else {
70 return None;
71 };
72
73 match constraint {
74 LowerBoundary(width) | Boundaries { lower: width, .. } => {
75 absolute_value_from_width(table, width, visible_columns)
76 }
77 _ => None,
78 }
79 }
80
81 /// A little wrapper, which resolves possible upper boundary constraints to their actual value for
82 /// the current table and terminal width.
83 ///
84 /// This returns the value of absolute characters that are allowed to be in this column. \
85 /// Upper boundaries with [Width::Fixed] just return their internal value. \
86 /// Upper boundaries with [Width::Percentage] return the percental amount of the current table
87 /// width.
88 pub fn max(
89 table: &Table,
90 constraint: &Option<ColumnConstraint>,
91 visible_columns: usize,
92 ) -> Option<u16> {
93 let constraint = if let Some(constraint) = constraint {
94 constraint
95 } else {
96 return None;
97 };
98
99 match constraint {
100 UpperBoundary(width) | Boundaries { upper: width, .. } => {
101 absolute_value_from_width(table, width, visible_columns)
102 }
103 _ => None,
104 }
105 }
106
107 /// Resolve an absolute value from a given boundary
108 pub fn absolute_value_from_width(
109 table: &Table,
110 width: &Width,
111 visible_columns: usize,
112 ) -> Option<u16> {
113 match width {
114 Width::Fixed(width) => Some(*width),
115 Width::Percentage(percent) => {
116 // Don't return a value, if we cannot determine the current table width.
117 let table_width = table.width().map(usize::from)?;
118
119 // Enforce at most 100%
120 let percent = std::cmp::min(*percent, 100u16);
121
122 // Subtract the borders from the table width.
123 let width = table_width.saturating_sub(count_border_columns(table, visible_columns));
124
125 // Calculate the absolute value in actual columns.
126 let width = (width * usize::from(percent) / 100)
127 .try_into()
128 .unwrap_or(u16::MAX);
129 Some(width)
130 }
131 }
132 }