nushell/crates/nu-command/tests/commands/mod.rs

114 lines
1.3 KiB
Rust
Raw Normal View History

mod alias;
mod all;
mod any;
mod append;
mod assignment;
mod break_;
special-case ExternalStream in bytes starts-with (#8203) # Description `bytes starts-with` converts the input into a `Value` before running .starts_with to find if the binary matches. This has two side effects: it makes the code simpler, only dealing in whole values, and simplifying a lot of input pipeline handling and value transforming it would otherwise have to do. _Especially_ in the presence of a cell path to drill into. It also makes buffers the entire input into memory, which can take up a lot of memory when dealing with large files, especially if you only want to check the first few bytes (like for a magic number). This PR adds a special branch on PipelineData::ExternalStream with a streaming version of starts_with. # User-Facing Changes Opening large files and running bytes starts-with on them will not take a long time. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # Drawbacks Streaming checking is more complicated, and there may be bugs. I tested it with multiple chunks with string data and binary data and it seems to work alright up to 8k and over bytes, though. The existing `operate` method still exists because the way it handles cell paths and values is complicated. This causes some "code duplication", or at least some intent duplication, between the value code and the streaming code. This might be worthwhile considering the performance gains (approaching infinity on larger inputs). Another thing to consider is that my ExternalStream branch considers string data as valid input. The operate branch only parses Binary values, so it would fail. `open` is kind of unpredictable on whether it returns string data or binary data, even when passing `--raw`. I think this can be a problem but not really one I'm trying to tackle in this PR, so, it's worth considering.
2023-02-26 15:17:44 +01:00
mod bytes;
mod cal;
mod cd;
mod compact;
mod continue_;
mod cp;
2022-05-23 18:59:34 +02:00
mod date;
mod def;
mod default;
mod do_;
mod drop;
mod each;
mod echo;
2020-10-06 12:21:20 +02:00
mod empty;
mod enter;
mod error_make;
mod every;
#[cfg(not(windows))]
mod exec;
mod export_def;
`string | fill` counts clusters, not graphemes; and doesn't count ANSI escape codes (#8134) Enhancement of new `fill` command (#7846) to handle content including ANSI escape codes for formatting or multi-code-point Unicode grapheme clusters. In both of these cases, the content is (many) bytes longer than its visible length, and `fill` was counting the extra bytes so not adding enough fill characters. # Description This script: ```rust # the teacher emoji `\u{1F9D1}\u{200D}\u{1F3EB}` is 3 code points, but only 1 print position wide. echo "This output should be 3 print positions wide, with leading and trailing `+`" $"\u{1F9D1}\u{200D}\u{1F3EB}" | fill -c "+" -w 3 -a "c" echo "This output should be 3 print positions wide, with leading and trailing `+`" $"(ansi green)a(ansi reset)" | fill -c "+" -w 3 -a c echo "" ``` Was producing this output: ```rust This output should be 3 print positions wide, with leading and trailing `+` 🧑‍🏫 This output should be 3 print positions wide, with leading and trailing `+` a ``` After this PR, it produces this output: ```rust This output should be 3 print positions wide, with leading and trailing `+` +🧑‍🏫+ This output should be 3 print positions wide, with leading and trailing `+` +a+ ``` # User-Facing Changes Users may have to undo fixes they may have introduced to work around the former behavior. I have one such in my prompt string that I can now revert. # Tests + Formatting Don't forget to add tests that cover your changes. -- Done Make sure you've run and fixed any issues with these commands: - [x] `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - [x] `cargo test --workspace` to check that all tests pass # After Submitting `fill` command not documented in the book, and it still talks about `str lpad/rpad`. I'll fix. Note added dependency on a new library `print-positions`, which is an iterator that yields a complete print position (cluster + Ansi sequence) per call. Should this be vendored?
2023-02-20 13:32:20 +01:00
mod fill;
mod find;
mod first;
2020-10-14 11:36:11 +02:00
mod flatten;
mod for_;
mod format;
mod g;
mod get;
mod glob;
mod group_by;
mod hash_;
mod headers;
mod help;
mod histogram;
2022-03-17 18:55:02 +01:00
mod insert;
mod into_filesize;
mod into_int;
SQL-style join command for Nushell tables (#8424) This PR adds a command `join` for performing SQL-style joins on Nushell tables: ``` 〉join -h Join two tables Usage: > join {flags} <right-table> <left-on> (right-on) Flags: -h, --help - Display the help message for this command -i, --inner - Inner join (default) -l, --left - Left-outer join -r, --right - Right-outer join -o, --outer - Outer join Signatures: <table> | join list<any>, <string>, <string?> -> <table> Parameters: right-table <list<any>>: The right table in the join left-on <string>: Name of column in input (left) table to join on (optional) right-on <string>: Name of column in right table to join on. Defaults to same column as left table. Examples: Join two tables > [{a: 1 b: 2}] | join [{a: 1 c: 3}] a ╭───┬───┬───╮ │ a │ b │ c │ ├───┼───┼───┤ │ 1 │ 2 │ 3 │ ╰───┴───┴───╯ ``` <table> <tbody> <tr> <td><img width="400" alt="image" src="https://user-images.githubusercontent.com/52205/224578744-eb9d133e-2510-4a3d-bd0a-d615f07a06b7.png"></td> </tr> </tbody> </table> # User-Facing Changes Adds a new command `join` # Tests + Formatting ``` cargo test -p nu-command commands::join ``` Don't forget to add tests that cover your changes. - [x] `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - [x] `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - [x] `cargo test --workspace` to check that all tests pass # After Submitting - [ ] If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date. --------- Co-authored-by: Reilly Wood <reilly.wood@icloud.com>
2023-03-17 00:57:20 +01:00
mod join;
mod last;
mod length;
mod let_;
mod lines;
mod loop_;
mod ls;
mod match_;
mod math;
mod merge;
mod mkdir;
2020-07-06 17:27:01 +02:00
mod move_;
mod mut_;
mod n;
mod network;
mod nu_check;
mod open;
mod p;
mod parse;
mod path;
mod platform;
mod prepend;
mod print;
#[cfg(feature = "sqlite")]
mod query;
mod random;
mod range;
mod redirection;
mod reduce;
mod reject;
2020-03-03 22:01:24 +01:00
mod rename;
mod return_;
mod reverse;
mod rm;
Table content rolling. (#3097) There are many use cases. Here we introduce the following: - The rows can be rolled `... | roll` (up) or `... | roll down` - Columns can be rolled too (the default is on the `left`, you can pass `... | roll column --opposite` to roll in the other direction) - You can `roll` the cells of a table and keeping the header names in the same order (`... | roll column --cells-only`) - Above examples can also be passed (Ex. `... | roll down 3`) a number to tell how many places to roll. Basic working example with rolling columns: ``` > echo '00000100' | split chars | each { str to-int } | rotate counter-clockwise _ | reject _ | rename bit1 bit2 bit3 bit4 bit5 bit6 bit7 bit8 ───┬──────┬──────┬──────┬──────┬──────┬──────┬──────┬────── # │ bit1 │ bit2 │ bit3 │ bit4 │ bit5 │ bit6 │ bit7 │ bit8 ───┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼────── 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 1 │ 0 │ 0 ───┴──────┴──────┴──────┴──────┴──────┴──────┴──────┴────── ``` We want to "shift" three bits to the left of the bitstring (four in decimal), let's try it: ``` > echo '00000100' | split chars | each { str to-int } | rotate counter-clockwise _ | reject _ | rename bit1 bit2 bit3 bit4 bit5 bit6 bit7 bit8 | roll column 3 ───┬──────┬──────┬──────┬──────┬──────┬──────┬──────┬────── # │ bit4 │ bit5 │ bit6 │ bit7 │ bit8 │ bit1 │ bit2 │ bit3 ───┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼────── 0 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 ───┴──────┴──────┴──────┴──────┴──────┴──────┴──────┴────── ``` The tables was rolled correctly (32 in decimal, for above bitstring). However, the *last three header names* look confusing. We can roll the cell contents only to fix it. ``` > echo '00000100' | split chars | each { str to-int } | rotate counter-clockwise _ | reject _ | rename bit1 bit2 bit3 bit4 bit5 bit6 bit7 bit8 | roll column 3 --cells-only ───┬──────┬──────┬──────┬──────┬──────┬──────┬──────┬────── # │ bit1 │ bit2 │ bit3 │ bit4 │ bit5 │ bit6 │ bit7 │ bit8 ───┼──────┼──────┼──────┼──────┼──────┼──────┼──────┼────── 0 │ 0 │ 0 │ 1 │ 0 │ 0 │ 0 │ 0 │ 0 ───┴──────┴──────┴──────┴──────┴──────┴──────┴──────┴────── ``` There we go. Let's compute it's decimal value now (should be 32) ``` > echo '00000100' | split chars | each { str to-int } | rotate counter-clockwise _ | reject _ | roll column 3 --cells-only | pivot bit --ignore-titles | get bit | reverse | each --numbered { = $it.item * (2 ** $it.index) } | math sum 32 ```
2021-02-23 19:29:07 +01:00
mod roll;
90 degree table rotations (clockwise and counter-clockwise) (#3086) 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 ───┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴─────────┴──────────┴──────────┴──────────┴────────── ```
2021-02-22 12:56:34 +01:00
mod rotate;
mod run_external;
mod save;
mod select;
mod semicolon;
mod seq;
mod seq_char;
mod shells;
mod skip;
mod sort;
mod sort_by;
mod source_env;
mod split_by;
mod split_column;
2020-05-24 08:41:30 +02:00
mod split_row;
mod str_;
WIP/ Checkout to new `tabled` (#6286) * nu-table/ Use latest tabled Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * nu-table/ Fix first column alignment Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * nu-table: Fix cargo clippy Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * nu-table: Fix color issue Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * nu-table: Fix footer row Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * nu-table: Bump tabled Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * nu-table: Bump tabled Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * nu-table: Bump tabled Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Update Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * nu-table/ Update * Use latest tabled Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Add optional -e, -c argument to `table` command for different view Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Fix clippy Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Fix clippy Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Update Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Fix cargo clippy Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Fix tests Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * nu-table: Add footer into -e/c mode Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Publish new expand mode Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Add width ctrl for Expand mode Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Refactorings Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Refactorings Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Add tests Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Add tests Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Merge with main Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Fix clippy Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Fix tests Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Fix tests Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Bump tabled Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * Add record expand and fix empty list issue Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com> * refactoring Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
2022-10-03 18:40:16 +02:00
mod table;
2022-04-07 22:49:28 +02:00
mod take;
mod to_text;
2020-02-18 21:54:32 +01:00
mod touch;
mod transpose;
mod try_;
mod uniq;
mod uniq_by;
mod update;
2022-03-17 18:55:02 +01:00
mod upsert;
mod url;
mod use_;
mod where_;
#[cfg(feature = "which-support")]
Obey precedence rules in which; Fix #2875 (#2885) * Obay precedence rules in which; Fix #2875 Before which did not obay the precedence of alias before def commands. Furthermore, `which -a echo` would only report either an alias or a def command or an internal command with the provided name. Not all. With this commit applied its fixed :) Example: ```shell /home/leo/repos/nushell(fix/which_reports_wrong_usage)> def echo [] {^echo hi} /home/leo/repos/nushell(fix/which_reports_wrong_usage)> echo hi /home/leo/repos/nushell(fix/which_reports_wrong_usage)> which -a echo ───┬──────┬──────────────────────────┬───────── # │ arg │ path │ builtin ───┼──────┼──────────────────────────┼───────── 0 │ echo │ Nushell custom command │ No 1 │ echo │ Nushell built-in command │ Yes 2 │ echo │ /usr/bin/echo │ No ───┴──────┴──────────────────────────┴───────── /home/leo/repos/nushell(fix/which_reports_wrong_usage)> alias echo = ^echo hi there /home/leo/repos/nushell(fix/which_reports_wrong_usage)> echo hi there /home/leo/repos/nushell(fix/which_reports_wrong_usage)> which -a echo ───┬──────┬──────────────────────────┬───────── # │ arg │ path │ builtin ───┼──────┼──────────────────────────┼───────── 0 │ echo │ Nushell alias │ No 1 │ echo │ Nushell custom command │ No 2 │ echo │ Nushell built-in command │ Yes 3 │ echo │ /usr/bin/echo │ No ───┴──────┴──────────────────────────┴───────── ``` * Fix clippy lint * Fix vec always Some even if empty
2021-01-08 18:44:31 +01:00
mod which;
2022-11-11 19:21:45 +01:00
mod while_;
2020-05-06 05:56:31 +02:00
mod with_env;
mod wrap;
mod zip;