Compare commits

...

36 Commits

Author SHA1 Message Date
d297199d7c Bump to 0.25.0 (#2860) 2021-01-05 18:10:24 +13:00
17a433996e rename set/set-env to let/let-env (#2859) 2021-01-05 12:30:55 +13:00
b9bb4692a4 Allow source during parsing. Hacky but works (#2855) 2021-01-04 19:32:17 +13:00
d05dcdda02 make nushell reduce dependence crates smaller and build fast (#2853)
* update to shadow-rs 0.4. use easy

* update shadow-rs to 0.5

* fix version not used

* update

* update Cargo.lock

* update Cargo.lock

* fix wasm build error when use dependence git2
fix error link:https://dev.azure.com/nushell/nushell/_build/results?buildId=4858&view=logs&j=1a745d4c-b027-5f34-06d8-d6f256bfe9f9&t=a0a335cb-fa1f-5bbf-be01-1a90d6899e54

* remove code not used; fix warning by RUSTFLAGS="-D warnings" build error

* upgrade shadow-rs 0.5.2

* upgrade shadow-rs 0.5.7

make nushell reduce dependence crates smaller and  build fast.
2021-01-04 06:14:03 +13:00
27fe356214 Add proper shadowing (#2851) 2021-01-03 20:48:02 +13:00
fc44df1e45 Don't leak set/set-env/source scopes via actions (#2849) 2021-01-03 19:44:21 +13:00
77f915befe Tighten how input streams handle nothing, and related (#2847) 2021-01-03 14:22:44 +13:00
a5f7600f6f Fix typos (#2842) 2021-01-02 17:24:32 +13:00
7eb8634ad7 Fix typo in sort-by error message (#2841) 2021-01-01 18:34:50 -06:00
452d8c06e9 Improve some errors, streamline internal error handling (#2839)
* Improve some errors, streamline internal error handling

* Fix lints
2021-01-02 08:52:19 +13:00
48f535f02e Display aliases and custom commands in which; fix #2810 (#2834)
* Display aliases and custom commands in which; Fix #2810

Example output of nu after the commit is applied:

```shell
/home/leo/repos/nushell(feature/which_inspect_alias)> def docker-ps [] { docker ps --format '{{json .}}' | from json -o }
/home/leo/repos/nushell(feature/which_inspect_alias)> which docker-ps
───┬───────────┬────────────────────────┬─────────
 # │    arg    │          path          │ builtin
───┼───────────┼────────────────────────┼─────────
 0 │ docker-ps │ nushell custom command │ No
───┴───────────┴────────────────────────┴─────────
/home/leo/repos/nushell(feature/which_inspect_alias)> alias d = gid pd
/home/leo/repos/nushell(feature/which_inspect_alias)> which d
───┬─────┬───────────────┬─────────
 # │ arg │     path      │ builtin
───┼─────┼───────────────┼─────────
 0 │ d   │ nushell alias │ No
───┴─────┴───────────────┴─────────
```

* Update documentation
2021-01-02 06:40:44 +13:00
43c10b0625 Properly handle commands defined inside of other commands (#2837)
* Fix function inner scopes

* tweak error
2021-01-01 19:23:54 +13:00
328b09fe04 Properly error when 'source' argument can't be found (#2836) 2021-01-01 17:33:38 +13:00
15d49e4096 Rust 1.49 Clippy Fixes (#2835) 2021-01-01 15:13:59 +13:00
3ef53fe2cd move create_default_context out of cli.rs and into its own mod (#2833) 2021-01-01 15:12:16 +13:00
7d8e759e98 Nucli refactor script mod (#2831)
* move process_script and run_script_standalone out of cli.rs

* cargo fmt

* code cleanup imports

* unused imports issue in cli.rs
2020-12-31 12:38:31 +13:00
69b3be61a4 Simplify run_block slightly (#2830)
* Simplify run_block slightly

* Add early return on C-c
2020-12-31 12:37:07 +13:00
79476a5cb2 Replace clipboard with arboard (#2832) 2020-12-31 06:16:02 +13:00
f449baf8de Change ls to output path (#2829)
* make name a path vs string

* add support for comparing path to string
2020-12-28 14:52:28 -06:00
5ff4bcfb7a Nucli refactor crate stream (#2828)
* nu-stream is building on its own, now clean up Cargo.toml

* replace the stream crate in nu-cli

* cc

* since we moved stream out of the nu-cli crate and into its own crate we need to remove pub(crate) and just make it pub

* clean up the prelude and hand merge everything together

* clean up Cargo.tom

* cargo fmt along with Cargo.lock
2020-12-28 18:34:27 +13:00
98537ce8b7 remove code not used. Fix use shadow-rs build warning (#2827)
* update to shadow-rs 0.4. use easy

* update shadow-rs to 0.5

* fix version not used

* update

* update Cargo.lock

* update Cargo.lock

* fix wasm build error when use dependence git2
fix error link:https://dev.azure.com/nushell/nushell/_build/results?buildId=4858&view=logs&j=1a745d4c-b027-5f34-06d8-d6f256bfe9f9&t=a0a335cb-fa1f-5bbf-be01-1a90d6899e54

* remove code not used; fix warning by RUSTFLAGS="-D warnings" build error

* upgrade shadow-rs 0.5.2
2020-12-28 08:00:14 +13:00
d2a00a2daa update to shadow-rs 0.5. make use easy (#2793)
* update to shadow-rs 0.4. use easy

* update shadow-rs to 0.5

* fix version not used

* update

* update Cargo.lock

* update Cargo.lock

* fix wasm build error when use dependence git2
fix error link:https://dev.azure.com/nushell/nushell/_build/results?buildId=4858&view=logs&j=1a745d4c-b027-5f34-06d8-d6f256bfe9f9&t=a0a335cb-fa1f-5bbf-be01-1a90d6899e54
2020-12-24 05:56:05 +13:00
f22938fc4a Add support for custom subcommands (#2814)
* Add support for custom subcommands

* clippy
2020-12-23 20:43:56 +13:00
1e67ae8e94 Fix broken links in the README (#2813) 2020-12-22 17:35:06 +13:00
c012d648fb Add experimental support for flags in custom commands (#2808)
* Add experimental support for flags in custom commands

* clippy
2020-12-21 20:36:59 +13:00
67acaae53c Rename cond math (#2807)
* Simplifies 'if' to work on the available scope rather than a stream

* Rename initializer/math for better readability

* Fix description

* fmt
2020-12-21 17:32:06 +13:00
e3da546e23 Simplifies 'if' to work on the available scope rather than a stream (#2805) 2020-12-21 16:02:39 +13:00
e5b136f70d Add script sourcing (#2803)
* Add script sourcing

* clippy
2020-12-19 20:47:34 +13:00
058ef69da3 Add set-env for setting environment variables (#2802) 2020-12-19 19:25:03 +13:00
2a483531a4 Bug report uses version command (#2797) 2020-12-19 18:25:32 +13:00
05202671db Improve errors on success (#2801) 2020-12-19 18:24:56 +13:00
8509873043 Parse mid-line comments (#2800) 2020-12-19 11:23:02 +13:00
57a2d695e2 Removing the defs inside of blocks for now (#2798) 2020-12-19 07:53:00 +13:00
0b5ab1ef22 Don't print a nothing value (#2796) 2020-12-19 05:48:22 +13:00
2eac79569c highlight trailing spaces in tables in darkgray (#2794)
* highlight trailing spaces in tables in darkgray

* added leading spaces highlighting

* added config point to change the color

* Trigger Build
2020-12-18 07:47:05 -06:00
ac578b8491 Multiline scripts part 2 (#2795)
* Begin allowing comments and multiline scripts.

* clippy

* Finish moving to groups. Test pass

* Keep going

* WIP

* WIP

* BROKEN WIP

* WIP

* WIP

* Fix more tests

* WIP: alias starts working

* Broken WIP

* Broken WIP

* Variables begin to work

* captures start working

* A little better but needs fixed scope

* Shorthand env setting

* Update main merge

* Broken WIP

* WIP

* custom command parsing

* Custom commands start working

* Fix coloring and parsing of block

* Almost there

* Add some tests

* Add more param types

* Bump version

* Fix benchmark

* Fix stuff
2020-12-18 20:53:49 +13:00
337 changed files with 5175 additions and 5157 deletions

View File

@ -12,9 +12,9 @@ A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1.
2.
3.
1.
2.
3.
**Expected behavior**
A clear and concise description of what you expected to happen.
@ -23,8 +23,25 @@ A clear and concise description of what you expected to happen.
If applicable, add screenshots to help explain your problem.
**Configuration (please complete the following information):**
- OS (e.g. Windows):
- Nu version (you can use the `version` command to find out):
- Optional features (if any):
Add any other context about the problem here.
Run `version | pivot` and paste the output to show OS, features, etc.
```
> version | pivot
╭───┬────────────────────┬───────────────────────────────────────────────────────────────────────╮
│ # │ Column0 │ Column1 │
├───┼────────────────────┼───────────────────────────────────────────────────────────────────────┤
│ 0 │ version │ 0.24.1 │
│ 1 │ build_os │ macos-x86_64 │
│ 2 │ rust_version │ rustc 1.48.0 │
│ 3 │ cargo_version │ cargo 1.48.0 │
│ 4 │ pkg_version │ 0.24.1 │
│ 5 │ build_time │ 2020-12-18 09:54:09 │
│ 6 │ build_rust_channel │ release │
│ 7 │ features │ ctrlc, default, directories, dirs, git, ichwh, ptree, rich-benchmark, │
│ │ │ rustyline, term, uuid, which, zip │
╰───┴────────────────────┴───────────────────────────────────────────────────────────────────────╯
```
**Add any other context about the problem here.**

261
Cargo.lock generated
View File

@ -88,6 +88,26 @@ version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33954243bd79057c2de7338850b85983a44588021f8a5fee574a8888c6de4344"
[[package]]
name = "arboard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "85149eb4159516fbc261f362153822672e4bdb5b3accc863a5777627c6d9fe72"
dependencies = [
"clipboard-win",
"core-graphics",
"image 0.23.12",
"lazy_static 1.4.0",
"libc",
"objc",
"objc-foundation",
"objc_id",
"scopeguard",
"thiserror",
"winapi 0.3.9",
"xcb",
]
[[package]]
name = "arrayref"
version = "0.3.6"
@ -575,6 +595,12 @@ dependencies = [
"utf8-width",
]
[[package]]
name = "bytemuck"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41aa2ec95ca3b5c54cf73c91acf06d24f4495d5f1b1c12506ae3483d646177ac"
[[package]]
name = "byteorder"
version = "1.3.4"
@ -706,25 +732,14 @@ dependencies = [
"vec_map",
]
[[package]]
name = "clipboard"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "25a904646c0340239dcf7c51677b33928bf24fdf424b79a57909c0109075b2e7"
dependencies = [
"clipboard-win",
"objc",
"objc-foundation",
"objc_id",
"x11-clipboard",
]
[[package]]
name = "clipboard-win"
version = "2.2.0"
version = "4.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e3a093d6fed558e5fe24c3dfc85a68bb68f1c824f440d3ba5aca189e2998786b"
checksum = "5123c6b97286809fea9e38d2c9bf530edbcb9fc0d8f8272c28b0c95f067fa92d"
dependencies = [
"error-code",
"str-buf",
"winapi 0.3.9",
]
@ -766,6 +781,12 @@ dependencies = [
"unicode-width",
]
[[package]]
name = "color_quant"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "colored"
version = "1.9.3"
@ -882,6 +903,18 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ea221b5284a47e40033bf9b66f35f984ec0ea2931eb03505246cd27a963f981b"
[[package]]
name = "core-graphics"
version = "0.21.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52a67c4378cf203eace8fb6567847eb641fd6ff933c1145a115c6ee820ebb978"
dependencies = [
"bitflags",
"core-foundation 0.9.1",
"foreign-types",
"libc",
]
[[package]]
name = "cpuid-bool"
version = "0.1.2"
@ -1192,6 +1225,16 @@ dependencies = [
"byteorder",
]
[[package]]
name = "deflate"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73770f8e1fe7d64df17ca66ad28994a0a623ea497fa69486e14984e715c5d174"
dependencies = [
"adler32",
"byteorder",
]
[[package]]
name = "derive-new"
version = "0.5.8"
@ -1467,6 +1510,16 @@ dependencies = [
"version_check",
]
[[package]]
name = "error-code"
version = "2.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b49c94f66f2d2c5ee8685039e458b4e6c9f13af7c28736baf10ce42966a5ab52"
dependencies = [
"libc",
"str-buf",
]
[[package]]
name = "event-listener"
version = "2.5.1"
@ -1919,16 +1972,14 @@ checksum = "f6503fe142514ca4799d4c26297c4248239fe8838d827db6bd6065c6ed29a6ce"
[[package]]
name = "git2"
version = "0.13.12"
version = "0.13.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca6f1a0238d7f8f8fd5ee642f4ebac4dbc03e03d1f78fbe7a3ede35dcf7e2224"
checksum = "802b535f89475f22bb2b3d589de07861417e67b1a358c7ec8e5005bf4f209133"
dependencies = [
"bitflags",
"libc",
"libgit2-sys",
"log 0.4.11",
"openssl-probe",
"openssl-sys",
"url",
]
@ -2391,7 +2442,23 @@ dependencies = [
"num-iter",
"num-rational 0.2.4",
"num-traits 0.2.14",
"png",
"png 0.15.3",
]
[[package]]
name = "image"
version = "0.23.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ce04077ead78e39ae8610ad26216aed811996b043d47beed5090db674f9e9b5"
dependencies = [
"bytemuck",
"byteorder",
"color_quant",
"num-iter",
"num-rational 0.3.2",
"num-traits 0.2.14",
"png 0.16.8",
"tiff",
]
[[package]]
@ -2573,15 +2640,13 @@ checksum = "4d58d1b70b004888f764dfbf6a26a3b0342a1632d33968e4a179d8011c760614"
[[package]]
name = "libgit2-sys"
version = "0.12.14+1.1.0"
version = "0.12.15+1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f25af58e6495f7caf2919d08f212de550cfa3ed2f5e744988938ea292b9f549"
checksum = "48c39622f3887bc7fb2fff6d06293f64b02c1dfa281212566ac155171d13b0eb"
dependencies = [
"cc",
"libc",
"libssh2-sys",
"libz-sys",
"openssl-sys",
"pkg-config",
]
@ -2606,20 +2671,6 @@ dependencies = [
"vcpkg",
]
[[package]]
name = "libssh2-sys"
version = "0.2.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df40b13fe7ea1be9b9dffa365a51273816c345fc1811478b57ed7d964fbfc4ce"
dependencies = [
"cc",
"libc",
"libz-sys",
"openssl-sys",
"pkg-config",
"vcpkg",
]
[[package]]
name = "libz-sys"
version = "1.1.2"
@ -3092,7 +3143,7 @@ dependencies = [
[[package]]
name = "nu"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"clap",
"ctrlc",
@ -3132,10 +3183,11 @@ dependencies = [
[[package]]
name = "nu-cli"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"Inflector",
"ansi_term 0.12.1",
"arboard",
"async-recursion",
"async-trait",
"base64 0.13.0",
@ -3146,7 +3198,6 @@ dependencies = [
"chrono",
"chrono-tz",
"clap",
"clipboard",
"codespan-reporting",
"csv",
"ctrlc",
@ -3181,6 +3232,7 @@ dependencies = [
"nu-plugin",
"nu-protocol",
"nu-source",
"nu-stream",
"nu-table",
"nu-test-support",
"nu-value-ext",
@ -3233,7 +3285,7 @@ dependencies = [
[[package]]
name = "nu-data"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"ansi_term 0.12.1",
"bigdecimal",
@ -3263,7 +3315,7 @@ dependencies = [
[[package]]
name = "nu-errors"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"ansi_term 0.12.1",
"bigdecimal",
@ -3282,7 +3334,7 @@ dependencies = [
[[package]]
name = "nu-json"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"lazy_static 1.4.0",
"num-traits 0.1.43",
@ -3292,7 +3344,7 @@ dependencies = [
[[package]]
name = "nu-parser"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"bigdecimal",
"codespan-reporting",
@ -3310,7 +3362,7 @@ dependencies = [
[[package]]
name = "nu-plugin"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"bigdecimal",
"indexmap",
@ -3326,7 +3378,7 @@ dependencies = [
[[package]]
name = "nu-protocol"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"bigdecimal",
"byte-unit",
@ -3349,7 +3401,7 @@ dependencies = [
[[package]]
name = "nu-source"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"derive-new",
"getset",
@ -3358,17 +3410,28 @@ dependencies = [
"termcolor",
]
[[package]]
name = "nu-stream"
version = "0.25.0"
dependencies = [
"futures 0.3.8",
"nu-errors",
"nu-protocol",
"nu-source",
]
[[package]]
name = "nu-table"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"ansi_term 0.12.1",
"regex 1.4.2",
"unicode-width",
]
[[package]]
name = "nu-test-support"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"bigdecimal",
"chrono",
@ -3386,7 +3449,7 @@ dependencies = [
[[package]]
name = "nu-value-ext"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"indexmap",
"itertools",
@ -3398,11 +3461,11 @@ dependencies = [
[[package]]
name = "nu_plugin_binaryview"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"ansi_term 0.12.1",
"crossterm 0.18.2",
"image",
"image 0.22.5",
"neso",
"nu-errors",
"nu-plugin",
@ -3414,7 +3477,7 @@ dependencies = [
[[package]]
name = "nu_plugin_chart"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"crossterm 0.18.2",
"nu-cli",
@ -3429,7 +3492,7 @@ dependencies = [
[[package]]
name = "nu_plugin_fetch"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"base64 0.12.3",
"futures 0.3.8",
@ -3443,7 +3506,7 @@ dependencies = [
[[package]]
name = "nu_plugin_from_bson"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"bigdecimal",
"bson",
@ -3457,7 +3520,7 @@ dependencies = [
[[package]]
name = "nu_plugin_from_sqlite"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"bigdecimal",
"nu-errors",
@ -3472,7 +3535,7 @@ dependencies = [
[[package]]
name = "nu_plugin_inc"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"nu-errors",
"nu-plugin",
@ -3485,7 +3548,7 @@ dependencies = [
[[package]]
name = "nu_plugin_match"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"nu-errors",
"nu-plugin",
@ -3496,7 +3559,7 @@ dependencies = [
[[package]]
name = "nu_plugin_post"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"base64 0.12.3",
"futures 0.3.8",
@ -3512,7 +3575,7 @@ dependencies = [
[[package]]
name = "nu_plugin_ps"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"futures 0.3.8",
"futures-timer",
@ -3526,7 +3589,7 @@ dependencies = [
[[package]]
name = "nu_plugin_s3"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"futures 0.3.8",
"nu-errors",
@ -3538,7 +3601,7 @@ dependencies = [
[[package]]
name = "nu_plugin_selector"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"nipper",
"nu-errors",
@ -3550,7 +3613,7 @@ dependencies = [
[[package]]
name = "nu_plugin_start"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"glob",
"nu-errors",
@ -3563,7 +3626,7 @@ dependencies = [
[[package]]
name = "nu_plugin_sys"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"battery",
"futures 0.3.8",
@ -3578,7 +3641,7 @@ dependencies = [
[[package]]
name = "nu_plugin_textview"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"ansi_term 0.12.1",
"bat",
@ -3593,7 +3656,7 @@ dependencies = [
[[package]]
name = "nu_plugin_to_bson"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"bson",
"nu-errors",
@ -3606,7 +3669,7 @@ dependencies = [
[[package]]
name = "nu_plugin_to_sqlite"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"hex 0.4.2",
"nu-errors",
@ -3621,7 +3684,7 @@ dependencies = [
[[package]]
name = "nu_plugin_tree"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"derive-new",
"nu-errors",
@ -3633,7 +3696,7 @@ dependencies = [
[[package]]
name = "nu_plugin_xpath"
version = "0.24.1"
version = "0.25.0"
dependencies = [
"bigdecimal",
"indexmap",
@ -4228,10 +4291,22 @@ checksum = "ef859a23054bbfee7811284275ae522f0434a3c8e7f4b74bd4a35ae7e1c4a283"
dependencies = [
"bitflags",
"crc32fast",
"deflate",
"deflate 0.7.20",
"inflate",
]
[[package]]
name = "png"
version = "0.16.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c3287920cb847dee3de33d301c463fba14dda99db24214ddf93f83d3021f4c6"
dependencies = [
"bitflags",
"crc32fast",
"deflate 0.8.6",
"miniz_oxide 0.3.7",
]
[[package]]
name = "polling"
version = "1.1.0"
@ -4720,9 +4795,9 @@ dependencies = [
[[package]]
name = "rusqlite"
version = "0.24.1"
version = "0.24.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7e3d4791ab5517217f51216a84a688b53c1ebf7988736469c538d02f46ddba68"
checksum = "d5f38ee71cbab2c827ec0ac24e76f82eca723cee92c509a65f67dee393c25112"
dependencies = [
"bitflags",
"fallible-iterator",
@ -5182,9 +5257,9 @@ dependencies = [
[[package]]
name = "shadow-rs"
version = "0.3.20"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa99d59fca26677e3582f69069226abb6b2ce7e02d1068bcfd57510fa16ab87"
checksum = "73d9796f3ab288d5f5a06430723004eeb134c6a0bb3674ba4a124c557f137127"
dependencies = [
"chrono",
"git2",
@ -5323,6 +5398,12 @@ version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8207e78455ffdf55661170876f88daf85356e4edd54e0a3dbc79586ca1e50cbe"
[[package]]
name = "str-buf"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d44a3643b4ff9caf57abcee9c2c621d6c03d9135e0d8b589bd9afb5992cb176a"
[[package]]
name = "string_cache"
version = "0.8.1"
@ -5585,6 +5666,17 @@ dependencies = [
"lazy_static 1.4.0",
]
[[package]]
name = "tiff"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a53f4706d65497df0c4349241deddf35f84cee19c87ed86ea8ca590f4464437"
dependencies = [
"jpeg-decoder",
"miniz_oxide 0.4.3",
"weezl",
]
[[package]]
name = "time"
version = "0.1.44"
@ -6344,6 +6436,12 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "weezl"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e2bb9fc8309084dd7cd651336673844c1d47f8ef6d2091ec160b27f5c4aa277"
[[package]]
name = "wepoll-sys-stjepang"
version = "1.0.8"
@ -6450,20 +6548,11 @@ dependencies = [
"pkg-config",
]
[[package]]
name = "x11-clipboard"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89bd49c06c9eb5d98e6ba6536cf64ac9f7ee3a009b2f53996d405b3944f6bcea"
dependencies = [
"xcb",
]
[[package]]
name = "xcb"
version = "0.8.2"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e917a3f24142e9ff8be2414e36c649d47d6cc2ba81f16201cdef96e533e02de"
checksum = "62056f63138b39116f82a540c983cc11f1c90cd70b3d492a70c25eaa50bd22a6"
dependencies = [
"libc",
"log 0.4.11",

View File

@ -10,7 +10,7 @@ license = "MIT"
name = "nu"
readme = "README.md"
repository = "https://github.com/nushell/nushell"
version = "0.24.1"
version = "0.25.0"
[workspace]
members = ["crates/*/"]
@ -18,33 +18,33 @@ members = ["crates/*/"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
nu-cli = {version = "0.24.1", path = "./crates/nu-cli"}
nu-data = {version = "0.24.1", path = "./crates/nu-data"}
nu-errors = {version = "0.24.1", path = "./crates/nu-errors"}
nu-parser = {version = "0.24.1", path = "./crates/nu-parser"}
nu-plugin = {version = "0.24.1", path = "./crates/nu-plugin"}
nu-protocol = {version = "0.24.1", path = "./crates/nu-protocol"}
nu-source = {version = "0.24.1", path = "./crates/nu-source"}
nu-value-ext = {version = "0.24.1", path = "./crates/nu-value-ext"}
nu-cli = {version = "0.25.0", path = "./crates/nu-cli"}
nu-data = {version = "0.25.0", path = "./crates/nu-data"}
nu-errors = {version = "0.25.0", path = "./crates/nu-errors"}
nu-parser = {version = "0.25.0", path = "./crates/nu-parser"}
nu-plugin = {version = "0.25.0", path = "./crates/nu-plugin"}
nu-protocol = {version = "0.25.0", path = "./crates/nu-protocol"}
nu-source = {version = "0.25.0", path = "./crates/nu-source"}
nu-value-ext = {version = "0.25.0", path = "./crates/nu-value-ext"}
nu_plugin_binaryview = {version = "0.24.1", path = "./crates/nu_plugin_binaryview", optional = true}
nu_plugin_chart = {version = "0.24.1", path = "./crates/nu_plugin_chart", optional = true}
nu_plugin_fetch = {version = "0.24.1", path = "./crates/nu_plugin_fetch", optional = true}
nu_plugin_from_bson = {version = "0.24.1", path = "./crates/nu_plugin_from_bson", optional = true}
nu_plugin_from_sqlite = {version = "0.24.1", path = "./crates/nu_plugin_from_sqlite", optional = true}
nu_plugin_inc = {version = "0.24.1", path = "./crates/nu_plugin_inc", optional = true}
nu_plugin_match = {version = "0.24.1", path = "./crates/nu_plugin_match", optional = true}
nu_plugin_post = {version = "0.24.1", path = "./crates/nu_plugin_post", optional = true}
nu_plugin_ps = {version = "0.24.1", path = "./crates/nu_plugin_ps", optional = true}
nu_plugin_s3 = {version = "0.24.1", path = "./crates/nu_plugin_s3", optional = true}
nu_plugin_start = {version = "0.24.1", path = "./crates/nu_plugin_start", optional = true}
nu_plugin_sys = {version = "0.24.1", path = "./crates/nu_plugin_sys", optional = true}
nu_plugin_textview = {version = "0.24.1", path = "./crates/nu_plugin_textview", optional = true}
nu_plugin_to_bson = {version = "0.24.1", path = "./crates/nu_plugin_to_bson", optional = true}
nu_plugin_to_sqlite = {version = "0.24.1", path = "./crates/nu_plugin_to_sqlite", optional = true}
nu_plugin_tree = {version = "0.24.1", path = "./crates/nu_plugin_tree", optional = true}
nu_plugin_xpath = {version = "0.24.1", path = "./crates/nu_plugin_xpath", optional = true}
nu_plugin_selector = {version = "0.24.1", path = "./crates/nu_plugin_selector", optional = true}
nu_plugin_binaryview = {version = "0.25.0", path = "./crates/nu_plugin_binaryview", optional = true}
nu_plugin_chart = {version = "0.25.0", path = "./crates/nu_plugin_chart", optional = true}
nu_plugin_fetch = {version = "0.25.0", path = "./crates/nu_plugin_fetch", optional = true}
nu_plugin_from_bson = {version = "0.25.0", path = "./crates/nu_plugin_from_bson", optional = true}
nu_plugin_from_sqlite = {version = "0.25.0", path = "./crates/nu_plugin_from_sqlite", optional = true}
nu_plugin_inc = {version = "0.25.0", path = "./crates/nu_plugin_inc", optional = true}
nu_plugin_match = {version = "0.25.0", path = "./crates/nu_plugin_match", optional = true}
nu_plugin_post = {version = "0.25.0", path = "./crates/nu_plugin_post", optional = true}
nu_plugin_ps = {version = "0.25.0", path = "./crates/nu_plugin_ps", optional = true}
nu_plugin_s3 = {version = "0.25.0", path = "./crates/nu_plugin_s3", optional = true}
nu_plugin_start = {version = "0.25.0", path = "./crates/nu_plugin_start", optional = true}
nu_plugin_sys = {version = "0.25.0", path = "./crates/nu_plugin_sys", optional = true}
nu_plugin_textview = {version = "0.25.0", path = "./crates/nu_plugin_textview", optional = true}
nu_plugin_to_bson = {version = "0.25.0", path = "./crates/nu_plugin_to_bson", optional = true}
nu_plugin_to_sqlite = {version = "0.25.0", path = "./crates/nu_plugin_to_sqlite", optional = true}
nu_plugin_tree = {version = "0.25.0", path = "./crates/nu_plugin_tree", optional = true}
nu_plugin_xpath = {version = "0.25.0", path = "./crates/nu_plugin_xpath", optional = true}
nu_plugin_selector = {version = "0.25.0", path = "./crates/nu_plugin_selector", optional = true}
# Required to bootstrap the main binary
clap = "2.33.3"
@ -56,7 +56,7 @@ itertools = "0.9.0"
[dev-dependencies]
dunce = "1.0.1"
nu-test-support = {version = "0.24.1", path = "./crates/nu-test-support"}
nu-test-support = {version = "0.25.0", path = "./crates/nu-test-support"}
[build-dependencies]

View File

@ -64,7 +64,7 @@ To install Nu via cargo (make sure you have installed [rustup](https://rustup.rs
cargo install nu
```
You can also build Nu yourself with all the bells and whistles (be sure to have installed the [dependencies](https://www.nushell.sh/book/en/installation.html#dependencies) for your platform), once you have checked out this repo with git:
You can also build Nu yourself with all the bells and whistles (be sure to have installed the [dependencies](https://www.nushell.sh/book/installation.html#dependencies) for your platform), once you have checked out this repo with git:
```bash
cargo build --workspace --features=extra
@ -234,7 +234,7 @@ Here we use the variable `$it` to refer to the value being piped to the external
### Configuration
Nu has early support for configuring the shell. You can refer to the book for a list of [all supported variables](https://www.nushell.sh/book/en/configuration.html).
Nu has early support for configuring the shell. You can refer to the book for a list of [all supported variables](https://www.nushell.sh/book/configuration.html).
To set one of these variables, you can use `config set`. For example:

View File

@ -1,28 +1,31 @@
[package]
authors = ["The Nu Project Contributors"]
build = "build.rs"
description = "CLI for nushell"
edition = "2018"
license = "MIT"
name = "nu-cli"
version = "0.24.1"
build = "build.rs"
version = "0.25.0"
[lib]
doctest = false
[dependencies]
nu-data = {version = "0.24.1", path = "../nu-data"}
nu-errors = {version = "0.24.1", path = "../nu-errors"}
nu-json = {version = "0.24.1", path = "../nu-json"}
nu-parser = {version = "0.24.1", path = "../nu-parser"}
nu-plugin = {version = "0.24.1", path = "../nu-plugin"}
nu-protocol = {version = "0.24.1", path = "../nu-protocol"}
nu-source = {version = "0.24.1", path = "../nu-source"}
nu-table = {version = "0.24.1", path = "../nu-table"}
nu-test-support = {version = "0.24.1", path = "../nu-test-support"}
nu-value-ext = {version = "0.24.1", path = "../nu-value-ext"}
nu-data = {version = "0.25.0", path = "../nu-data"}
nu-errors = {version = "0.25.0", path = "../nu-errors"}
nu-json = {version = "0.25.0", path = "../nu-json"}
nu-parser = {version = "0.25.0", path = "../nu-parser"}
nu-plugin = {version = "0.25.0", path = "../nu-plugin"}
nu-protocol = {version = "0.25.0", path = "../nu-protocol"}
nu-source = {version = "0.25.0", path = "../nu-source"}
nu-stream = {version = "0.25.0", path = "../nu-stream"}
nu-table = {version = "0.25.0", path = "../nu-table"}
nu-test-support = {version = "0.25.0", path = "../nu-test-support"}
nu-value-ext = {version = "0.25.0", path = "../nu-value-ext"}
Inflector = "0.11"
ansi_term = "0.12.1"
arboard = {version = "1.1.0", optional = true}
async-recursion = "0.3.1"
async-trait = "0.1.40"
base64 = "0.13.0"
@ -33,7 +36,6 @@ calamine = "0.16.1"
chrono = {version = "0.4.15", features = ["serde"]}
chrono-tz = "0.5.3"
clap = "2.33.3"
clipboard = {version = "0.5.0", optional = true}
codespan-reporting = "0.9.5"
csv = "1.1.3"
ctrlc = {version = "3.1.6", optional = true}
@ -47,8 +49,8 @@ encoding_rs = "0.8.24"
filesize = "0.2.0"
fs_extra = "1.2.0"
futures = {version = "0.3.5", features = ["compat", "io-compat"]}
futures_codec = "0.4.1"
futures-util = "0.3.5"
futures_codec = "0.4.1"
getset = "0.1.1"
git2 = {version = "0.13.11", default_features = false, optional = true}
glob = "0.3.0"
@ -57,7 +59,6 @@ htmlescape = "0.3.1"
ical = "0.6.0"
ichwh = {version = "0.3.4", optional = true}
indexmap = {version = "1.6.0", features = ["serde-1"]}
Inflector = "0.11"
itertools = "0.9.0"
lazy_static = "1.*"
log = "0.4.11"
@ -115,17 +116,17 @@ users = "0.10.0"
[dependencies.rusqlite]
features = ["bundled", "blob"]
optional = true
version = "0.24.1"
version = "0.24.2"
[build-dependencies]
shadow-rs = "0.3.20"
shadow-rs = "0.5"
[dev-dependencies]
quickcheck = "0.9.2"
quickcheck_macros = "0.9.1"
[features]
clipboard-cli = ["clipboard"]
clipboard-cli = ["arboard"]
rich-benchmark = ["heim"]
rustyline-support = ["rustyline"]
stable = []

View File

@ -1,6 +1,3 @@
fn main() -> shadow_rs::SdResult<()> {
let src_path = std::env::var("CARGO_MANIFEST_DIR")?;
let out_path = std::env::var("OUT_DIR")?;
shadow_rs::Shadow::build(src_path, out_path)?;
Ok(())
shadow_rs::new()
}

View File

@ -1,17 +1,19 @@
use crate::commands::classified::block::run_block;
use crate::commands::classified::maybe_text_codec::{MaybeTextCodec, StringOrBinary};
use crate::commands::default_context::create_default_context;
use crate::evaluation_context::EvaluationContext;
use crate::path::canonicalize;
use crate::prelude::*;
use crate::script::{print_err, run_script_standalone};
#[allow(unused_imports)]
pub(crate) use crate::script::{process_script, LineResult};
#[cfg(feature = "rustyline-support")]
use crate::shell::Helper;
use crate::EnvironmentSyncer;
use futures_codec::FramedRead;
use nu_errors::ShellError;
use nu_protocol::hir::{ClassifiedCommand, Expression, InternalCommand, Literal, NamedArguments};
use nu_protocol::{Primitive, ReturnSuccess, Scope, UntaggedValue, Value};
use nu_parser::ParserScope;
use nu_protocol::{UntaggedValue, Value};
use log::{debug, trace};
#[cfg(feature = "rustyline-support")]
use rustyline::{
self,
@ -22,8 +24,7 @@ use rustyline::{
};
use std::error::Error;
use std::iter::Iterator;
use std::path::{Path, PathBuf};
use std::sync::atomic::Ordering;
use std::path::PathBuf;
pub fn search_paths() -> Vec<std::path::PathBuf> {
use std::env;
@ -56,250 +57,8 @@ pub fn search_paths() -> Vec<std::path::PathBuf> {
search_paths
}
pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Box<dyn Error>> {
let mut context = EvaluationContext::basic()?;
{
use crate::commands::*;
context.add_commands(vec![
whole_stream_command(NuPlugin),
// System/file operations
whole_stream_command(Exec),
whole_stream_command(Pwd),
whole_stream_command(Ls),
whole_stream_command(Du),
whole_stream_command(Cd),
whole_stream_command(Remove),
whole_stream_command(Open),
whole_stream_command(Config),
whole_stream_command(ConfigGet),
whole_stream_command(ConfigSet),
whole_stream_command(ConfigSetInto),
whole_stream_command(ConfigClear),
whole_stream_command(ConfigLoad),
whole_stream_command(ConfigRemove),
whole_stream_command(ConfigPath),
whole_stream_command(Help),
whole_stream_command(History),
whole_stream_command(Save),
whole_stream_command(Touch),
whole_stream_command(Cpy),
whole_stream_command(Date),
whole_stream_command(DateListTimeZone),
whole_stream_command(DateNow),
whole_stream_command(DateToTable),
whole_stream_command(DateToTimeZone),
whole_stream_command(DateFormat),
whole_stream_command(Cal),
whole_stream_command(Mkdir),
whole_stream_command(Mv),
whole_stream_command(Kill),
whole_stream_command(Version),
whole_stream_command(Clear),
whole_stream_command(Describe),
whole_stream_command(Which),
whole_stream_command(Debug),
whole_stream_command(Alias),
whole_stream_command(WithEnv),
whole_stream_command(Do),
whole_stream_command(Sleep),
// Statistics
whole_stream_command(Size),
whole_stream_command(Count),
whole_stream_command(Benchmark),
// Metadata
whole_stream_command(Tags),
// Shells
whole_stream_command(Next),
whole_stream_command(Previous),
whole_stream_command(Shells),
whole_stream_command(Enter),
whole_stream_command(Exit),
// Viz
whole_stream_command(Chart),
// Viewers
whole_stream_command(Autoview),
whole_stream_command(Table),
// Text manipulation
whole_stream_command(Hash),
whole_stream_command(HashBase64),
whole_stream_command(Split),
whole_stream_command(SplitColumn),
whole_stream_command(SplitRow),
whole_stream_command(SplitChars),
whole_stream_command(Lines),
whole_stream_command(Echo),
whole_stream_command(Parse),
whole_stream_command(Str),
whole_stream_command(StrToDecimal),
whole_stream_command(StrToInteger),
whole_stream_command(StrDowncase),
whole_stream_command(StrUpcase),
whole_stream_command(StrCapitalize),
whole_stream_command(StrFindReplace),
whole_stream_command(StrFrom),
whole_stream_command(StrSubstring),
whole_stream_command(StrSet),
whole_stream_command(StrToDatetime),
whole_stream_command(StrContains),
whole_stream_command(StrIndexOf),
whole_stream_command(StrTrim),
whole_stream_command(StrTrimLeft),
whole_stream_command(StrTrimRight),
whole_stream_command(StrStartsWith),
whole_stream_command(StrEndsWith),
whole_stream_command(StrCollect),
whole_stream_command(StrLength),
whole_stream_command(StrLPad),
whole_stream_command(StrReverse),
whole_stream_command(StrRPad),
whole_stream_command(StrCamelCase),
whole_stream_command(StrPascalCase),
whole_stream_command(StrKebabCase),
whole_stream_command(StrSnakeCase),
whole_stream_command(StrScreamingSnakeCase),
whole_stream_command(BuildString),
whole_stream_command(Ansi),
whole_stream_command(Char),
// Column manipulation
whole_stream_command(Move),
whole_stream_command(Reject),
whole_stream_command(Select),
whole_stream_command(Get),
whole_stream_command(Update),
whole_stream_command(Insert),
whole_stream_command(IntoInt),
whole_stream_command(SplitBy),
// Row manipulation
whole_stream_command(Reverse),
whole_stream_command(Append),
whole_stream_command(Prepend),
whole_stream_command(SortBy),
whole_stream_command(GroupBy),
whole_stream_command(GroupByDate),
whole_stream_command(First),
whole_stream_command(Last),
whole_stream_command(Every),
whole_stream_command(Nth),
whole_stream_command(Drop),
whole_stream_command(Format),
whole_stream_command(FileSize),
whole_stream_command(Where),
whole_stream_command(If),
whole_stream_command(Compact),
whole_stream_command(Default),
whole_stream_command(Skip),
whole_stream_command(SkipUntil),
whole_stream_command(SkipWhile),
whole_stream_command(Keep),
whole_stream_command(KeepUntil),
whole_stream_command(KeepWhile),
whole_stream_command(Range),
whole_stream_command(Rename),
whole_stream_command(Uniq),
whole_stream_command(Each),
whole_stream_command(EachGroup),
whole_stream_command(EachWindow),
whole_stream_command(Empty),
// Table manipulation
whole_stream_command(Flatten),
whole_stream_command(Move),
whole_stream_command(Merge),
whole_stream_command(Shuffle),
whole_stream_command(Wrap),
whole_stream_command(Pivot),
whole_stream_command(Headers),
whole_stream_command(Reduce),
// Data processing
whole_stream_command(Histogram),
whole_stream_command(Autoenv),
whole_stream_command(AutoenvTrust),
whole_stream_command(AutoenvUnTrust),
whole_stream_command(Math),
whole_stream_command(MathAbs),
whole_stream_command(MathAverage),
whole_stream_command(MathEval),
whole_stream_command(MathMedian),
whole_stream_command(MathMinimum),
whole_stream_command(MathMode),
whole_stream_command(MathMaximum),
whole_stream_command(MathStddev),
whole_stream_command(MathSummation),
whole_stream_command(MathVariance),
whole_stream_command(MathProduct),
whole_stream_command(MathRound),
whole_stream_command(MathFloor),
whole_stream_command(MathCeil),
// File format output
whole_stream_command(To),
whole_stream_command(ToCSV),
whole_stream_command(ToHTML),
whole_stream_command(ToJSON),
whole_stream_command(ToMarkdown),
whole_stream_command(ToTOML),
whole_stream_command(ToTSV),
whole_stream_command(ToURL),
whole_stream_command(ToYAML),
whole_stream_command(ToXML),
// File format input
whole_stream_command(From),
whole_stream_command(FromCSV),
whole_stream_command(FromEML),
whole_stream_command(FromTSV),
whole_stream_command(FromSSV),
whole_stream_command(FromINI),
whole_stream_command(FromJSON),
whole_stream_command(FromODS),
whole_stream_command(FromTOML),
whole_stream_command(FromURL),
whole_stream_command(FromXLSX),
whole_stream_command(FromXML),
whole_stream_command(FromYAML),
whole_stream_command(FromYML),
whole_stream_command(FromIcs),
whole_stream_command(FromVcf),
// "Private" commands (not intended to be accessed directly)
whole_stream_command(RunExternalCommand { interactive }),
// Random value generation
whole_stream_command(Random),
whole_stream_command(RandomBool),
whole_stream_command(RandomDice),
#[cfg(feature = "uuid_crate")]
whole_stream_command(RandomUUID),
whole_stream_command(RandomInteger),
whole_stream_command(RandomDecimal),
whole_stream_command(RandomChars),
// Path
whole_stream_command(PathBasename),
whole_stream_command(PathCommand),
whole_stream_command(PathDirname),
whole_stream_command(PathExists),
whole_stream_command(PathExpand),
whole_stream_command(PathExtension),
whole_stream_command(PathFilestem),
whole_stream_command(PathType),
// Url
whole_stream_command(UrlCommand),
whole_stream_command(UrlScheme),
whole_stream_command(UrlPath),
whole_stream_command(UrlHost),
whole_stream_command(UrlQuery),
whole_stream_command(Seq),
whole_stream_command(SeqDates),
]);
#[cfg(feature = "clipboard-cli")]
{
context.add_commands(vec![whole_stream_command(crate::commands::clip::Clip)]);
}
}
Ok(context)
}
pub async fn run_vec_of_pipelines(
pipelines: Vec<String>,
pub async fn run_script_file(
file_contents: String,
redirect_stdin: bool,
) -> Result<(), Box<dyn Error>> {
let mut syncer = EnvironmentSyncer::new();
@ -321,9 +80,7 @@ pub async fn run_vec_of_pipelines(
let _ = run_startup_commands(&mut context, &config).await;
for pipeline in pipelines {
run_pipeline_standalone(pipeline, redirect_stdin, &mut context, true).await?;
}
run_script_standalone(file_contents, redirect_stdin, &context, true).await?;
Ok(())
}
@ -368,9 +125,15 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box<dyn Error>> {
let _ = run_startup_commands(&mut context, &configuration).await;
// Give ourselves a scope to work in
context.scope.enter_scope();
let history_path = crate::commands::history::history_path(&configuration);
let _ = rl.load_history(&history_path);
let mut session_text = String::new();
let mut line_start: usize = 0;
let skip_welcome_message = configuration
.var("skip_welcome_message")
.map(|x| x.is_true())
@ -401,10 +164,13 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box<dyn Error>> {
if let Some(prompt) = configuration.var("prompt") {
let prompt_line = prompt.as_string()?;
let (result, err) = nu_parser::lite_parse(&prompt_line, 0);
context.scope.enter_scope();
let (prompt_block, err) = nu_parser::parse(&prompt_line, 0, &context.scope);
if err.is_some() {
use crate::git::current_branch;
context.scope.exit_scope();
format!(
"\x1b[32m{}{}\x1b[m> ",
cwd,
@ -414,18 +180,12 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box<dyn Error>> {
}
)
} else {
let prompt_block = nu_parser::classify_block(&result, context.registry());
// let env = context.get_env();
let env = context.get_env();
let run_result = run_block(&prompt_block, &context, InputStream::empty()).await;
context.scope.exit_scope();
match run_block(
&prompt_block.block,
&mut context,
InputStream::empty(),
Scope::from_env(env),
)
.await
{
match run_result {
Ok(result) => match result.collect_string(Tag::unknown()).await {
Ok(string_result) => {
let errors = context.get_errors();
@ -482,8 +242,23 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box<dyn Error>> {
initial_command = None;
}
if let Ok(line) = &readline {
line_start = session_text.len();
session_text.push_str(line);
session_text.push('\n');
}
let line = match convert_rustyline_result_to_string(readline) {
LineResult::Success(s) => process_line(&s, &mut context, false, true).await,
LineResult::Success(_) => {
process_script(
&session_text[line_start..],
&context,
false,
line_start,
true,
)
.await
}
x => x,
};
@ -509,7 +284,7 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box<dyn Error>> {
LineResult::Success(line) => {
rl.add_history_entry(&line);
let _ = rl.save_history(&history_path);
context.maybe_print_errors(Text::from(line));
context.maybe_print_errors(Text::from(session_text.clone()));
}
LineResult::ClearHistory => {
@ -522,10 +297,10 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box<dyn Error>> {
let _ = rl.save_history(&history_path);
context.with_host(|_host| {
print_err(err, &Text::from(line.clone()));
print_err(err, &Text::from(session_text.clone()));
});
context.maybe_print_errors(Text::from(line.clone()));
context.maybe_print_errors(Text::from(session_text.clone()));
}
LineResult::CtrlC => {
@ -610,8 +385,7 @@ async fn run_startup_commands(
} => {
for pipeline in pipelines {
if let Ok(pipeline_string) = pipeline.as_string() {
let _ =
run_pipeline_standalone(pipeline_string, false, context, false).await;
let _ = run_script_standalone(pipeline_string, false, context, false).await;
}
}
}
@ -626,50 +400,6 @@ async fn run_startup_commands(
Ok(())
}
pub async fn run_pipeline_standalone(
pipeline: String,
redirect_stdin: bool,
context: &mut EvaluationContext,
exit_on_error: bool,
) -> Result<(), Box<dyn Error>> {
let line = process_line(&pipeline, context, redirect_stdin, false).await;
match line {
LineResult::Success(line) => {
let error_code = {
let errors = context.current_errors.clone();
let errors = errors.lock();
if errors.len() > 0 {
1
} else {
0
}
};
context.maybe_print_errors(Text::from(line));
if error_code != 0 && exit_on_error {
std::process::exit(error_code);
}
}
LineResult::Error(line, err) => {
context.with_host(|_host| {
print_err(err, &Text::from(line.clone()));
});
context.maybe_print_errors(Text::from(line));
if exit_on_error {
std::process::exit(1);
}
}
_ => {}
}
Ok(())
}
#[cfg(feature = "rustyline-support")]
fn default_rustyline_editor_configuration() -> Editor<Helper> {
#[cfg(windows)]
@ -851,272 +581,42 @@ fn rustyline_hinter(config: &dyn nu_data::config::Conf) -> Option<rustyline::hin
Some(rustyline::hint::HistoryHinter {})
}
fn chomp_newline(s: &str) -> &str {
if let Some(s) = s.strip_suffix('\n') {
s
} else {
s
}
}
#[derive(Debug)]
pub enum LineResult {
Success(String),
Error(String, ShellError),
Break,
CtrlC,
CtrlD,
ClearHistory,
}
pub async fn parse_and_eval(line: &str, ctx: &mut EvaluationContext) -> Result<String, ShellError> {
pub async fn parse_and_eval(line: &str, ctx: &EvaluationContext) -> Result<String, ShellError> {
// FIXME: do we still need this?
let line = if let Some(s) = line.strip_suffix('\n') {
s
} else {
line
};
let (lite_result, err) = nu_parser::lite_parse(&line, 0);
// TODO ensure the command whose examples we're testing is actually in the pipeline
ctx.scope.enter_scope();
let (classified_block, err) = nu_parser::parse(&line, 0, &ctx.scope);
if let Some(err) = err {
ctx.scope.exit_scope();
return Err(err.into());
}
// TODO ensure the command whose examples we're testing is actually in the pipeline
let classified_block = nu_parser::classify_block(&lite_result, ctx.registry());
let input_stream = InputStream::empty();
let env = ctx.get_env();
ctx.scope.add_env(env);
run_block(
&classified_block.block,
ctx,
input_stream,
Scope::from_env(env),
)
.await?
.collect_string(Tag::unknown())
.await
.map(|x| x.item)
}
let result = run_block(&classified_block, ctx, input_stream).await;
ctx.scope.exit_scope();
/// Process the line by parsing the text to turn it into commands, classify those commands so that we understand what is being called in the pipeline, and then run this pipeline
pub async fn process_line(
line: &str,
ctx: &mut EvaluationContext,
redirect_stdin: bool,
cli_mode: bool,
) -> LineResult {
if line.trim() == "" {
LineResult::Success(line.to_string())
} else {
let line = chomp_newline(line);
ctx.raw_input = line.to_string();
let (result, err) = nu_parser::lite_parse(&line, 0);
if let Some(err) = err {
return LineResult::Error(line.to_string(), err.into());
}
debug!("=== Parsed ===");
debug!("{:#?}", result);
let classified_block = nu_parser::classify_block(&result, ctx.registry());
debug!("{:#?}", classified_block);
//println!("{:#?}", pipeline);
if let Some(failure) = classified_block.failed {
return LineResult::Error(line.to_string(), failure.into());
}
// There's a special case to check before we process the pipeline:
// If we're giving a path by itself
// ...and it's not a command in the path
// ...and it doesn't have any arguments
// ...and we're in the CLI
// ...then change to this directory
if cli_mode
&& classified_block.block.block.len() == 1
&& classified_block.block.block[0].list.len() == 1
{
if let ClassifiedCommand::Internal(InternalCommand {
ref name, ref args, ..
}) = classified_block.block.block[0].list[0]
{
let internal_name = name;
let name = args
.positional
.as_ref()
.and_then(|potionals| {
potionals.get(0).map(|e| {
if let Expression::Literal(Literal::String(ref s)) = e.expr {
&s
} else {
""
}
})
})
.unwrap_or("");
if internal_name == "run_external"
&& args
.positional
.as_ref()
.map(|ref v| v.len() == 1)
.unwrap_or(true)
&& args
.named
.as_ref()
.map(NamedArguments::is_empty)
.unwrap_or(true)
&& canonicalize(ctx.shell_manager.path(), name).is_ok()
&& Path::new(&name).is_dir()
&& !crate::commands::classified::external::did_find_command(&name)
{
// Here we work differently if we're in Windows because of the expected Windows behavior
#[cfg(windows)]
{
if name.ends_with(':') {
// This looks like a drive shortcut. We need to a) switch drives and b) go back to the previous directory we were viewing on that drive
// But first, we need to save where we are now
let current_path = ctx.shell_manager.path();
let split_path: Vec<_> = current_path.split(':').collect();
if split_path.len() > 1 {
ctx.windows_drives_previous_cwd
.lock()
.insert(split_path[0].to_string(), current_path);
}
let name = name.to_uppercase();
let new_drive: Vec<_> = name.split(':').collect();
if let Some(val) =
ctx.windows_drives_previous_cwd.lock().get(new_drive[0])
{
ctx.shell_manager.set_path(val.to_string());
return LineResult::Success(line.to_string());
} else {
ctx.shell_manager
.set_path(format!("{}\\", name.to_string()));
return LineResult::Success(line.to_string());
}
} else {
ctx.shell_manager.set_path(name.to_string());
return LineResult::Success(line.to_string());
}
}
#[cfg(not(windows))]
{
ctx.shell_manager.set_path(name.to_string());
return LineResult::Success(line.to_string());
}
}
}
}
let input_stream = if redirect_stdin {
let file = futures::io::AllowStdIo::new(std::io::stdin());
let stream = FramedRead::new(file, MaybeTextCodec::default()).map(|line| {
if let Ok(line) = line {
let primitive = match line {
StringOrBinary::String(s) => Primitive::String(s),
StringOrBinary::Binary(b) => Primitive::Binary(b.into_iter().collect()),
};
Ok(Value {
value: UntaggedValue::Primitive(primitive),
tag: Tag::unknown(),
})
} else {
panic!("Internal error: could not read lines of text from stdin")
}
});
stream.to_input_stream()
} else {
InputStream::empty()
};
trace!("{:#?}", classified_block);
let env = ctx.get_env();
match run_block(
&classified_block.block,
ctx,
input_stream,
Scope::from_env(env),
)
.await
{
Ok(input) => {
// Running a pipeline gives us back a stream that we can then
// work through. At the top level, we just want to pull on the
// values to compute them.
use futures::stream::TryStreamExt;
let context = RunnableContext {
input,
shell_manager: ctx.shell_manager.clone(),
host: ctx.host.clone(),
ctrl_c: ctx.ctrl_c.clone(),
current_errors: ctx.current_errors.clone(),
registry: ctx.registry.clone(),
name: Tag::unknown(),
raw_input: line.to_string(),
};
if let Ok(mut output_stream) =
crate::commands::autoview::command::autoview(context).await
{
loop {
match output_stream.try_next().await {
Ok(Some(ReturnSuccess::Value(Value {
value: UntaggedValue::Error(e),
..
}))) => return LineResult::Error(line.to_string(), e),
Ok(Some(_item)) => {
if ctx.ctrl_c.load(Ordering::SeqCst) {
break;
}
}
Ok(None) => break,
Err(e) => return LineResult::Error(line.to_string(), e),
}
}
}
LineResult::Success(line.to_string())
}
Err(err) => LineResult::Error(line.to_string(), err),
}
}
}
pub fn print_err(err: ShellError, source: &Text) {
if let Some(diag) = err.into_diagnostic() {
let source = source.to_string();
let mut files = codespan_reporting::files::SimpleFiles::new();
files.add("shell", source);
let writer = codespan_reporting::term::termcolor::StandardStream::stderr(
codespan_reporting::term::termcolor::ColorChoice::Always,
);
let config = codespan_reporting::term::Config::default();
let _ = std::panic::catch_unwind(move || {
let _ = codespan_reporting::term::emit(&mut writer.lock(), &config, &files, &diag);
});
}
result?.collect_string(Tag::unknown()).await.map(|x| x.item)
}
#[cfg(test)]
mod tests {
#[quickcheck]
fn quickcheck_parse(data: String) -> bool {
let (lite_block, err) = nu_parser::lite_parse(&data, 0);
if err.is_none() {
let (tokens, err) = nu_parser::lex(&data, 0);
let (lite_block, err2) = nu_parser::group(tokens);
if err.is_none() && err2.is_none() {
let context = crate::evaluation_context::EvaluationContext::basic().unwrap();
let _ = nu_parser::classify_block(&lite_block, context.registry());
let _ = nu_parser::classify_block(&lite_block, &context.scope);
}
true
}

View File

@ -1,64 +0,0 @@
use crate::commands::Command;
use indexmap::IndexMap;
use nu_errors::ShellError;
use nu_parser::SignatureRegistry;
use nu_protocol::Signature;
use parking_lot::Mutex;
use std::sync::Arc;
#[derive(Debug, Clone, Default)]
pub struct CommandRegistry {
registry: Arc<Mutex<IndexMap<String, Command>>>,
}
impl SignatureRegistry for CommandRegistry {
fn has(&self, name: &str) -> bool {
let registry = self.registry.lock();
registry.contains_key(name)
}
fn get(&self, name: &str) -> Option<Signature> {
let registry = self.registry.lock();
registry.get(name).map(|command| command.signature())
}
fn clone_box(&self) -> Box<dyn SignatureRegistry> {
Box::new(self.clone())
}
}
impl CommandRegistry {
pub fn new() -> CommandRegistry {
CommandRegistry {
registry: Arc::new(Mutex::new(IndexMap::default())),
}
}
}
impl CommandRegistry {
pub fn get_command(&self, name: &str) -> Option<Command> {
let registry = self.registry.lock();
registry.get(name).cloned()
}
pub fn expect_command(&self, name: &str) -> Result<Command, ShellError> {
self.get_command(name).ok_or_else(|| {
ShellError::untagged_runtime_error(format!("Could not load command: {}", name))
})
}
pub fn has(&self, name: &str) -> bool {
let registry = self.registry.lock();
registry.contains_key(name)
}
pub fn insert(&mut self, name: impl Into<String>, command: Command) {
let mut registry = self.registry.lock();
registry.insert(name.into(), command);
}
pub fn names(&self) -> Vec<String> {
let registry = self.registry.lock();
registry.keys().cloned().collect()
}
}

View File

@ -4,7 +4,6 @@ pub(crate) mod macros;
mod from_delimited_data;
mod to_delimited_data;
pub(crate) mod alias;
pub(crate) mod ansi;
pub(crate) mod append;
pub(crate) mod args;
@ -29,7 +28,9 @@ pub(crate) mod count;
pub(crate) mod cp;
pub(crate) mod date;
pub(crate) mod debug;
pub(crate) mod def;
pub(crate) mod default;
pub(crate) mod default_context;
pub(crate) mod describe;
pub(crate) mod do_;
pub(crate) mod drop;
@ -72,6 +73,8 @@ pub(crate) mod insert;
pub(crate) mod into_int;
pub(crate) mod keep;
pub(crate) mod last;
pub(crate) mod let_;
pub(crate) mod let_env;
pub(crate) mod lines;
pub(crate) mod ls;
pub(crate) mod math;
@ -95,7 +98,6 @@ pub(crate) mod reject;
pub(crate) mod rename;
pub(crate) mod reverse;
pub(crate) mod rm;
pub(crate) mod run_alias;
pub(crate) mod run_external;
pub(crate) mod save;
pub(crate) mod select;
@ -107,6 +109,7 @@ pub(crate) mod size;
pub(crate) mod skip;
pub(crate) mod sleep;
pub(crate) mod sort_by;
pub(crate) mod source;
pub(crate) mod split;
pub(crate) mod split_by;
pub(crate) mod str_;
@ -137,7 +140,6 @@ pub(crate) use command::{
whole_stream_command, Command, Example, UnevaluatedCallInfo, WholeStreamCommand,
};
pub(crate) use alias::Alias;
pub(crate) use ansi::Ansi;
pub(crate) use append::Command as Append;
pub(crate) use autoenv::Autoenv;
@ -156,6 +158,7 @@ pub(crate) use count::Count;
pub(crate) use cp::Cpy;
pub(crate) use date::{Date, DateFormat, DateListTimeZone, DateNow, DateToTable, DateToTimeZone};
pub(crate) use debug::Debug;
pub(crate) use def::Def;
pub(crate) use default::Default;
pub(crate) use describe::Describe;
pub(crate) use do_::Do;
@ -209,6 +212,8 @@ pub(crate) use insert::Command as Insert;
pub(crate) use into_int::IntoInt;
pub(crate) use keep::{Keep, KeepUntil, KeepWhile};
pub(crate) use last::Last;
pub(crate) use let_::Let;
pub(crate) use let_env::LetEnv;
pub(crate) use lines::Lines;
pub(crate) use ls::Ls;
pub(crate) use math::{
@ -252,6 +257,7 @@ pub(crate) use size::Size;
pub(crate) use skip::{Skip, SkipUntil, SkipWhile};
pub(crate) use sleep::Sleep;
pub(crate) use sort_by::SortBy;
pub(crate) use source::Source;
pub(crate) use split::{Split, SplitChars, SplitColumn, SplitRow};
pub(crate) use split_by::SplitBy;
pub(crate) use str_::{

View File

@ -1,221 +0,0 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use crate::types::deduction::{VarDeclaration, VarSyntaxShapeDeductor};
use deduction_to_signature::DeductionToSignature;
use log::trace;
use nu_data::config;
use nu_errors::ShellError;
use nu_protocol::{
hir::Block, CommandAction, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use nu_source::Tagged;
pub struct Alias;
#[derive(Deserialize)]
pub struct AliasArgs {
pub name: Tagged<String>,
pub args: Vec<Value>,
pub block: Block,
pub infer: Option<bool>,
pub save: Option<bool>,
}
#[async_trait]
impl WholeStreamCommand for Alias {
fn name(&self) -> &str {
"alias"
}
fn signature(&self) -> Signature {
Signature::build("alias")
.required("name", SyntaxShape::String, "the name of the alias")
.required("args", SyntaxShape::Table, "the arguments to the alias")
.required(
"block",
SyntaxShape::Block,
"the block to run as the body of the alias",
)
.switch("infer", "infer argument types (experimental)", Some('i'))
.switch("save", "save the alias to your config", Some('s'))
}
fn usage(&self) -> &str {
"Define a shortcut for another command."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
alias(args, registry).await
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "An alias without parameters",
example: "alias say-hi [] { echo 'Hello!' }",
result: None,
},
Example {
description: "An alias with a single parameter",
example: "alias l [x] { ls $x }",
result: None,
},
]
}
}
pub async fn alias(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let mut raw_input = args.raw_input.clone();
let (
AliasArgs {
name,
args: list,
block,
infer,
save,
},
_ctx,
) = args.process(&registry).await?;
if let Some(true) = save {
let mut result = nu_data::config::read(name.clone().tag, &None)?;
// process the alias to remove the --save flag
let left_brace = raw_input.find('{').unwrap_or(0);
let right_brace = raw_input.rfind('}').unwrap_or_else(|| raw_input.len());
let left = raw_input[..left_brace]
.replace("--save", "") // TODO using regex (or reconstruct string from AST?)
.replace("-si", "-i")
.replace("-s ", "")
.replace("-is", "-i");
let right = raw_input[right_brace..]
.replace("--save", "")
.replace("-si", "-i")
.replace("-s ", "")
.replace("-is", "-i");
raw_input = format!("{}{}{}", left, &raw_input[left_brace..right_brace], right);
// create a value from raw_input alias
let alias: Value = raw_input.trim().to_string().into();
let alias_start = raw_input.find('[').unwrap_or(0); // used to check if the same alias already exists
// add to startup if alias doesn't exist and replace if it does
match result.get_mut("startup") {
Some(startup) => {
if let UntaggedValue::Table(ref mut commands) = startup.value {
if let Some(command) = commands.iter_mut().find(|command| {
let cmd_str = command.as_string().unwrap_or_default();
cmd_str.starts_with(&raw_input[..alias_start])
}) {
*command = alias;
} else {
commands.push(alias);
}
}
}
None => {
let table = UntaggedValue::table(&[alias]);
result.insert("startup".to_string(), table.into_value(Tag::default()));
}
}
config::write(&result, &None)?;
}
let mut processed_args: Vec<VarDeclaration> = vec![];
for (_, item) in list.iter().enumerate() {
match item.as_string() {
Ok(var_name) => {
let dollar_var_name = format!("${}", var_name);
processed_args.push(VarDeclaration {
name: dollar_var_name,
// type_decl: None,
span: item.tag.span,
});
}
Err(_) => {
return Err(ShellError::labeled_error(
"Expected a string",
"expected a string",
item.tag(),
));
}
}
}
trace!("Found vars: {:?}", processed_args);
let inferred_shapes = {
if let Some(true) = infer {
VarSyntaxShapeDeductor::infer_vars(&processed_args, &block, &registry)?
} else {
processed_args.into_iter().map(|arg| (arg, None)).collect()
}
};
let signature = DeductionToSignature::get(&name.item, &inferred_shapes);
trace!("Inferred signature: {:?}", signature);
Ok(OutputStream::one(ReturnSuccess::action(
CommandAction::AddAlias(Box::new(signature), block),
)))
}
#[cfg(test)]
mod tests {
use super::Alias;
use super::ShellError;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
Ok(test_examples(Alias {})?)
}
}
mod deduction_to_signature {
//For now this logic is relativly simple.
//For each var, one mandatory positional is added.
//As soon as more support for optional positional arguments is arrived,
//this logic might be a little bit more tricky.
use crate::types::deduction::{Deduction, VarDeclaration};
use nu_protocol::{PositionalType, Signature, SyntaxShape};
pub struct DeductionToSignature {}
impl DeductionToSignature {
pub fn get(
cmd_name: &str,
deductions: &[(VarDeclaration, Option<Deduction>)],
) -> Signature {
let mut signature = Signature::build(cmd_name);
for (decl, deduction) in deductions {
match deduction {
None => signature.positional.push((
PositionalType::optional(&decl.name, SyntaxShape::Any),
decl.name.clone(),
)),
Some(deduction) => match deduction {
Deduction::VarShapeDeduction(normal_var_deduction) => {
signature.positional.push((
PositionalType::optional(
&decl.name,
normal_var_deduction[0].deduction,
),
decl.name.clone(),
))
}
},
}
}
signature
}
}
}

View File

@ -118,12 +118,8 @@ Format: #
]
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let (AnsiArgs { color, escape, osc }, _) = args.process(&registry).await?;
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let (AnsiArgs { color, escape, osc }, _) = args.process().await?;
if let Some(e) = escape {
let esc_vec: Vec<char> = e.item.chars().collect();

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -29,12 +28,8 @@ impl WholeStreamCommand for Command {
"Append a row to the table"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let (Arguments { mut value }, input) = args.process(registry).await?;
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let (Arguments { mut value }, input) = args.process().await?;
let input: Vec<Value> = input.collect().await;

View File

@ -61,14 +61,9 @@ The file can contain several optional sections:
fn signature(&self) -> Signature {
Signature::build("autoenv")
}
async fn run(
&self,
_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(crate::commands::help::get_help(&Autoenv, &registry))
UntaggedValue::string(crate::commands::help::get_help(&Autoenv, &args.scope))
.into_value(Tag::unknown()),
)))
}
@ -77,15 +72,15 @@ The file can contain several optional sections:
vec![Example {
description: "Example .nu-env file",
example: r#"cat .nu-env
[env]
mykey = "myvalue"
[env]
mykey = "myvalue"
[scriptvars]
myscript = "echo myval"
[scriptvars]
myscript = "echo myval"
[scripts]
entryscripts = ["touch hello.txt", "touch hello2.txt"]
exitscripts = ["touch bye.txt"]"#,
[scripts]
entryscripts = ["touch hello.txt", "touch hello2.txt"]
exitscripts = ["touch bye.txt"]"#,
result: None,
}]
}

View File

@ -22,14 +22,11 @@ impl WholeStreamCommand for AutoenvTrust {
"Trust a .nu-env file in the current or given directory"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let ctx = EvaluationContext::from_args(&args);
let file_to_trust = match args.call_info.evaluate(registry).await?.args.nth(0) {
let file_to_trust = match args.call_info.evaluate(&ctx).await?.args.nth(0) {
Some(Value {
value: UntaggedValue::Primitive(Primitive::String(ref path)),
tag: _,

View File

@ -26,13 +26,10 @@ impl WholeStreamCommand for AutoenvUnTrust {
"Untrust a .nu-env file in the current or given directory"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let file_to_untrust = match args.call_info.evaluate(registry).await?.args.nth(0) {
let ctx = EvaluationContext::from_args(&args);
let file_to_untrust = match args.call_info.evaluate(&ctx).await?.args.nth(0) {
Some(Value {
value: UntaggedValue::Primitive(Primitive::String(ref path)),
tag: _,

View File

@ -5,7 +5,7 @@ use crate::primitive::get_color_config;
use nu_data::value::format_leaf;
use nu_errors::ShellError;
use nu_protocol::hir::{self, Expression, ExternalRedirection, Literal, SpannedExpression};
use nu_protocol::{Primitive, Scope, Signature, UntaggedValue, Value};
use nu_protocol::{Primitive, Signature, UntaggedValue, Value};
use nu_table::TextStyle;
use parking_lot::Mutex;
use std::sync::atomic::AtomicBool;
@ -26,20 +26,15 @@ impl WholeStreamCommand for Command {
"View the contents of the pipeline as a table or list."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
autoview(RunnableContext {
input: args.input,
registry: registry.clone(),
scope: args.scope.clone(),
shell_manager: args.shell_manager,
host: args.host,
ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag,
raw_input: args.raw_input,
})
.await
}
@ -65,7 +60,7 @@ pub struct RunnableContextWithoutInput {
pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>,
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
pub ctrl_c: Arc<AtomicBool>,
pub registry: CommandRegistry,
pub scope: Scope,
pub name: Tag,
}
@ -76,7 +71,7 @@ impl RunnableContextWithoutInput {
host: context.host,
ctrl_c: context.ctrl_c,
current_errors: context.current_errors,
registry: context.registry,
scope: context.scope,
name: context.name,
};
(context.input, new_context)
@ -109,7 +104,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
if let Some(table) = table {
let command_args = create_default_command_args(&context).with_input(stream);
let result = table.run(command_args, &context.registry).await?;
let result = table.run(command_args).await?;
result.collect::<Vec<_>>().await;
}
}
@ -126,7 +121,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
);
let command_args =
create_default_command_args(&context).with_input(stream);
let result = text.run(command_args, &context.registry).await?;
let result = text.run(command_args).await?;
result.collect::<Vec<_>>().await;
} else {
out!("{}", s);
@ -149,7 +144,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
);
let command_args =
create_default_command_args(&context).with_input(stream);
let result = text.run(command_args, &context.registry).await?;
let result = text.run(command_args).await?;
result.collect::<Vec<_>>().await;
} else {
out!("{}\n", s);
@ -224,7 +219,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
stream.push_back(x);
let command_args =
create_default_command_args(&context).with_input(stream);
let result = binary.run(command_args, &context.registry).await?;
let result = binary.run(command_args).await?;
result.collect::<Vec<_>>().await;
} else {
use pretty_hex::*;
@ -276,7 +271,12 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
nu_table::draw_table(&table, term_width, &color_hm);
}
Value {
value: UntaggedValue::Primitive(Primitive::Nothing),
..
} => {
// Do nothing
}
Value {
value: ref item, ..
} => {
@ -285,7 +285,7 @@ pub async fn autoview(context: RunnableContext) -> Result<OutputStream, ShellErr
stream.push_back(x);
let command_args =
create_default_command_args(&context).with_input(stream);
let result = table.run(command_args, &context.registry).await?;
let result = table.run(command_args).await?;
result.collect::<Vec<_>>().await;
} else {
out!("{:?}", item);
@ -318,8 +318,8 @@ fn create_default_command_args(context: &RunnableContextWithoutInput) -> RawComm
external_redirection: ExternalRedirection::Stdout,
},
name_tag: context.name.clone(),
scope: Scope::create(),
},
scope: Scope::new(),
}
}

View File

@ -5,8 +5,8 @@ use crate::prelude::*;
use heim::cpu::time;
use nu_errors::ShellError;
use nu_protocol::{
hir::{Block, ClassifiedCommand, Commands, InternalCommand},
Dictionary, Scope, Signature, SyntaxShape, UntaggedValue, Value,
hir::{Block, CapturedBlock, ClassifiedCommand, Group, InternalCommand, Pipeline},
Dictionary, Signature, SyntaxShape, UntaggedValue, Value,
};
use rand::{
distributions::Alphanumeric,
@ -19,8 +19,8 @@ pub struct Benchmark;
#[derive(Deserialize, Debug)]
struct BenchmarkArgs {
block: Block,
passthrough: Option<Block>,
block: CapturedBlock,
passthrough: Option<CapturedBlock>,
}
#[async_trait]
@ -48,12 +48,8 @@ impl WholeStreamCommand for Benchmark {
"Runs a block and returns the time it took to execute it"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
benchmark(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
benchmark(args).await
}
fn examples(&self) -> Vec<Example> {
@ -72,29 +68,25 @@ impl WholeStreamCommand for Benchmark {
}
}
async fn benchmark(
raw_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn benchmark(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = raw_args.call_info.args.span;
let mut context = EvaluationContext::from_raw(&raw_args, &registry);
let scope = raw_args.call_info.scope.clone();
let (BenchmarkArgs { block, passthrough }, input) = raw_args.process(&registry).await?;
let mut context = EvaluationContext::from_raw(&raw_args);
let scope = raw_args.scope.clone();
let (BenchmarkArgs { block, passthrough }, input) = raw_args.process().await?;
let env = scope.env();
let env = scope.get_env_vars();
let name = generate_free_name(&env);
let mut env = IndexMap::new();
env.insert(name, generate_random_env_value());
let scope = Scope::append_env(scope, env);
scope.add_env_var(name, generate_random_env_value());
let start_time = Instant::now();
#[cfg(feature = "rich-benchmark")]
let start = time().await;
let result = run_block(&block, &mut context, input, scope.clone()).await;
context.scope.enter_scope();
let result = run_block(&block.block, &context, input).await;
context.scope.exit_scope();
let output = result?.into_vec().await;
#[cfg(feature = "rich-benchmark")]
@ -110,7 +102,7 @@ async fn benchmark(
let real_time = into_big_int(end_time - start_time);
indexmap.insert("real time".to_string(), real_time);
benchmark_output(indexmap, output, passthrough, &tag, &mut context, scope).await
benchmark_output(indexmap, output, passthrough, &tag, &mut context).await
}
// return advanced stats
#[cfg(feature = "rich-benchmark")]
@ -129,10 +121,10 @@ async fn benchmark(
let idle_time = into_big_int(end.idle() - start.idle());
indexmap.insert("idle time".to_string(), idle_time);
benchmark_output(indexmap, output, passthrough, &tag, &mut context, scope).await
benchmark_output(indexmap, output, passthrough, &tag, &mut context).await
} else {
Err(ShellError::untagged_runtime_error(
"Could not retreive CPU time",
"Could not retrieve CPU time",
))
}
}
@ -140,10 +132,9 @@ async fn benchmark(
async fn benchmark_output<T, Output>(
indexmap: IndexMap<String, BigInt>,
block_output: Output,
passthrough: Option<Block>,
passthrough: Option<CapturedBlock>,
tag: T,
context: &mut EvaluationContext,
scope: Arc<Scope>,
) -> Result<OutputStream, ShellError>
where
T: Into<Tag> + Copy,
@ -161,9 +152,12 @@ where
let benchmark_output = InputStream::one(value);
// add autoview for an empty block
let time_block = add_implicit_autoview(time_block);
let time_block = add_implicit_autoview(time_block.block);
let _ = run_block(&time_block, context, benchmark_output, scope).await?;
context.scope.enter_scope();
let result = run_block(&time_block, context, benchmark_output).await;
context.scope.exit_scope();
result?;
context.clear_errors();
Ok(block_output.into())
@ -175,15 +169,19 @@ where
fn add_implicit_autoview(mut block: Block) -> Block {
if block.block.is_empty() {
block.push({
let mut commands = Commands::new(block.span);
commands.push(ClassifiedCommand::Internal(InternalCommand::new(
"autoview".to_string(),
block.span,
block.span,
)));
commands
});
let group = Group::new(
vec![{
let mut commands = Pipeline::new(block.span);
commands.push(ClassifiedCommand::Internal(InternalCommand::new(
"autoview".to_string(),
block.span,
block.span,
)));
commands
}],
block.span,
);
block.push(group);
}
block
}

View File

@ -27,13 +27,9 @@ impl WholeStreamCommand for BuildString {
"Builds a string from the arguments"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let (BuildStringArgs { rest }, _) = args.process(&registry).await?;
let (BuildStringArgs { rest }, _) = args.process().await?;
let mut output_string = String::new();

View File

@ -41,12 +41,8 @@ impl WholeStreamCommand for Cal {
"Display a calendar."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
cal(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
cal(args).await
}
fn examples(&self) -> Vec<Example> {
@ -70,12 +66,8 @@ impl WholeStreamCommand for Cal {
}
}
pub async fn cal(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
pub async fn cal(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let mut calendar_vec_deque = VecDeque::new();
let tag = args.call_info.name_tag.clone();

View File

@ -32,14 +32,10 @@ impl WholeStreamCommand for Cd {
"Change to a new path."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let shell_manager = args.shell_manager.clone();
let (args, _): (CdArgs, _) = args.process(&registry).await?;
let (args, _): (CdArgs, _) = args.process().await?;
shell_manager.cd(args, name)
}

View File

@ -56,12 +56,8 @@ impl WholeStreamCommand for Char {
]
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let (CharArgs { name, unicode }, _) = args.process(&registry).await?;
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let (CharArgs { name, unicode }, _) = args.process().await?;
if unicode {
let decoded_char = string_to_unicode_char(&name.item);

View File

@ -20,20 +20,15 @@ impl WholeStreamCommand for Chart {
"Displays charts."
}
async fn run(
&self,
_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
if registry.get_command("chart bar").is_none() {
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
if args.scope.get_command("chart bar").is_none() {
return Err(ShellError::untagged_runtime_error(
"nu_plugin_chart not installed.",
));
}
let registry = registry.clone();
Ok(OutputStream::one(Ok(ReturnSuccess::Value(
UntaggedValue::string(crate::commands::help::get_help(&Chart, &registry))
UntaggedValue::string(crate::commands::help::get_help(&Chart, &args.scope))
.into_value(Tag::unknown()),
))))
}

View File

@ -2,39 +2,68 @@ use crate::commands::classified::expr::run_expression_block;
use crate::commands::classified::internal::run_internal_command;
use crate::evaluation_context::EvaluationContext;
use crate::prelude::*;
use crate::stream::InputStream;
use async_recursion::async_recursion;
use futures::stream::TryStreamExt;
use nu_errors::ShellError;
use nu_protocol::hir::{Block, ClassifiedCommand, Commands};
use nu_protocol::{ReturnSuccess, Scope, UntaggedValue, Value};
use nu_protocol::hir::{
Block, Call, ClassifiedCommand, Expression, Pipeline, SpannedExpression, Synthetic,
};
use nu_protocol::{ReturnSuccess, UntaggedValue, Value};
use nu_stream::InputStream;
use std::sync::atomic::Ordering;
#[async_recursion]
pub async fn run_block(
block: &Block,
ctx: &mut EvaluationContext,
ctx: &EvaluationContext,
mut input: InputStream,
scope: Arc<Scope>,
) -> Result<InputStream, ShellError> {
let mut output: Result<InputStream, ShellError> = Ok(InputStream::empty());
for pipeline in &block.block {
for (_, definition) in block.definitions.iter() {
ctx.scope.add_definition(definition.clone());
}
for group in &block.block {
match output {
Ok(inp) if inp.is_empty() => {}
Ok(inp) => {
let mut output_stream = inp.to_output_stream();
loop {
// Run autoview on the values we've seen so far
// We may want to make this configurable for other kinds of hosting
if let Some(autoview) = ctx.get_command("autoview") {
let mut output_stream = match ctx
.run_command(
autoview,
Tag::unknown(),
Call::new(
Box::new(SpannedExpression::new(
Expression::Synthetic(Synthetic::String("autoview".into())),
Span::unknown(),
)),
Span::unknown(),
),
inp,
)
.await
{
Ok(x) => x,
Err(e) => {
return Err(e);
}
};
match output_stream.try_next().await {
Ok(Some(ReturnSuccess::Value(Value {
value: UntaggedValue::Error(e),
..
}))) => return Err(e),
}))) => {
return Err(e);
}
Ok(Some(_item)) => {
if let Some(err) = ctx.get_errors().get(0) {
ctx.clear_errors();
return Err(err.clone());
}
if ctx.ctrl_c.load(Ordering::SeqCst) {
break;
return Ok(InputStream::empty());
}
}
Ok(None) => {
@ -42,9 +71,10 @@ pub async fn run_block(
ctx.clear_errors();
return Err(err.clone());
}
break;
}
Err(e) => return Err(e),
Err(e) => {
return Err(e);
}
}
}
}
@ -52,35 +82,131 @@ pub async fn run_block(
return Err(e);
}
}
output = run_pipeline(pipeline, ctx, input, scope.clone()).await;
output = Ok(InputStream::empty());
for pipeline in &group.pipelines {
match output {
Ok(inp) if inp.is_empty() => {}
Ok(inp) => {
let mut output_stream = inp.to_output_stream();
input = InputStream::empty();
match output_stream.try_next().await {
Ok(Some(ReturnSuccess::Value(Value {
value: UntaggedValue::Error(e),
..
}))) => {
return Err(e);
}
Ok(Some(_item)) => {
if let Some(err) = ctx.get_errors().get(0) {
ctx.clear_errors();
return Err(err.clone());
}
if ctx.ctrl_c.load(Ordering::SeqCst) {
// This early return doesn't return the result
// we have so far, but breaking out of this loop
// causes lifetime issues. A future contribution
// could attempt to return the current output.
// https://github.com/nushell/nushell/pull/2830#discussion_r550319687
return Ok(InputStream::empty());
}
}
Ok(None) => {
if let Some(err) = ctx.get_errors().get(0) {
ctx.clear_errors();
return Err(err.clone());
}
}
Err(e) => {
return Err(e);
}
}
}
Err(e) => {
return Err(e);
}
}
output = run_pipeline(pipeline, ctx, input).await;
input = InputStream::empty();
}
}
output
}
#[async_recursion]
async fn run_pipeline(
commands: &Commands,
ctx: &mut EvaluationContext,
commands: &Pipeline,
ctx: &EvaluationContext,
mut input: InputStream,
scope: Arc<Scope>,
) -> Result<InputStream, ShellError> {
for item in commands.list.clone() {
input = match item {
ClassifiedCommand::Dynamic(_) => {
return Err(ShellError::unimplemented("Dynamic commands"))
ClassifiedCommand::Dynamic(call) => {
let mut args = vec![];
if let Some(positional) = call.positional {
for pos in &positional {
let result = run_expression_block(pos, ctx).await?.into_vec().await;
args.push(result);
}
}
match &call.head.expr {
Expression::Block(block) => {
ctx.scope.enter_scope();
for (param, value) in block.params.positional.iter().zip(args.iter()) {
ctx.scope.add_var(param.0.name(), value[0].clone());
}
let result = run_block(&block, ctx, input).await;
ctx.scope.exit_scope();
let result = result?;
return Ok(result);
}
Expression::Variable(v, span) => {
if let Some(value) = ctx.scope.get_var(v) {
match &value.value {
UntaggedValue::Block(captured_block) => {
ctx.scope.enter_scope();
ctx.scope.add_vars(&captured_block.captured.entries);
for (param, value) in captured_block
.block
.params
.positional
.iter()
.zip(args.iter())
{
ctx.scope.add_var(param.0.name(), value[0].clone());
}
let result = run_block(&captured_block.block, ctx, input).await;
ctx.scope.exit_scope();
let result = result?;
return Ok(result);
}
_ => {
return Err(ShellError::labeled_error("Dynamic commands must start with a block (or variable pointing to a block)", "needs to be a block", call.head.span));
}
}
} else {
return Err(ShellError::labeled_error(
"Variable not found",
"variable not found",
span,
));
}
}
_ => {
return Err(ShellError::labeled_error("Dynamic commands must start with a block (or variable pointing to a block)", "needs to be a block", call.head.span));
}
}
}
ClassifiedCommand::Expr(expr) => {
run_expression_block(*expr, ctx, scope.clone()).await?
}
ClassifiedCommand::Expr(expr) => run_expression_block(&*expr, ctx).await?,
ClassifiedCommand::Error(err) => return Err(err.into()),
ClassifiedCommand::Internal(left) => {
run_internal_command(left, ctx, input, scope.clone()).await?
}
ClassifiedCommand::Internal(left) => run_internal_command(left, ctx, input).await?,
};
}

View File

@ -6,20 +6,17 @@ use log::{log_enabled, trace};
use futures::stream::once;
use nu_errors::ShellError;
use nu_protocol::hir::SpannedExpression;
use nu_protocol::Scope;
pub(crate) async fn run_expression_block(
expr: SpannedExpression,
context: &mut EvaluationContext,
scope: Arc<Scope>,
expr: &SpannedExpression,
ctx: &EvaluationContext,
) -> Result<InputStream, ShellError> {
if log_enabled!(log::Level::Trace) {
trace!(target: "nu::run::expr", "->");
trace!(target: "nu::run::expr", "{:?}", expr);
}
let registry = context.registry().clone();
let output = evaluate_baseline_expr(&expr, &registry, scope).await?;
let output = evaluate_baseline_expr(expr, ctx).await?;
Ok(once(async { Ok(output) }).to_input_stream())
}

View File

@ -16,14 +16,13 @@ use log::trace;
use nu_errors::ShellError;
use nu_protocol::hir::Expression;
use nu_protocol::hir::{ExternalCommand, ExternalRedirection};
use nu_protocol::{Primitive, Scope, ShellTypeName, UntaggedValue, Value};
use nu_protocol::{Primitive, ShellTypeName, UntaggedValue, Value};
use nu_source::Tag;
pub(crate) async fn run_external_command(
command: ExternalCommand,
context: &mut EvaluationContext,
input: InputStream,
scope: Arc<Scope>,
external_redirection: ExternalRedirection,
) -> Result<InputStream, ShellError> {
trace!(target: "nu::run::external", "-> {}", command.name);
@ -36,14 +35,13 @@ pub(crate) async fn run_external_command(
));
}
run_with_stdin(command, context, input, scope, external_redirection).await
run_with_stdin(command, context, input, external_redirection).await
}
async fn run_with_stdin(
command: ExternalCommand,
context: &mut EvaluationContext,
input: InputStream,
scope: Arc<Scope>,
external_redirection: ExternalRedirection,
) -> Result<InputStream, ShellError> {
let path = context.shell_manager.path();
@ -53,7 +51,7 @@ async fn run_with_stdin(
let mut command_args = vec![];
for arg in command.args.iter() {
let is_literal = matches!(arg.expr, Expression::Literal(_));
let value = evaluate_baseline_expr(arg, &context.registry, scope.clone()).await?;
let value = evaluate_baseline_expr(arg, context).await?;
// Skip any arguments that don't really exist, treating them as optional
// FIXME: we may want to preserve the gap in the future, though it's hard to say
@ -132,7 +130,7 @@ async fn run_with_stdin(
&process_args[..],
input,
external_redirection,
scope,
&context.scope,
)
}
@ -142,7 +140,7 @@ fn spawn(
args: &[String],
input: InputStream,
external_redirection: ExternalRedirection,
scope: Arc<Scope>,
scope: &Scope,
) -> Result<InputStream, ShellError> {
let command = command.clone();
@ -173,7 +171,7 @@ fn spawn(
trace!(target: "nu::run::external", "cwd = {:?}", &path);
process.env_clear();
process.envs(scope.env());
process.envs(scope.get_env_vars());
// We want stdout regardless of what
// we are doing ($it case or pipe stdin)
@ -542,8 +540,6 @@ mod tests {
#[cfg(feature = "which")]
use nu_errors::ShellError;
#[cfg(feature = "which")]
use nu_protocol::Scope;
#[cfg(feature = "which")]
use nu_test_support::commands::ExternalBuilder;
// async fn read(mut stream: OutputStream) -> Option<Value> {
@ -568,15 +564,11 @@ mod tests {
let mut ctx =
EvaluationContext::basic().expect("There was a problem creating a basic context.");
assert!(run_external_command(
cmd,
&mut ctx,
input,
Scope::create(),
ExternalRedirection::Stdout
)
.await
.is_err());
assert!(
run_external_command(cmd, &mut ctx, input, ExternalRedirection::Stdout)
.await
.is_err()
);
Ok(())
}

View File

@ -1,17 +1,16 @@
use crate::commands::command::whole_stream_command;
use crate::commands::run_alias::AliasCommand;
use std::sync::atomic::Ordering;
use crate::commands::UnevaluatedCallInfo;
use crate::prelude::*;
use log::{log_enabled, trace};
use nu_errors::ShellError;
use nu_protocol::hir::{ExternalRedirection, InternalCommand};
use nu_protocol::{CommandAction, Primitive, ReturnSuccess, Scope, UntaggedValue, Value};
use nu_protocol::{CommandAction, Primitive, ReturnSuccess, UntaggedValue, Value};
pub(crate) async fn run_internal_command(
command: InternalCommand,
context: &mut EvaluationContext,
context: &EvaluationContext,
input: InputStream,
scope: Arc<Scope>,
) -> Result<InputStream, ShellError> {
if log_enabled!(log::Level::Trace) {
trace!(target: "nu::run::internal", "->");
@ -19,10 +18,13 @@ pub(crate) async fn run_internal_command(
}
let objects: InputStream = trace_stream!(target: "nu::trace_stream::internal", "input" = input);
let internal_command = context.expect_command(&command.name);
let internal_command = context.scope.expect_command(&command.name);
if command.name == "autoenv untrust" {
context.user_recently_used_autoenv_untrust = true;
context
.user_recently_used_autoenv_untrust
.store(true, Ordering::SeqCst);
}
let result = {
@ -31,14 +33,12 @@ pub(crate) async fn run_internal_command(
internal_command?,
Tag::unknown_anchor(command.name_span),
command.args.clone(),
scope.clone(),
objects,
)
.await?
};
let head = Arc::new(command.args.head.clone());
//let context = Arc::new(context.clone());
let context = context.clone();
let command = Arc::new(command);
@ -47,26 +47,24 @@ pub(crate) async fn run_internal_command(
.then(move |item| {
let head = head.clone();
let command = command.clone();
let mut context = context.clone();
let scope = scope.clone();
let context = context.clone();
async move {
match item {
Ok(ReturnSuccess::Action(action)) => match action {
CommandAction::ChangePath(path) => {
context.shell_manager.set_path(path);
InputStream::from_stream(futures::stream::iter(vec![]))
InputStream::empty()
}
CommandAction::Exit => std::process::exit(0), // TODO: save history.txt
CommandAction::Error(err) => {
context.error(err.clone());
InputStream::one(UntaggedValue::Error(err).into_untagged_value())
context.error(err);
InputStream::empty()
}
CommandAction::AutoConvert(tagged_contents, extension) => {
let contents_tag = tagged_contents.tag.clone();
let command_name = format!("from {}", extension);
let command = command.clone();
if let Some(converter) = context.registry.get_command(&command_name)
{
if let Some(converter) = context.scope.get_command(&command_name) {
let new_args = RawCommandArgs {
host: context.host.clone(),
ctrl_c: context.ctrl_c.clone(),
@ -81,14 +79,11 @@ pub(crate) async fn run_internal_command(
external_redirection: ExternalRedirection::Stdout,
},
name_tag: Tag::unknown_anchor(command.name_span),
scope,
},
scope: context.scope.clone(),
};
let result = converter
.run(
new_args.with_input(vec![tagged_contents]),
&context.registry,
)
.run(new_args.with_input(vec![tagged_contents]))
.await;
match result {
@ -122,8 +117,8 @@ pub(crate) async fn run_internal_command(
futures::stream::iter(output).to_input_stream()
}
Err(e) => {
context.add_error(e);
Err(err) => {
context.error(err);
InputStream::empty()
}
}
@ -139,13 +134,12 @@ pub(crate) async fn run_internal_command(
context.shell_manager.insert_at_current(Box::new(
match HelpShell::for_command(
UntaggedValue::string(cmd).into_value(tag),
&context.registry(),
&context.scope,
) {
Ok(v) => v,
Err(err) => {
return InputStream::one(
UntaggedValue::Error(err).into_untagged_value(),
)
context.error(err);
return InputStream::empty();
}
},
));
@ -153,12 +147,11 @@ pub(crate) async fn run_internal_command(
}
_ => {
context.shell_manager.insert_at_current(Box::new(
match HelpShell::index(&context.registry()) {
match HelpShell::index(&context.scope) {
Ok(v) => v,
Err(err) => {
return InputStream::one(
UntaggedValue::Error(err).into_untagged_value(),
)
context.error(err);
return InputStream::empty();
}
},
));
@ -176,21 +169,13 @@ pub(crate) async fn run_internal_command(
match FilesystemShell::with_location(location) {
Ok(v) => v,
Err(err) => {
return InputStream::one(
UntaggedValue::Error(err.into())
.into_untagged_value(),
)
context.error(err.into());
return InputStream::empty();
}
},
));
InputStream::from_stream(futures::stream::iter(vec![]))
}
CommandAction::AddAlias(sig, block) => {
context.add_commands(vec![whole_stream_command(
AliasCommand::new(*sig, block),
)]);
InputStream::from_stream(futures::stream::iter(vec![]))
}
CommandAction::AddPlugins(path) => {
match crate::plugin::scan(vec![std::path::PathBuf::from(path)]) {
Ok(plugins) => {
@ -203,39 +188,37 @@ pub(crate) async fn run_internal_command(
.collect(),
);
InputStream::from_stream(futures::stream::iter(vec![]))
InputStream::empty()
}
Err(reason) => {
context.error(reason.clone());
InputStream::one(
UntaggedValue::Error(reason).into_untagged_value(),
)
context.error(reason);
InputStream::empty()
}
}
}
CommandAction::PreviousShell => {
context.shell_manager.prev();
InputStream::from_stream(futures::stream::iter(vec![]))
InputStream::empty()
}
CommandAction::NextShell => {
context.shell_manager.next();
InputStream::from_stream(futures::stream::iter(vec![]))
InputStream::empty()
}
CommandAction::LeaveShell => {
context.shell_manager.remove_at_current();
if context.shell_manager.is_empty() {
std::process::exit(0); // TODO: save history.txt
}
InputStream::from_stream(futures::stream::iter(vec![]))
InputStream::empty()
}
},
Ok(ReturnSuccess::Value(Value {
value: UntaggedValue::Error(err),
tag,
..
})) => {
context.error(err.clone());
InputStream::one(UntaggedValue::Error(err).into_value(tag))
context.error(err);
InputStream::empty()
}
Ok(ReturnSuccess::Value(v)) => InputStream::one(v),
@ -255,8 +238,8 @@ pub(crate) async fn run_internal_command(
}
Err(err) => {
context.error(err.clone());
InputStream::one(UntaggedValue::Error(err).into_untagged_value())
context.error(err);
InputStream::empty()
}
}
}

View File

@ -104,24 +104,13 @@ impl WholeStreamCommand for PluginFilter {
&self.config.usage
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
run_filter(self.path.clone(), args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_filter(self.path.clone(), (args)).await
}
}
async fn run_filter(
path: String,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn run_filter(path: String, args: CommandArgs) -> Result<OutputStream, ShellError> {
trace!("filter_plugin :: {}", path);
let registry = registry.clone();
let scope = args.call_info.scope.clone();
let bos = futures::stream::iter(vec![
UntaggedValue::Primitive(Primitive::BeginningOfStream).into_untagged_value()
@ -130,7 +119,7 @@ async fn run_filter(
UntaggedValue::Primitive(Primitive::EndOfStream).into_untagged_value()
]);
let args = args.evaluate_once_with_scope(&registry, scope).await?;
let args = args.evaluate_once().await?;
let real_path = Path::new(&path);
let ext = real_path.extension();
@ -390,22 +379,13 @@ impl WholeStreamCommand for PluginSink {
&self.config.usage
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
run_sink(self.path.clone(), args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_sink(self.path.clone(), args).await
}
}
async fn run_sink(
path: String,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
async fn run_sink(path: String, args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let call_info = args.call_info.clone();
let input: Vec<Value> = args.input.collect().await;

View File

@ -20,7 +20,7 @@ impl WholeStreamCommand for Clear {
"Clears the terminal"
}
async fn run(&self, _: CommandArgs, _: &CommandRegistry) -> Result<OutputStream, ShellError> {
async fn run(&self, _: CommandArgs) -> Result<OutputStream, ShellError> {
if cfg!(windows) {
Command::new("cmd")
.args(&["/C", "cls"])

View File

@ -1,11 +1,10 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use futures::stream::StreamExt;
use nu_errors::ShellError;
use nu_protocol::{Signature, Value};
use clipboard::{ClipboardContext, ClipboardProvider};
use arboard::Clipboard;
pub struct Clip;
@ -23,12 +22,8 @@ impl WholeStreamCommand for Clip {
"Copy the contents of the pipeline to the copy/paste buffer"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
clip(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
clip(args).await
}
fn examples(&self) -> Vec<Example> {
@ -47,16 +42,12 @@ impl WholeStreamCommand for Clip {
}
}
pub async fn clip(
args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn clip(args: CommandArgs) -> Result<OutputStream, ShellError> {
let input = args.input;
let name = args.call_info.name_tag.clone();
let values: Vec<Value> = input.collect().await;
if let Ok(clip_context) = ClipboardProvider::new() {
let mut clip_context: ClipboardContext = clip_context;
if let Ok(mut clip_context) = Clipboard::new() {
let mut new_copy_data = String::new();
if !values.is_empty() {
@ -81,7 +72,7 @@ pub async fn clip(
}
}
match clip_context.set_contents(new_copy_data) {
match clip_context.set_text(new_copy_data) {
Ok(_) => {}
Err(_) => {
return Err(ShellError::labeled_error(

View File

@ -1,28 +1,26 @@
use crate::command_registry::CommandRegistry;
use crate::commands::help::get_help;
use crate::deserializer::ConfigDeserializer;
use crate::evaluate::evaluate_args::evaluate_args;
use crate::prelude::*;
use crate::{commands::help::get_help, run_block};
use derive_new::new;
use getset::Getters;
use nu_errors::ShellError;
use nu_protocol::hir;
use nu_protocol::{CallInfo, EvaluatedArgs, ReturnSuccess, Scope, Signature, UntaggedValue, Value};
use nu_protocol::hir::{self, Block};
use nu_protocol::{CallInfo, EvaluatedArgs, ReturnSuccess, Signature, UntaggedValue, Value};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use std::ops::Deref;
use std::sync::atomic::AtomicBool;
#[derive(Deserialize, Serialize, Debug, Clone)]
#[derive(Debug, Clone)]
pub struct UnevaluatedCallInfo {
pub args: hir::Call,
pub name_tag: Tag,
pub scope: Arc<Scope>,
}
impl UnevaluatedCallInfo {
pub async fn evaluate(self, registry: &CommandRegistry) -> Result<CallInfo, ShellError> {
let args = evaluate_args(&self.args, registry, self.scope.clone()).await?;
pub async fn evaluate(self, ctx: &EvaluationContext) -> Result<CallInfo, ShellError> {
let args = evaluate_args(&self.args, ctx).await?;
Ok(CallInfo {
args,
@ -43,8 +41,8 @@ pub struct CommandArgs {
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
pub shell_manager: ShellManager,
pub call_info: UnevaluatedCallInfo,
pub scope: Scope,
pub input: InputStream,
pub raw_input: String,
}
#[derive(Getters, Clone)]
@ -54,6 +52,7 @@ pub struct RawCommandArgs {
pub ctrl_c: Arc<AtomicBool>,
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
pub shell_manager: ShellManager,
pub scope: Scope,
pub call_info: UnevaluatedCallInfo,
}
@ -65,8 +64,8 @@ impl RawCommandArgs {
current_errors: self.current_errors,
shell_manager: self.shell_manager,
call_info: self.call_info,
scope: self.scope,
input: input.into(),
raw_input: String::default(),
}
}
}
@ -78,15 +77,14 @@ impl std::fmt::Debug for CommandArgs {
}
impl CommandArgs {
pub async fn evaluate_once(
self,
registry: &CommandRegistry,
) -> Result<EvaluatedWholeStreamCommandArgs, ShellError> {
pub async fn evaluate_once(self) -> Result<EvaluatedWholeStreamCommandArgs, ShellError> {
let ctx = EvaluationContext::from_args(&self);
let host = self.host.clone();
let ctrl_c = self.ctrl_c.clone();
let shell_manager = self.shell_manager.clone();
let input = self.input;
let call_info = self.call_info.evaluate(registry).await?;
let call_info = self.call_info.evaluate(&ctx).await?;
let scope = self.scope.clone();
Ok(EvaluatedWholeStreamCommandArgs::new(
host,
@ -94,39 +92,12 @@ impl CommandArgs {
shell_manager,
call_info,
input,
scope,
))
}
pub async fn evaluate_once_with_scope(
self,
registry: &CommandRegistry,
scope: Arc<Scope>,
) -> Result<EvaluatedWholeStreamCommandArgs, ShellError> {
let host = self.host.clone();
let ctrl_c = self.ctrl_c.clone();
let shell_manager = self.shell_manager.clone();
let input = self.input;
let call_info = UnevaluatedCallInfo {
name_tag: self.call_info.name_tag,
args: self.call_info.args,
scope: scope.clone(),
};
let call_info = call_info.evaluate(registry).await?;
Ok(EvaluatedWholeStreamCommandArgs::new(
host,
ctrl_c,
shell_manager,
call_info,
input,
))
}
pub async fn process<'de, T: Deserialize<'de>>(
self,
registry: &CommandRegistry,
) -> Result<(T, InputStream), ShellError> {
let args = self.evaluate_once(registry).await?;
pub async fn process<'de, T: Deserialize<'de>>(self) -> Result<(T, InputStream), ShellError> {
let args = self.evaluate_once().await?;
let call_info = args.call_info.clone();
let mut deserializer = ConfigDeserializer::from_call_info(call_info);
@ -141,14 +112,13 @@ pub struct RunnableContext {
pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>,
pub ctrl_c: Arc<AtomicBool>,
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
pub registry: CommandRegistry,
pub scope: Scope,
pub name: Tag,
pub raw_input: String,
}
impl RunnableContext {
pub fn get_command(&self, name: &str) -> Option<Command> {
self.registry.get_command(name)
self.scope.get_command(name)
}
}
@ -171,6 +141,7 @@ impl EvaluatedWholeStreamCommandArgs {
shell_manager: ShellManager,
call_info: CallInfo,
input: impl Into<InputStream>,
scope: Scope,
) -> EvaluatedWholeStreamCommandArgs {
EvaluatedWholeStreamCommandArgs {
args: EvaluatedCommandArgs {
@ -178,6 +149,7 @@ impl EvaluatedWholeStreamCommandArgs {
ctrl_c,
shell_manager,
call_info,
scope,
},
input: input.into(),
}
@ -207,6 +179,7 @@ pub struct EvaluatedCommandArgs {
pub ctrl_c: Arc<AtomicBool>,
pub shell_manager: ShellManager,
pub call_info: CallInfo,
pub scope: Scope,
}
impl EvaluatedCommandArgs {
@ -247,11 +220,7 @@ pub trait WholeStreamCommand: Send + Sync {
fn usage(&self) -> &str;
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError>;
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError>;
fn is_binary(&self) -> bool {
false
@ -267,6 +236,97 @@ pub trait WholeStreamCommand: Send + Sync {
}
}
// Custom commands are blocks, so we can use the information in the block to also
// implement a WholeStreamCommand
#[allow(clippy::suspicious_else_formatting)]
#[async_trait]
impl WholeStreamCommand for Block {
fn name(&self) -> &str {
&self.params.name
}
fn signature(&self) -> Signature {
self.params.clone()
}
fn usage(&self) -> &str {
""
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let call_info = args.call_info.clone();
let mut block = self.clone();
block.set_redirect(call_info.args.external_redirection);
let ctx = EvaluationContext::from_args(&args);
let evaluated = call_info.evaluate(&ctx).await?;
let input = args.input;
ctx.scope.enter_scope();
if let Some(args) = evaluated.args.positional {
// FIXME: do not do this
for arg in args.into_iter().zip(self.params.positional.iter()) {
let name = arg.1 .0.name();
if name.starts_with('$') {
ctx.scope.add_var(name, arg.0);
} else {
ctx.scope.add_var(format!("${}", name), arg.0);
}
}
}
if let Some(args) = evaluated.args.named {
for named in &block.params.named {
let name = named.0;
if let Some(value) = args.get(name) {
if name.starts_with('$') {
ctx.scope.add_var(name, value.clone());
} else {
ctx.scope.add_var(format!("${}", name), value.clone());
}
} else if name.starts_with('$') {
ctx.scope
.add_var(name, UntaggedValue::nothing().into_untagged_value());
} else {
ctx.scope.add_var(
format!("${}", name),
UntaggedValue::nothing().into_untagged_value(),
);
}
}
} else {
for named in &block.params.named {
let name = named.0;
if name.starts_with('$') {
ctx.scope
.add_var(name, UntaggedValue::nothing().into_untagged_value());
} else {
ctx.scope.add_var(
format!("${}", name),
UntaggedValue::nothing().into_untagged_value(),
);
}
}
}
let result = run_block(&block, &ctx, input).await;
ctx.scope.exit_scope();
result.map(|x| x.to_output_stream())
}
fn is_binary(&self) -> bool {
false
}
fn is_internal(&self) -> bool {
false
}
fn examples(&self) -> Vec<Example> {
vec![]
}
}
#[derive(Clone)]
pub struct Command(Arc<dyn WholeStreamCommand>);
@ -306,19 +366,14 @@ impl Command {
self.0.examples()
}
pub async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
if args.call_info.switch_present("help") {
let cl = self.0.clone();
let registry = registry.clone();
Ok(OutputStream::one(Ok(ReturnSuccess::Value(
UntaggedValue::string(get_help(&*cl, &registry)).into_value(Tag::unknown()),
UntaggedValue::string(get_help(&*cl, &args.scope)).into_value(Tag::unknown()),
))))
} else {
self.0.run(args, registry).await
self.0.run(args).await
}
}

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use futures::future;
@ -28,12 +27,8 @@ impl WholeStreamCommand for Compact {
"Creates a table with non-empty rows"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
compact(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
compact(args).await
}
fn examples(&self) -> Vec<Example> {
@ -45,12 +40,8 @@ impl WholeStreamCommand for Compact {
}
}
pub async fn compact(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (CompactArgs { rest: columns }, input) = args.process(&registry).await?;
pub async fn compact(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (CompactArgs { rest: columns }, input) = args.process().await?;
Ok(input
.filter_map(move |item| {
future::ready(if columns.is_empty() {

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -20,12 +19,8 @@ impl WholeStreamCommand for SubCommand {
"clear the config"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
clear(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
clear(args).await
}
fn examples(&self) -> Vec<Example> {
@ -37,10 +32,7 @@ impl WholeStreamCommand for SubCommand {
}
}
pub async fn clear(
args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn clear(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name_span = args.call_info.name_tag.clone();
// NOTE: None because we are not loading a new config file, we just want to read from the

View File

@ -1,6 +1,6 @@
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use crate::{CommandArgs, CommandRegistry, OutputStream};
use crate::{CommandArgs, OutputStream};
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
@ -20,11 +20,7 @@ impl WholeStreamCommand for Command {
"Configuration management."
}
async fn run(
&self,
args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let name_span = args.call_info.name_tag.clone();
let name = args.call_info.name_tag;
let result = nu_data::config::read(name_span, &None)?;

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -29,12 +28,8 @@ impl WholeStreamCommand for SubCommand {
"Gets a value from the config"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
get(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
get(args).await
}
fn examples(&self) -> Vec<Example> {
@ -46,12 +41,9 @@ impl WholeStreamCommand for SubCommand {
}
}
pub async fn get(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn get(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name_tag = args.call_info.name_tag.clone();
let (GetArgs { path }, _) = args.process(&registry).await?;
let (GetArgs { path }, _) = args.process().await?;
// NOTE: None because we are not loading a new config file, we just want to read from the
// existing config

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -31,22 +30,15 @@ impl WholeStreamCommand for SubCommand {
"Loads the config from the path given"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
set(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
set(args).await
}
}
pub async fn set(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn set(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let name_span = args.call_info.name_tag.clone();
let (LoadArgs { load }, _) = args.process(&registry).await?;
let (LoadArgs { load }, _) = args.process().await?;
let configuration = load.item().clone();

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -20,12 +19,8 @@ impl WholeStreamCommand for SubCommand {
"return the path to the config file"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
path(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
path(args).await
}
fn examples(&self) -> Vec<Example> {
@ -37,10 +32,7 @@ impl WholeStreamCommand for SubCommand {
}
}
pub async fn path(
args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn path(args: CommandArgs) -> Result<OutputStream, ShellError> {
let path = config::default_path()?;
Ok(OutputStream::one(ReturnSuccess::value(

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -30,12 +29,8 @@ impl WholeStreamCommand for SubCommand {
"Removes a value from the config"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
remove(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
remove(args).await
}
fn examples(&self) -> Vec<Example> {
@ -47,12 +42,9 @@ impl WholeStreamCommand for SubCommand {
}
}
pub async fn remove(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn remove(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name_span = args.call_info.name_tag.clone();
let (RemoveArgs { remove }, _) = args.process(&registry).await?;
let (RemoveArgs { remove }, _) = args.process().await?;
let mut result = nu_data::config::read(name_span, &None)?;

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -28,12 +27,8 @@ impl WholeStreamCommand for SubCommand {
"Sets a value in the config"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
set(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
set(args).await
}
fn examples(&self) -> Vec<Example> {
@ -62,12 +57,9 @@ impl WholeStreamCommand for SubCommand {
}
}
pub async fn set(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn set(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name_tag = args.call_info.name_tag.clone();
let (SetArgs { path, mut value }, _) = args.process(&registry).await?;
let (SetArgs { path, mut value }, _) = args.process().await?;
// NOTE: None because we are not loading a new config file, we just want to read from the
// existing config

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -30,12 +29,8 @@ impl WholeStreamCommand for SubCommand {
"Sets a value in the config"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
set_into(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
set_into(args).await
}
fn examples(&self) -> Vec<Example> {
@ -47,14 +42,11 @@ impl WholeStreamCommand for SubCommand {
}
}
pub async fn set_into(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn set_into(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name_span = args.call_info.name_tag.clone();
let name = args.call_info.name_tag.clone();
let (SetIntoArgs { set_into: v }, input) = args.process(&registry).await?;
let (SetIntoArgs { set_into: v }, input) = args.process().await?;
// NOTE: None because we are not loading a new config file, we just want to read from the
// existing config

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use futures::stream::StreamExt;
@ -30,13 +29,9 @@ impl WholeStreamCommand for Count {
"Show the total number of rows or items."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let (CountArgs { column }, input) = args.process(&registry).await?;
let (CountArgs { column }, input) = args.process().await?;
let rows: Vec<Value> = input.collect().await;
let count = if column {

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -36,14 +35,10 @@ impl WholeStreamCommand for Cpy {
"Copy files."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let shell_manager = args.shell_manager.clone();
let name = args.call_info.name_tag.clone();
let (args, _) = args.process(&registry).await?;
let (args, _) = args.process().await?;
shell_manager.cp(args, name)
}

View File

@ -19,15 +19,9 @@ impl WholeStreamCommand for Command {
"Apply date function"
}
async fn run(
&self,
_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(crate::commands::help::get_help(&Command, &registry))
UntaggedValue::string(crate::commands::help::get_help(&Command, &args.scope))
.into_value(Tag::unknown()),
)))
}

View File

@ -31,12 +31,8 @@ impl WholeStreamCommand for Date {
"Format a given date using the given format string."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
format(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
format(args).await
}
fn examples(&self) -> Vec<Example> {
@ -55,13 +51,9 @@ impl WholeStreamCommand for Date {
}
}
pub async fn format(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
pub async fn format(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let (FormatArgs { format, table }, input) = args.process(&registry).await?;
let (FormatArgs { format, table }, input) = args.process().await?;
Ok(input
.map(move |value| match value {

View File

@ -21,12 +21,8 @@ impl WholeStreamCommand for Date {
"List supported time zones."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
list_timezone(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
list_timezone(args).await
}
fn examples(&self) -> Vec<Example> {
@ -45,11 +41,8 @@ impl WholeStreamCommand for Date {
}
}
async fn list_timezone(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(&registry).await?;
async fn list_timezone(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let tag = args.call_info.name_tag.clone();
let list = TZ_VARIANTS.iter().map(move |tz| {

View File

@ -20,20 +20,13 @@ impl WholeStreamCommand for Date {
"Get the current date."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
now(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
now(args).await
}
}
pub async fn now(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(&registry).await?;
pub async fn now(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let tag = args.call_info.name_tag.clone();
let now: DateTime<Local> = Local::now();

View File

@ -21,12 +21,8 @@ impl WholeStreamCommand for Date {
"Print the date in a structured table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
to_table(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
to_table(args).await
}
fn examples(&self) -> Vec<Example> {
@ -38,12 +34,8 @@ impl WholeStreamCommand for Date {
}
}
async fn to_table(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
async fn to_table(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let tag = args.call_info.name_tag.clone();
let input = args.input;

View File

@ -33,12 +33,8 @@ Use `date list-timezone` to list all supported time zones.
"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
to_timezone(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
to_timezone(args).await
}
fn examples(&self) -> Vec<Example> {
@ -62,13 +58,9 @@ Use `date list-timezone` to list all supported time zones.
}
}
async fn to_timezone(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn to_timezone(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let (DateToTimeZoneArgs { timezone }, input) = args.process(&registry).await?;
let (DateToTimeZoneArgs { timezone }, input) = args.process().await?;
Ok(input
.map(move |value| match value {

View File

@ -0,0 +1,55 @@
use crate::prelude::*;
use chrono::{DateTime, Utc};
use nu_errors::ShellError;
use crate::commands::date::utils::date_to_value;
use crate::commands::WholeStreamCommand;
use nu_protocol::Signature;
pub struct Date;
#[async_trait]
impl WholeStreamCommand for Date {
fn name(&self) -> &str {
"date utc"
}
fn signature(&self) -> Signature {
Signature::build("date utc")
}
fn usage(&self) -> &str {
"return the current date in utc."
}
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
utc(args).await
}
}
pub async fn utc(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let tag = args.call_info.name_tag.clone();
let no_fmt = "".to_string();
let value = {
let local: DateTime<Utc> = Utc::now();
date_to_value(local, tag, no_fmt)
};
Ok(OutputStream::one(value))
}
#[cfg(test)]
mod tests {
use super::Date;
use super::ShellError;
#[test]
fn examples_work_as_expected() -> Result<(), ShellError> {
use crate::examples::test as test_examples;
Ok(test_examples(Date {})?)
}
}

View File

@ -24,21 +24,13 @@ impl WholeStreamCommand for Debug {
"Print the Rust debug representation of the values"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
debug_value(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
debug_value(args).await
}
}
async fn debug_value(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (DebugArgs { raw }, input) = args.process(&registry).await?;
async fn debug_value(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (DebugArgs { raw }, input) = args.process().await?;
Ok(input
.map(move |v| {
if raw {

View File

@ -0,0 +1,48 @@
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{hir::CapturedBlock, Signature, SyntaxShape, Value};
use nu_source::Tagged;
pub struct Def;
#[derive(Deserialize)]
pub struct DefArgs {
pub name: Tagged<String>,
pub args: Tagged<Vec<Value>>,
pub block: CapturedBlock,
}
#[async_trait]
impl WholeStreamCommand for Def {
fn name(&self) -> &str {
"def"
}
fn signature(&self) -> Signature {
Signature::build("def")
.required("name", SyntaxShape::String, "the name of the command")
.required(
"params",
SyntaxShape::Table,
"the parameters of the command",
)
.required("block", SyntaxShape::Block, "the body of the command")
}
fn usage(&self) -> &str {
"Create a command and set it to a definition."
}
async fn run(&self, _args: CommandArgs) -> Result<OutputStream, ShellError> {
// Currently, we don't do anything here because we should have already
// installed the definition as we entered the scope
// We just create a command so that we can get proper coloring
Ok(OutputStream::empty())
}
fn examples(&self) -> Vec<Example> {
vec![]
}
}

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -34,12 +33,8 @@ impl WholeStreamCommand for Default {
"Sets a default row's column if missing."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
default(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
default(args).await
}
fn examples(&self) -> Vec<Example> {
@ -51,12 +46,8 @@ impl WholeStreamCommand for Default {
}
}
async fn default(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (DefaultArgs { column, value }, input) = args.process(&registry).await?;
async fn default(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (DefaultArgs { column, value }, input) = args.process().await?;
Ok(input
.map(move |item| {

View File

@ -0,0 +1,248 @@
use crate::prelude::*;
use std::error::Error;
pub fn create_default_context(interactive: bool) -> Result<EvaluationContext, Box<dyn Error>> {
let context = EvaluationContext::basic()?;
{
use crate::commands::*;
context.add_commands(vec![
// Fundamentals
whole_stream_command(NuPlugin),
whole_stream_command(Let),
whole_stream_command(LetEnv),
whole_stream_command(Def),
whole_stream_command(Source),
// System/file operations
whole_stream_command(Exec),
whole_stream_command(Pwd),
whole_stream_command(Ls),
whole_stream_command(Du),
whole_stream_command(Cd),
whole_stream_command(Remove),
whole_stream_command(Open),
whole_stream_command(Config),
whole_stream_command(ConfigGet),
whole_stream_command(ConfigSet),
whole_stream_command(ConfigSetInto),
whole_stream_command(ConfigClear),
whole_stream_command(ConfigLoad),
whole_stream_command(ConfigRemove),
whole_stream_command(ConfigPath),
whole_stream_command(Help),
whole_stream_command(History),
whole_stream_command(Save),
whole_stream_command(Touch),
whole_stream_command(Cpy),
whole_stream_command(Date),
whole_stream_command(DateListTimeZone),
whole_stream_command(DateNow),
whole_stream_command(DateToTable),
whole_stream_command(DateToTimeZone),
whole_stream_command(DateFormat),
whole_stream_command(Cal),
whole_stream_command(Mkdir),
whole_stream_command(Mv),
whole_stream_command(Kill),
whole_stream_command(Version),
whole_stream_command(Clear),
whole_stream_command(Describe),
whole_stream_command(Which),
whole_stream_command(Debug),
whole_stream_command(WithEnv),
whole_stream_command(Do),
whole_stream_command(Sleep),
// Statistics
whole_stream_command(Size),
whole_stream_command(Count),
whole_stream_command(Benchmark),
// Metadata
whole_stream_command(Tags),
// Shells
whole_stream_command(Next),
whole_stream_command(Previous),
whole_stream_command(Shells),
whole_stream_command(Enter),
whole_stream_command(Exit),
// Viz
whole_stream_command(Chart),
// Viewers
whole_stream_command(Autoview),
whole_stream_command(Table),
// Text manipulation
whole_stream_command(Hash),
whole_stream_command(HashBase64),
whole_stream_command(Split),
whole_stream_command(SplitColumn),
whole_stream_command(SplitRow),
whole_stream_command(SplitChars),
whole_stream_command(Lines),
whole_stream_command(Echo),
whole_stream_command(Parse),
whole_stream_command(Str),
whole_stream_command(StrToDecimal),
whole_stream_command(StrToInteger),
whole_stream_command(StrDowncase),
whole_stream_command(StrUpcase),
whole_stream_command(StrCapitalize),
whole_stream_command(StrFindReplace),
whole_stream_command(StrFrom),
whole_stream_command(StrSubstring),
whole_stream_command(StrSet),
whole_stream_command(StrToDatetime),
whole_stream_command(StrContains),
whole_stream_command(StrIndexOf),
whole_stream_command(StrTrim),
whole_stream_command(StrTrimLeft),
whole_stream_command(StrTrimRight),
whole_stream_command(StrStartsWith),
whole_stream_command(StrEndsWith),
whole_stream_command(StrCollect),
whole_stream_command(StrLength),
whole_stream_command(StrLPad),
whole_stream_command(StrReverse),
whole_stream_command(StrRPad),
whole_stream_command(StrCamelCase),
whole_stream_command(StrPascalCase),
whole_stream_command(StrKebabCase),
whole_stream_command(StrSnakeCase),
whole_stream_command(StrScreamingSnakeCase),
whole_stream_command(BuildString),
whole_stream_command(Ansi),
whole_stream_command(Char),
// Column manipulation
whole_stream_command(Move),
whole_stream_command(Reject),
whole_stream_command(Select),
whole_stream_command(Get),
whole_stream_command(Update),
whole_stream_command(Insert),
whole_stream_command(IntoInt),
whole_stream_command(SplitBy),
// Row manipulation
whole_stream_command(Reverse),
whole_stream_command(Append),
whole_stream_command(Prepend),
whole_stream_command(SortBy),
whole_stream_command(GroupBy),
whole_stream_command(GroupByDate),
whole_stream_command(First),
whole_stream_command(Last),
whole_stream_command(Every),
whole_stream_command(Nth),
whole_stream_command(Drop),
whole_stream_command(Format),
whole_stream_command(FileSize),
whole_stream_command(Where),
whole_stream_command(If),
whole_stream_command(Compact),
whole_stream_command(Default),
whole_stream_command(Skip),
whole_stream_command(SkipUntil),
whole_stream_command(SkipWhile),
whole_stream_command(Keep),
whole_stream_command(KeepUntil),
whole_stream_command(KeepWhile),
whole_stream_command(Range),
whole_stream_command(Rename),
whole_stream_command(Uniq),
whole_stream_command(Each),
whole_stream_command(EachGroup),
whole_stream_command(EachWindow),
whole_stream_command(Empty),
// Table manipulation
whole_stream_command(Flatten),
whole_stream_command(Move),
whole_stream_command(Merge),
whole_stream_command(Shuffle),
whole_stream_command(Wrap),
whole_stream_command(Pivot),
whole_stream_command(Headers),
whole_stream_command(Reduce),
// Data processing
whole_stream_command(Histogram),
whole_stream_command(Autoenv),
whole_stream_command(AutoenvTrust),
whole_stream_command(AutoenvUnTrust),
whole_stream_command(Math),
whole_stream_command(MathAbs),
whole_stream_command(MathAverage),
whole_stream_command(MathEval),
whole_stream_command(MathMedian),
whole_stream_command(MathMinimum),
whole_stream_command(MathMode),
whole_stream_command(MathMaximum),
whole_stream_command(MathStddev),
whole_stream_command(MathSummation),
whole_stream_command(MathVariance),
whole_stream_command(MathProduct),
whole_stream_command(MathRound),
whole_stream_command(MathFloor),
whole_stream_command(MathCeil),
// File format output
whole_stream_command(To),
whole_stream_command(ToCSV),
whole_stream_command(ToHTML),
whole_stream_command(ToJSON),
whole_stream_command(ToMarkdown),
whole_stream_command(ToTOML),
whole_stream_command(ToTSV),
whole_stream_command(ToURL),
whole_stream_command(ToYAML),
whole_stream_command(ToXML),
// File format input
whole_stream_command(From),
whole_stream_command(FromCSV),
whole_stream_command(FromEML),
whole_stream_command(FromTSV),
whole_stream_command(FromSSV),
whole_stream_command(FromINI),
whole_stream_command(FromJSON),
whole_stream_command(FromODS),
whole_stream_command(FromTOML),
whole_stream_command(FromURL),
whole_stream_command(FromXLSX),
whole_stream_command(FromXML),
whole_stream_command(FromYAML),
whole_stream_command(FromYML),
whole_stream_command(FromIcs),
whole_stream_command(FromVcf),
// "Private" commands (not intended to be accessed directly)
whole_stream_command(RunExternalCommand { interactive }),
// Random value generation
whole_stream_command(Random),
whole_stream_command(RandomBool),
whole_stream_command(RandomDice),
#[cfg(feature = "uuid_crate")]
whole_stream_command(RandomUUID),
whole_stream_command(RandomInteger),
whole_stream_command(RandomDecimal),
whole_stream_command(RandomChars),
// Path
whole_stream_command(PathBasename),
whole_stream_command(PathCommand),
whole_stream_command(PathDirname),
whole_stream_command(PathExists),
whole_stream_command(PathExpand),
whole_stream_command(PathExtension),
whole_stream_command(PathFilestem),
whole_stream_command(PathType),
// Url
whole_stream_command(UrlCommand),
whole_stream_command(UrlScheme),
whole_stream_command(UrlPath),
whole_stream_command(UrlHost),
whole_stream_command(UrlQuery),
whole_stream_command(Seq),
whole_stream_command(SeqDates),
]);
#[cfg(feature = "clipboard-cli")]
{
context.add_commands(vec![whole_stream_command(crate::commands::clip::Clip)]);
}
}
Ok(context)
}

View File

@ -23,19 +23,12 @@ impl WholeStreamCommand for Describe {
"Describes the objects in the stream."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
describe(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
describe(args).await
}
}
pub async fn describe(
args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn describe(args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(args
.input
.map(|row| {

View File

@ -2,15 +2,13 @@ use crate::commands::classified::block::run_block;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{
hir::Block, hir::ExternalRedirection, ReturnSuccess, Signature, SyntaxShape, Value,
};
use nu_protocol::{hir::CapturedBlock, hir::ExternalRedirection, Signature, SyntaxShape, Value};
pub struct Do;
#[derive(Deserialize, Debug)]
struct DoArgs {
block: Block,
block: CapturedBlock,
ignore_errors: bool,
}
@ -34,12 +32,8 @@ impl WholeStreamCommand for Do {
"Runs a block, optionally ignoring errors"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
do_(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
do_(args).await
}
fn examples(&self) -> Vec<Example> {
@ -52,28 +46,23 @@ impl WholeStreamCommand for Do {
Example {
description: "Run the block and ignore errors",
example: r#"do -i { thisisnotarealcommand }"#,
result: Some(vec![Value::nothing()]),
result: Some(vec![]),
},
]
}
}
async fn do_(
raw_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn do_(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
let external_redirection = raw_args.call_info.args.external_redirection;
let mut context = EvaluationContext::from_raw(&raw_args, &registry);
let scope = raw_args.call_info.scope.clone();
let context = EvaluationContext::from_raw(&raw_args);
let (
DoArgs {
ignore_errors,
mut block,
},
input,
) = raw_args.process(&registry).await?;
) = raw_args.process().await?;
let block_redirection = match external_redirection {
ExternalRedirection::None => {
@ -93,9 +82,10 @@ async fn do_(
x => x,
};
block.set_redirect(block_redirection);
let result = run_block(&block, &mut context, input, scope).await;
block.block.set_redirect(block_redirection);
context.scope.enter_scope();
let result = run_block(&block.block, &context, input).await;
context.scope.exit_scope();
if ignore_errors {
// To properly ignore errors we need to redirect stderr, consume it, and remove
@ -107,7 +97,7 @@ async fn do_(
context.clear_errors();
Ok(futures::stream::iter(output).to_output_stream())
}
Err(_) => Ok(OutputStream::one(ReturnSuccess::value(Value::nothing()))),
Err(_) => Ok(OutputStream::empty()),
}
} else {
result.map(|x| x.to_output_stream())

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -30,12 +29,8 @@ impl WholeStreamCommand for Drop {
"Remove the last number of rows. If you want to remove columns, try 'reject'."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
drop(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
drop(args).await
}
fn examples(&self) -> Vec<Example> {
@ -57,8 +52,8 @@ impl WholeStreamCommand for Drop {
}
}
async fn drop(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let (DropArgs { rows }, input) = args.process(&registry).await?;
async fn drop(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (DropArgs { rows }, input) = args.process().await?;
let v: Vec<_> = input.into_vec().await;
let rows_to_drop = if let Some(quantity) = rows {

View File

@ -73,12 +73,8 @@ impl WholeStreamCommand for Du {
"Find disk usage sizes of specified items"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
du(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
du(args).await
}
fn examples(&self) -> Vec<Example> {
@ -90,13 +86,12 @@ impl WholeStreamCommand for Du {
}
}
async fn du(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn du(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let ctrl_c = args.ctrl_c.clone();
let ctrl_c_copy = ctrl_c.clone();
let (args, _): (DuArgs, _) = args.process(&registry).await?;
let (args, _): (DuArgs, _) = args.process().await?;
let exclude = args.exclude.map_or(Ok(None), move |x| {
Pattern::new(&x.item)
.map(Option::Some)

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::classified::block::run_block;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
@ -6,7 +5,7 @@ use crate::prelude::*;
use futures::stream::once;
use nu_errors::ShellError;
use nu_protocol::{
hir::Block, Scope, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value,
hir::CapturedBlock, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value,
};
use nu_source::Tagged;
@ -14,7 +13,7 @@ pub struct Each;
#[derive(Deserialize)]
pub struct EachArgs {
block: Block,
block: CapturedBlock,
numbered: Tagged<bool>,
}
@ -38,12 +37,8 @@ impl WholeStreamCommand for Each {
"Run a block on each row of the table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
each(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
each(args).await
}
fn examples(&self) -> Vec<Example> {
@ -73,9 +68,8 @@ impl WholeStreamCommand for Each {
}
pub async fn process_row(
block: Arc<Block>,
scope: Arc<Scope>,
mut context: Arc<EvaluationContext>,
captured_block: Arc<Box<CapturedBlock>>,
context: Arc<EvaluationContext>,
input: Value,
) -> Result<OutputStream, ShellError> {
let input_clone = input.clone();
@ -83,24 +77,29 @@ pub async fn process_row(
// a parameter to the block (so it gets assigned to a variable that can be used inside the block) or
// if it wants the contents as as an input stream
let input_stream = if !block.params.is_empty() {
let input_stream = if !captured_block.block.params.positional.is_empty() {
InputStream::empty()
} else {
once(async { Ok(input_clone) }).to_input_stream()
};
let scope = if !block.params.is_empty() {
// FIXME: add check for more than parameter, once that's supported
Scope::append_var(scope, block.params[0].clone(), input)
} else {
scope
};
context.scope.enter_scope();
context.scope.add_vars(&captured_block.captured.entries);
Ok(
run_block(&block, Arc::make_mut(&mut context), input_stream, scope)
.await?
.to_output_stream(),
)
if !captured_block.block.params.positional.is_empty() {
// FIXME: add check for more than parameter, once that's supported
context
.scope
.add_var(captured_block.block.params.positional[0].0.name(), input);
} else {
context.scope.add_var("$it", input);
}
let result = run_block(&captured_block.block, &*context, input_stream).await;
context.scope.exit_scope();
Ok(result?.to_output_stream())
}
pub(crate) fn make_indexed_item(index: usize, item: Value) -> Value {
@ -111,27 +110,22 @@ pub(crate) fn make_indexed_item(index: usize, item: Value) -> Value {
dict.into_value()
}
async fn each(
raw_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let scope = raw_args.call_info.scope.clone();
let context = Arc::new(EvaluationContext::from_raw(&raw_args, &registry));
let (each_args, input): (EachArgs, _) = raw_args.process(&registry).await?;
let block = Arc::new(each_args.block);
async fn each(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
let context = Arc::new(EvaluationContext::from_raw(&raw_args));
let (each_args, input): (EachArgs, _) = raw_args.process().await?;
let block = Arc::new(Box::new(each_args.block));
if each_args.numbered.item {
Ok(input
.enumerate()
.then(move |input| {
let block = block.clone();
let scope = scope.clone();
let context = context.clone();
let row = make_indexed_item(input.0, input.1);
async {
match process_row(block, scope, context, row).await {
match process_row(block, context, row).await {
Ok(s) => s,
Err(e) => OutputStream::one(Err(e)),
}
@ -143,11 +137,10 @@ async fn each(
Ok(input
.then(move |input| {
let block = block.clone();
let scope = scope.clone();
let context = context.clone();
async {
match process_row(block, scope, context, input).await {
match process_row(block, context, input).await {
Ok(s) => s,
Err(e) => OutputStream::one(Err(e)),
}

View File

@ -2,7 +2,9 @@ use crate::commands::each::process_row;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{hir::Block, ReturnSuccess, Scope, Signature, SyntaxShape, UntaggedValue, Value};
use nu_protocol::{
hir::CapturedBlock, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use nu_source::Tagged;
use serde::Deserialize;
@ -11,7 +13,7 @@ pub struct EachGroup;
#[derive(Deserialize)]
pub struct EachGroupArgs {
group_size: Tagged<usize>,
block: Block,
block: CapturedBlock,
//numbered: Tagged<bool>,
}
@ -43,22 +45,14 @@ impl WholeStreamCommand for EachGroup {
}]
}
async fn run(
&self,
raw_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let scope = raw_args.call_info.scope.clone();
let context = Arc::new(EvaluationContext::from_raw(&raw_args, &registry));
let (each_args, input): (EachGroupArgs, _) = raw_args.process(&registry).await?;
let block = Arc::new(each_args.block);
async fn run(&self, raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
let context = Arc::new(EvaluationContext::from_raw(&raw_args));
let (each_args, input): (EachGroupArgs, _) = raw_args.process().await?;
let block = Arc::new(Box::new(each_args.block));
Ok(input
.chunks(each_args.group_size.item)
.then(move |input| {
run_block_on_vec(input, block.clone(), scope.clone(), context.clone())
})
.then(move |input| run_block_on_vec(input, block.clone(), context.clone()))
.flatten()
.to_output_stream())
}
@ -66,8 +60,7 @@ impl WholeStreamCommand for EachGroup {
pub(crate) fn run_block_on_vec(
input: Vec<Value>,
block: Arc<Block>,
scope: Arc<Scope>,
block: Arc<Box<CapturedBlock>>,
context: Arc<EvaluationContext>,
) -> impl Future<Output = OutputStream> {
let value = Value {
@ -76,7 +69,7 @@ pub(crate) fn run_block_on_vec(
};
async {
match process_row(block, scope, context, value).await {
match process_row(block, context, value).await {
Ok(s) => {
// We need to handle this differently depending on whether process_row
// returned just 1 value or if it returned multiple as a stream.

View File

@ -3,7 +3,7 @@ use crate::commands::WholeStreamCommand;
use crate::prelude::*;
//use itertools::Itertools;
use nu_errors::ShellError;
use nu_protocol::{hir::Block, Primitive, Signature, SyntaxShape, UntaggedValue};
use nu_protocol::{hir::CapturedBlock, Primitive, Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged;
use serde::Deserialize;
@ -12,7 +12,7 @@ pub struct EachWindow;
#[derive(Deserialize)]
pub struct EachWindowArgs {
window_size: Tagged<usize>,
block: Block,
block: CapturedBlock,
stride: Option<Tagged<usize>>,
}
@ -50,16 +50,10 @@ impl WholeStreamCommand for EachWindow {
}]
}
async fn run(
&self,
raw_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let scope = raw_args.call_info.scope.clone();
let context = Arc::new(EvaluationContext::from_raw(&raw_args, &registry));
let (each_args, mut input): (EachWindowArgs, _) = raw_args.process(&registry).await?;
let block = Arc::new(each_args.block);
async fn run(&self, raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
let context = Arc::new(EvaluationContext::from_raw(&raw_args));
let (each_args, mut input): (EachWindowArgs, _) = raw_args.process().await?;
let block = Arc::new(Box::new(each_args.block));
let mut window: Vec<_> = input
.by_ref()
@ -80,13 +74,12 @@ impl WholeStreamCommand for EachWindow {
window.push(input);
let block = block.clone();
let scope = scope.clone();
let context = context.clone();
let local_window = window.clone();
async move {
if i % stride == 0 {
Some(run_block_on_vec(local_window, block, scope, context).await)
Some(run_block_on_vec(local_window, block, context).await)
} else {
None
}

View File

@ -8,7 +8,7 @@ use nu_protocol::{
pub struct Echo;
#[derive(Deserialize)]
#[derive(Deserialize, Debug)]
pub struct EchoArgs {
pub rest: Vec<Value>,
}
@ -27,12 +27,8 @@ impl WholeStreamCommand for Echo {
"Echo the arguments back to the user."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
echo(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
echo(args).await
}
fn examples(&self) -> Vec<Example> {
@ -51,9 +47,8 @@ impl WholeStreamCommand for Echo {
}
}
async fn echo(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (args, _): (EchoArgs, _) = args.process(&registry).await?;
async fn echo(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (args, _): (EchoArgs, _) = args.process().await?;
let stream = args.rest.into_iter().map(|i| match i.as_string() {
Ok(s) => OutputStream::one(Ok(ReturnSuccess::Value(
@ -69,7 +64,7 @@ async fn echo(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
value: UntaggedValue::Primitive(Primitive::Range(range)),
tag,
} => futures::stream::iter(RangeIterator::new(*range, tag)).to_output_stream(),
_ => OutputStream::one(Ok(ReturnSuccess::Value(i.clone()))),
x => OutputStream::one(Ok(ReturnSuccess::Value(x))),
},
});

View File

@ -1,17 +1,15 @@
use crate::command_registry::CommandRegistry;
use crate::commands::classified::block::run_block;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{
hir::Block, ColumnPath, Primitive, ReturnSuccess, Scope, Signature, SyntaxShape, UntaggedValue,
Value,
hir::CapturedBlock, ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape,
UntaggedValue, Value,
};
use nu_source::Tagged;
use nu_value_ext::{as_string, ValueExt};
use futures::stream::once;
use indexmap::indexmap;
#[derive(Deserialize)]
pub struct Arguments {
@ -37,12 +35,8 @@ impl WholeStreamCommand for Command {
"Check for empty values"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
is_empty(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
is_empty(args).await
}
fn examples(&self) -> Vec<Example> {
@ -87,16 +81,12 @@ impl WholeStreamCommand for Command {
}
}
async fn is_empty(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn is_empty(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let name_tag = Arc::new(args.call_info.name_tag.clone());
let context = Arc::new(EvaluationContext::from_raw(&args, &registry));
let scope = args.call_info.scope.clone();
let (Arguments { rest }, input) = args.process(&registry).await?;
let (columns, default_block): (Vec<ColumnPath>, Option<Block>) = arguments(rest)?;
let context = Arc::new(EvaluationContext::from_raw(&args));
let (Arguments { rest }, input) = args.process().await?;
let (columns, default_block): (Vec<ColumnPath>, Option<Box<CapturedBlock>>) = arguments(rest)?;
let default_block = Arc::new(default_block);
if input.is_empty() {
@ -107,13 +97,12 @@ async fn is_empty(
return Ok(InputStream::from_stream(stream)
.then(move |input| {
let tag = name_tag.clone();
let scope = scope.clone();
let context = context.clone();
let block = default_block.clone();
let columns = vec![];
async {
match process_row(scope, context, input, block, columns, tag).await {
match process_row(context, input, block, columns, tag).await {
Ok(s) => s,
Err(e) => OutputStream::one(Err(e)),
}
@ -126,13 +115,12 @@ async fn is_empty(
Ok(input
.then(move |input| {
let tag = name_tag.clone();
let scope = scope.clone();
let context = context.clone();
let block = default_block.clone();
let columns = columns.clone();
async {
match process_row(scope, context, input, block, columns, tag).await {
match process_row(context, input, block, columns, tag).await {
Ok(s) => s,
Err(e) => OutputStream::one(Err(e)),
}
@ -142,7 +130,9 @@ async fn is_empty(
.to_output_stream())
}
fn arguments(rest: Vec<Value>) -> Result<(Vec<ColumnPath>, Option<Block>), ShellError> {
fn arguments(
rest: Vec<Value>,
) -> Result<(Vec<ColumnPath>, Option<Box<CapturedBlock>>), ShellError> {
let mut rest = rest;
let mut columns = vec![];
let mut default = None;
@ -172,10 +162,9 @@ fn arguments(rest: Vec<Value>) -> Result<(Vec<ColumnPath>, Option<Block>), Shell
}
async fn process_row(
scope: Arc<Scope>,
mut context: Arc<EvaluationContext>,
context: Arc<EvaluationContext>,
input: Value,
default_block: Arc<Option<Block>>,
default_block: Arc<Option<Box<CapturedBlock>>>,
column_paths: Vec<ColumnPath>,
tag: Arc<Tag>,
) -> Result<OutputStream, ShellError> {
@ -187,16 +176,14 @@ async fn process_row(
let for_block = input.clone();
let input_stream = once(async { Ok(for_block) }).to_input_stream();
let scope = Scope::append_var(scope, "$it", input.clone());
context.scope.enter_scope();
context.scope.add_vars(&default_block.captured.entries);
context.scope.add_var("$it", input.clone());
let mut stream = run_block(
&default_block,
Arc::make_mut(&mut context),
input_stream,
scope,
)
.await?;
let stream = run_block(&default_block.block, &*context, input_stream).await;
context.scope.exit_scope();
let mut stream = stream?;
*results = Some({
let values = stream.drain_vec().await;

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::UnevaluatedCallInfo;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
@ -50,12 +49,8 @@ For a more complete list of encodings please refer to the encoding_rs
documentation link at https://docs.rs/encoding_rs/0.8.23/encoding_rs/#statics"#
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
enter(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
enter(args).await
}
fn examples(&self) -> Vec<Example> {
@ -79,19 +74,15 @@ documentation link at https://docs.rs/encoding_rs/0.8.23/encoding_rs/#statics"#
}
}
async fn enter(
raw_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let scope = raw_args.call_info.scope.clone();
async fn enter(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
let scope = raw_args.scope.clone();
let shell_manager = raw_args.shell_manager.clone();
let head = raw_args.call_info.args.head.clone();
let ctrl_c = raw_args.ctrl_c.clone();
let current_errors = raw_args.current_errors.clone();
let host = raw_args.host.clone();
let tag = raw_args.call_info.name_tag.clone();
let (EnterArgs { location, encoding }, _) = raw_args.process(&registry).await?;
let (EnterArgs { location, encoding }, _) = raw_args.process().await?;
let location_string = location.display().to_string();
let location_clone = location_string.clone();
@ -101,7 +92,7 @@ async fn enter(
if spec.len() == 2 {
let (_, command) = (spec[0], spec[1]);
if registry.has(command) {
if scope.has_command(command) {
return Ok(OutputStream::one(ReturnSuccess::action(
CommandAction::EnterHelpShell(
UntaggedValue::string(command).into_value(Tag::unknown()),
@ -121,11 +112,12 @@ async fn enter(
let cwd = shell_manager.path();
let full_path = std::path::PathBuf::from(cwd);
let span = location.span();
let (file_extension, tagged_contents) = crate::commands::open::fetch(
&full_path,
&PathBuf::from(location_clone),
tag.span,
span,
encoding,
)
.await?;
@ -134,7 +126,7 @@ async fn enter(
UntaggedValue::Primitive(Primitive::String(_)) => {
if let Some(extension) = file_extension {
let command_name = format!("from {}", extension);
if let Some(converter) = registry.get_command(&command_name) {
if let Some(converter) = scope.get_command(&command_name) {
let new_args = RawCommandArgs {
host,
ctrl_c,
@ -149,12 +141,12 @@ async fn enter(
external_redirection: ExternalRedirection::Stdout,
},
name_tag: tag.clone(),
scope: scope.clone(),
},
scope: scope.clone(),
};
let tag = tagged_contents.tag.clone();
let mut result = converter
.run(new_args.with_input(vec![tagged_contents]), &registry)
.run(new_args.with_input(vec![tagged_contents]))
.await?;
let result_vec: Vec<Result<ReturnSuccess, ShellError>> =
result.drain_vec().await;

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -37,12 +36,8 @@ impl WholeStreamCommand for Every {
"Show (or skip) every n-th row, starting from the first one."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
every(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
every(args).await
}
fn examples(&self) -> Vec<Example> {
@ -68,9 +63,8 @@ impl WholeStreamCommand for Every {
}
}
async fn every(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (EveryArgs { stride, skip }, input) = args.process(&registry).await?;
async fn every(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (EveryArgs { stride, skip }, input) = args.process().await?;
let stride = stride.item;
let skip = skip.item;

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -30,12 +29,8 @@ impl WholeStreamCommand for Exec {
"Execute command"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
exec(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
exec(args).await
}
fn examples(&self) -> Vec<Example> {
@ -55,13 +50,12 @@ impl WholeStreamCommand for Exec {
}
#[cfg(unix)]
async fn exec(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
async fn exec(args: CommandArgs) -> Result<OutputStream, ShellError> {
use std::os::unix::process::CommandExt;
use std::process::Command;
let registry = registry.clone();
let name = args.call_info.name_tag.clone();
let (args, _): (ExecArgs, _) = args.process(&registry).await?;
let (args, _): (ExecArgs, _) = args.process().await?;
let mut command = Command::new(args.command.item);
for tagged_arg in args.rest {
@ -78,7 +72,7 @@ async fn exec(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
}
#[cfg(not(unix))]
async fn exec(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
async fn exec(args: CommandArgs) -> Result<OutputStream, ShellError> {
Err(ShellError::labeled_error(
"Error on exec",
"exec is not supported on your platform",

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::command::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -20,12 +19,8 @@ impl WholeStreamCommand for Exit {
"Exit the current shell (or all shells)"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
exit(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
exit(args).await
}
fn examples(&self) -> Vec<Example> {
@ -44,12 +39,8 @@ impl WholeStreamCommand for Exit {
}
}
pub async fn exit(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
pub async fn exit(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let command_action = if args.call_info.args.has("now") {
CommandAction::Exit

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -30,12 +29,8 @@ impl WholeStreamCommand for First {
"Show only the first number of rows."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
first(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
first(args).await
}
fn examples(&self) -> Vec<Example> {
@ -57,9 +52,8 @@ impl WholeStreamCommand for First {
}
}
async fn first(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (FirstArgs { rows }, input) = args.process(&registry).await?;
async fn first(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (FirstArgs { rows }, input) = args.process().await?;
let rows_desired = if let Some(quantity) = rows {
*quantity
} else {

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -28,12 +27,8 @@ impl WholeStreamCommand for Command {
"Flatten the table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
flatten(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
flatten(args).await
}
fn examples(&self) -> Vec<Example> {
@ -57,13 +52,9 @@ impl WholeStreamCommand for Command {
}
}
async fn flatten(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn flatten(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let registry = registry.clone();
let (Arguments { rest: columns }, input) = args.process(&registry).await?;
let (Arguments { rest: columns }, input) = args.process().await?;
Ok(input
.map(move |item| {

View File

@ -1,9 +1,8 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::evaluate::evaluate_baseline_expr;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Scope, Signature, SyntaxShape, UntaggedValue};
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged;
use std::borrow::Borrow;
@ -32,12 +31,8 @@ impl WholeStreamCommand for Format {
"Format columns into a string using a simple pattern."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
format_command(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
format_command(args).await
}
fn examples(&self) -> Vec<Example> {
@ -49,13 +44,9 @@ impl WholeStreamCommand for Format {
}
}
async fn format_command(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = Arc::new(registry.clone());
let scope = args.call_info.scope.clone();
let (FormatArgs { pattern }, input) = args.process(&registry).await?;
async fn format_command(args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = Arc::new(EvaluationContext::from_args(&args));
let (FormatArgs { pattern }, input) = args.process().await?;
let format_pattern = format(&pattern);
let commands = Arc::new(format_pattern);
@ -64,8 +55,7 @@ async fn format_command(
.then(move |value| {
let mut output = String::new();
let commands = commands.clone();
let registry = registry.clone();
let scope = scope.clone();
let ctx = ctx.clone();
async move {
for command in &*commands {
@ -77,15 +67,13 @@ async fn format_command(
// FIXME: use the correct spans
let full_column_path = nu_parser::parse_full_column_path(
&(c.to_string()).spanned(Span::unknown()),
&*registry,
&ctx.scope,
);
let result = evaluate_baseline_expr(
&full_column_path.0,
&registry,
Scope::append_var(scope.clone(), "$it", value.clone()),
)
.await;
ctx.scope.enter_scope();
ctx.scope.add_var("$it", value.clone());
let result = evaluate_baseline_expr(&full_column_path.0, &*ctx).await;
ctx.scope.exit_scope();
if let Ok(c) = result {
output

View File

@ -43,12 +43,8 @@ impl WholeStreamCommand for FileSize {
"Converts a column of filesizes to some specified format"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
filesize(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
filesize(args).await
}
fn examples(&self) -> Vec<Example> {
@ -77,7 +73,7 @@ async fn process_row(
match replace_for {
Ok(s) => match convert_bytes_to_string_using_format(s, format) {
Ok(b) => OutputStream::one(ReturnSuccess::value(
input.replace_data_at_column_path(&field, b).expect("Given that the existance check was already done, this souldn't trigger never"),
input.replace_data_at_column_path(&field, b).expect("Given that the existence check was already done, this shouldn't trigger never"),
)),
Err(e) => OutputStream::one(Err(e)),
},
@ -86,12 +82,8 @@ async fn process_row(
})
}
async fn filesize(
raw_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (Arguments { field, format }, input) = raw_args.process(&registry).await?;
async fn filesize(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
let (Arguments { field, format }, input) = raw_args.process().await?;
let field = Arc::new(field);
Ok(input

View File

@ -19,14 +19,9 @@ impl WholeStreamCommand for From {
"Parse content (string or binary) as a table (input format based on subcommand, like csv, ini, json, toml)"
}
async fn run(
&self,
_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(crate::commands::help::get_help(&From, &registry))
UntaggedValue::string(crate::commands::help::get_help(&From, &args.scope))
.into_value(Tag::unknown()),
)))
}

View File

@ -37,12 +37,8 @@ impl WholeStreamCommand for FromCSV {
"Parse text as .csv and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_csv(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_csv(args).await
}
fn examples(&self) -> Vec<Example> {
@ -66,11 +62,7 @@ impl WholeStreamCommand for FromCSV {
}
}
async fn from_csv(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn from_csv(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let (
@ -79,7 +71,7 @@ async fn from_csv(
separator,
},
input,
) = args.process(&registry).await?;
) = args.process().await?;
let sep = match separator {
Some(Value {
value: UntaggedValue::Primitive(Primitive::String(s)),

View File

@ -35,12 +35,8 @@ impl WholeStreamCommand for FromEML {
"Parse text as .eml and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_eml(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_eml(args).await
}
}
@ -77,13 +73,9 @@ fn headerfieldvalue_to_value(tag: &Tag, value: &HeaderFieldValue) -> UntaggedVal
}
}
async fn from_eml(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn from_eml(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let registry = registry.clone();
let (eml_args, input): (FromEMLArgs, _) = args.process(&registry).await?;
let (eml_args, input): (FromEMLArgs, _) = args.process().await?;
let value = input.collect_string(tag.clone()).await?;
let body_preview = eml_args

View File

@ -23,21 +23,13 @@ impl WholeStreamCommand for FromIcs {
"Parse text as .ics and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_ics(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_ics(args).await
}
}
async fn from_ics(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
async fn from_ics(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let tag = args.name_tag();
let input = args.input;

View File

@ -20,12 +20,8 @@ impl WholeStreamCommand for FromINI {
"Parse text as .ini and create table"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_ini(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_ini(args).await
}
}
@ -64,12 +60,8 @@ pub fn from_ini_string_to_value(
Ok(convert_ini_top_to_nu_value(&v, tag))
}
async fn from_ini(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
async fn from_ini(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let tag = args.name_tag();
let input = args.input;
let concat_string = input.collect_string(tag.clone()).await?;

View File

@ -28,12 +28,8 @@ impl WholeStreamCommand for FromJSON {
"Parse text as .json and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_json(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_json(args).await
}
}
@ -72,14 +68,10 @@ pub fn from_json_string_to_value(s: String, tag: impl Into<Tag>) -> nu_json::Res
Ok(convert_json_value_to_nu_value(&v, tag))
}
async fn from_json(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn from_json(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name_tag = args.call_info.name_tag.clone();
let registry = registry.clone();
let (FromJSONArgs { objects }, input) = args.process(&registry).await?;
let (FromJSONArgs { objects }, input) = args.process().await?;
let concat_string = input.collect_string(name_tag.clone()).await?;
let string_clone: Vec<_> = concat_string.item.lines().map(|x| x.to_string()).collect();

View File

@ -31,29 +31,21 @@ impl WholeStreamCommand for FromODS {
"Parse OpenDocument Spreadsheet(.ods) data and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_ods(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_ods(args).await
}
}
async fn from_ods(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn from_ods(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let span = tag.span;
let registry = registry.clone();
let (
FromODSArgs {
headerless: _headerless,
},
input,
) = args.process(&registry).await?;
) = args.process().await?;
let bytes = input.collect_binary(tag.clone()).await?;
let buf: Cursor<Vec<u8>> = Cursor::new(bytes.item);
let mut ods = Ods::<_>::new(buf).map_err(|_| {

View File

@ -46,12 +46,8 @@ impl WholeStreamCommand for FromSSV {
"Parse text as space-separated values and create a table. The default minimum number of spaces counted as a separator is 2."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_ssv(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_ssv(args).await
}
}
@ -251,12 +247,8 @@ fn from_ssv_string_to_value(
Some(UntaggedValue::Table(rows).into_value(&tag))
}
async fn from_ssv(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn from_ssv(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let registry = registry.clone();
let (
FromSSVArgs {
headerless,
@ -264,7 +256,7 @@ async fn from_ssv(
minimum_spaces,
},
input,
) = args.process(&registry).await?;
) = args.process().await?;
let concat_string = input.collect_string(name.clone()).await?;
let split_at = match minimum_spaces {
Some(number) => number.item,

View File

@ -19,12 +19,8 @@ impl WholeStreamCommand for FromTOML {
"Parse text as .toml and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_toml(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_toml(args).await
}
}
@ -65,12 +61,8 @@ pub fn from_toml_string_to_value(s: String, tag: impl Into<Tag>) -> Result<Value
Ok(convert_toml_value_to_nu_value(&v, tag))
}
pub async fn from_toml(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
pub async fn from_toml(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let tag = args.name_tag();
let input = args.input;

View File

@ -29,22 +29,14 @@ impl WholeStreamCommand for FromTSV {
"Parse text as .tsv and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_tsv(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_tsv(args).await
}
}
async fn from_tsv(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn from_tsv(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let (FromTSVArgs { headerless }, input) = args.process(&registry).await?;
let (FromTSVArgs { headerless }, input) = args.process().await?;
from_delimited_data(headerless, '\t', "TSV", input, name).await
}

View File

@ -19,21 +19,13 @@ impl WholeStreamCommand for FromURL {
"Parse url-encoded string as a table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_url(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_url(args).await
}
}
async fn from_url(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
async fn from_url(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let tag = args.name_tag();
let input = args.input;

View File

@ -22,21 +22,13 @@ impl WholeStreamCommand for FromVcf {
"Parse text as .vcf and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_vcf(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_vcf(args).await
}
}
async fn from_vcf(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
async fn from_vcf(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let tag = args.name_tag();
let input = args.input;

View File

@ -31,28 +31,20 @@ impl WholeStreamCommand for FromXLSX {
"Parse binary Excel(.xlsx) data and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_xlsx(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_xlsx(args).await
}
}
async fn from_xlsx(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn from_xlsx(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.name_tag.clone();
let span = tag.span;
let registry = registry.clone();
let (
FromXLSXArgs {
headerless: _headerless,
},
input,
) = args.process(&registry).await?;
) = args.process().await?;
let value = input.collect_binary(tag.clone()).await?;
let buf: Cursor<Vec<u8>> = Cursor::new(value.item);

View File

@ -19,12 +19,8 @@ impl WholeStreamCommand for FromXML {
"Parse text as .xml and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_xml(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_xml(args).await
}
}
@ -39,7 +35,7 @@ fn from_attributes_to_value(attributes: &[roxmltree::Attribute], tag: impl Into<
collected.into_value()
}
fn from_node_to_value<'a, 'd>(n: &roxmltree::Node<'a, 'd>, tag: impl Into<Tag>) -> Value {
fn from_node_to_value(n: &roxmltree::Node, tag: impl Into<Tag>) -> Value {
let tag = tag.into();
if n.is_element() {
@ -99,12 +95,8 @@ pub fn from_xml_string_to_value(s: String, tag: impl Into<Tag>) -> Result<Value,
Ok(from_document_to_value(&parsed, tag))
}
async fn from_xml(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
async fn from_xml(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let tag = args.name_tag();
let input = args.input;

View File

@ -19,12 +19,8 @@ impl WholeStreamCommand for FromYAML {
"Parse text as .yaml/.yml and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_yaml(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_yaml(args).await
}
}
@ -44,12 +40,8 @@ impl WholeStreamCommand for FromYML {
"Parse text as .yaml/.yml and create table."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
from_yaml(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
from_yaml(args).await
}
}
@ -141,12 +133,8 @@ pub fn from_yaml_string_to_value(s: String, tag: impl Into<Tag>) -> Result<Value
Ok(convert_yaml_value_to_nu_value(&v, tag)?)
}
async fn from_yaml(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let args = args.evaluate_once(&registry).await?;
async fn from_yaml(args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once().await?;
let tag = args.name_tag();
let input = args.input;

View File

@ -34,12 +34,8 @@ impl WholeStreamCommand for Get {
"Open given cells as text."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
get(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
get(args).await
}
fn examples(&self) -> Vec<Example> {
@ -58,12 +54,8 @@ impl WholeStreamCommand for Get {
}
}
pub async fn get(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (GetArgs { rest: column_paths }, mut input) = args.process(&registry).await?;
pub async fn get(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (GetArgs { rest: column_paths }, mut input) = args.process().await?;
if column_paths.is_empty() {
let vec = input.drain_vec().await;

View File

@ -1,7 +1,6 @@
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use crate::utils::suggestions::suggestions;
use indexmap::indexmap;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged;
@ -32,12 +31,8 @@ impl WholeStreamCommand for Command {
"Create a new table grouped."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
group_by(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
group_by(args).await
}
#[allow(clippy::unwrap_used)]
@ -133,15 +128,10 @@ enum Grouper {
ByBlock,
}
pub async fn group_by(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn group_by(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let registry = registry.clone();
let scope = args.call_info.scope.clone();
let context = Arc::new(EvaluationContext::from_raw(&args, &registry));
let (Arguments { grouper }, input) = args.process(&registry).await?;
let context = Arc::new(EvaluationContext::from_raw(&args));
let (Arguments { grouper }, input) = args.process().await?;
let values: Vec<Value> = input.collect().await;
let mut keys: Vec<Result<String, ShellError>> = vec![];
@ -157,10 +147,9 @@ pub async fn group_by(
for value in values.iter() {
let run = block.clone();
let scope = scope.clone();
let context = context.clone();
match crate::commands::each::process_row(run, scope, context, value.clone()).await {
match crate::commands::each::process_row(run, context, value.clone()).await {
Ok(mut s) => {
let collection: Vec<Result<ReturnSuccess, ShellError>> =
s.drain_vec().await;

View File

@ -38,12 +38,8 @@ impl WholeStreamCommand for GroupByDate {
"creates a table grouped by date."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
group_by_date(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
group_by_date(args).await
}
fn examples(&self) -> Vec<Example> {
@ -63,11 +59,7 @@ enum GroupByColumn {
Name(Option<Tagged<String>>),
}
pub async fn group_by_date(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
pub async fn group_by_date(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let (
GroupByDateArgs {
@ -75,7 +67,7 @@ pub async fn group_by_date(
format,
},
input,
) = args.process(&registry).await?;
) = args.process().await?;
let values: Vec<Value> = input.collect().await;
if values.is_empty() {

View File

@ -65,12 +65,8 @@ impl WholeStreamCommand for SubCommand {
"base64 encode or decode a value"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
operate(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
operate(args).await
}
fn examples(&self) -> Vec<Example> {
@ -100,12 +96,7 @@ impl WholeStreamCommand for SubCommand {
}
}
async fn operate(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name_tag = &args.call_info.name_tag.clone();
let (
@ -116,7 +107,7 @@ async fn operate(
rest,
},
input,
) = args.process(&registry).await?;
) = args.process().await?;
if encode.item && decode.item {
return Ok(OutputStream::one(Err(ShellError::labeled_error(

View File

@ -22,15 +22,9 @@ impl WholeStreamCommand for Command {
"Apply hash function."
}
async fn run(
&self,
_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(crate::commands::help::get_help(&Command, &registry))
UntaggedValue::string(crate::commands::help::get_help(&Command, &args.scope))
.into_value(Tag::unknown()),
)))
}

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use futures::stream::StreamExt;
@ -23,12 +22,8 @@ impl WholeStreamCommand for Headers {
"Use the first row of the table as column names"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
headers(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
headers(args).await
}
fn examples(&self) -> Vec<Example> {
@ -47,10 +42,7 @@ impl WholeStreamCommand for Headers {
}
}
pub async fn headers(
args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
pub async fn headers(args: CommandArgs) -> Result<OutputStream, ShellError> {
let input = args.input;
let rows: Vec<Value> = input.collect().await;

View File

@ -29,12 +29,8 @@ impl WholeStreamCommand for Help {
"Display help information about commands."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
help(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
help(args).await
}
}
@ -53,21 +49,21 @@ pub(crate) fn command_dict(command: Command, tag: impl Into<Tag>) -> Value {
cmd_dict.into_value()
}
async fn help(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn help(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let (HelpArgs { rest }, ..) = args.process(&registry).await?;
let scope = args.scope.clone();
let (HelpArgs { rest }, ..) = args.process().await?;
if !rest.is_empty() {
if rest[0].item == "commands" {
let mut sorted_names = registry.names();
let mut sorted_names = scope.get_command_names();
sorted_names.sort();
let (mut subcommand_names, command_names) = sorted_names
.into_iter()
// Internal only commands shouldn't be displayed
.filter(|cmd_name| {
registry
scope
.get_command(&cmd_name)
.filter(|command| !command.is_internal())
.is_some()
@ -77,13 +73,13 @@ async fn help(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
fn process_name(
dict: &mut TaggedDictBuilder,
cmd_name: &str,
registry: CommandRegistry,
scope: Scope,
rest: Vec<Tagged<String>>,
name: Tag,
) -> Result<(), ShellError> {
let document_tag = rest[0].tag.clone();
let value = command_dict(
registry.get_command(&cmd_name).ok_or_else(|| {
scope.get_command(&cmd_name).ok_or_else(|| {
ShellError::labeled_error(
format!("Could not load {}", cmd_name),
"could not load command",
@ -114,7 +110,7 @@ async fn help(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
fn make_subcommands_table(
subcommand_names: &mut Vec<String>,
cmd_name: &str,
registry: CommandRegistry,
scope: Scope,
rest: Vec<Tagged<String>>,
name: Tag,
) -> Result<Value, ShellError> {
@ -132,7 +128,7 @@ async fn help(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
process_name(
&mut short_desc,
&cmd_name,
registry.clone(),
scope.clone(),
rest.clone(),
name.clone(),
)?;
@ -154,7 +150,7 @@ async fn help(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
process_name(
&mut short_desc,
&cmd_name,
registry.clone(),
scope.clone(),
rest.clone(),
name.clone(),
)?;
@ -163,7 +159,7 @@ async fn help(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
make_subcommands_table(
&mut subcommand_names,
&cmd_name,
registry.clone(),
scope.clone(),
rest.clone(),
name.clone(),
)?,
@ -174,22 +170,22 @@ async fn help(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
Ok(futures::stream::iter(iterator).to_output_stream())
} else if rest[0].item == "generate_docs" {
Ok(OutputStream::one(ReturnSuccess::value(generate_docs(
&registry,
&scope,
))))
} else if rest.len() == 2 {
// Check for a subcommand
let command_name = format!("{} {}", rest[0].item, rest[1].item);
if let Some(command) = registry.get_command(&command_name) {
if let Some(command) = scope.get_command(&command_name) {
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(get_help(command.stream_command(), &registry))
UntaggedValue::string(get_help(command.stream_command(), &scope))
.into_value(Tag::unknown()),
)))
} else {
Ok(OutputStream::empty())
}
} else if let Some(command) = registry.get_command(&rest[0].item) {
} else if let Some(command) = scope.get_command(&rest[0].item) {
Ok(OutputStream::one(ReturnSuccess::value(
UntaggedValue::string(get_help(command.stream_command(), &registry))
UntaggedValue::string(get_help(command.stream_command(), &scope))
.into_value(Tag::unknown()),
)))
} else {
@ -228,8 +224,8 @@ You can also learn more at https://www.nushell.sh/book/"#;
}
}
pub fn get_help(cmd: &dyn WholeStreamCommand, registry: &CommandRegistry) -> String {
get_documentation(cmd, registry, &DocumentationConfig::default())
pub fn get_help(cmd: &dyn WholeStreamCommand, scope: &Scope) -> String {
get_documentation(cmd, scope, &DocumentationConfig::default())
}
#[cfg(test)]

View File

@ -32,12 +32,8 @@ impl WholeStreamCommand for Histogram {
"Creates a new table with a histogram based on the column name passed in."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
histogram(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
histogram(args).await
}
fn examples(&self) -> Vec<Example> {
@ -62,13 +58,9 @@ impl WholeStreamCommand for Histogram {
}
}
pub async fn histogram(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
pub async fn histogram(args: CommandArgs) -> Result<OutputStream, ShellError> {
let name = args.call_info.name_tag.clone();
let (input, args) = args.evaluate_once(&registry).await?.parts();
let (input, args) = args.evaluate_once().await?.parts();
let values: Vec<Value> = input.collect().await;
@ -98,7 +90,7 @@ pub async fn histogram(
} else if let Some((key, _)) = columns[0].split_last() {
key.as_string()
} else {
"frecuency".to_string()
"frequency".to_string()
};
let column = if let Some(ref column) = column_grouper {
@ -157,7 +149,7 @@ pub async fn histogram(
.ok_or_else(|| {
ShellError::labeled_error(
"Unable to load group labels",
"unabled to load group labels",
"unable to load group labels",
&name,
)
})?

View File

@ -48,22 +48,15 @@ impl WholeStreamCommand for History {
"Display command history."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
history(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
history(args).await
}
}
async fn history(
args: CommandArgs,
_registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
async fn history(args: CommandArgs) -> Result<OutputStream, ShellError> {
let config: Box<dyn Conf> = Box::new(NuConfig::new());
let tag = args.call_info.name_tag.clone();
let (Arguments { clear }, _) = args.process(&_registry).await?;
let (Arguments { clear }, _) = args.process().await?;
let path = history_path(&config);

View File

@ -1,20 +1,19 @@
use crate::command_registry::CommandRegistry;
use crate::commands::classified::block::run_block;
use crate::commands::WholeStreamCommand;
use crate::evaluate::evaluate_baseline_expr;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{
hir::Block, hir::ClassifiedCommand, Scope, Signature, SyntaxShape, UntaggedValue,
hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue,
};
pub struct If;
#[derive(Deserialize)]
pub struct IfArgs {
condition: Block,
then_case: Block,
else_case: Block,
condition: CapturedBlock,
then_case: CapturedBlock,
else_case: CapturedBlock,
}
#[async_trait]
@ -27,7 +26,7 @@ impl WholeStreamCommand for If {
Signature::build("if")
.required(
"condition",
SyntaxShape::Math,
SyntaxShape::MathExpression,
"the condition that must match",
)
.required(
@ -46,37 +45,28 @@ impl WholeStreamCommand for If {
"Run blocks if a condition is true or false."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
if_command(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
if_command(args).await
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Run a block if a condition is true",
example: "echo 10 | if $it > 5 { echo 'greater than 5' } { echo 'less than or equal to 5' }",
example: "let x = 10; if $x > 5 { echo 'greater than 5' } { echo 'less than or equal to 5' }",
result: Some(vec![UntaggedValue::string("greater than 5").into()]),
},
Example {
description: "Run a block if a condition is false",
example: "echo 1 | if $it > 5 { echo 'greater than 5' } { echo 'less than or equal to 5' }",
example: "let x = 1; if $x > 5 { echo 'greater than 5' } { echo 'less than or equal to 5' }",
result: Some(vec![UntaggedValue::string("less than or equal to 5").into()]),
},
]
}
}
async fn if_command(
raw_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = Arc::new(registry.clone());
let scope = raw_args.call_info.scope.clone();
async fn if_command(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = raw_args.call_info.name_tag.clone();
let context = Arc::new(EvaluationContext::from_raw(&raw_args, &registry));
let context = Arc::new(EvaluationContext::from_raw(&raw_args));
let (
IfArgs {
@ -85,18 +75,18 @@ async fn if_command(
else_case,
},
input,
) = raw_args.process(&registry).await?;
let condition = {
if condition.block.len() != 1 {
) = raw_args.process().await?;
let cond = {
if condition.block.block.len() != 1 {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
match condition.block[0].list.get(0) {
Some(item) => match item {
ClassifiedCommand::Expr(expr) => expr.clone(),
match condition.block.block[0].pipelines.get(0) {
Some(item) => match item.list.get(0) {
Some(ClassifiedCommand::Expr(expr)) => expr.clone(),
_ => {
return Err(ShellError::labeled_error(
"Expected a condition",
@ -115,60 +105,27 @@ async fn if_command(
}
};
Ok(input
.then(move |input| {
let condition = condition.clone();
let then_case = then_case.clone();
let else_case = else_case.clone();
let registry = registry.clone();
let scope = Scope::append_var(scope.clone(), "$it", input);
let mut context = context.clone();
context.scope.enter_scope();
context.scope.add_vars(&condition.captured.entries);
async move {
//FIXME: should we use the scope that's brought in as well?
let condition = evaluate_baseline_expr(&condition, &*registry, scope.clone()).await;
//FIXME: should we use the scope that's brought in as well?
let condition = evaluate_baseline_expr(&cond, &*context).await;
match condition {
Ok(condition) => match condition.as_bool() {
Ok(b) => {
let result = if b {
run_block(&then_case.block, &*context, input).await
} else {
run_block(&else_case.block, &*context, input).await
};
context.scope.exit_scope();
match condition {
Ok(condition) => match condition.as_bool() {
Ok(b) => {
if b {
match run_block(
&then_case,
Arc::make_mut(&mut context),
InputStream::empty(),
scope,
)
.await
{
Ok(stream) => stream.to_output_stream(),
Err(e) => futures::stream::iter(vec![Err(e)].into_iter())
.to_output_stream(),
}
} else {
match run_block(
&else_case,
Arc::make_mut(&mut context),
InputStream::empty(),
scope,
)
.await
{
Ok(stream) => stream.to_output_stream(),
Err(e) => futures::stream::iter(vec![Err(e)].into_iter())
.to_output_stream(),
}
}
}
Err(e) => {
futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream()
}
},
Err(e) => futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream(),
}
result.map(|x| x.to_output_stream())
}
})
.flatten()
.to_output_stream())
Err(e) => Ok(futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream()),
},
Err(e) => Ok(futures::stream::iter(vec![Err(e)].into_iter()).to_output_stream()),
}
}
#[cfg(test)]

View File

@ -1,15 +1,13 @@
use crate::command_registry::CommandRegistry;
use crate::commands::classified::block::run_block;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{
ColumnPath, Primitive, ReturnSuccess, Scope, Signature, SyntaxShape, UntaggedValue, Value,
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use nu_value_ext::ValueExt;
use futures::stream::once;
use indexmap::indexmap;
pub struct Command;
@ -39,12 +37,8 @@ impl WholeStreamCommand for Command {
"Insert a new column with a given value."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
insert(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
insert(args).await
}
fn examples(&self) -> Vec<Example> {
@ -71,8 +65,7 @@ impl WholeStreamCommand for Command {
}
async fn process_row(
scope: Arc<Scope>,
mut context: Arc<EvaluationContext>,
context: Arc<EvaluationContext>,
input: Value,
mut value: Arc<Value>,
field: Arc<ColumnPath>,
@ -87,9 +80,13 @@ async fn process_row(
let for_block = input.clone();
let input_stream = once(async { Ok(for_block) }).to_input_stream();
let scope = Scope::append_var(scope, "$it", input.clone());
context.scope.enter_scope();
context.scope.add_vars(&block.captured.entries);
context.scope.add_var("$it", input.clone());
let result = run_block(&block, Arc::make_mut(&mut context), input_stream, scope).await;
let result = run_block(&block.block, &*context, input_stream).await;
context.scope.exit_scope();
match result {
Ok(mut stream) => {
@ -139,8 +136,9 @@ async fn process_row(
Value {
value: UntaggedValue::Primitive(Primitive::Nothing),
..
} => match scope
.var("$it")
} => match context
.scope
.get_var("$it")
.unwrap_or_else(|| UntaggedValue::nothing().into_untagged_value())
.insert_data_at_column_path(&field, value.clone())
{
@ -155,26 +153,20 @@ async fn process_row(
})
}
async fn insert(
raw_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let scope = raw_args.call_info.scope.clone();
let context = Arc::new(EvaluationContext::from_raw(&raw_args, &registry));
let (Arguments { column, value }, input) = raw_args.process(&registry).await?;
async fn insert(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
let context = Arc::new(EvaluationContext::from_raw(&raw_args));
let (Arguments { column, value }, input) = raw_args.process().await?;
let value = Arc::new(value);
let column = Arc::new(column);
Ok(input
.then(move |input| {
let scope = scope.clone();
let context = context.clone();
let value = value.clone();
let column = column.clone();
async {
match process_row(scope, context, input, value, column).await {
match process_row(context, input, value, column).await {
Ok(s) => s,
Err(e) => OutputStream::one(Err(e)),
}

View File

@ -26,12 +26,8 @@ impl WholeStreamCommand for IntoInt {
"Convert value to integer"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
into_int(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
into_int(args).await
}
fn examples(&self) -> Vec<Example> {
@ -43,12 +39,8 @@ impl WholeStreamCommand for IntoInt {
}
}
async fn into_int(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (args, _): (IntoIntArgs, _) = args.process(&registry).await?;
async fn into_int(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (args, _): (IntoIntArgs, _) = args.process().await?;
let stream = args.rest.into_iter().map(|i| match i {
Value {

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -30,12 +29,8 @@ impl WholeStreamCommand for Command {
"Keep the number of rows only"
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
keep(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
keep(args).await
}
fn examples(&self) -> Vec<Example> {
@ -59,9 +54,8 @@ impl WholeStreamCommand for Command {
}
}
async fn keep(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let (Arguments { rows }, input) = args.process(&registry).await?;
async fn keep(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (Arguments { rows }, input) = args.process().await?;
let rows_desired = if let Some(quantity) = rows {
*quantity
} else {

View File

@ -3,7 +3,8 @@ use crate::evaluate::evaluate_baseline_expr;
use crate::prelude::*;
use log::trace;
use nu_errors::ShellError;
use nu_protocol::{hir::ClassifiedCommand, Scope, Signature, SyntaxShape, UntaggedValue, Value};
use nu_parser::ParserScope;
use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
pub struct SubCommand;
@ -17,7 +18,7 @@ impl WholeStreamCommand for SubCommand {
Signature::build("keep until")
.required(
"condition",
SyntaxShape::Math,
SyntaxShape::RowCondition,
"The condition that must be met to stop keeping rows",
)
.filter()
@ -27,33 +28,30 @@ impl WholeStreamCommand for SubCommand {
"Keeps rows until the condition matches."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = Arc::new(registry.clone());
let scope = args.call_info.scope.clone();
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = Arc::new(EvaluationContext::from_args(&args));
let call_info = args.evaluate_once(&registry).await?;
let call_info = args.evaluate_once().await?;
let block = call_info.args.expect_nth(0)?.clone();
let condition = Arc::new(match block {
let (condition, captured) = match block {
Value {
value: UntaggedValue::Block(block),
value: UntaggedValue::Block(captured_block),
tag,
} => {
if block.block.len() != 1 {
if captured_block.block.block.len() != 1 {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
match block.block[0].list.get(0) {
Some(item) => match item {
ClassifiedCommand::Expr(expr) => expr.clone(),
match captured_block.block.block[0].pipelines.get(0) {
Some(item) => match item.list.get(0) {
Some(ClassifiedCommand::Expr(expr)) => {
(Arc::new(expr.clone()), captured_block.captured.clone())
}
_ => {
return Err(ShellError::labeled_error(
"Expected a condition",
@ -78,18 +76,21 @@ impl WholeStreamCommand for SubCommand {
tag,
));
}
});
};
Ok(call_info
.input
.take_while(move |item| {
let condition = condition.clone();
let registry = registry.clone();
let scope = Scope::append_var(scope.clone(), "$it", item.clone());
let ctx = ctx.clone();
ctx.scope.enter_scope();
ctx.scope.add_vars(&captured.entries);
ctx.scope.add_var("$it", item.clone());
trace!("ITEM = {:?}", item);
async move {
let result = evaluate_baseline_expr(&*condition, &registry, scope).await;
let result = evaluate_baseline_expr(&*condition, &*ctx).await;
ctx.scope.exit_scope();
trace!("RESULT = {:?}", result);
!matches!(result, Ok(ref v) if v.is_true())

View File

@ -3,7 +3,7 @@ use crate::evaluate::evaluate_baseline_expr;
use crate::prelude::*;
use log::trace;
use nu_errors::ShellError;
use nu_protocol::{hir::ClassifiedCommand, Scope, Signature, SyntaxShape, UntaggedValue, Value};
use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
pub struct SubCommand;
@ -17,7 +17,7 @@ impl WholeStreamCommand for SubCommand {
Signature::build("keep while")
.required(
"condition",
SyntaxShape::Math,
SyntaxShape::RowCondition,
"The condition that must be met to keep rows",
)
.filter()
@ -27,32 +27,29 @@ impl WholeStreamCommand for SubCommand {
"Keeps rows while the condition matches."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = Arc::new(registry.clone());
let scope = args.call_info.scope.clone();
let call_info = args.evaluate_once(&registry).await?;
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let ctx = Arc::new(EvaluationContext::from_args(&args));
let call_info = args.evaluate_once().await?;
let block = call_info.args.expect_nth(0)?.clone();
let condition = Arc::new(match block {
let (condition, captured) = match block {
Value {
value: UntaggedValue::Block(block),
value: UntaggedValue::Block(captured_block),
tag,
} => {
if block.block.len() != 1 {
if captured_block.block.block.len() != 1 {
return Err(ShellError::labeled_error(
"Expected a condition",
"expected a condition",
tag,
));
}
match block.block[0].list.get(0) {
Some(item) => match item {
ClassifiedCommand::Expr(expr) => expr.clone(),
match captured_block.block.block[0].pipelines.get(0) {
Some(item) => match item.list.get(0) {
Some(ClassifiedCommand::Expr(expr)) => {
(Arc::new(expr.clone()), captured_block.captured.clone())
}
_ => {
return Err(ShellError::labeled_error(
"Expected a condition",
@ -77,18 +74,22 @@ impl WholeStreamCommand for SubCommand {
tag,
));
}
});
};
Ok(call_info
.input
.take_while(move |item| {
let condition = condition.clone();
let registry = registry.clone();
let scope = Scope::append_var(scope.clone(), "$it", item.clone());
let ctx = ctx.clone();
ctx.scope.enter_scope();
ctx.scope.add_var("$it", item.clone());
ctx.scope.add_vars(&captured.entries);
trace!("ITEM = {:?}", item);
async move {
let result = evaluate_baseline_expr(&*condition, &registry, scope).await;
let result = evaluate_baseline_expr(&*condition, &*ctx).await;
ctx.scope.exit_scope();
trace!("RESULT = {:?}", result);
matches!(result, Ok(ref v) if v.is_true())

View File

@ -1,4 +1,3 @@
use crate::command_registry::CommandRegistry;
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
@ -38,12 +37,8 @@ impl WholeStreamCommand for Kill {
"Kill a process using the process id."
}
async fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
kill(args, registry).await
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
kill(args).await
}
fn examples(&self) -> Vec<Example> {
@ -62,9 +57,7 @@ impl WholeStreamCommand for Kill {
}
}
async fn kill(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
async fn kill(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (
KillArgs {
pid,
@ -73,7 +66,7 @@ async fn kill(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
quiet,
},
..,
) = args.process(&registry).await?;
) = args.process().await?;
let mut cmd = if cfg!(windows) {
let mut cmd = Command::new("taskkill");

Some files were not shown because too many files have changed in this diff Show More