mirror of
https://github.com/nushell/nushell.git
synced 2025-06-19 17:38:14 +02:00
Sorry was a little bit busy close #14842 I've added a test but I'd check if it solved it. cc: @fdncred __________________________ **Unrelated** Recently got a pretty good format idea (https://github.com/zhiburt/tabled/issues/472) Just wanna highlight that we could probably experiment with it, if it being a bit elaborated. It's sort of KV table which nushell already has, But it's more for a default table where each row/record being rendered as a KV table. It's not something super nice I guess but maybe it could get some appliance. So yes pointing it out just in case. Like these. ``` ┌──────────────┬───────────────────────────────────────────────┐ │ Field │ Value │ ├──────────────┼───────────────────────────────────────────────┤ │ Company │ INTEL CORP │ ├──────────────┼───────────────────────────────────────────────┤ │ Street │ 2200 MISSION COLLEGE BLVD, RNB-4-151 │ ├──────────────┼───────────────────────────────────────────────┤ │ City │ SANTA CLARA │ ├──────────────┼───────────────────────────────────────────────┤ │ ZIP code │ 95054 │ ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ │ Company │ Apple Inc. │ ├──────────────┼───────────────────────────────────────────────┤ │ Street │ ONE APPLE PARK WAY │ ├──────────────┼───────────────────────────────────────────────┤ │ City │ CUPERTINO │ ├──────────────┼───────────────────────────────────────────────┤ │ ZIP code │ 95014 │ └──────────────┴───────────────────────────────────────────────┘ ┌──────────────────────────────────────────────────────────────┐ │ INTEL CORP │ ├──────────────┬───────────────────────────────────────────────┤ │ Street │ 2200 MISSION COLLEGE BLVD, RNB-4-151 │ ├──────────────┼───────────────────────────────────────────────┤ │ City │ SANTA CLARA │ ├──────────────┼───────────────────────────────────────────────┤ │ ZIP code │ 95054 │ ├──────────────┴───────────────────────────────────────────────┤ │ Apple Inc. │ ├──────────────┬───────────────────────────────────────────────┤ │ Street │ ONE APPLE PARK WAY │ ├──────────────┼───────────────────────────────────────────────┤ │ City │ CUPERTINO │ ├──────────────┼───────────────────────────────────────────────┤ │ ZIP code │ 95014 │ └──────────────┴───────────────────────────────────────────────┘ ``` PS: Now thinking about it, it's sort of like doing a iteration over rows and building a current KV table, Which is interesting cause we could do it row by row, in which case doing CTRLC would not ruin build but got some data rendered. All though it's a different kind of approach. Just saying.
This commit is contained in:
parent
28ca0e7116
commit
9a0ae7c4c0
@ -3119,3 +3119,33 @@ fn table_colors() {
|
||||
));
|
||||
assert_eq!(actual.out, "╭───┬───╮│ a │ 1 ││ b │ 2 │╰───┴───╯");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_index() {
|
||||
let actual = nu!("[[ index var ]; [ abc 1 ] [ def 2 ] [ ghi 3 ]] | table --width=80");
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"╭─────┬─────╮\
|
||||
│ # │ var │\
|
||||
├─────┼─────┤\
|
||||
│ abc │ 1 │\
|
||||
│ def │ 2 │\
|
||||
│ ghi │ 3 │\
|
||||
╰─────┴─────╯"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn table_index_expand() {
|
||||
let actual = nu!("[[ index var ]; [ abc 1 ] [ def 2 ] [ ghi 3 ]] | table --width=80 --expand");
|
||||
assert_eq!(
|
||||
actual.out,
|
||||
"╭─────┬─────╮\
|
||||
│ # │ var │\
|
||||
├─────┼─────┤\
|
||||
│ abc │ 1 │\
|
||||
│ def │ 2 │\
|
||||
│ ghi │ 3 │\
|
||||
╰─────┴─────╯"
|
||||
);
|
||||
}
|
||||
|
@ -236,20 +236,25 @@ fn get_table_row_index(item: &Value, config: &Config, row: usize, offset: usize)
|
||||
fn collect_headers(headers: Vec<String>, index: bool) -> Vec<NuRecordsValue> {
|
||||
// The header with the INDEX is removed from the table headers since
|
||||
// it is added to the natural table index
|
||||
|
||||
if !index {
|
||||
headers
|
||||
.into_iter()
|
||||
.filter(|header| header != INDEX_COLUMN_NAME)
|
||||
.map(NuRecordsValue::new)
|
||||
.collect()
|
||||
let length = if index {
|
||||
headers.len() + 1
|
||||
} else {
|
||||
let mut v = Vec::with_capacity(headers.len() + 1);
|
||||
headers.len()
|
||||
};
|
||||
|
||||
let mut v = Vec::with_capacity(length);
|
||||
|
||||
if index {
|
||||
v.insert(0, NuRecordsValue::new("#".into()));
|
||||
for text in headers {
|
||||
v.push(NuRecordsValue::new(text));
|
||||
}
|
||||
|
||||
for text in headers {
|
||||
if text == INDEX_COLUMN_NAME {
|
||||
continue;
|
||||
}
|
||||
|
||||
v
|
||||
v.push(NuRecordsValue::new(text));
|
||||
}
|
||||
|
||||
v
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user