close #9038

---------

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
Maxim Zhiburt 2023-04-28 20:50:42 +03:00 committed by GitHub
parent 59b85e549c
commit 9804cd82f8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 28 deletions

View File

@ -4,7 +4,7 @@ use nu_protocol::{ast::PathMember, Config, ShellError, Span, TableIndexMode, Val
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use crate::{Cell, NuTable};
use crate::{Cell, NuTable, NuText};
use super::{
clean_charset, create_table_config, get_empty_style, get_header_style, get_index_style,
@ -176,27 +176,7 @@ fn to_table_with_header(
let skip_index = usize::from(with_index);
for (col, header) in headers.iter().enumerate().skip(skip_index) {
let (text, style) = match item {
Value::Record { .. } => {
let path = PathMember::String {
val: header.clone(),
span: Span::unknown(),
optional: false,
};
let value = item.clone().follow_cell_path(&[path], false);
match value {
Ok(value) => get_value_style(&value, opts.config, opts.style_computer),
Err(_) => get_empty_style(opts.style_computer),
}
}
value if matches!(value, Value::String { .. }) => {
let (text, style) = get_value_style(value, opts.config, opts.style_computer);
let text = clean_charset(&text);
(text, style)
}
value => get_value_style(value, opts.config, opts.style_computer),
};
let (text, style) = get_string_value_with_header(item, header, &opts);
table.insert((row + 1, col), text);
table.set_cell_style((row + 1, col), style);
@ -229,11 +209,7 @@ fn to_table_with_no_header(
table.insert((row, 0), text);
}
let (mut text, style) = get_value_style(item, opts.config, opts.style_computer);
let is_string_value = matches!(item, Value::String { .. });
if is_string_value {
text = clean_charset(&text);
}
let (text, style) = get_string_value(item, &opts);
let pos = (row, with_index as usize);
table.insert(pos, text);
@ -243,6 +219,35 @@ fn to_table_with_no_header(
Ok(Some(table))
}
fn get_string_value_with_header(item: &Value, header: &str, opts: &BuildConfig) -> NuText {
match item {
Value::Record { .. } => {
let path = PathMember::String {
val: header.to_owned(),
span: Span::unknown(),
optional: false,
};
let value = item.clone().follow_cell_path(&[path], false);
match value {
Ok(value) => get_string_value(&value, opts),
Err(_) => get_empty_style(opts.style_computer),
}
}
value => get_string_value(value, opts),
}
}
fn get_string_value(item: &Value, opts: &BuildConfig) -> NuText {
let (mut text, style) = get_value_style(item, opts.config, opts.style_computer);
let is_string_value = matches!(item, Value::String { .. });
if is_string_value {
text = clean_charset(&text);
}
(text, style)
}
fn get_table_row_index(item: &Value, config: &Config, row: usize, offset: usize) -> String {
match item {
Value::Record { .. } => item

View File

@ -46,7 +46,8 @@ pub fn value_to_clean_styled_string(val: &Value, cfg: &Config, style: &StyleComp
}
pub fn clean_charset(text: &str) -> String {
text.replace(['\r', '\t'], " ")
// todo: optimize, I bet it can be done in 1 path
text.replace('\t', " ").replace('\r', "")
}
const INDEX_COLUMN_NAME: &str = "index";