mirror of
https://github.com/nushell/nushell.git
synced 2025-06-20 18:08:36 +02:00
nu-table: (table -e) Reuse NuRecordsValue::width in some cases (#15902)
Just remove a few calculations of width for values which will be inserted anyhow. So it must be just a bit faster (in base case).
This commit is contained in:
parent
4aeede2dd5
commit
2fe25d6299
@ -92,6 +92,16 @@ impl NuTable {
|
|||||||
self.count_cols
|
self.count_cols
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn create(text: String) -> NuRecordsValue {
|
||||||
|
Text::new(text)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert_value(&mut self, pos: (usize, usize), value: NuRecordsValue) {
|
||||||
|
let width = value.width() + indent_sum(self.config.indent);
|
||||||
|
self.widths[pos.1] = max(self.widths[pos.1], width);
|
||||||
|
self.data[pos.0][pos.1] = value;
|
||||||
|
}
|
||||||
|
|
||||||
pub fn insert(&mut self, pos: (usize, usize), text: String) {
|
pub fn insert(&mut self, pos: (usize, usize), text: String) {
|
||||||
let text = Text::new(text);
|
let text = Text::new(text);
|
||||||
let width = text.width() + indent_sum(self.config.indent);
|
let width = text.width() + indent_sum(self.config.indent);
|
||||||
|
@ -3,6 +3,7 @@ use std::cmp::max;
|
|||||||
use nu_color_config::{Alignment, StyleComputer, TextStyle};
|
use nu_color_config::{Alignment, StyleComputer, TextStyle};
|
||||||
use nu_engine::column::get_columns;
|
use nu_engine::column::get_columns;
|
||||||
use nu_protocol::{Config, Record, ShellError, Span, Value};
|
use nu_protocol::{Config, Record, ShellError, Span, Value};
|
||||||
|
use tabled::grid::records::vec_records::Cell;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
NuTable, TableOpts, TableOutput,
|
NuTable, TableOpts, TableOutput,
|
||||||
@ -186,13 +187,14 @@ fn expand_list(input: &[Value], cfg: Cfg<'_>) -> TableResult {
|
|||||||
.and_then(|val| val.get(INDEX_COLUMN_NAME))
|
.and_then(|val| val.get(INDEX_COLUMN_NAME))
|
||||||
.map(|value| value.to_expanded_string("", cfg.opts.config))
|
.map(|value| value.to_expanded_string("", cfg.opts.config))
|
||||||
.unwrap_or_else(|| index.to_string());
|
.unwrap_or_else(|| index.to_string());
|
||||||
let index_width = string_width(&index_value);
|
let index_value = NuTable::create(index_value);
|
||||||
|
let index_width = index_value.width();
|
||||||
if available_width <= index_width + extra_width + pad_width {
|
if available_width <= index_width + extra_width + pad_width {
|
||||||
// NOTE: we don't wanna wrap index; so we return
|
// NOTE: we don't wanna wrap index; so we return
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
|
|
||||||
table.insert((row, 0), index_value);
|
table.insert_value((row, 0), index_value);
|
||||||
|
|
||||||
index_column_width = max(index_column_width, index_width);
|
index_column_width = max(index_column_width, index_width);
|
||||||
}
|
}
|
||||||
@ -241,9 +243,10 @@ fn expand_list(input: &[Value], cfg: Cfg<'_>) -> TableResult {
|
|||||||
.and_then(|val| val.get(INDEX_COLUMN_NAME))
|
.and_then(|val| val.get(INDEX_COLUMN_NAME))
|
||||||
.map(|value| value.to_expanded_string("", cfg.opts.config))
|
.map(|value| value.to_expanded_string("", cfg.opts.config))
|
||||||
.unwrap_or_else(|| index.to_string());
|
.unwrap_or_else(|| index.to_string());
|
||||||
let index_width = string_width(&index_value);
|
let index_value = NuTable::create(index_value);
|
||||||
|
let index_width = index_value.width();
|
||||||
|
|
||||||
table.insert((row + 1, 0), index_value);
|
table.insert_value((row + 1, 0), index_value);
|
||||||
index_column_width = max(index_column_width, index_width);
|
index_column_width = max(index_column_width, index_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -294,11 +297,13 @@ fn expand_list(input: &[Value], cfg: Cfg<'_>) -> TableResult {
|
|||||||
|
|
||||||
let inner_cfg = cfg_expand_reset_table(cfg.clone(), available);
|
let inner_cfg = cfg_expand_reset_table(cfg.clone(), available);
|
||||||
let cell = expand_entry_with_header(item, &header, inner_cfg);
|
let cell = expand_entry_with_header(item, &header, inner_cfg);
|
||||||
let value_width = string_width(&cell.text); // TODO: optimize cause when we expand we alrready know the width (most of the time or all)
|
// TODO: optimize cause when we expand we alrready know the width (most of the time or all)
|
||||||
|
let value = NuTable::create(cell.text);
|
||||||
|
let value_width = value.width();
|
||||||
|
|
||||||
column_width = max(column_width, value_width);
|
column_width = max(column_width, value_width);
|
||||||
|
|
||||||
table.insert((row + 1, column), cell.text);
|
table.insert_value((row + 1, column), value);
|
||||||
table.insert_style((row + 1, column), cell.style);
|
table.insert_style((row + 1, column), cell.style);
|
||||||
|
|
||||||
total_column_rows = total_column_rows.saturating_add(cell.size);
|
total_column_rows = total_column_rows.saturating_add(cell.size);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user