mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 07:05:47 +02:00
Refactor table
cmd and nu-table
with Record API (#10930)
# Description - Simplify `table` record highlight with `.get_mut` - pretty straight forward - Use record iterators in `table` abbreviation logic - This required some rework if we go from guaranted contiguous arrays to iterators - Refactor `nu-table` internals to new record API # User-Facing Changes None intened # Tests + Formatting (-)
This commit is contained in:
committed by
GitHub
parent
f45aed257f
commit
7ebae0b5f7
@ -1,5 +1,5 @@
|
||||
use nu_color_config::StyleComputer;
|
||||
use nu_protocol::{Config, Value};
|
||||
use nu_protocol::{Config, Record, Value};
|
||||
|
||||
use crate::UnstructuredTable;
|
||||
|
||||
@ -40,17 +40,22 @@ fn collapsed_table(
|
||||
|
||||
fn colorize_value(value: &mut Value, config: &Config, style_computer: &StyleComputer) {
|
||||
match value {
|
||||
Value::Record { val: record, .. } => {
|
||||
for val in &mut record.vals {
|
||||
colorize_value(val, config, style_computer);
|
||||
}
|
||||
|
||||
Value::Record { ref mut val, .. } => {
|
||||
let style = get_index_style(style_computer);
|
||||
if let Some(color) = style.color_style {
|
||||
for header in &mut record.cols {
|
||||
*header = color.paint(header.to_owned()).to_string();
|
||||
}
|
||||
}
|
||||
// Take ownership of the record and reassign to &mut
|
||||
// We do this to have owned keys through `.into_iter`
|
||||
let record = std::mem::take(val);
|
||||
*val = record
|
||||
.into_iter()
|
||||
.map(|(mut header, mut val)| {
|
||||
colorize_value(&mut val, config, style_computer);
|
||||
|
||||
if let Some(color) = style.color_style {
|
||||
header = color.paint(header).to_string();
|
||||
}
|
||||
(header, val)
|
||||
})
|
||||
.collect::<Record>();
|
||||
}
|
||||
Value::List { vals, .. } => {
|
||||
for val in vals {
|
||||
|
@ -344,8 +344,7 @@ fn expanded_table_list(input: &[Value], cfg: Cfg<'_>) -> TableResult {
|
||||
fn expanded_table_kv(record: &Record, cfg: Cfg<'_>) -> StringResult {
|
||||
let theme = load_theme_from_config(cfg.opts.config);
|
||||
let key_width = record
|
||||
.cols
|
||||
.iter()
|
||||
.columns()
|
||||
.map(|col| string_width(col))
|
||||
.max()
|
||||
.unwrap_or(0);
|
||||
|
@ -157,17 +157,18 @@ fn build_vertical_array(vals: Vec<Value>, config: &Config) -> TableValue {
|
||||
}
|
||||
|
||||
fn is_valid_record(vals: &[Value]) -> bool {
|
||||
let mut used_cols: Option<&[String]> = None;
|
||||
let mut first_record: Option<&Record> = None;
|
||||
for val in vals {
|
||||
match val {
|
||||
Value::Record { val, .. } => {
|
||||
let cols_are_not_equal =
|
||||
used_cols.is_some() && !matches!(used_cols, Some(used) if val.cols == used);
|
||||
if cols_are_not_equal {
|
||||
return false;
|
||||
}
|
||||
|
||||
used_cols = Some(&val.cols);
|
||||
if let Some(known) = first_record {
|
||||
let equal = known.columns().eq(val.columns());
|
||||
if !equal {
|
||||
return false;
|
||||
}
|
||||
} else {
|
||||
first_record = Some(val)
|
||||
};
|
||||
}
|
||||
_ => return false,
|
||||
}
|
||||
@ -195,8 +196,8 @@ fn build_map_from_record(vals: Vec<Value>, config: &Config) -> TableValue {
|
||||
for val in vals {
|
||||
match val {
|
||||
Value::Record { val, .. } => {
|
||||
for (i, cell) in val.vals.into_iter().take(count_columns).enumerate() {
|
||||
let cell = convert_nu_value_to_table_value(cell, config);
|
||||
for (i, (_key, val)) in val.into_iter().take(count_columns).enumerate() {
|
||||
let cell = convert_nu_value_to_table_value(val, config);
|
||||
list[i].push(cell);
|
||||
}
|
||||
}
|
||||
@ -211,7 +212,7 @@ fn build_map_from_record(vals: Vec<Value>, config: &Config) -> TableValue {
|
||||
|
||||
fn get_columns_in_record(vals: &[Value]) -> Vec<String> {
|
||||
match vals.iter().next() {
|
||||
Some(Value::Record { val, .. }) => val.cols.clone(),
|
||||
Some(Value::Record { val, .. }) => val.columns().cloned().collect(),
|
||||
_ => vec![],
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user