1use crate::{TableOutput, TableTheme, clean_charset, colorize_space_str, string_wrap};
2use nu_color_config::{Alignment, StyleComputer, TextStyle};
3use nu_protocol::{
4 Config, FooterMode, ShellError, Span, TableMode, TrimStrategy, Value,
5 shell_error::generic::GenericError,
6};
7use nu_utils::terminal_size;
8
9pub type NuText = (String, TextStyle);
10pub type TableResult = Result<Option<TableOutput>, ShellError>;
11pub type StringResult = Result<Option<String>, ShellError>;
12
13pub const INDEX_COLUMN_NAME: &str = "index";
14
15pub fn configure_table(
16 out: &mut TableOutput,
17 config: &Config,
18 comp: &StyleComputer,
19 mode: TableMode,
20) {
21 let with_footer = is_footer_needed(config, out);
22 let theme = load_theme(mode);
23
24 out.table.set_theme(theme);
25 out.table
26 .set_structure(out.with_index, out.with_header, with_footer);
27 out.table.set_trim(config.table.trim.clone());
28 out.table
29 .set_border_header(config.table.header_on_separator);
30 out.table.set_border_color(lookup_separator_color(comp));
31}
32
33fn is_footer_needed(config: &Config, out: &TableOutput) -> bool {
34 let mut count_rows = out.table.count_rows();
35 if config.table.footer_inheritance {
36 count_rows = out.count_rows;
37 }
38
39 with_footer(config, out.with_header, count_rows)
40}
41
42pub fn nu_value_to_string_colored(val: &Value, cfg: &Config, comp: &StyleComputer) -> String {
43 let (mut text, style) = nu_value_to_string(val, cfg, comp);
44
45 let is_string = matches!(val, Value::String { .. });
46 if is_string {
47 text = clean_charset(&text);
48 }
49
50 if let Some(color) = style.color_style {
51 text = color.paint(text).to_string();
52 }
53
54 if is_string {
55 colorize_space_str(&mut text, comp);
56 }
57
58 text
59}
60
61pub fn nu_value_to_string(val: &Value, cfg: &Config, style: &StyleComputer) -> NuText {
62 let float_precision = cfg.float_precision as usize;
63 let text = val.to_abbreviated_string(cfg);
64 make_styled_value(text, val, float_precision, style)
65}
66
67pub fn nu_value_to_string_clean(val: &Value, cfg: &Config, style_comp: &StyleComputer) -> NuText {
70 let (text, style) = nu_value_to_string(val, cfg, style_comp);
71 let mut text = clean_charset(&text);
72 colorize_space_str(&mut text, style_comp);
73
74 (text, style)
75}
76
77pub fn error_sign(text: String, style_computer: &StyleComputer) -> (String, TextStyle) {
78 let style = style_computer.compute("empty", &Value::nothing(Span::unknown()));
81 (text, TextStyle::with_style(Alignment::Center, style))
82}
83
84pub fn wrap_text(text: &str, width: usize, config: &Config) -> String {
85 let keep_words = config.table.trim == TrimStrategy::wrap(true);
86 string_wrap(text, width, keep_words)
87}
88
89pub fn get_header_style(style_computer: &StyleComputer) -> TextStyle {
90 TextStyle::with_style(
91 Alignment::Center,
92 style_computer.compute("header", &Value::string("", Span::unknown())),
93 )
94}
95
96pub fn get_index_style(style_computer: &StyleComputer) -> TextStyle {
97 TextStyle::with_style(
98 Alignment::Right,
99 style_computer.compute("row_index", &Value::string("", Span::unknown())),
100 )
101}
102
103pub fn get_leading_trailing_space_style(style_computer: &StyleComputer) -> TextStyle {
104 TextStyle::with_style(
105 Alignment::Right,
106 style_computer.compute(
107 "leading_trailing_space_bg",
108 &Value::string("", Span::unknown()),
109 ),
110 )
111}
112
113pub fn get_value_style(value: &Value, config: &Config, style_computer: &StyleComputer) -> NuText {
114 match value {
115 Value::Float { val, .. } => (
117 format!("{:.prec$}", val, prec = config.float_precision as usize),
118 style_computer.style_primitive(value),
119 ),
120 _ => (
121 value.to_abbreviated_string(config),
122 style_computer.style_primitive(value),
123 ),
124 }
125}
126
127fn make_styled_value(
128 text: String,
129 value: &Value,
130 float_precision: usize,
131 style_computer: &StyleComputer,
132) -> NuText {
133 match value {
134 Value::Float { .. } => {
135 let precise_number = match convert_with_precision(&text, float_precision) {
137 Ok(num) => num,
138 Err(e) => e.to_string(),
139 };
140
141 (precise_number, style_computer.style_primitive(value))
142 }
143 _ => (text, style_computer.style_primitive(value)),
144 }
145}
146
147fn convert_with_precision(val: &str, precision: usize) -> Result<String, ShellError> {
148 let val_float = match val.trim().parse::<f64>() {
150 Ok(f) => f,
151 Err(e) => {
152 return Err(ShellError::Generic(
153 GenericError::new_internal(
154 format!("error converting string [{}] to f64", &val),
155 "",
156 )
157 .with_help(e.to_string()),
158 ));
159 }
160 };
161 Ok(format!("{val_float:.precision$}"))
162}
163
164pub fn load_theme(mode: TableMode) -> TableTheme {
165 match mode {
166 TableMode::Basic => TableTheme::basic(),
167 TableMode::Thin => TableTheme::thin(),
168 TableMode::Light => TableTheme::light(),
169 TableMode::Compact => TableTheme::compact(),
170 TableMode::Frameless => TableTheme::frameless(),
171 TableMode::WithLove => TableTheme::with_love(),
172 TableMode::CompactDouble => TableTheme::compact_double(),
173 TableMode::Rounded => TableTheme::rounded(),
174 TableMode::Reinforced => TableTheme::reinforced(),
175 TableMode::Heavy => TableTheme::heavy(),
176 TableMode::None => TableTheme::none(),
177 TableMode::Psql => TableTheme::psql(),
178 TableMode::Markdown => TableTheme::markdown(),
179 TableMode::Dots => TableTheme::dots(),
180 TableMode::Restructured => TableTheme::restructured(),
181 TableMode::AsciiRounded => TableTheme::ascii_rounded(),
182 TableMode::BasicCompact => TableTheme::basic_compact(),
183 TableMode::Single => TableTheme::single(),
184 TableMode::Double => TableTheme::double(),
185 }
186}
187
188fn lookup_separator_color(style_computer: &StyleComputer) -> nu_ansi_term::Style {
189 style_computer.compute("separator", &Value::nothing(Span::unknown()))
190}
191
192fn with_footer(config: &Config, with_header: bool, count_records: usize) -> bool {
193 with_header && need_footer(config, count_records as u64)
194}
195
196fn need_footer(config: &Config, count_records: u64) -> bool {
197 match config.footer_mode {
198 FooterMode::RowCount(limit) => count_records > limit,
200 FooterMode::Always => true,
202 FooterMode::Never => false,
204 FooterMode::Auto => {
206 let (_width, height) = match terminal_size() {
207 Ok((w, h)) => (w as u64, h as u64),
208 _ => (0, 0),
209 };
210 height <= count_records
211 }
212 }
213}
214
215pub fn check_value(value: &Value) -> Result<(), ShellError> {
216 match value {
217 Value::Error { error, .. } => Err(*error.clone()),
218 _ => Ok(()),
219 }
220}