Debian Codegraph @

src/utils/arrangement/mod.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::collectionsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std collections/::BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#;
2
3 use superrust-analyzer cargo comfy-table 7.2.2 utils/::ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#;
4 use craterust-analyzer cargo comfy-table 7.2.2 crate/::{stylerust-analyzer cargo comfy-table 7.2.2 style/::ContentArrangementrust-analyzer cargo comfy-table 7.2.2 style/table/ContentArrangement#, tablerust-analyzer cargo comfy-table 7.2.2 table/::Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#};
5
6 pub mod constraintrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/constraint/;
7 mod disabledrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/disabled/;
8 mod dynamicrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/dynamic/;
9 pub mod helperrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/helper/;
10
11 type DisplayInfosrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/DisplayInfos# = BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#<usize, ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#>;
12
13 /// Determine the width of each column depending on the content of the given table.
14 /// The results uses Option<usize>, since users can choose to hide columns.
15 pub fn arrange_contentrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/arrange_content().(table: &Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#) -> Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#> {
16 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().);
17 let mut infos = BTreeMaprust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/BTreeMap#::newrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V>`]new().();
18
19 let max_content_widths = table.column_max_content_widthsrust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]column_max_content_widths().();
20
21 // Check if we can already resolve some constraints.
22 // This step also populates the ColumnDisplayInfo structs.
23 let visible_columns = helperrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/helper/::count_visible_columnsrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/helper/count_visible_columns().(&table.columnsrust-analyzer cargo comfy-table 7.2.2 table/Table#columns.);
24 for column in table.columnsrust-analyzer cargo comfy-table 7.2.2 table/Table#columns..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().() {
25 if column.constraintrust-analyzer cargo comfy-table 7.2.2 column/Column#constraint..is_somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]is_some().() {
26 constraintrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/constraint/::evaluaterust-analyzer cargo comfy-table 7.2.2 utils/arrangement/constraint/evaluate().(
27 table,
28 visible_columns,
29 &mut infos,
30 column,
31 max_content_widths[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().column.indexrust-analyzer cargo comfy-table 7.2.2 column/Column#index.]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().,
32 );
33 }
34 }
35 #[cfg(feature = "_debug")]
36 printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("After initial constraints: {infos:#?}");
37
38 // Fallback to `ContentArrangement::Disabled`, if we don't have any information
39 // on how wide the table should be.
40 let table_width = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(table_width) = table_width {
41 table_width
42 } else {
43 disabledrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/disabled/::arrangerust-analyzer cargo comfy-table 7.2.2 utils/arrangement/disabled/arrange().(table, &mut infos, visible_columns, &max_content_widths);
44 return infos.into_valuesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]into_values().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
45 };
46
47 match &table.arrangementrust-analyzer cargo comfy-table 7.2.2 table/Table#arrangement. {
48 ContentArrangementrust-analyzer cargo comfy-table 7.2.2 style/table/ContentArrangement#::Disabledrust-analyzer cargo comfy-table 7.2.2 style/table/ContentArrangement#Disabled# => {
49 disabledrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/disabled/::arrangerust-analyzer cargo comfy-table 7.2.2 utils/arrangement/disabled/arrange().(table, &mut infos, visible_columns, &max_content_widths)
50 }
51 ContentArrangementrust-analyzer cargo comfy-table 7.2.2 style/table/ContentArrangement#::Dynamicrust-analyzer cargo comfy-table 7.2.2 style/table/ContentArrangement#Dynamic# | ContentArrangementrust-analyzer cargo comfy-table 7.2.2 style/table/ContentArrangement#::DynamicFullWidthrust-analyzer cargo comfy-table 7.2.2 style/table/ContentArrangement#DynamicFullWidth# => {
52 dynamicrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/dynamic/::arrangerust-analyzer cargo comfy-table 7.2.2 utils/arrangement/dynamic/arrange().(table, &mut infos, table_width, &max_content_widths);
53 }
54 }
55
56 infos.into_valuesrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc collections/btree/map/impl#[`BTreeMap<K, V, A>`]into_values().().collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().()
57 }
58
59 #[cfg(test)]
60 mod testsrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/tests/ {
61 use superrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/::*;
62
63 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
64 fn test_disabled_arrangementrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/tests/test_disabled_arrangement().() {
65 let mut table = Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#::newrust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]new().();
66 table.set_headerrust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]set_header().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["head", "head", "head"]);
67 table.add_rowrust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]add_row().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["__", "fivef", "sixsix"]);
68
69 let display_infos = arrange_contentrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/arrange_content().(&table);
70
71 // The width should be the width of the rows + padding
72 let widths: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u16> = display_infos.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(ColumnDisplayInforust-analyzer cargo comfy-table 7.2.2 utils/ColumnDisplayInfo#::widthrust-analyzer cargo comfy-table 7.2.2 utils/impl#[ColumnDisplayInfo]width().).collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
73 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(widths, vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![6, 7, 8]);
74 }
75
76 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
77 fn test_discover_columnsrust-analyzer cargo comfy-table 7.2.2 utils/arrangement/tests/test_discover_columns().() {
78 let mut table = Tablerust-analyzer cargo comfy-table 7.2.2 table/Table#::newrust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]new().();
79 table.add_rowrust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]add_row().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!!["one", "two"]);
80
81 // Get the first row and add a new cell, which would create a new column.
82 let row = table.row_mutrust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]row_mut().(0).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap().();
83 row.add_cellrust-analyzer cargo comfy-table 7.2.2 row/impl#[Row]add_cell().("three".intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
84
85 // The table cannot know about the new cell yet, which is why we expect two columns.
86 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(table.columnsrust-analyzer cargo comfy-table 7.2.2 table/Table#columns..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 2);
87
88 // After scanning for new columns however, it should show up.
89 table.discover_columnsrust-analyzer cargo comfy-table 7.2.2 table/impl#[Table]discover_columns().();
90 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(table.columnsrust-analyzer cargo comfy-table 7.2.2 table/Table#columns..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), 3);
91 }
92 }