Skip to main content

nu_table/types/
mod.rs

1use nu_color_config::StyleComputer;
2use nu_protocol::{Config, Signals, Span, TableIndexMode, TableMode};
3
4use crate::{NuTable, common::INDEX_COLUMN_NAME};
5
6mod collapse;
7mod expanded;
8mod general;
9
10pub use collapse::CollapsedTable;
11pub use expanded::ExpandedTable;
12pub use general::JustTable;
13
14pub struct TableOutput {
15    /// A table structure.
16    pub table: NuTable,
17    /// A flag whether a header was present in the table.
18    pub with_header: bool,
19    /// A flag whether a index was present in the table.
20    pub with_index: bool,
21    /// The value may be bigger then table.count_rows(),
22    /// Specifically in case of expanded table we collect the whole structure size here.
23    pub count_rows: usize,
24}
25
26impl TableOutput {
27    pub fn new(table: NuTable, with_header: bool, with_index: bool, count_rows: usize) -> Self {
28        Self {
29            table,
30            with_header,
31            with_index,
32            count_rows,
33        }
34    }
35    pub fn from_table(table: NuTable, with_header: bool, with_index: bool) -> Self {
36        let count_rows = table.count_rows();
37        Self::new(table, with_header, with_index, count_rows)
38    }
39}
40
41#[derive(Debug, Clone)]
42pub struct TableOpts<'a> {
43    pub signals: &'a Signals,
44    pub config: &'a Config,
45    pub style_computer: std::rc::Rc<StyleComputer<'a>>,
46    pub span: Span,
47    pub width: usize,
48    pub mode: TableMode,
49    pub index_offset: usize,
50    pub index_remove: bool,
51    pub width_priority_columns: Vec<String>,
52}
53
54impl<'a> TableOpts<'a> {
55    #[allow(clippy::too_many_arguments)]
56    pub fn new(
57        config: &'a Config,
58        style_computer: StyleComputer<'a>,
59        signals: &'a Signals,
60        span: Span,
61        width: usize,
62        mode: TableMode,
63        index_offset: usize,
64        index_remove: bool,
65        width_priority_columns: Vec<String>,
66    ) -> Self {
67        let style_computer = std::rc::Rc::new(style_computer);
68
69        Self {
70            signals,
71            config,
72            style_computer,
73            span,
74            width,
75            mode,
76            index_offset,
77            index_remove,
78            width_priority_columns,
79        }
80    }
81}
82
83fn has_index(opts: &TableOpts<'_>, headers: &[String]) -> bool {
84    let with_index = match opts.config.table.index_mode {
85        TableIndexMode::Always => true,
86        TableIndexMode::Never => false,
87        TableIndexMode::Auto => headers.iter().any(|header| header == INDEX_COLUMN_NAME),
88    };
89
90    with_index && !opts.index_remove
91}