nu_table/types/
mod.rs

1use nu_color_config::StyleComputer;
2use nu_protocol::{Config, Signals, Span, TableIndexMode, TableMode};
3
4use crate::{common::INDEX_COLUMN_NAME, NuTable};
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}
52
53impl<'a> TableOpts<'a> {
54    #[allow(clippy::too_many_arguments)]
55    pub fn new(
56        config: &'a Config,
57        style_computer: StyleComputer<'a>,
58        signals: &'a Signals,
59        span: Span,
60        width: usize,
61        mode: TableMode,
62        index_offset: usize,
63        index_remove: bool,
64    ) -> Self {
65        let style_computer = std::rc::Rc::new(style_computer);
66
67        Self {
68            signals,
69            config,
70            style_computer,
71            span,
72            width,
73            mode,
74            index_offset,
75            index_remove,
76        }
77    }
78}
79
80fn has_index(opts: &TableOpts<'_>, headers: &[String]) -> bool {
81    let with_index = match opts.config.table.index_mode {
82        TableIndexMode::Always => true,
83        TableIndexMode::Never => false,
84        TableIndexMode::Auto => headers.iter().any(|header| header == INDEX_COLUMN_NAME),
85    };
86
87    with_index && !opts.index_remove
88}