| 1 | use superrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/::{ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#, DisplayInfosrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/DisplayInfos#, helperrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/helper/::*}; |
| 2 | use craterust-analyzer cargo comfy-table 7.2.2 crate/::{ |
| 3 | Columnrust-analyzer cargo comfy-table 7.2.2 column/Column#, Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#, |
| 4 | stylerust-analyzer cargo comfy-table 7.2.2 style/::{ColumnConstraintrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#, ColumnConstraintrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#::*, Widthrust-analyzer cargo comfy-table 7.2.2 style/column/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 evaluaterust-analyzer cargo comfy-table 7.2.2 utils/arrangement/constraint/evaluate().( |
| 16 | table: &Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#, |
| 17 | visible_columns: usize, |
| 18 | infos: &mut DisplayInfosrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/DisplayInfos#, |
| 19 | column: &Columnrust-analyzer cargo comfy-table 7.2.2 column/Column#, |
| 20 | max_content_width: u16, |
| 21 | ) { |
| 22 | match &column.constraintrust-analyzer cargo comfy-table 7.2.2 column/Column#constraint. { |
| 23 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(ContentWidthrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#ContentWidth#) => { |
| 24 | let info = ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#::newrust-analyzer cargo comfy-table 7.2.2 utils/impl#[ColumnDisplayInfo]new().(column, max_content_width); |
| 25 | infos.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]insert().(column.indexrust-analyzer cargo comfy-table 7.2.2 column/Column#index., info); |
| 26 | } |
| 27 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Absoluterust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#Absolute#(width)) => { |
| 28 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(width) = absolute_value_from_widthrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/constraint/absolute_value_from_width().(table, width, visible_columns) { |
| 29 | // The column should get always get a fixed width. |
| 30 | let width = absolute_width_with_paddingrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/helper/absolute_width_with_padding().(column, width); |
| 31 | let info = ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#::newrust-analyzer cargo comfy-table 7.2.2 utils/impl#[ColumnDisplayInfo]new().(column, width); |
| 32 | infos.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]insert().(column.indexrust-analyzer cargo comfy-table 7.2.2 column/Column#index., info); |
| 33 | } |
| 34 | } |
| 35 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Hiddenrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#Hidden#) => { |
| 36 | let mut info = ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#::newrust-analyzer cargo comfy-table 7.2.2 utils/impl#[ColumnDisplayInfo]new().(column, max_content_width); |
| 37 | info.is_hiddenrust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#is_hidden. = true; |
| 38 | infos.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]insert().(column.indexrust-analyzer cargo comfy-table 7.2.2 column/Column#index., info); |
| 39 | } |
| 40 | _ => {} |
| 41 | } |
| 42 | |
| 43 | if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(min_width) = minrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/constraint/min().(table, &column.constraintrust-analyzer cargo comfy-table 7.2.2 column/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 rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[u16][`Add<Self>`]add().column.padding_widthrust-analyzer cargo comfy-table 7.2.2 column/impl#[Column]padding_width().(); |
| 47 | if max_width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u16][`PartialOrd<Self>`]le().min_width { |
| 48 | let width = absolute_width_with_paddingrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/helper/absolute_width_with_padding().(column, min_width); |
| 49 | let info = ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#::newrust-analyzer cargo comfy-table 7.2.2 utils/impl#[ColumnDisplayInfo]new().(column, width); |
| 50 | infos.insertrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]insert().(column.indexrust-analyzer cargo comfy-table 7.2.2 column/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 minrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/constraint/min().( |
| 63 | table: &Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#, |
| 64 | constraint: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ColumnConstraintrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#>, |
| 65 | visible_columns: usize, |
| 66 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u16> { |
| 67 | let constraint = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(constraint) = constraint { |
| 68 | constraint |
| 69 | } else { |
| 70 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 71 | }; |
| 72 | |
| 73 | match constraint { |
| 74 | LowerBoundaryrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#LowerBoundary#(width) | Boundariesrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#Boundaries# { lowerrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#Boundaries#lower.: width, .. } => { |
| 75 | absolute_value_from_widthrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/constraint/absolute_value_from_width().(table, width, visible_columns) |
| 76 | } |
| 77 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#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 maxrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/constraint/max().( |
| 89 | table: &Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#, |
| 90 | constraint: &Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<ColumnConstraintrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#>, |
| 91 | visible_columns: usize, |
| 92 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u16> { |
| 93 | let constraint = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(constraint) = constraint { |
| 94 | constraint |
| 95 | } else { |
| 96 | return Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#; |
| 97 | }; |
| 98 | |
| 99 | match constraint { |
| 100 | UpperBoundaryrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#UpperBoundary#(width) | Boundariesrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#Boundaries# { upperrust-analyzer cargo comfy-table 7.2.2 style/column/ColumnConstraint#Boundaries#upper.: width, .. } => { |
| 101 | absolute_value_from_widthrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/constraint/absolute_value_from_width().(table, width, visible_columns) |
| 102 | } |
| 103 | _ => Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /// Resolve an absolute value from a given boundary |
| 108 | pub fn absolute_value_from_widthrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/constraint/absolute_value_from_width().( |
| 109 | table: &Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#, |
| 110 | width: &Widthrust-analyzer cargo comfy-table 7.2.2 style/column/Width#, |
| 111 | visible_columns: usize, |
| 112 | ) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<u16> { |
| 113 | match width { |
| 114 | Widthrust-analyzer cargo comfy-table 7.2.2 style/column/Width#::Fixedrust-analyzer cargo comfy-table 7.2.2 style/column/Width#Fixed#(width) => Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().width), |
| 115 | Widthrust-analyzer cargo comfy-table 7.2.2 style/column/Width#::Percentagerust-analyzer cargo comfy-table 7.2.2 style/column/Width#Percentage#(percent) => { |
| 116 | // Don't return a value, if we cannot determine the current table width. |
| 117 | let table_width = table.widthrust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]width().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]map().(usize::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[usize][`From<u16>`]from().)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`][Try]branch().; |
| 118 | |
| 119 | // Enforce at most 100% |
| 120 | let percent = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::cmprust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/::minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/min().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().percent, 100u16); |
| 121 | |
| 122 | // Subtract the borders from the table width. |
| 123 | let width = table_width.saturating_subrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[usize]saturating_sub().(count_border_columnsrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/helper/count_border_columns().(table, visible_columns)); |
| 124 | |
| 125 | // Calculate the absolute value in actual columns. |
| 126 | let width = (width rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Mul<Self>`]mul().usize::fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/num/impl#[usize][`From<u16>`]from().(percent) rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div()./rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Div<Self>`]div().100) |
| 127 | .try_intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`TryInto<U>`]try_into().() |
| 128 | .unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(u16::MAXrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u16]MAX.); |
| 129 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(width) |
| 130 | } |
| 131 | } |
| 132 | } |