nu_explore/explore/
config.rs

1//! Configuration types for the explore command.
2
3use crate::explore::nu_common::create_map;
4use nu_ansi_term::{Color, Style};
5use nu_color_config::get_color_map;
6use nu_protocol::Config;
7
8#[derive(Debug, Clone)]
9pub struct ExploreConfig {
10    pub table: TableConfig,
11    pub selected_cell: Style,
12    pub status_info: Style,
13    pub status_success: Style,
14    pub status_warn: Style,
15    pub status_error: Style,
16    pub status_bar_background: Style,
17    pub status_bar_text: Style,
18    pub cmd_bar_text: Style,
19    pub cmd_bar_background: Style,
20    pub highlight: Style,
21    pub title_bar_background: Style,
22    pub title_bar_text: Style,
23    /// if true, the explore view will immediately try to run the command as it is typed
24    pub try_reactive: bool,
25}
26
27impl Default for ExploreConfig {
28    fn default() -> Self {
29        Self {
30            table: TableConfig::default(),
31            selected_cell: color(None, Some(Color::LightBlue)),
32            status_info: color(None, None),
33            status_success: color(Some(Color::Black), Some(Color::Green)),
34            status_warn: color(None, None),
35            status_error: color(Some(Color::White), Some(Color::Red)),
36            // Use None to inherit from terminal/nushell theme
37            status_bar_background: color(None, None),
38            status_bar_text: color(None, None),
39            cmd_bar_text: color(None, None),
40            cmd_bar_background: color(None, None),
41            highlight: color(Some(Color::Black), Some(Color::Yellow)),
42            // Use None to inherit from terminal/nushell theme
43            title_bar_background: color(None, None),
44            title_bar_text: color(None, None),
45            try_reactive: false,
46        }
47    }
48}
49
50impl ExploreConfig {
51    /// take the default explore config and update it with relevant values from the nu config
52    pub fn from_nu_config(config: &Config) -> Self {
53        let mut ret = Self::default();
54
55        ret.table.column_padding_left = config.table.padding.left;
56        ret.table.column_padding_right = config.table.padding.right;
57
58        let explore_cfg_hash_map = config.explore.clone();
59        let colors = get_color_map(&explore_cfg_hash_map);
60
61        if let Some(s) = colors.get("status_bar_text") {
62            ret.status_bar_text = *s;
63        }
64
65        if let Some(s) = colors.get("status_bar_background") {
66            ret.status_bar_background = *s;
67        }
68
69        if let Some(s) = colors.get("command_bar_text") {
70            ret.cmd_bar_text = *s;
71        }
72
73        if let Some(s) = colors.get("command_bar_background") {
74            ret.cmd_bar_background = *s;
75        }
76
77        if let Some(s) = colors.get("command_bar_background") {
78            ret.cmd_bar_background = *s;
79        }
80
81        if let Some(s) = colors.get("selected_cell") {
82            ret.selected_cell = *s;
83        }
84
85        if let Some(s) = colors.get("title_bar_text") {
86            ret.title_bar_text = *s;
87        }
88
89        if let Some(s) = colors.get("title_bar_background") {
90            ret.title_bar_background = *s;
91        }
92
93        if let Some(hm) = explore_cfg_hash_map.get("status").and_then(create_map) {
94            let colors = get_color_map(&hm);
95
96            if let Some(s) = colors.get("info") {
97                ret.status_info = *s;
98            }
99
100            if let Some(s) = colors.get("success") {
101                ret.status_success = *s;
102            }
103
104            if let Some(s) = colors.get("warn") {
105                ret.status_warn = *s;
106            }
107
108            if let Some(s) = colors.get("error") {
109                ret.status_error = *s;
110            }
111        }
112
113        if let Some(hm) = explore_cfg_hash_map.get("try").and_then(create_map)
114            && let Some(reactive) = hm.get("reactive")
115            && let Ok(b) = reactive.as_bool()
116        {
117            ret.try_reactive = b;
118        }
119
120        ret
121    }
122}
123
124#[derive(Debug, Default, Clone, Copy)]
125pub struct TableConfig {
126    pub separator_style: Style,
127    pub show_index: bool,
128    pub show_header: bool,
129    pub column_padding_left: usize,
130    pub column_padding_right: usize,
131}
132
133const fn color(foreground: Option<Color>, background: Option<Color>) -> Style {
134    Style {
135        background,
136        foreground,
137        is_blink: false,
138        is_bold: false,
139        is_dimmed: false,
140        is_hidden: false,
141        is_italic: false,
142        is_reverse: false,
143        is_strikethrough: false,
144        is_underline: false,
145        prefix_with_reset: false,
146    }
147}