1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
mod collapse;
mod expanded;
mod general;

use std::sync::{atomic::AtomicBool, Arc};

pub use collapse::CollapsedTable;
pub use expanded::ExpandedTable;
pub use general::JustTable;
use nu_color_config::StyleComputer;
use nu_protocol::{Config, Span, TableIndexMode, TableMode};

use crate::{common::INDEX_COLUMN_NAME, NuTable};

pub struct TableOutput {
    pub table: NuTable,
    pub with_header: bool,
    pub with_index: bool,
}

impl TableOutput {
    pub fn new(table: NuTable, with_header: bool, with_index: bool) -> Self {
        Self {
            table,
            with_header,
            with_index,
        }
    }
}

#[derive(Debug, Clone)]
pub struct TableOpts<'a> {
    ctrlc: Option<Arc<AtomicBool>>,
    config: &'a Config,
    style_computer: &'a StyleComputer<'a>,
    span: Span,
    width: usize,
    indent: (usize, usize),
    mode: TableMode,
    index_offset: usize,
    index_remove: bool,
}

impl<'a> TableOpts<'a> {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        config: &'a Config,
        style_computer: &'a StyleComputer<'a>,
        ctrlc: Option<Arc<AtomicBool>>,
        span: Span,
        width: usize,
        indent: (usize, usize),
        mode: TableMode,
        index_offset: usize,
        index_remove: bool,
    ) -> Self {
        Self {
            ctrlc,
            config,
            style_computer,
            span,
            indent,
            width,
            mode,
            index_offset,
            index_remove,
        }
    }
}

fn has_index(opts: &TableOpts<'_>, headers: &[String]) -> bool {
    let with_index = match opts.config.table_index_mode {
        TableIndexMode::Always => true,
        TableIndexMode::Never => false,
        TableIndexMode::Auto => headers.iter().any(|header| header == INDEX_COLUMN_NAME),
    };

    with_index && !opts.index_remove
}