mirror of
https://github.com/nushell/nushell.git
synced 2024-11-23 08:53:29 +01:00
803826cdcd
Also for 180 degree is expected. Rotation is not exactly like pivoting (transposing) for instance, given the following table: ``` > echo [[col1, col2, col3]; [cell1, cell2, cell3] [cell4, cell5, cell6]] ───┬───────┬───────┬─────── # │ col1 │ col2 │ col3 ───┼───────┼───────┼─────── 0 │ cell1 │ cell2 │ cell3 1 │ cell4 │ cell5 │ cell6 ───┴───────┴───────┴─────── ``` To rotate it counter clockwise by 90 degrees, we can resort to first transposing (`pivot`) them adding a new column (preferably integers), sort by that column from highest to lowest, then remove the column and we have a counter clockwise rotation. ``` > echo [[col1, col2, col3]; [cell1, cell2, cell3] [cell4, cell5, cell6]] | pivot | each --numbered { = $it.item | insert idx $it.index } | sort-by idx | reverse | reject idx ───┬─────────┬─────────┬───────── # │ Column0 │ Column1 │ Column2 ───┼─────────┼─────────┼───────── 0 │ col3 │ cell3 │ cell6 1 │ col2 │ cell2 │ cell5 2 │ col1 │ cell1 │ cell4 ───┴─────────┴─────────┴───────── ``` Which we can get easily, in this case, by doing: ``` > echo [[col1, col2, cel3]; [cell1, cell2, cell3] [cell4, cell5, cell6]] | rotate counter-clockwise ───┬─────────┬─────────┬───────── # │ Column0 │ Column1 │ Column2 ───┼─────────┼─────────┼───────── 0 │ col3 │ cell3 │ cell6 1 │ col2 │ cell2 │ cell5 2 │ col1 │ cell1 │ cell4 ───┴─────────┴─────────┴───────── ``` There are also many powerful use cases with rotation, it makes a breeze creating tables with many columns, say: ``` echo 0..12 | rotate counter-clockwise | reject Column0 ───┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬─────────┬──────────┬──────────┬──────────┬────────── # │ Column1 │ Column2 │ Column3 │ Column4 │ Column5 │ Column6 │ Column7 │ Column8 │ Column9 │ Column10 │ Column11 │ Column12 │ Column13 ───┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼──────────┼──────────┼──────────┼────────── 0 │ 0 │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 10 │ 11 │ 12 ───┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴──────────┴──────────┴──────────┴────────── ```
84 lines
1.9 KiB
Rust
84 lines
1.9 KiB
Rust
use nu_test_support::{nu, pipeline};
|
|
|
|
#[test]
|
|
fn counter_clockwise() {
|
|
let table = pipeline(
|
|
r#"
|
|
echo [
|
|
[col1, col2, EXPECTED];
|
|
|
|
[---, "|||", XX1]
|
|
[---, "|||", XX2]
|
|
[---, "|||", XX3]
|
|
]
|
|
"#,
|
|
);
|
|
|
|
let expected = nu!(cwd: ".", pipeline(
|
|
r#"
|
|
echo [
|
|
[ Column0, Column1, Column2, Column3];
|
|
|
|
[ EXPECTED, XX1, XX2, XX3]
|
|
[ col2, "|||", "|||", "|||"]
|
|
[ col1, ---, ---, ---]
|
|
]
|
|
| where Column0 == EXPECTED
|
|
| get Column1 Column2 Column3
|
|
| str collect "-"
|
|
"#,
|
|
));
|
|
|
|
let actual = nu!(
|
|
cwd: ".",
|
|
format!("{} | {}", table, pipeline(r#"
|
|
rotate counter-clockwise
|
|
| where Column0 == EXPECTED
|
|
| get Column1 Column2 Column3
|
|
| str collect "-"
|
|
"#)));
|
|
|
|
assert_eq!(actual.out, expected.out);
|
|
}
|
|
|
|
#[test]
|
|
fn clockwise() {
|
|
let table = pipeline(
|
|
r#"
|
|
echo [
|
|
[col1, col2, EXPECTED];
|
|
|
|
[ ---, "|||", XX1]
|
|
[ ---, "|||", XX2]
|
|
[ ---, "|||", XX3]
|
|
]
|
|
"#,
|
|
);
|
|
|
|
let expected = nu!(cwd: ".", pipeline(
|
|
r#"
|
|
echo [
|
|
[ Column0, Column1, Column2, Column3];
|
|
|
|
[ ---, ---, ---, col1]
|
|
[ "|||", "|||", "|||", col2]
|
|
[ XX3, XX2, XX1, EXPECTED]
|
|
]
|
|
| where Column3 == EXPECTED
|
|
| get Column0 Column1 Column2
|
|
| str collect "-"
|
|
"#,
|
|
));
|
|
|
|
let actual = nu!(
|
|
cwd: ".",
|
|
format!("{} | {}", table, pipeline(r#"
|
|
rotate
|
|
| where Column3 == EXPECTED
|
|
| get Column0 Column1 Column2
|
|
| str collect "-"
|
|
"#)));
|
|
|
|
assert_eq!(actual.out, expected.out);
|
|
}
|