nu-table/ Fix indexing issue for table --expand (#9484)

close #9481

---------

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
Maxim Zhiburt 2023-06-20 11:27:00 +03:00 committed by GitHub
parent 69bf43ef56
commit 2ec1364925
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 27 additions and 6 deletions

View File

@ -424,7 +424,7 @@ fn build_table_batch(
flatten_separator, flatten_separator,
} => { } => {
let sep = flatten_separator.unwrap_or_else(|| String::from(' ')); let sep = flatten_separator.unwrap_or_else(|| String::from(' '));
ExpandedTable::new(limit, flatten, sep).build_list(&vals, opts) ExpandedTable::new(limit, flatten, sep).build_list(&vals, opts, row_offset)
} }
TableView::Collapsed => { TableView::Collapsed => {
let span = opts.span(); let span = opts.span();
@ -659,7 +659,7 @@ impl PagingTableCreator {
flatten_separator, flatten_separator,
}; };
build_table_batch(batch, view, 0, opts) build_table_batch(batch, view, self.row_offset, opts)
} }
fn build_collapsed(&mut self, batch: Vec<Value>) -> StringResult { fn build_collapsed(&mut self, batch: Vec<Value>) -> StringResult {
@ -674,7 +674,7 @@ impl PagingTableCreator {
let span = self.head; let span = self.head;
let opts = BuildConfig::new(ctrlc, &config, &style_computer, span, term_width); let opts = BuildConfig::new(ctrlc, &config, &style_computer, span, term_width);
build_table_batch(batch, TableView::Collapsed, 0, opts) build_table_batch(batch, TableView::Collapsed, self.row_offset, opts)
} }
fn build_general(&mut self, batch: Vec<Value>) -> StringResult { fn build_general(&mut self, batch: Vec<Value>) -> StringResult {

View File

@ -2369,3 +2369,19 @@ fn _split_str_by_width(s: &str, w: usize) -> Vec<String> {
lines lines
} }
#[test]
fn table_expand_index_offset() {
let actual = nu!(r#"1..1002 | table --expand"#);
let suffix = "╭──────┬──────╮│ 1000 │ 1001 ││ 1001 │ 1002 │╰──────┴──────╯";
let expected_suffix = actual.out.strip_suffix(suffix);
assert!(expected_suffix.is_some(), "{:?}", actual.out);
}
#[test]
fn table_index_offset() {
let actual = nu!(r#"1..1002 | table"#);
let suffix = "╭──────┬──────╮│ 1000 │ 1001 ││ 1001 │ 1002 │╰──────┴──────╯";
let expected_suffix = actual.out.strip_suffix(suffix);
assert!(expected_suffix.is_some(), "{:?}", actual.out);
}

View File

@ -50,7 +50,7 @@ fn try_build_list(
style_computer: &StyleComputer, style_computer: &StyleComputer,
) -> String { ) -> String {
let opts = BuildConfig::new(ctrlc, config, style_computer, Span::unknown(), usize::MAX); let opts = BuildConfig::new(ctrlc, config, style_computer, Span::unknown(), usize::MAX);
let result = ExpandedTable::new(None, false, String::new()).build_list(&vals, opts); let result = ExpandedTable::new(None, false, String::new()).build_list(&vals, opts, 0);
match result { match result {
Ok(Some(out)) => out, Ok(Some(out)) => out,
Ok(None) | Err(_) => { Ok(None) | Err(_) => {

View File

@ -59,7 +59,12 @@ impl ExpandedTable {
expanded_table_kv(cols, vals, opts) expanded_table_kv(cols, vals, opts)
} }
pub fn build_list(&self, vals: &[Value], opts: BuildConfig<'_>) -> StringResult { pub fn build_list(
&self,
vals: &[Value],
opts: BuildConfig<'_>,
row_offset: usize,
) -> StringResult {
let opts1 = Options { let opts1 = Options {
ctrlc: opts.ctrlc, ctrlc: opts.ctrlc,
config: opts.config, config: opts.config,
@ -68,7 +73,7 @@ impl ExpandedTable {
span: opts.span, span: opts.span,
format: self.clone(), format: self.clone(),
}; };
let out = match expanded_table_list(vals, 0, opts1)? { let out = match expanded_table_list(vals, row_offset, opts1)? {
Some(out) => out, Some(out) => out,
None => return Ok(None), None => return Ok(None),
}; };