From 93e8f6c05e1e1187d5b674d6b633deb839c84899 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 10 Jan 2021 15:50:49 +1300 Subject: [PATCH] Split nu-cli into nu-cli/nu-engine (#2898) We split off the evaluation engine part of nu-cli into its own crate. This helps improve build times for nu-cli by 17% in my tests. It also helps us see a bit better what's the core engine portion vs the part specific to the interactive CLI piece. There's more than can be done here, but I think it's a good start in the right direction. --- Cargo.lock | 54 +++ Cargo.toml | 3 +- crates/nu-cli/Cargo.toml | 1 + crates/nu-cli/src/cli.rs | 56 ++- crates/nu-cli/src/commands.rs | 5 +- crates/nu-cli/src/commands/ansi.rs | 2 +- crates/nu-cli/src/commands/append.rs | 2 +- crates/nu-cli/src/commands/autoenv.rs | 5 +- crates/nu-cli/src/commands/autoenv_trust.rs | 2 +- crates/nu-cli/src/commands/autoenv_untrust.rs | 2 +- .../nu-cli/src/commands/autoview/command.rs | 2 +- crates/nu-cli/src/commands/benchmark.rs | 4 +- crates/nu-cli/src/commands/build_string.rs | 2 +- crates/nu-cli/src/commands/cal.rs | 2 +- crates/nu-cli/src/commands/cd.rs | 11 +- crates/nu-cli/src/commands/char_.rs | 2 +- crates/nu-cli/src/commands/chart.rs | 5 +- .../src/commands/classified/external.rs | 13 +- crates/nu-cli/src/commands/classified/mod.rs | 5 - crates/nu-cli/src/commands/clear.rs | 2 +- crates/nu-cli/src/commands/clip.rs | 2 +- crates/nu-cli/src/commands/command.rs | 376 +----------------- crates/nu-cli/src/commands/compact.rs | 2 +- crates/nu-cli/src/commands/config/clear.rs | 2 +- crates/nu-cli/src/commands/config/command.rs | 5 +- crates/nu-cli/src/commands/config/get.rs | 2 +- crates/nu-cli/src/commands/config/load.rs | 2 +- crates/nu-cli/src/commands/config/path.rs | 2 +- crates/nu-cli/src/commands/config/remove.rs | 2 +- crates/nu-cli/src/commands/config/set.rs | 2 +- crates/nu-cli/src/commands/config/set_into.rs | 2 +- crates/nu-cli/src/commands/count.rs | 2 +- crates/nu-cli/src/commands/cp.rs | 11 +- crates/nu-cli/src/commands/date/command.rs | 5 +- crates/nu-cli/src/commands/date/format.rs | 2 +- .../nu-cli/src/commands/date/list_timezone.rs | 2 +- crates/nu-cli/src/commands/date/now.rs | 2 +- crates/nu-cli/src/commands/date/to_table.rs | 2 +- .../nu-cli/src/commands/date/to_timezone.rs | 2 +- crates/nu-cli/src/commands/date/utc.rs | 2 +- crates/nu-cli/src/commands/debug.rs | 2 +- crates/nu-cli/src/commands/def.rs | 2 +- crates/nu-cli/src/commands/default.rs | 2 +- crates/nu-cli/src/commands/default_context.rs | 3 +- crates/nu-cli/src/commands/describe.rs | 2 +- crates/nu-cli/src/commands/do_.rs | 4 +- crates/nu-cli/src/commands/drop.rs | 2 +- crates/nu-cli/src/commands/du.rs | 266 +------------ crates/nu-cli/src/commands/each/command.rs | 4 +- crates/nu-cli/src/commands/each/group.rs | 2 +- crates/nu-cli/src/commands/each/window.rs | 2 +- crates/nu-cli/src/commands/echo.rs | 2 +- crates/nu-cli/src/commands/empty.rs | 4 +- crates/nu-cli/src/commands/enter.rs | 4 +- crates/nu-cli/src/commands/every.rs | 2 +- crates/nu-cli/src/commands/exec.rs | 2 +- crates/nu-cli/src/commands/exit.rs | 1 - crates/nu-cli/src/commands/first.rs | 2 +- crates/nu-cli/src/commands/flatten.rs | 2 +- crates/nu-cli/src/commands/format/command.rs | 4 +- .../src/commands/format/format_filesize.rs | 2 +- crates/nu-cli/src/commands/from.rs | 5 +- crates/nu-cli/src/commands/from_csv.rs | 2 +- crates/nu-cli/src/commands/from_eml.rs | 2 +- crates/nu-cli/src/commands/from_ics.rs | 2 +- crates/nu-cli/src/commands/from_ini.rs | 2 +- crates/nu-cli/src/commands/from_json.rs | 2 +- crates/nu-cli/src/commands/from_ods.rs | 2 +- crates/nu-cli/src/commands/from_ssv.rs | 2 +- crates/nu-cli/src/commands/from_toml.rs | 2 +- crates/nu-cli/src/commands/from_tsv.rs | 2 +- crates/nu-cli/src/commands/from_url.rs | 2 +- crates/nu-cli/src/commands/from_vcf.rs | 2 +- crates/nu-cli/src/commands/from_xlsx.rs | 2 +- crates/nu-cli/src/commands/from_xml.rs | 2 +- crates/nu-cli/src/commands/from_yaml.rs | 2 +- crates/nu-cli/src/commands/get.rs | 2 +- crates/nu-cli/src/commands/group_by.rs | 2 +- crates/nu-cli/src/commands/group_by_date.rs | 2 +- crates/nu-cli/src/commands/hash_/base64_.rs | 2 +- crates/nu-cli/src/commands/hash_/command.rs | 5 +- crates/nu-cli/src/commands/headers.rs | 2 +- crates/nu-cli/src/commands/help.rs | 26 +- crates/nu-cli/src/commands/histogram.rs | 2 +- crates/nu-cli/src/commands/history.rs | 24 +- crates/nu-cli/src/commands/if_.rs | 6 +- crates/nu-cli/src/commands/insert.rs | 4 +- crates/nu-cli/src/commands/into_int.rs | 2 +- crates/nu-cli/src/commands/keep/command.rs | 2 +- crates/nu-cli/src/commands/keep/until.rs | 4 +- crates/nu-cli/src/commands/keep/while_.rs | 4 +- crates/nu-cli/src/commands/kill.rs | 2 +- crates/nu-cli/src/commands/last.rs | 2 +- crates/nu-cli/src/commands/let_.rs | 2 +- crates/nu-cli/src/commands/let_env.rs | 2 +- crates/nu-cli/src/commands/lines.rs | 2 +- crates/nu-cli/src/commands/ls.rs | 15 +- crates/nu-cli/src/commands/math/abs.rs | 2 +- crates/nu-cli/src/commands/math/avg.rs | 2 +- crates/nu-cli/src/commands/math/ceil.rs | 2 +- crates/nu-cli/src/commands/math/command.rs | 5 +- crates/nu-cli/src/commands/math/eval.rs | 2 +- crates/nu-cli/src/commands/math/floor.rs | 2 +- crates/nu-cli/src/commands/math/max.rs | 2 +- crates/nu-cli/src/commands/math/median.rs | 2 +- crates/nu-cli/src/commands/math/min.rs | 2 +- crates/nu-cli/src/commands/math/mode.rs | 2 +- crates/nu-cli/src/commands/math/product.rs | 2 +- crates/nu-cli/src/commands/math/round.rs | 2 +- crates/nu-cli/src/commands/math/stddev.rs | 2 +- crates/nu-cli/src/commands/math/sum.rs | 2 +- crates/nu-cli/src/commands/math/variance.rs | 2 +- crates/nu-cli/src/commands/merge.rs | 4 +- crates/nu-cli/src/commands/mkdir.rs | 12 +- crates/nu-cli/src/commands/move_/command.rs | 2 +- crates/nu-cli/src/commands/move_/mv.rs | 10 +- crates/nu-cli/src/commands/next.rs | 2 +- crates/nu-cli/src/commands/nth.rs | 2 +- crates/nu-cli/src/commands/nu/plugin.rs | 7 +- crates/nu-cli/src/commands/open.rs | 4 +- crates/nu-cli/src/commands/parse/command.rs | 2 +- crates/nu-cli/src/commands/path/basename.rs | 2 +- crates/nu-cli/src/commands/path/command.rs | 5 +- crates/nu-cli/src/commands/path/dirname.rs | 2 +- crates/nu-cli/src/commands/path/exists.rs | 2 +- crates/nu-cli/src/commands/path/expand.rs | 2 +- crates/nu-cli/src/commands/path/extension.rs | 2 +- crates/nu-cli/src/commands/path/filestem.rs | 2 +- crates/nu-cli/src/commands/path/type.rs | 4 +- crates/nu-cli/src/commands/pivot.rs | 2 +- crates/nu-cli/src/commands/prepend.rs | 2 +- crates/nu-cli/src/commands/prev.rs | 2 +- crates/nu-cli/src/commands/pwd.rs | 2 +- crates/nu-cli/src/commands/random/bool.rs | 2 +- crates/nu-cli/src/commands/random/chars.rs | 2 +- crates/nu-cli/src/commands/random/command.rs | 5 +- crates/nu-cli/src/commands/random/decimal.rs | 4 +- crates/nu-cli/src/commands/random/dice.rs | 2 +- crates/nu-cli/src/commands/random/integer.rs | 4 +- crates/nu-cli/src/commands/random/uuid.rs | 2 +- crates/nu-cli/src/commands/range.rs | 4 +- crates/nu-cli/src/commands/reduce.rs | 7 +- crates/nu-cli/src/commands/reject.rs | 2 +- crates/nu-cli/src/commands/rename.rs | 2 +- crates/nu-cli/src/commands/reverse.rs | 2 +- crates/nu-cli/src/commands/rm.rs | 16 +- crates/nu-cli/src/commands/run_external.rs | 4 +- crates/nu-cli/src/commands/save.rs | 2 +- crates/nu-cli/src/commands/select.rs | 2 +- crates/nu-cli/src/commands/seq.rs | 2 +- crates/nu-cli/src/commands/seq_dates.rs | 2 +- crates/nu-cli/src/commands/shells.rs | 2 +- crates/nu-cli/src/commands/shuffle.rs | 2 +- crates/nu-cli/src/commands/size.rs | 2 +- crates/nu-cli/src/commands/skip/command.rs | 2 +- crates/nu-cli/src/commands/skip/until.rs | 4 +- crates/nu-cli/src/commands/skip/while_.rs | 4 +- crates/nu-cli/src/commands/sleep.rs | 2 +- crates/nu-cli/src/commands/sort_by.rs | 2 +- crates/nu-cli/src/commands/source.rs | 2 +- crates/nu-cli/src/commands/split/chars.rs | 2 +- crates/nu-cli/src/commands/split/column.rs | 2 +- crates/nu-cli/src/commands/split/command.rs | 5 +- crates/nu-cli/src/commands/split/row.rs | 2 +- crates/nu-cli/src/commands/split_by.rs | 2 +- crates/nu-cli/src/commands/str_/capitalize.rs | 2 +- .../src/commands/str_/case/camel_case.rs | 2 +- .../src/commands/str_/case/kebab_case.rs | 2 +- .../src/commands/str_/case/pascal_case.rs | 2 +- .../str_/case/screaming_snake_case.rs | 2 +- .../src/commands/str_/case/snake_case.rs | 2 +- crates/nu-cli/src/commands/str_/collect.rs | 2 +- crates/nu-cli/src/commands/str_/command.rs | 5 +- crates/nu-cli/src/commands/str_/contains.rs | 2 +- crates/nu-cli/src/commands/str_/downcase.rs | 2 +- crates/nu-cli/src/commands/str_/ends_with.rs | 2 +- .../nu-cli/src/commands/str_/find_replace.rs | 2 +- crates/nu-cli/src/commands/str_/from.rs | 2 +- crates/nu-cli/src/commands/str_/index_of.rs | 2 +- crates/nu-cli/src/commands/str_/length.rs | 2 +- crates/nu-cli/src/commands/str_/lpad.rs | 2 +- crates/nu-cli/src/commands/str_/reverse.rs | 2 +- crates/nu-cli/src/commands/str_/rpad.rs | 2 +- crates/nu-cli/src/commands/str_/set.rs | 2 +- .../nu-cli/src/commands/str_/starts_with.rs | 2 +- crates/nu-cli/src/commands/str_/substring.rs | 2 +- .../nu-cli/src/commands/str_/to_datetime.rs | 2 +- crates/nu-cli/src/commands/str_/to_decimal.rs | 2 +- crates/nu-cli/src/commands/str_/to_integer.rs | 2 +- .../src/commands/str_/trim/trim_both_ends.rs | 2 +- .../src/commands/str_/trim/trim_left.rs | 2 +- .../src/commands/str_/trim/trim_right.rs | 2 +- crates/nu-cli/src/commands/str_/upcase.rs | 2 +- crates/nu-cli/src/commands/table/command.rs | 2 +- crates/nu-cli/src/commands/tags.rs | 2 +- crates/nu-cli/src/commands/to.rs | 5 +- crates/nu-cli/src/commands/to_csv.rs | 2 +- crates/nu-cli/src/commands/to_html.rs | 2 +- crates/nu-cli/src/commands/to_json.rs | 2 +- crates/nu-cli/src/commands/to_md.rs | 2 +- crates/nu-cli/src/commands/to_toml.rs | 2 +- crates/nu-cli/src/commands/to_tsv.rs | 2 +- crates/nu-cli/src/commands/to_url.rs | 2 +- crates/nu-cli/src/commands/to_xml.rs | 2 +- crates/nu-cli/src/commands/to_yaml.rs | 2 +- crates/nu-cli/src/commands/touch.rs | 2 +- crates/nu-cli/src/commands/uniq.rs | 2 +- crates/nu-cli/src/commands/update.rs | 4 +- crates/nu-cli/src/commands/url_/command.rs | 5 +- crates/nu-cli/src/commands/url_/host.rs | 2 +- crates/nu-cli/src/commands/url_/path.rs | 2 +- crates/nu-cli/src/commands/url_/query.rs | 2 +- crates/nu-cli/src/commands/url_/scheme.rs | 2 +- crates/nu-cli/src/commands/version.rs | 2 +- crates/nu-cli/src/commands/where_.rs | 4 +- crates/nu-cli/src/commands/which_.rs | 2 +- crates/nu-cli/src/commands/with_env.rs | 4 +- crates/nu-cli/src/commands/wrap.rs | 2 +- crates/nu-cli/src/completion/command.rs | 2 +- crates/nu-cli/src/completion/flag.rs | 2 +- crates/nu-cli/src/completion/mod.rs | 2 +- crates/nu-cli/src/env.rs | 4 +- crates/nu-cli/src/env/basic_host.rs | 77 ++++ crates/nu-cli/src/env/environment.rs | 33 +- crates/nu-cli/src/env/environment_syncer.rs | 28 +- crates/nu-cli/src/examples.rs | 15 +- crates/nu-cli/src/lib.rs | 14 - crates/nu-cli/src/line_editor.rs | 2 +- crates/nu-cli/src/prelude.rs | 97 +---- crates/nu-cli/src/script.rs | 13 +- crates/nu-cli/src/shell.rs | 7 - crates/nu-cli/src/shell/completer.rs | 2 +- crates/nu-cli/src/shell/helper.rs | 10 +- crates/nu-cli/src/types/deduction.rs | 7 +- crates/nu-cli/src/utils.rs | 289 -------------- crates/nu-engine/Cargo.toml | 48 +++ crates/nu-engine/src/call_info.rs | 27 ++ crates/nu-engine/src/command_args.rs | 173 ++++++++ .../{nu-cli => nu-engine}/src/deserializer.rs | 0 .../src/documentation.rs | 13 +- crates/nu-engine/src/env/environment.rs | 30 ++ crates/{nu-cli => nu-engine}/src/env/host.rs | 97 +---- crates/nu-engine/src/env/mod.rs | 2 + .../src/evaluate}/block.rs | 8 +- .../src/evaluate/evaluate_args.rs | 6 +- .../src/evaluate/evaluator.rs | 12 +- .../src/evaluate}/expr.rs | 5 +- .../src/evaluate}/internal.rs | 19 +- .../{nu-cli => nu-engine}/src/evaluate/mod.rs | 5 +- .../src/evaluate/operator.rs | 0 .../src/evaluate/scope.rs | 10 +- .../src/evaluate/variables.rs | 7 +- .../src/evaluation_context.rs | 60 +-- crates/nu-engine/src/example.rs | 7 + crates/nu-engine/src/filesystem/dir_info.rs | 267 +++++++++++++ .../src/filesystem}/filesystem_shell.rs | 39 +- crates/nu-engine/src/filesystem/mod.rs | 4 + .../src => nu-engine/src/filesystem}/path.rs | 0 crates/nu-engine/src/filesystem/utils.rs | 210 ++++++++++ crates/nu-engine/src/history_path.rs | 22 + crates/nu-engine/src/lib.rs | 39 ++ .../src}/maybe_text_codec.rs | 1 - .../src/plugin/build_plugin.rs} | 8 +- crates/nu-engine/src/plugin/mod.rs | 2 + .../src/plugin/run_plugin.rs} | 12 +- .../src/shell/help_shell.rs | 46 ++- .../shell.rs => nu-engine/src/shell/mod.rs} | 23 +- .../src/shell/painter.rs | 3 +- .../src/shell/palette.rs | 1 + crates/nu-engine/src/shell/shell_args.rs | 50 +++ .../src/shell/shell_manager.rs | 31 +- .../src/shell/value_shell.rs | 31 +- crates/nu-engine/src/whole_stream_command.rs | 197 +++++++++ crates/nu-protocol/src/lib.rs | 1 + crates/nu-protocol/src/value.rs | 1 + .../nu-protocol/src/value/value_structure.rs | 80 ++++ crates/nu-stream/src/input.rs | 22 +- crates/nu-stream/src/interruptible.rs | 13 + crates/nu-stream/src/lib.rs | 1 + crates/nu-stream/src/prelude.rs | 2 +- 280 files changed, 1890 insertions(+), 1750 deletions(-) create mode 100644 crates/nu-cli/src/env/basic_host.rs create mode 100644 crates/nu-engine/Cargo.toml create mode 100644 crates/nu-engine/src/call_info.rs create mode 100644 crates/nu-engine/src/command_args.rs rename crates/{nu-cli => nu-engine}/src/deserializer.rs (100%) rename crates/{nu-cli => nu-engine}/src/documentation.rs (96%) create mode 100644 crates/nu-engine/src/env/environment.rs rename crates/{nu-cli => nu-engine}/src/env/host.rs (58%) create mode 100644 crates/nu-engine/src/env/mod.rs rename crates/{nu-cli/src/commands/classified => nu-engine/src/evaluate}/block.rs (97%) rename crates/{nu-cli => nu-engine}/src/evaluate/evaluate_args.rs (89%) rename crates/{nu-cli => nu-engine}/src/evaluate/evaluator.rs (97%) rename crates/{nu-cli/src/commands/classified => nu-engine/src/evaluate}/expr.rs (80%) rename crates/{nu-cli/src/commands/classified => nu-engine/src/evaluate}/internal.rs (95%) rename crates/{nu-cli => nu-engine}/src/evaluate/mod.rs (65%) rename crates/{nu-cli => nu-engine}/src/evaluate/operator.rs (100%) rename crates/{nu-cli => nu-engine}/src/evaluate/scope.rs (97%) rename crates/{nu-cli => nu-engine}/src/evaluate/variables.rs (91%) rename crates/{nu-cli => nu-engine}/src/evaluation_context.rs (67%) create mode 100644 crates/nu-engine/src/example.rs create mode 100644 crates/nu-engine/src/filesystem/dir_info.rs rename crates/{nu-cli/src/shell => nu-engine/src/filesystem}/filesystem_shell.rs (98%) create mode 100644 crates/nu-engine/src/filesystem/mod.rs rename crates/{nu-cli/src => nu-engine/src/filesystem}/path.rs (100%) create mode 100644 crates/nu-engine/src/filesystem/utils.rs create mode 100644 crates/nu-engine/src/history_path.rs create mode 100644 crates/nu-engine/src/lib.rs rename crates/{nu-cli/src/commands/classified => nu-engine/src}/maybe_text_codec.rs (99%) rename crates/{nu-cli/src/plugin.rs => nu-engine/src/plugin/build_plugin.rs} (95%) create mode 100644 crates/nu-engine/src/plugin/mod.rs rename crates/{nu-cli/src/commands/classified/plugin.rs => nu-engine/src/plugin/run_plugin.rs} (97%) rename crates/{nu-cli => nu-engine}/src/shell/help_shell.rs (85%) rename crates/{nu-cli/src/shell/shell.rs => nu-engine/src/shell/mod.rs} (73%) rename crates/{nu-cli => nu-engine}/src/shell/painter.rs (95%) rename crates/{nu-cli => nu-engine}/src/shell/palette.rs (99%) create mode 100644 crates/nu-engine/src/shell/shell_args.rs rename crates/{nu-cli => nu-engine}/src/shell/shell_manager.rs (83%) rename crates/{nu-cli => nu-engine}/src/shell/value_shell.rs (93%) create mode 100644 crates/nu-engine/src/whole_stream_command.rs create mode 100644 crates/nu-protocol/src/value/value_structure.rs diff --git a/Cargo.lock b/Cargo.lock index ebdc0e2b37..cfecfbdbb6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3024,6 +3024,7 @@ dependencies = [ "log 0.4.11", "nu-cli", "nu-data", + "nu-engine", "nu-errors", "nu-parser", "nu-plugin", @@ -3097,6 +3098,7 @@ dependencies = [ "log 0.4.11", "meval", "nu-data", + "nu-engine", "nu-errors", "nu-json", "nu-parser", @@ -3154,6 +3156,18 @@ dependencies = [ "zip", ] +[[package]] +name = "nu-core-commands" +version = "0.25.1" +dependencies = [ + "async-trait", + "eml-parser", + "nu-errors", + "nu-protocol", + "nu-source", + "serde 1.0.118", +] + [[package]] name = "nu-data" version = "0.25.1" @@ -3184,6 +3198,46 @@ dependencies = [ "users", ] +[[package]] +name = "nu-engine" +version = "0.25.1" +dependencies = [ + "ansi_term 0.12.1", + "async-recursion", + "async-trait", + "bytes 0.5.6", + "derive-new", + "dunce", + "encoding_rs", + "filesize", + "fs_extra", + "futures 0.3.8", + "futures-util", + "futures_codec", + "getset", + "glob", + "indexmap", + "itertools", + "log 0.4.11", + "nu-data", + "nu-errors", + "nu-parser", + "nu-plugin", + "nu-protocol", + "nu-source", + "nu-stream", + "nu-test-support", + "nu-value-ext", + "parking_lot 0.11.1", + "rayon", + "serde 1.0.118", + "serde_json", + "tempfile", + "termcolor", + "umask", + "users", +] + [[package]] name = "nu-errors" version = "0.25.1" diff --git a/Cargo.toml b/Cargo.toml index a059f4aa55..7caeb05318 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ members = ["crates/*/"] [dependencies] nu-cli = {version = "0.25.1", path = "./crates/nu-cli"} nu-data = {version = "0.25.1", path = "./crates/nu-data"} +nu-engine = {version = "0.25.1", path = "./crates/nu-engine"} nu-errors = {version = "0.25.1", path = "./crates/nu-errors"} nu-parser = {version = "0.25.1", path = "./crates/nu-parser"} nu-plugin = {version = "0.25.1", path = "./crates/nu-plugin"} @@ -50,9 +51,9 @@ nu_plugin_xpath = {version = "0.25.1", path = "./crates/nu_plugin_xpath", option clap = "2.33.3" ctrlc = {version = "3.1.6", optional = true} futures = {version = "0.3.5", features = ["compat", "io-compat"]} +itertools = "0.10.0" log = "0.4.11" pretty_env_logger = "0.4.0" -itertools = "0.10.0" [dev-dependencies] dunce = "1.0.1" diff --git a/crates/nu-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index 5907464333..93837745b8 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -12,6 +12,7 @@ doctest = false [dependencies] nu-data = {version = "0.25.1", path = "../nu-data"} +nu-engine = {version = "0.25.1", path = "../nu-engine"} nu-errors = {version = "0.25.1", path = "../nu-errors"} nu-json = {version = "0.25.1", path = "../nu-json"} nu-parser = {version = "0.25.1", path = "../nu-parser"} diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 4716794f3a..54f5462a1c 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -1,7 +1,13 @@ -use crate::commands::classified::block::run_block; use crate::commands::default_context::create_default_context; -use crate::evaluation_context::EvaluationContext; +use crate::env::basic_host::BasicHost; use crate::line_editor::configure_ctrl_c; +use nu_engine::run_block; +use nu_engine::EvaluationContext; +use nu_engine::{FilesystemShell, Scope, ShellManager}; +use parking_lot::Mutex; +use std::sync::atomic::AtomicBool; +use std::sync::atomic::AtomicUsize; +use std::sync::Arc; #[allow(unused_imports)] pub(crate) use crate::script::{process_script, LineResult}; @@ -64,6 +70,40 @@ pub fn search_paths() -> Vec { search_paths } +pub fn maybe_print_errors(context: &EvaluationContext, source: Text) -> bool { + let errors = context.current_errors.clone(); + let mut errors = errors.lock(); + + if errors.len() > 0 { + let error = errors[0].clone(); + *errors = vec![]; + + crate::script::print_err(error, &source); + true + } else { + false + } +} + +pub fn basic_shell_manager() -> Result> { + Ok(ShellManager { + current_shell: Arc::new(AtomicUsize::new(0)), + shells: Arc::new(Mutex::new(vec![Box::new(FilesystemShell::basic()?)])), + }) +} + +pub fn basic_evaluation_context() -> Result> { + Ok(EvaluationContext { + scope: Scope::new(), + host: Arc::new(parking_lot::Mutex::new(Box::new(BasicHost))), + current_errors: Arc::new(Mutex::new(vec![])), + ctrl_c: Arc::new(AtomicBool::new(false)), + user_recently_used_autoenv_untrust: Arc::new(AtomicBool::new(false)), + shell_manager: crate::cli::basic_shell_manager()?, + windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())), + }) +} + pub async fn run_script_file( file_contents: String, redirect_stdin: bool, @@ -121,7 +161,7 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box> { // Give ourselves a scope to work in context.scope.enter_scope(); - let history_path = crate::commands::history::history_path(&configuration); + let history_path = nu_engine::history_path(&configuration); let _ = rl.load_history(&history_path); let mut session_text = String::new(); @@ -182,7 +222,7 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box> { Ok(result) => match result.collect_string(Tag::unknown()).await { Ok(string_result) => { let errors = context.get_errors(); - context.maybe_print_errors(Text::from(prompt_line)); + maybe_print_errors(&context, Text::from(prompt_line)); context.clear_errors(); if !errors.is_empty() { @@ -277,7 +317,7 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box> { LineResult::Success(line) => { rl.add_history_entry(&line); let _ = rl.save_history(&history_path); - context.maybe_print_errors(Text::from(session_text.clone())); + maybe_print_errors(&context, Text::from(session_text.clone())); } LineResult::ClearHistory => { @@ -293,7 +333,7 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box> { print_err(err, &Text::from(session_text.clone())); }); - context.maybe_print_errors(Text::from(session_text.clone())); + maybe_print_errors(&context, Text::from(session_text.clone())); } LineResult::CtrlC => { @@ -337,7 +377,7 @@ pub async fn cli(mut context: EvaluationContext) -> Result<(), Box> { } pub fn register_plugins(context: &mut EvaluationContext) -> Result<(), ShellError> { - if let Ok(plugins) = crate::plugin::scan(search_paths()) { + if let Ok(plugins) = nu_engine::plugin::build_plugin::scan(search_paths()) { context.add_commands( plugins .into_iter() @@ -411,7 +451,7 @@ mod tests { let (tokens, err) = nu_parser::lex(&data, 0); let (lite_block, err2) = nu_parser::block(tokens); if err.is_none() && err2.is_none() { - let context = crate::evaluation_context::EvaluationContext::basic().unwrap(); + let context = crate::cli::basic_evaluation_context().unwrap(); let _ = nu_parser::classify_block(&lite_block, &context.scope); } true diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index e3b04ec90a..03f7fe98ee 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -136,9 +136,6 @@ pub(crate) mod wrap; pub(crate) use autoview::Autoview; pub(crate) use cd::Cd; -pub(crate) use command::{ - whole_stream_command, Command, Example, UnevaluatedCallInfo, WholeStreamCommand, -}; pub(crate) use ansi::Ansi; pub(crate) use append::Command as Append; @@ -290,8 +287,8 @@ pub(crate) use wrap::Wrap; #[cfg(test)] mod tests { use super::*; - use crate::commands::whole_stream_command; use crate::examples::{test_anchors, test_examples}; + use nu_engine::{whole_stream_command, Command}; use nu_errors::ShellError; fn full_tests() -> Vec { diff --git a/crates/nu-cli/src/commands/ansi.rs b/crates/nu-cli/src/commands/ansi.rs index d722e914cb..a353f3b9ad 100644 --- a/crates/nu-cli/src/commands/ansi.rs +++ b/crates/nu-cli/src/commands/ansi.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use ansi_term::Color; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/append.rs b/crates/nu-cli/src/commands/append.rs index fa511a2f5b..7924405aa8 100644 --- a/crates/nu-cli/src/commands/append.rs +++ b/crates/nu-cli/src/commands/append.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/autoenv.rs b/crates/nu-cli/src/commands/autoenv.rs index 66cbf9fc9a..35d3f67436 100644 --- a/crates/nu-cli/src/commands/autoenv.rs +++ b/crates/nu-cli/src/commands/autoenv.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; use serde::Deserialize; @@ -63,8 +63,7 @@ The file can contain several optional sections: } async fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( - UntaggedValue::string(crate::commands::help::get_help(&Autoenv, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&Autoenv, &args.scope)).into_value(Tag::unknown()), ))) } diff --git a/crates/nu-cli/src/commands/autoenv_trust.rs b/crates/nu-cli/src/commands/autoenv_trust.rs index bc55049c13..665ca05fa0 100644 --- a/crates/nu-cli/src/commands/autoenv_trust.rs +++ b/crates/nu-cli/src/commands/autoenv_trust.rs @@ -1,6 +1,6 @@ use super::autoenv::read_trusted; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::SyntaxShape; use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/autoenv_untrust.rs b/crates/nu-cli/src/commands/autoenv_untrust.rs index 0d9d825d38..38cfc5c172 100644 --- a/crates/nu-cli/src/commands/autoenv_untrust.rs +++ b/crates/nu-cli/src/commands/autoenv_untrust.rs @@ -1,6 +1,6 @@ use super::autoenv::Trusted; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::SyntaxShape; use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/autoview/command.rs b/crates/nu-cli/src/commands/autoview/command.rs index 4e50287fe4..a83d03681f 100644 --- a/crates/nu-cli/src/commands/autoview/command.rs +++ b/crates/nu-cli/src/commands/autoview/command.rs @@ -1,8 +1,8 @@ use crate::commands::autoview::options::{ConfigExtensions, NuConfig as AutoViewConfiguration}; -use crate::commands::{UnevaluatedCallInfo, WholeStreamCommand}; use crate::prelude::*; use crate::primitive::get_color_config; use nu_data::value::format_leaf; +use nu_engine::{UnevaluatedCallInfo, WholeStreamCommand}; use nu_errors::ShellError; use nu_protocol::hir::{self, Expression, ExternalRedirection, Literal, SpannedExpression}; use nu_protocol::{Primitive, Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/benchmark.rs b/crates/nu-cli/src/commands/benchmark.rs index 8348278057..2aab2b48de 100644 --- a/crates/nu-cli/src/commands/benchmark.rs +++ b/crates/nu-cli/src/commands/benchmark.rs @@ -1,8 +1,8 @@ -use crate::commands::classified::block::run_block; -use crate::commands::WholeStreamCommand; use crate::prelude::*; #[cfg(feature = "rich-benchmark")] use heim::cpu::time; +use nu_engine::run_block; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ hir::{Block, CapturedBlock, ClassifiedCommand, Group, InternalCommand, Pipeline}, diff --git a/crates/nu-cli/src/commands/build_string.rs b/crates/nu-cli/src/commands/build_string.rs index eca9b7db6a..90ba76ec0a 100644 --- a/crates/nu-cli/src/commands/build_string.rs +++ b/crates/nu-cli/src/commands/build_string.rs @@ -1,8 +1,8 @@ use crate::prelude::*; use nu_errors::ShellError; -use crate::commands::WholeStreamCommand; use nu_data::value::format_leaf; +use nu_engine::WholeStreamCommand; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; #[derive(Deserialize)] diff --git a/crates/nu-cli/src/commands/cal.rs b/crates/nu-cli/src/commands/cal.rs index 0f8c9f6d1d..721f4e0d40 100644 --- a/crates/nu-cli/src/commands/cal.rs +++ b/crates/nu-cli/src/commands/cal.rs @@ -1,7 +1,7 @@ -use crate::commands::{command::EvaluatedWholeStreamCommandArgs, WholeStreamCommand}; use crate::prelude::*; use chrono::{Datelike, Local, NaiveDate}; use indexmap::IndexMap; +use nu_engine::{EvaluatedWholeStreamCommandArgs, WholeStreamCommand}; use nu_errors::ShellError; use nu_protocol::{Dictionary, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/cd.rs b/crates/nu-cli/src/commands/cd.rs index a088501465..727a3fa7d8 100644 --- a/crates/nu-cli/src/commands/cd.rs +++ b/crates/nu-cli/src/commands/cd.rs @@ -1,16 +1,9 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; -use std::path::PathBuf; - +use nu_engine::shell::CdArgs; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; -use nu_source::Tagged; - -#[derive(Deserialize)] -pub struct CdArgs { - pub(crate) path: Option>, -} pub struct Cd; diff --git a/crates/nu-cli/src/commands/char_.rs b/crates/nu-cli/src/commands/char_.rs index 88669ee1fd..7c7d415972 100644 --- a/crates/nu-cli/src/commands/char_.rs +++ b/crates/nu-cli/src/commands/char_.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/chart.rs b/crates/nu-cli/src/commands/chart.rs index 0ee1b61777..a8824d8d34 100644 --- a/crates/nu-cli/src/commands/chart.rs +++ b/crates/nu-cli/src/commands/chart.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; @@ -28,8 +28,7 @@ impl WholeStreamCommand for Chart { } Ok(OutputStream::one(Ok(ReturnSuccess::Value( - UntaggedValue::string(crate::commands::help::get_help(&Chart, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&Chart, &args.scope)).into_value(Tag::unknown()), )))) } } diff --git a/crates/nu-cli/src/commands/classified/external.rs b/crates/nu-cli/src/commands/classified/external.rs index c9dc745e33..48db3f3a03 100644 --- a/crates/nu-cli/src/commands/classified/external.rs +++ b/crates/nu-cli/src/commands/classified/external.rs @@ -1,7 +1,7 @@ -use crate::commands::classified::maybe_text_codec::{MaybeTextCodec, StringOrBinary}; -use crate::evaluate::evaluate_baseline_expr; use crate::futures::ThreadedReceiver; use crate::prelude::*; +use nu_engine::evaluate_baseline_expr; +use nu_engine::{MaybeTextCodec, StringOrBinary}; use std::borrow::Cow; use std::io::Write; @@ -18,6 +18,7 @@ use nu_protocol::hir::Expression; use nu_protocol::hir::{ExternalCommand, ExternalRedirection}; use nu_protocol::{Primitive, ShellTypeName, UntaggedValue, Value}; use nu_source::Tag; +use nu_stream::trace_stream; pub(crate) async fn run_external_command( command: ExternalCommand, @@ -532,7 +533,7 @@ mod tests { add_double_quotes, argument_is_quoted, escape_double_quotes, expand_tilde, remove_quotes, }; #[cfg(feature = "which")] - use super::{run_external_command, EvaluationContext, InputStream}; + use super::{run_external_command, InputStream}; #[cfg(feature = "which")] use futures::executor::block_on; @@ -560,8 +561,8 @@ mod tests { let cmd = ExternalBuilder::for_name("i_dont_exist.exe").build(); let input = InputStream::empty(); - let mut ctx = - EvaluationContext::basic().expect("There was a problem creating a basic context."); + let mut ctx = crate::cli::basic_evaluation_context() + .expect("There was a problem creating a basic context."); assert!( run_external_command(cmd, &mut ctx, input, ExternalRedirection::Stdout) @@ -575,7 +576,7 @@ mod tests { // async fn failure_run() -> Result<(), ShellError> { // let cmd = ExternalBuilder::for_name("fail").build(); - // let mut ctx = EvaluationContext::basic().expect("There was a problem creating a basic context."); + // let mut ctx = crate::cli::basic_evaluation_context().expect("There was a problem creating a basic context."); // let stream = run_external_command(cmd, &mut ctx, None, false) // .await? // .expect("There was a problem running the external command."); diff --git a/crates/nu-cli/src/commands/classified/mod.rs b/crates/nu-cli/src/commands/classified/mod.rs index 14d23068e2..6f666bb5f5 100644 --- a/crates/nu-cli/src/commands/classified/mod.rs +++ b/crates/nu-cli/src/commands/classified/mod.rs @@ -1,10 +1,5 @@ -pub(crate) mod block; mod dynamic; -pub(crate) mod expr; pub(crate) mod external; -pub(crate) mod internal; -pub(crate) mod maybe_text_codec; -pub(crate) mod plugin; #[allow(unused_imports)] pub(crate) use dynamic::Command as DynamicCommand; diff --git a/crates/nu-cli/src/commands/clear.rs b/crates/nu-cli/src/commands/clear.rs index 39b9bd524d..86ab539cf4 100644 --- a/crates/nu-cli/src/commands/clear.rs +++ b/crates/nu-cli/src/commands/clear.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::Signature; use std::process::Command; diff --git a/crates/nu-cli/src/commands/clip.rs b/crates/nu-cli/src/commands/clip.rs index a86e6de478..2a132d5ec4 100644 --- a/crates/nu-cli/src/commands/clip.rs +++ b/crates/nu-cli/src/commands/clip.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use futures::stream::StreamExt; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, Value}; diff --git a/crates/nu-cli/src/commands/command.rs b/crates/nu-cli/src/commands/command.rs index 7eaf5c8592..2ff4926dfc 100644 --- a/crates/nu-cli/src/commands/command.rs +++ b/crates/nu-cli/src/commands/command.rs @@ -1,111 +1,9 @@ -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_engine::Command; use nu_errors::ShellError; -use nu_protocol::hir::{self, Block}; -use nu_protocol::{CallInfo, EvaluatedArgs, ReturnSuccess, Signature, UntaggedValue, Value}; use parking_lot::Mutex; -use serde::Deserialize; -use std::ops::Deref; use std::sync::atomic::AtomicBool; -#[derive(Debug, Clone)] -pub struct UnevaluatedCallInfo { - pub args: hir::Call, - pub name_tag: Tag, -} - -impl UnevaluatedCallInfo { - pub async fn evaluate(self, ctx: &EvaluationContext) -> Result { - let args = evaluate_args(&self.args, ctx).await?; - - Ok(CallInfo { - args, - name_tag: self.name_tag, - }) - } - - pub fn switch_present(&self, switch: &str) -> bool { - self.args.switch_preset(switch) - } -} - -#[derive(Getters)] -#[get = "pub(crate)"] -pub struct CommandArgs { - pub host: Arc>>, - pub ctrl_c: Arc, - pub current_errors: Arc>>, - pub shell_manager: ShellManager, - pub call_info: UnevaluatedCallInfo, - pub scope: Scope, - pub input: InputStream, -} - -#[derive(Getters, Clone)] -#[get = "pub(crate)"] -pub struct RawCommandArgs { - pub host: Arc>>, - pub ctrl_c: Arc, - pub current_errors: Arc>>, - pub shell_manager: ShellManager, - pub scope: Scope, - pub call_info: UnevaluatedCallInfo, -} - -impl RawCommandArgs { - pub fn with_input(self, input: impl Into) -> CommandArgs { - CommandArgs { - host: self.host, - ctrl_c: self.ctrl_c, - current_errors: self.current_errors, - shell_manager: self.shell_manager, - call_info: self.call_info, - scope: self.scope, - input: input.into(), - } - } -} - -impl std::fmt::Debug for CommandArgs { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.call_info.fmt(f) - } -} - -impl CommandArgs { - pub async fn evaluate_once(self) -> Result { - 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(&ctx).await?; - let scope = self.scope.clone(); - - Ok(EvaluatedWholeStreamCommandArgs::new( - host, - ctrl_c, - shell_manager, - call_info, - input, - scope, - )) - } - - 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); - - Ok((T::deserialize(&mut deserializer)?, args.input)) - } -} - pub struct RunnableContext { pub input: InputStream, pub shell_manager: ShellManager, @@ -121,275 +19,3 @@ impl RunnableContext { self.scope.get_command(name) } } - -pub struct EvaluatedWholeStreamCommandArgs { - pub args: EvaluatedCommandArgs, - pub input: InputStream, -} - -impl Deref for EvaluatedWholeStreamCommandArgs { - type Target = EvaluatedCommandArgs; - fn deref(&self) -> &Self::Target { - &self.args - } -} - -impl EvaluatedWholeStreamCommandArgs { - pub fn new( - host: Arc>, - ctrl_c: Arc, - shell_manager: ShellManager, - call_info: CallInfo, - input: impl Into, - scope: Scope, - ) -> EvaluatedWholeStreamCommandArgs { - EvaluatedWholeStreamCommandArgs { - args: EvaluatedCommandArgs { - host, - ctrl_c, - shell_manager, - call_info, - scope, - }, - input: input.into(), - } - } - - pub fn name_tag(&self) -> Tag { - self.args.call_info.name_tag.clone() - } - - pub fn parts(self) -> (InputStream, EvaluatedArgs) { - let EvaluatedWholeStreamCommandArgs { args, input } = self; - - (input, args.call_info.args) - } - - pub fn split(self) -> (InputStream, EvaluatedCommandArgs) { - let EvaluatedWholeStreamCommandArgs { args, input } = self; - - (input, args) - } -} - -#[derive(Getters, new)] -#[get = "pub(crate)"] -pub struct EvaluatedCommandArgs { - pub host: Arc>, - pub ctrl_c: Arc, - pub shell_manager: ShellManager, - pub call_info: CallInfo, - pub scope: Scope, -} - -impl EvaluatedCommandArgs { - pub fn nth(&self, pos: usize) -> Option<&Value> { - self.call_info.args.nth(pos) - } - - /// Get the nth positional argument, error if not possible - pub fn expect_nth(&self, pos: usize) -> Result<&Value, ShellError> { - self.call_info - .args - .nth(pos) - .ok_or_else(|| ShellError::unimplemented("Better error: expect_nth")) - } - - pub fn get(&self, name: &str) -> Option<&Value> { - self.call_info.args.get(name) - } - - pub fn has(&self, name: &str) -> bool { - self.call_info.args.has(name) - } -} - -pub struct Example { - pub example: &'static str, - pub description: &'static str, - pub result: Option>, -} - -#[async_trait] -pub trait WholeStreamCommand: Send + Sync { - fn name(&self) -> &str; - - fn signature(&self) -> Signature { - Signature::new(self.name()).desc(self.usage()).filter() - } - - fn usage(&self) -> &str; - - async fn run(&self, args: CommandArgs) -> Result; - - fn is_binary(&self) -> bool { - false - } - - // Commands that are not meant to be run by users - fn is_internal(&self) -> bool { - false - } - - fn examples(&self) -> Vec { - Vec::new() - } -} - -// 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 { - &self.params.usage - } - - async fn run(&self, args: CommandArgs) -> Result { - 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 { - vec![] - } -} - -#[derive(Clone)] -pub struct Command(Arc); - -impl PrettyDebugWithSource for Command { - fn pretty_debug(&self, source: &str) -> DebugDocBuilder { - b::typed( - "whole stream command", - b::description(self.name()) - + b::space() - + b::equals() - + b::space() - + self.signature().pretty_debug(source), - ) - } -} - -impl std::fmt::Debug for Command { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "Command({})", self.name()) - } -} - -impl Command { - pub fn name(&self) -> &str { - self.0.name() - } - - pub fn signature(&self) -> Signature { - self.0.signature() - } - - pub fn usage(&self) -> &str { - self.0.usage() - } - - pub fn examples(&self) -> Vec { - self.0.examples() - } - - pub async fn run(&self, args: CommandArgs) -> Result { - if args.call_info.switch_present("help") { - let cl = self.0.clone(); - Ok(OutputStream::one(Ok(ReturnSuccess::Value( - UntaggedValue::string(get_help(&*cl, &args.scope)).into_value(Tag::unknown()), - )))) - } else { - self.0.run(args).await - } - } - - pub fn is_binary(&self) -> bool { - self.0.is_binary() - } - - pub fn is_internal(&self) -> bool { - self.0.is_internal() - } - - pub fn stream_command(&self) -> &dyn WholeStreamCommand { - &*self.0 - } -} - -pub fn whole_stream_command(command: impl WholeStreamCommand + 'static) -> Command { - Command(Arc::new(command)) -} diff --git a/crates/nu-cli/src/commands/compact.rs b/crates/nu-cli/src/commands/compact.rs index 07f94ed709..afde2eb2bc 100644 --- a/crates/nu-cli/src/commands/compact.rs +++ b/crates/nu-cli/src/commands/compact.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use futures::future; use futures::stream::StreamExt; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/config/clear.rs b/crates/nu-cli/src/commands/config/clear.rs index 9f7b338871..9d91dfbf95 100644 --- a/crates/nu-cli/src/commands/config/clear.rs +++ b/crates/nu-cli/src/commands/config/clear.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/config/command.rs b/crates/nu-cli/src/commands/config/command.rs index 8d36b692fd..3a81372faa 100644 --- a/crates/nu-cli/src/commands/config/command.rs +++ b/crates/nu-cli/src/commands/config/command.rs @@ -1,8 +1,9 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; -use crate::{CommandArgs, OutputStream}; +use nu_engine::CommandArgs; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; +use nu_stream::OutputStream; pub struct Command; diff --git a/crates/nu-cli/src/commands/config/get.rs b/crates/nu-cli/src/commands/config/get.rs index f9d9948bea..df07939156 100644 --- a/crates/nu-cli/src/commands/config/get.rs +++ b/crates/nu-cli/src/commands/config/get.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ColumnPath, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/config/load.rs b/crates/nu-cli/src/commands/config/load.rs index 10b333a769..d389ada77b 100644 --- a/crates/nu-cli/src/commands/config/load.rs +++ b/crates/nu-cli/src/commands/config/load.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/config/path.rs b/crates/nu-cli/src/commands/config/path.rs index 68d92c167c..394942a59b 100644 --- a/crates/nu-cli/src/commands/config/path.rs +++ b/crates/nu-cli/src/commands/config/path.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/config/remove.rs b/crates/nu-cli/src/commands/config/remove.rs index 6f09b2fbd8..d37da4b439 100644 --- a/crates/nu-cli/src/commands/config/remove.rs +++ b/crates/nu-cli/src/commands/config/remove.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/config/set.rs b/crates/nu-cli/src/commands/config/set.rs index 31a26dbe82..03b1c004a2 100644 --- a/crates/nu-cli/src/commands/config/set.rs +++ b/crates/nu-cli/src/commands/config/set.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ColumnPath, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/config/set_into.rs b/crates/nu-cli/src/commands/config/set_into.rs index 80f5a90f8a..a9561cd191 100644 --- a/crates/nu-cli/src/commands/config/set_into.rs +++ b/crates/nu-cli/src/commands/config/set_into.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/count.rs b/crates/nu-cli/src/commands/count.rs index c733af5b3c..b3a0c6d6ca 100644 --- a/crates/nu-cli/src/commands/count.rs +++ b/crates/nu-cli/src/commands/count.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use futures::stream::StreamExt; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/cp.rs b/crates/nu-cli/src/commands/cp.rs index 5d2bbebbb9..591f40ed4b 100644 --- a/crates/nu-cli/src/commands/cp.rs +++ b/crates/nu-cli/src/commands/cp.rs @@ -1,19 +1,10 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; -use nu_source::Tagged; -use std::path::PathBuf; pub struct Cpy; -#[derive(Deserialize)] -pub struct CopyArgs { - pub src: Tagged, - pub dst: Tagged, - pub recursive: Tagged, -} - #[async_trait] impl WholeStreamCommand for Cpy { fn name(&self) -> &str { diff --git a/crates/nu-cli/src/commands/date/command.rs b/crates/nu-cli/src/commands/date/command.rs index 551b22205e..146ee3ff82 100644 --- a/crates/nu-cli/src/commands/date/command.rs +++ b/crates/nu-cli/src/commands/date/command.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; @@ -21,8 +21,7 @@ impl WholeStreamCommand for Command { async fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( - UntaggedValue::string(crate::commands::help::get_help(&Command, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&Command, &args.scope)).into_value(Tag::unknown()), ))) } } diff --git a/crates/nu-cli/src/commands/date/format.rs b/crates/nu-cli/src/commands/date/format.rs index 180d7d9360..dfb6efeb35 100644 --- a/crates/nu-cli/src/commands/date/format.rs +++ b/crates/nu-cli/src/commands/date/format.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ Dictionary, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, diff --git a/crates/nu-cli/src/commands/date/list_timezone.rs b/crates/nu-cli/src/commands/date/list_timezone.rs index a0cf6158f4..9d6595ef88 100644 --- a/crates/nu-cli/src/commands/date/list_timezone.rs +++ b/crates/nu-cli/src/commands/date/list_timezone.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use chrono_tz::TZ_VARIANTS; use indexmap::IndexMap; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Dictionary, ReturnSuccess, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/date/now.rs b/crates/nu-cli/src/commands/date/now.rs index 7f8dd8ab39..da66b56e1b 100644 --- a/crates/nu-cli/src/commands/date/now.rs +++ b/crates/nu-cli/src/commands/date/now.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use chrono::{DateTime, Local}; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/date/to_table.rs b/crates/nu-cli/src/commands/date/to_table.rs index 0039a3602a..3a117759e7 100644 --- a/crates/nu-cli/src/commands/date/to_table.rs +++ b/crates/nu-cli/src/commands/date/to_table.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use chrono::{Datelike, Timelike}; use indexmap::IndexMap; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Dictionary, Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/date/to_timezone.rs b/crates/nu-cli/src/commands/date/to_timezone.rs index 2273fffd31..9fb194a882 100644 --- a/crates/nu-cli/src/commands/date/to_timezone.rs +++ b/crates/nu-cli/src/commands/date/to_timezone.rs @@ -1,6 +1,6 @@ use crate::commands::date::parser::{datetime_in_timezone, ParseErrorKind}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/date/utc.rs b/crates/nu-cli/src/commands/date/utc.rs index 56064a302a..79d7b94c19 100644 --- a/crates/nu-cli/src/commands/date/utc.rs +++ b/crates/nu-cli/src/commands/date/utc.rs @@ -3,7 +3,7 @@ use chrono::{DateTime, Utc}; use nu_errors::ShellError; use crate::commands::date::utils::date_to_value; -use crate::commands::WholeStreamCommand; +use nu_engine::WholeStreamCommand; use nu_protocol::Signature; pub struct Date; diff --git a/crates/nu-cli/src/commands/debug.rs b/crates/nu-cli/src/commands/debug.rs index 8d3d1a6c36..ef1ac91f83 100644 --- a/crates/nu-cli/src/commands/debug.rs +++ b/crates/nu-cli/src/commands/debug.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/def.rs b/crates/nu-cli/src/commands/def.rs index 924b2b5b94..e503d270c7 100644 --- a/crates/nu-cli/src/commands/def.rs +++ b/crates/nu-cli/src/commands/def.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{hir::CapturedBlock, Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/default.rs b/crates/nu-cli/src/commands/default.rs index 98b8b61c6f..fe275b1e8c 100644 --- a/crates/nu-cli/src/commands/default.rs +++ b/crates/nu-cli/src/commands/default.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/default_context.rs b/crates/nu-cli/src/commands/default_context.rs index 71ca5bf218..9bfb922e69 100644 --- a/crates/nu-cli/src/commands/default_context.rs +++ b/crates/nu-cli/src/commands/default_context.rs @@ -1,8 +1,9 @@ use crate::prelude::*; +use nu_engine::whole_stream_command; use std::error::Error; pub fn create_default_context(interactive: bool) -> Result> { - let context = EvaluationContext::basic()?; + let context = crate::cli::basic_evaluation_context()?; { use crate::commands::*; diff --git a/crates/nu-cli/src/commands/describe.rs b/crates/nu-cli/src/commands/describe.rs index 9821f321e3..0ae2eda3d5 100644 --- a/crates/nu-cli/src/commands/describe.rs +++ b/crates/nu-cli/src/commands/describe.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/do_.rs b/crates/nu-cli/src/commands/do_.rs index 9d94f621b4..4040a9d96f 100644 --- a/crates/nu-cli/src/commands/do_.rs +++ b/crates/nu-cli/src/commands/do_.rs @@ -1,6 +1,6 @@ -use crate::commands::classified::block::run_block; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::run_block; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{hir::CapturedBlock, hir::ExternalRedirection, Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/drop.rs b/crates/nu-cli/src/commands/drop.rs index a809e22eec..526a16f8e6 100644 --- a/crates/nu-cli/src/commands/drop.rs +++ b/crates/nu-cli/src/commands/drop.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/du.rs b/crates/nu-cli/src/commands/du.rs index 763012b1d2..a6261aa2a0 100644 --- a/crates/nu-cli/src/commands/du.rs +++ b/crates/nu-cli/src/commands/du.rs @@ -1,13 +1,11 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; -use filesize::file_real_size_fast; use glob::*; -use indexmap::map::IndexMap; +use nu_engine::WholeStreamCommand; +use nu_engine::{DirBuilder, DirInfo, FileInfo}; use nu_errors::ShellError; -use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; +use nu_protocol::{ReturnSuccess, Signature, SyntaxShape}; use nu_source::Tagged; use std::path::PathBuf; -use std::sync::atomic::Ordering; const NAME: &str = "du"; const GLOB_PARAMS: MatchOptions = MatchOptions { @@ -156,269 +154,11 @@ async fn du(args: CommandArgs) -> Result { .to_output_stream()) } -pub struct DirBuilder { - tag: Tag, - min: Option, - deref: bool, - exclude: Option, - all: bool, -} - -impl DirBuilder { - pub fn new( - tag: Tag, - min: Option, - deref: bool, - exclude: Option, - all: bool, - ) -> DirBuilder { - DirBuilder { - tag, - min, - deref, - exclude, - all, - } - } -} - -pub struct DirInfo { - dirs: Vec, - files: Vec, - errors: Vec, - size: u64, - blocks: u64, - path: PathBuf, - tag: Tag, -} - -struct FileInfo { - path: PathBuf, - size: u64, - blocks: Option, - tag: Tag, -} - -impl FileInfo { - fn new(path: impl Into, deref: bool, tag: Tag) -> Result { - let path = path.into(); - let m = if deref { - std::fs::metadata(&path) - } else { - std::fs::symlink_metadata(&path) - }; - - match m { - Ok(d) => { - let block_size = file_real_size_fast(&path, &d).ok(); - - Ok(FileInfo { - path, - blocks: block_size, - size: d.len(), - tag, - }) - } - Err(e) => Err(e.into()), - } - } -} - -impl DirInfo { - pub fn new( - path: impl Into, - params: &DirBuilder, - depth: Option, - ctrl_c: Arc, - ) -> Self { - let path = path.into(); - - let mut s = Self { - dirs: Vec::new(), - errors: Vec::new(), - files: Vec::new(), - size: 0, - blocks: 0, - tag: params.tag.clone(), - path, - }; - - match std::fs::read_dir(&s.path) { - Ok(d) => { - for f in d { - if ctrl_c.load(Ordering::SeqCst) { - break; - } - - match f { - Ok(i) => match i.file_type() { - Ok(t) if t.is_dir() => { - s = s.add_dir(i.path(), depth, ¶ms, ctrl_c.clone()) - } - Ok(_t) => s = s.add_file(i.path(), ¶ms), - Err(e) => s = s.add_error(e.into()), - }, - Err(e) => s = s.add_error(e.into()), - } - } - } - Err(e) => s = s.add_error(e.into()), - } - s - } - - fn add_dir( - mut self, - path: impl Into, - mut depth: Option, - params: &DirBuilder, - ctrl_c: Arc, - ) -> Self { - if let Some(current) = depth { - if let Some(new) = current.checked_sub(1) { - depth = Some(new); - } else { - return self; - } - } - - let d = DirInfo::new(path, ¶ms, depth, ctrl_c); - self.size += d.size; - self.blocks += d.blocks; - self.dirs.push(d); - self - } - - fn add_file(mut self, f: impl Into, params: &DirBuilder) -> Self { - let f = f.into(); - let include = params - .exclude - .as_ref() - .map_or(true, |x| !x.matches_path(&f)); - if include { - match FileInfo::new(f, params.deref, self.tag.clone()) { - Ok(file) => { - let inc = params.min.map_or(true, |s| file.size >= s); - if inc { - self.size += file.size; - self.blocks += file.blocks.unwrap_or(0); - if params.all { - self.files.push(file); - } - } - } - Err(e) => self = self.add_error(e), - } - } - self - } - - fn add_error(mut self, e: ShellError) -> Self { - self.errors.push(e); - self - } - - pub fn get_size(&self) -> u64 { - self.size - } -} - fn glob_err_into(e: GlobError) -> ShellError { let e = e.into_error(); ShellError::from(e) } -fn value_from_vec(vec: Vec, tag: &Tag) -> Value -where - V: Into, -{ - if vec.is_empty() { - UntaggedValue::nothing() - } else { - let values = vec.into_iter().map(Into::into).collect::>(); - UntaggedValue::Table(values) - } - .into_value(tag) -} - -impl From for Value { - fn from(d: DirInfo) -> Self { - let mut r: IndexMap = IndexMap::new(); - - r.insert( - "path".to_string(), - UntaggedValue::filepath(d.path).into_value(&d.tag), - ); - - r.insert( - "apparent".to_string(), - UntaggedValue::filesize(d.size).into_value(&d.tag), - ); - - r.insert( - "physical".to_string(), - UntaggedValue::filesize(d.blocks).into_value(&d.tag), - ); - - r.insert("directories".to_string(), value_from_vec(d.dirs, &d.tag)); - - r.insert("files".to_string(), value_from_vec(d.files, &d.tag)); - - if !d.errors.is_empty() { - let v = UntaggedValue::Table( - d.errors - .into_iter() - .map(move |e| UntaggedValue::Error(e).into_untagged_value()) - .collect::>(), - ) - .into_value(&d.tag); - - r.insert("errors".to_string(), v); - } - - Value { - value: UntaggedValue::row(r), - tag: d.tag, - } - } -} - -impl From for Value { - fn from(f: FileInfo) -> Self { - let mut r: IndexMap = IndexMap::new(); - - r.insert( - "path".to_string(), - UntaggedValue::filepath(f.path).into_value(&f.tag), - ); - - r.insert( - "apparent".to_string(), - UntaggedValue::filesize(f.size).into_value(&f.tag), - ); - - let b = f - .blocks - .map(UntaggedValue::filesize) - .unwrap_or_else(UntaggedValue::nothing) - .into_value(&f.tag); - - r.insert("physical".to_string(), b); - - r.insert( - "directories".to_string(), - UntaggedValue::nothing().into_value(&f.tag), - ); - - r.insert( - "files".to_string(), - UntaggedValue::nothing().into_value(&f.tag), - ); - - UntaggedValue::row(r).into_value(&f.tag) - } -} - #[cfg(test)] mod tests { use super::Du; diff --git a/crates/nu-cli/src/commands/each/command.rs b/crates/nu-cli/src/commands/each/command.rs index e22e2aabd3..ff5aca94c9 100644 --- a/crates/nu-cli/src/commands/each/command.rs +++ b/crates/nu-cli/src/commands/each/command.rs @@ -1,6 +1,6 @@ -use crate::commands::classified::block::run_block; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::run_block; +use nu_engine::WholeStreamCommand; use futures::stream::once; use nu_errors::ShellError; diff --git a/crates/nu-cli/src/commands/each/group.rs b/crates/nu-cli/src/commands/each/group.rs index 179609a5f4..2953a33de7 100644 --- a/crates/nu-cli/src/commands/each/group.rs +++ b/crates/nu-cli/src/commands/each/group.rs @@ -1,6 +1,6 @@ use crate::commands::each::process_row; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ hir::CapturedBlock, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, diff --git a/crates/nu-cli/src/commands/each/window.rs b/crates/nu-cli/src/commands/each/window.rs index 584b276fd3..2cb91c4b18 100644 --- a/crates/nu-cli/src/commands/each/window.rs +++ b/crates/nu-cli/src/commands/each/window.rs @@ -1,6 +1,6 @@ use crate::commands::each::group::run_block_on_vec; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; //use itertools::Itertools; use nu_errors::ShellError; use nu_protocol::{hir::CapturedBlock, Primitive, Signature, SyntaxShape, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/echo.rs b/crates/nu-cli/src/commands/echo.rs index a1faafcebe..f8b2c26b46 100644 --- a/crates/nu-cli/src/commands/echo.rs +++ b/crates/nu-cli/src/commands/echo.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::hir::Operator; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/empty.rs b/crates/nu-cli/src/commands/empty.rs index 1cfdad57fd..8b0b7bb8ab 100644 --- a/crates/nu-cli/src/commands/empty.rs +++ b/crates/nu-cli/src/commands/empty.rs @@ -1,6 +1,6 @@ -use crate::commands::classified::block::run_block; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::run_block; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ hir::CapturedBlock, ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, diff --git a/crates/nu-cli/src/commands/enter.rs b/crates/nu-cli/src/commands/enter.rs index 06ac7ee070..9baf77a72c 100644 --- a/crates/nu-cli/src/commands/enter.rs +++ b/crates/nu-cli/src/commands/enter.rs @@ -1,6 +1,6 @@ -use crate::commands::UnevaluatedCallInfo; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::UnevaluatedCallInfo; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::hir::ExternalRedirection; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/every.rs b/crates/nu-cli/src/commands/every.rs index ae81982d97..e06bd411f3 100644 --- a/crates/nu-cli/src/commands/every.rs +++ b/crates/nu-cli/src/commands/every.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/exec.rs b/crates/nu-cli/src/commands/exec.rs index 71009df4d6..8e2841ee6d 100644 --- a/crates/nu-cli/src/commands/exec.rs +++ b/crates/nu-cli/src/commands/exec.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/exit.rs b/crates/nu-cli/src/commands/exit.rs index e731173e4d..08bb53f884 100644 --- a/crates/nu-cli/src/commands/exit.rs +++ b/crates/nu-cli/src/commands/exit.rs @@ -1,4 +1,3 @@ -use crate::commands::command::WholeStreamCommand; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{CommandAction, ReturnSuccess, Signature}; diff --git a/crates/nu-cli/src/commands/first.rs b/crates/nu-cli/src/commands/first.rs index e6bef4bc20..ae056c0dd6 100644 --- a/crates/nu-cli/src/commands/first.rs +++ b/crates/nu-cli/src/commands/first.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/flatten.rs b/crates/nu-cli/src/commands/flatten.rs index ba8bf8d360..301772986b 100644 --- a/crates/nu-cli/src/commands/flatten.rs +++ b/crates/nu-cli/src/commands/flatten.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ Dictionary, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value, diff --git a/crates/nu-cli/src/commands/format/command.rs b/crates/nu-cli/src/commands/format/command.rs index 3af25efd16..3fb1b75bce 100644 --- a/crates/nu-cli/src/commands/format/command.rs +++ b/crates/nu-cli/src/commands/format/command.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; -use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; +use nu_engine::evaluate_baseline_expr; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/format/format_filesize.rs b/crates/nu-cli/src/commands/format/format_filesize.rs index 9dd9d14f9a..a97186bdad 100644 --- a/crates/nu-cli/src/commands/format/format_filesize.rs +++ b/crates/nu-cli/src/commands/format/format_filesize.rs @@ -1,7 +1,7 @@ use crate::prelude::*; use nu_errors::ShellError; -use crate::commands::WholeStreamCommand; +use nu_engine::WholeStreamCommand; use nu_protocol::{ ColumnPath, Primitive::Filesize, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, UntaggedValue::Primitive, Value, diff --git a/crates/nu-cli/src/commands/from.rs b/crates/nu-cli/src/commands/from.rs index 644820df70..7a5b468c17 100644 --- a/crates/nu-cli/src/commands/from.rs +++ b/crates/nu-cli/src/commands/from.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; @@ -21,8 +21,7 @@ impl WholeStreamCommand for From { async fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( - UntaggedValue::string(crate::commands::help::get_help(&From, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&From, &args.scope)).into_value(Tag::unknown()), ))) } } diff --git a/crates/nu-cli/src/commands/from_csv.rs b/crates/nu-cli/src/commands/from_csv.rs index 6c9ca76814..8c16bf8e0c 100644 --- a/crates/nu-cli/src/commands/from_csv.rs +++ b/crates/nu-cli/src/commands/from_csv.rs @@ -1,6 +1,6 @@ use crate::commands::from_delimited_data::from_delimited_data; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/from_eml.rs b/crates/nu-cli/src/commands/from_eml.rs index 1fdfe48f8a..a818b2448d 100644 --- a/crates/nu-cli/src/commands/from_eml.rs +++ b/crates/nu-cli/src/commands/from_eml.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use ::eml_parser::eml::*; use ::eml_parser::EmlParser; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/from_ics.rs b/crates/nu-cli/src/commands/from_ics.rs index cf094451bd..6ba93ea692 100644 --- a/crates/nu-cli/src/commands/from_ics.rs +++ b/crates/nu-cli/src/commands/from_ics.rs @@ -1,8 +1,8 @@ extern crate ical; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use ical::parser::ical::component::*; use ical::property::Property; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value}; use std::io::BufReader; diff --git a/crates/nu-cli/src/commands/from_ini.rs b/crates/nu-cli/src/commands/from_ini.rs index af5d33ec07..8b1ad288d0 100644 --- a/crates/nu-cli/src/commands/from_ini.rs +++ b/crates/nu-cli/src/commands/from_ini.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, Signature, TaggedDictBuilder, UntaggedValue, Value}; use std::collections::HashMap; diff --git a/crates/nu-cli/src/commands/from_json.rs b/crates/nu-cli/src/commands/from_json.rs index d221699e8d..752df27786 100644 --- a/crates/nu-cli/src/commands/from_json.rs +++ b/crates/nu-cli/src/commands/from_json.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/from_ods.rs b/crates/nu-cli/src/commands/from_ods.rs index 5a207b9772..678ae52302 100644 --- a/crates/nu-cli/src/commands/from_ods.rs +++ b/crates/nu-cli/src/commands/from_ods.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use calamine::*; use nu_data::TaggedListBuilder; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue}; use std::io::Cursor; diff --git a/crates/nu-cli/src/commands/from_ssv.rs b/crates/nu-cli/src/commands/from_ssv.rs index a62a938e29..5bbaaac71b 100644 --- a/crates/nu-cli/src/commands/from_ssv.rs +++ b/crates/nu-cli/src/commands/from_ssv.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ Primitive, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value, diff --git a/crates/nu-cli/src/commands/from_toml.rs b/crates/nu-cli/src/commands/from_toml.rs index 8fc7c6f0bd..f1ad40517b 100644 --- a/crates/nu-cli/src/commands/from_toml.rs +++ b/crates/nu-cli/src/commands/from_toml.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/from_tsv.rs b/crates/nu-cli/src/commands/from_tsv.rs index a85bd134d7..3af3c4de5f 100644 --- a/crates/nu-cli/src/commands/from_tsv.rs +++ b/crates/nu-cli/src/commands/from_tsv.rs @@ -1,6 +1,6 @@ use crate::commands::from_delimited_data::from_delimited_data; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::Signature; diff --git a/crates/nu-cli/src/commands/from_url.rs b/crates/nu-cli/src/commands/from_url.rs index 3a14cb24c2..4d93884ded 100644 --- a/crates/nu-cli/src/commands/from_url.rs +++ b/crates/nu-cli/src/commands/from_url.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/from_vcf.rs b/crates/nu-cli/src/commands/from_vcf.rs index 180627da60..06f20d11c3 100644 --- a/crates/nu-cli/src/commands/from_vcf.rs +++ b/crates/nu-cli/src/commands/from_vcf.rs @@ -1,8 +1,8 @@ extern crate ical; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use ical::parser::vcard::component::*; use ical::property::Property; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/from_xlsx.rs b/crates/nu-cli/src/commands/from_xlsx.rs index 32d1a5b24f..9a8766940b 100644 --- a/crates/nu-cli/src/commands/from_xlsx.rs +++ b/crates/nu-cli/src/commands/from_xlsx.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use calamine::*; use nu_data::TaggedListBuilder; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue}; use std::io::Cursor; diff --git a/crates/nu-cli/src/commands/from_xml.rs b/crates/nu-cli/src/commands/from_xml.rs index 4882ab93d0..e458df84c8 100644 --- a/crates/nu-cli/src/commands/from_xml.rs +++ b/crates/nu-cli/src/commands/from_xml.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/from_yaml.rs b/crates/nu-cli/src/commands/from_yaml.rs index b626fc21dd..90df48b180 100644 --- a/crates/nu-cli/src/commands/from_yaml.rs +++ b/crates/nu-cli/src/commands/from_yaml.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, Signature, TaggedDictBuilder, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/get.rs b/crates/nu-cli/src/commands/get.rs index 63af75170b..7e08dd655a 100644 --- a/crates/nu-cli/src/commands/get.rs +++ b/crates/nu-cli/src/commands/get.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use indexmap::set::IndexSet; use log::trace; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ did_you_mean, ColumnPath, Dictionary, PathMember, Primitive, ReturnSuccess, Signature, diff --git a/crates/nu-cli/src/commands/group_by.rs b/crates/nu-cli/src/commands/group_by.rs index 503bc7dd66..91fed292d0 100644 --- a/crates/nu-cli/src/commands/group_by.rs +++ b/crates/nu-cli/src/commands/group_by.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use crate::utils::suggestions::suggestions; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/group_by_date.rs b/crates/nu-cli/src/commands/group_by_date.rs index 2756ce772b..c09d7ce423 100644 --- a/crates/nu-cli/src/commands/group_by_date.rs +++ b/crates/nu-cli/src/commands/group_by_date.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use crate::utils::suggestions::suggestions; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/hash_/base64_.rs b/crates/nu-cli/src/commands/hash_/base64_.rs index 695a39a1c5..d7c0c5793c 100644 --- a/crates/nu-cli/src/commands/hash_/base64_.rs +++ b/crates/nu-cli/src/commands/hash_/base64_.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/hash_/command.rs b/crates/nu-cli/src/commands/hash_/command.rs index 3ad8dd5efa..05bf8b6818 100644 --- a/crates/nu-cli/src/commands/hash_/command.rs +++ b/crates/nu-cli/src/commands/hash_/command.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; @@ -24,8 +24,7 @@ impl WholeStreamCommand for Command { async fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( - UntaggedValue::string(crate::commands::help::get_help(&Command, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&Command, &args.scope)).into_value(Tag::unknown()), ))) } } diff --git a/crates/nu-cli/src/commands/headers.rs b/crates/nu-cli/src/commands/headers.rs index a584333d3a..95ce2dac1c 100644 --- a/crates/nu-cli/src/commands/headers.rs +++ b/crates/nu-cli/src/commands/headers.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use futures::stream::StreamExt; use indexmap::IndexMap; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::Dictionary; use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/help.rs b/crates/nu-cli/src/commands/help.rs index 60e507050a..7cf1e18176 100644 --- a/crates/nu-cli/src/commands/help.rs +++ b/crates/nu-cli/src/commands/help.rs @@ -1,8 +1,7 @@ -use crate::commands::command::Command; -use crate::commands::WholeStreamCommand; -use crate::documentation::{generate_docs, get_documentation, DocumentationConfig}; use crate::prelude::*; -use nu_data::command::signature_dict; +use nu_engine::command_dict; +use nu_engine::documentation::generate_docs; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value}; use nu_source::{SpannedItem, Tagged}; @@ -34,21 +33,6 @@ impl WholeStreamCommand for Help { } } -pub(crate) fn command_dict(command: Command, tag: impl Into) -> Value { - let tag = tag.into(); - - let mut cmd_dict = TaggedDictBuilder::new(&tag); - - cmd_dict.insert_untagged("name", UntaggedValue::string(command.name())); - - cmd_dict.insert_untagged("type", UntaggedValue::string("Command")); - - cmd_dict.insert_value("signature", signature_dict(command.signature(), tag)); - cmd_dict.insert_untagged("usage", UntaggedValue::string(command.usage())); - - cmd_dict.into_value() -} - async fn help(args: CommandArgs) -> Result { let name = args.call_info.name_tag.clone(); let scope = args.scope.clone(); @@ -224,10 +208,6 @@ You can also learn more at https://www.nushell.sh/book/"#; } } -pub fn get_help(cmd: &dyn WholeStreamCommand, scope: &Scope) -> String { - get_documentation(cmd, scope, &DocumentationConfig::default()) -} - #[cfg(test)] mod tests { use super::Help; diff --git a/crates/nu-cli/src/commands/histogram.rs b/crates/nu-cli/src/commands/histogram.rs index 1121d3cd59..fa92a1ec36 100644 --- a/crates/nu-cli/src/commands/histogram.rs +++ b/crates/nu-cli/src/commands/histogram.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ ColumnPath, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value, diff --git a/crates/nu-cli/src/commands/history.rs b/crates/nu-cli/src/commands/history.rs index dc6ac0263c..27d7cf1022 100644 --- a/crates/nu-cli/src/commands/history.rs +++ b/crates/nu-cli/src/commands/history.rs @@ -1,31 +1,11 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_data::config::{Conf, NuConfig}; +use nu_engine::history_path; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; use std::fs::File; use std::io::{BufRead, BufReader}; -use std::path::PathBuf; - -const DEFAULT_LOCATION: &str = "history.txt"; - -pub fn history_path(config: &dyn Conf) -> PathBuf { - let default_path = nu_data::config::user_data() - .map(|mut p| { - p.push(DEFAULT_LOCATION); - p - }) - .unwrap_or_else(|_| PathBuf::from(DEFAULT_LOCATION)); - - config - .var("history-path") - .map_or(default_path.clone(), |custom_path| { - match custom_path.as_string() { - Ok(path) => PathBuf::from(path), - Err(_) => default_path, - } - }) -} #[derive(Deserialize)] struct Arguments { diff --git a/crates/nu-cli/src/commands/if_.rs b/crates/nu-cli/src/commands/if_.rs index 48894ae529..c9ddbf728b 100644 --- a/crates/nu-cli/src/commands/if_.rs +++ b/crates/nu-cli/src/commands/if_.rs @@ -1,7 +1,7 @@ -use crate::commands::classified::block::run_block; -use crate::commands::WholeStreamCommand; -use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; +use nu_engine::evaluate_baseline_expr; +use nu_engine::run_block; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, diff --git a/crates/nu-cli/src/commands/insert.rs b/crates/nu-cli/src/commands/insert.rs index 8d2518b9cb..5530d41640 100644 --- a/crates/nu-cli/src/commands/insert.rs +++ b/crates/nu-cli/src/commands/insert.rs @@ -1,6 +1,6 @@ -use crate::commands::classified::block::run_block; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::run_block; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, diff --git a/crates/nu-cli/src/commands/into_int.rs b/crates/nu-cli/src/commands/into_int.rs index 581b7894bb..782554e2ba 100644 --- a/crates/nu-cli/src/commands/into_int.rs +++ b/crates/nu-cli/src/commands/into_int.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/keep/command.rs b/crates/nu-cli/src/commands/keep/command.rs index 8104ef5243..05787bcf01 100644 --- a/crates/nu-cli/src/commands/keep/command.rs +++ b/crates/nu-cli/src/commands/keep/command.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/keep/until.rs b/crates/nu-cli/src/commands/keep/until.rs index e5c5ff6162..e7d09b24ff 100644 --- a/crates/nu-cli/src/commands/keep/until.rs +++ b/crates/nu-cli/src/commands/keep/until.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; -use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; use log::trace; +use nu_engine::evaluate_baseline_expr; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_parser::ParserScope; use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/keep/while_.rs b/crates/nu-cli/src/commands/keep/while_.rs index 0fb0bc2322..d1cb42b409 100644 --- a/crates/nu-cli/src/commands/keep/while_.rs +++ b/crates/nu-cli/src/commands/keep/while_.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; -use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; use log::trace; +use nu_engine::evaluate_baseline_expr; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/kill.rs b/crates/nu-cli/src/commands/kill.rs index 18f424f8e3..fff2daf7bf 100644 --- a/crates/nu-cli/src/commands/kill.rs +++ b/crates/nu-cli/src/commands/kill.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/last.rs b/crates/nu-cli/src/commands/last.rs index a916370940..5431020bac 100644 --- a/crates/nu-cli/src/commands/last.rs +++ b/crates/nu-cli/src/commands/last.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/let_.rs b/crates/nu-cli/src/commands/let_.rs index b7a04f1da3..4a2375fb37 100644 --- a/crates/nu-cli/src/commands/let_.rs +++ b/crates/nu-cli/src/commands/let_.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use crate::{commands::WholeStreamCommand, evaluate::evaluate_baseline_expr}; +use nu_engine::{evaluate_baseline_expr, WholeStreamCommand}; use nu_errors::ShellError; use nu_protocol::{hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape}; diff --git a/crates/nu-cli/src/commands/let_env.rs b/crates/nu-cli/src/commands/let_env.rs index a7f118f939..109f46e8a0 100644 --- a/crates/nu-cli/src/commands/let_env.rs +++ b/crates/nu-cli/src/commands/let_env.rs @@ -1,5 +1,5 @@ use crate::prelude::*; -use crate::{commands::WholeStreamCommand, evaluate::evaluate_baseline_expr}; +use nu_engine::{evaluate_baseline_expr, WholeStreamCommand}; use nu_errors::ShellError; use nu_protocol::{hir::CapturedBlock, hir::ClassifiedCommand, Signature, SyntaxShape}; diff --git a/crates/nu-cli/src/commands/lines.rs b/crates/nu-cli/src/commands/lines.rs index 0b4e2060bf..942c00c573 100644 --- a/crates/nu-cli/src/commands/lines.rs +++ b/crates/nu-cli/src/commands/lines.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; use parking_lot::Mutex; diff --git a/crates/nu-cli/src/commands/ls.rs b/crates/nu-cli/src/commands/ls.rs index b355b1fca8..94dc5d2b99 100644 --- a/crates/nu-cli/src/commands/ls.rs +++ b/crates/nu-cli/src/commands/ls.rs @@ -1,23 +1,10 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; -use nu_source::Tagged; -use std::path::PathBuf; pub struct Ls; -#[derive(Deserialize)] -pub struct LsArgs { - pub path: Option>, - pub all: bool, - pub long: bool, - #[serde(rename = "short-names")] - pub short_names: bool, - #[serde(rename = "du")] - pub du: bool, -} - #[async_trait] impl WholeStreamCommand for Ls { fn name(&self) -> &str { diff --git a/crates/nu-cli/src/commands/math/abs.rs b/crates/nu-cli/src/commands/math/abs.rs index c54c8d27cb..44dada2bf7 100644 --- a/crates/nu-cli/src/commands/math/abs.rs +++ b/crates/nu-cli/src/commands/math/abs.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/math/avg.rs b/crates/nu-cli/src/commands/math/avg.rs index c0b80fa7c1..fd18632c95 100644 --- a/crates/nu-cli/src/commands/math/avg.rs +++ b/crates/nu-cli/src/commands/math/avg.rs @@ -2,7 +2,7 @@ use crate::prelude::*; use crate::commands::math::reducers::{reducer_for, Reduce}; use crate::commands::math::utils::run_with_function; -use crate::commands::WholeStreamCommand; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/math/ceil.rs b/crates/nu-cli/src/commands/math/ceil.rs index 61f544c7c5..2adfc07241 100644 --- a/crates/nu-cli/src/commands/math/ceil.rs +++ b/crates/nu-cli/src/commands/math/ceil.rs @@ -1,7 +1,7 @@ use crate::commands::math::utils::run_with_numerical_functions_on_stream; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use bigdecimal::One; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/math/command.rs b/crates/nu-cli/src/commands/math/command.rs index 8055865991..1330bd5438 100644 --- a/crates/nu-cli/src/commands/math/command.rs +++ b/crates/nu-cli/src/commands/math/command.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; @@ -21,8 +21,7 @@ impl WholeStreamCommand for Command { async fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(Ok(ReturnSuccess::Value( - UntaggedValue::string(crate::commands::help::get_help(&Command, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&Command, &args.scope)).into_value(Tag::unknown()), )))) } } diff --git a/crates/nu-cli/src/commands/math/eval.rs b/crates/nu-cli/src/commands/math/eval.rs index 78ccb5e373..33aef8428f 100644 --- a/crates/nu-cli/src/commands/math/eval.rs +++ b/crates/nu-cli/src/commands/math/eval.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/math/floor.rs b/crates/nu-cli/src/commands/math/floor.rs index c3d4f362dc..787206b64d 100644 --- a/crates/nu-cli/src/commands/math/floor.rs +++ b/crates/nu-cli/src/commands/math/floor.rs @@ -1,7 +1,7 @@ use crate::commands::math::utils::run_with_numerical_functions_on_stream; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use bigdecimal::One; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/math/max.rs b/crates/nu-cli/src/commands/math/max.rs index 18cd2496c6..62407f2ce8 100644 --- a/crates/nu-cli/src/commands/math/max.rs +++ b/crates/nu-cli/src/commands/math/max.rs @@ -1,7 +1,7 @@ use crate::commands::math::reducers::{reducer_for, Reduce}; use crate::commands::math::utils::run_with_function; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/math/median.rs b/crates/nu-cli/src/commands/math/median.rs index c0da9a3180..c5721e6ed2 100644 --- a/crates/nu-cli/src/commands/math/median.rs +++ b/crates/nu-cli/src/commands/math/median.rs @@ -1,8 +1,8 @@ use crate::commands::math::reducers::{reducer_for, Reduce}; use crate::commands::math::utils::run_with_function; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use bigdecimal::FromPrimitive; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ hir::{convert_number_to_u64, Number, Operator}, diff --git a/crates/nu-cli/src/commands/math/min.rs b/crates/nu-cli/src/commands/math/min.rs index bb8b3bf0a7..03b55d27ee 100644 --- a/crates/nu-cli/src/commands/math/min.rs +++ b/crates/nu-cli/src/commands/math/min.rs @@ -1,7 +1,7 @@ use crate::commands::math::reducers::{reducer_for, Reduce}; use crate::commands::math::utils::run_with_function; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/math/mode.rs b/crates/nu-cli/src/commands/math/mode.rs index 504dc234a4..d79a8610c1 100644 --- a/crates/nu-cli/src/commands/math/mode.rs +++ b/crates/nu-cli/src/commands/math/mode.rs @@ -1,6 +1,6 @@ use crate::commands::math::utils::run_with_function; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, UntaggedValue, Value}; use std::cmp::Ordering; diff --git a/crates/nu-cli/src/commands/math/product.rs b/crates/nu-cli/src/commands/math/product.rs index f07fa0bed4..3372c680ef 100644 --- a/crates/nu-cli/src/commands/math/product.rs +++ b/crates/nu-cli/src/commands/math/product.rs @@ -1,7 +1,7 @@ use crate::commands::math::reducers::{reducer_for, Reduce}; use crate::commands::math::utils::run_with_function; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ hir::{convert_number_to_u64, Number}, diff --git a/crates/nu-cli/src/commands/math/round.rs b/crates/nu-cli/src/commands/math/round.rs index b80ac34ebd..fafe4d4b4a 100644 --- a/crates/nu-cli/src/commands/math/round.rs +++ b/crates/nu-cli/src/commands/math/round.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/math/stddev.rs b/crates/nu-cli/src/commands/math/stddev.rs index 001daca686..c6f4e40afa 100644 --- a/crates/nu-cli/src/commands/math/stddev.rs +++ b/crates/nu-cli/src/commands/math/stddev.rs @@ -1,6 +1,6 @@ use super::variance::compute_variance as variance; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Dictionary, Primitive, ReturnSuccess, Signature, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/math/sum.rs b/crates/nu-cli/src/commands/math/sum.rs index 4f44cf5cf0..ab1a74f203 100644 --- a/crates/nu-cli/src/commands/math/sum.rs +++ b/crates/nu-cli/src/commands/math/sum.rs @@ -1,7 +1,7 @@ use crate::commands::math::reducers::{reducer_for, Reduce}; use crate::commands::math::utils::run_with_function; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/math/variance.rs b/crates/nu-cli/src/commands/math/variance.rs index b08f1609c0..bd79aff630 100644 --- a/crates/nu-cli/src/commands/math/variance.rs +++ b/crates/nu-cli/src/commands/math/variance.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use bigdecimal::FromPrimitive; use nu_data::value::compute_values; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ hir::Operator, Dictionary, Primitive, ReturnSuccess, Signature, UntaggedValue, Value, diff --git a/crates/nu-cli/src/commands/merge.rs b/crates/nu-cli/src/commands/merge.rs index 1671026422..69b8ec1a9c 100644 --- a/crates/nu-cli/src/commands/merge.rs +++ b/crates/nu-cli/src/commands/merge.rs @@ -1,7 +1,7 @@ -use crate::commands::classified::block::run_block; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_data::value::merge_values; +use nu_engine::run_block; +use nu_engine::WholeStreamCommand; use indexmap::IndexMap; use nu_errors::ShellError; diff --git a/crates/nu-cli/src/commands/mkdir.rs b/crates/nu-cli/src/commands/mkdir.rs index 4a34d1073f..4850bdba9a 100644 --- a/crates/nu-cli/src/commands/mkdir.rs +++ b/crates/nu-cli/src/commands/mkdir.rs @@ -1,19 +1,9 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; -use nu_source::Tagged; -use std::path::PathBuf; - pub struct Mkdir; -#[derive(Deserialize)] -pub struct MkdirArgs { - pub rest: Vec>, - #[serde(rename = "show-created-paths")] - pub show_created_paths: bool, -} - #[async_trait] impl WholeStreamCommand for Mkdir { fn name(&self) -> &str { diff --git a/crates/nu-cli/src/commands/move_/command.rs b/crates/nu-cli/src/commands/move_/command.rs index 77c50332b6..865679c600 100644 --- a/crates/nu-cli/src/commands/move_/command.rs +++ b/crates/nu-cli/src/commands/move_/command.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_data::base::select_fields; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ColumnPath, ReturnSuccess, Signature, SyntaxShape, Value}; use nu_source::HasFallibleSpan; diff --git a/crates/nu-cli/src/commands/move_/mv.rs b/crates/nu-cli/src/commands/move_/mv.rs index 44562b5ca0..0d38e3d3d4 100644 --- a/crates/nu-cli/src/commands/move_/mv.rs +++ b/crates/nu-cli/src/commands/move_/mv.rs @@ -1,18 +1,10 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; -use nu_source::Tagged; -use std::path::PathBuf; pub struct Mv; -#[derive(Deserialize)] -pub struct Arguments { - pub src: Tagged, - pub dst: Tagged, -} - #[async_trait] impl WholeStreamCommand for Mv { fn name(&self) -> &str { diff --git a/crates/nu-cli/src/commands/next.rs b/crates/nu-cli/src/commands/next.rs index 014cf45d22..5c451c4445 100644 --- a/crates/nu-cli/src/commands/next.rs +++ b/crates/nu-cli/src/commands/next.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{CommandAction, ReturnSuccess, Signature}; diff --git a/crates/nu-cli/src/commands/nth.rs b/crates/nu-cli/src/commands/nth.rs index 0a0e13ff69..67bef9c9c9 100644 --- a/crates/nu-cli/src/commands/nth.rs +++ b/crates/nu-cli/src/commands/nth.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/nu/plugin.rs b/crates/nu-cli/src/commands/nu/plugin.rs index acf7fb0a4c..7da2478e2d 100644 --- a/crates/nu-cli/src/commands/nu/plugin.rs +++ b/crates/nu-cli/src/commands/nu/plugin.rs @@ -1,8 +1,8 @@ use std::path::PathBuf; -use crate::commands::WholeStreamCommand; -use crate::path::canonicalize; use crate::prelude::*; +use nu_engine::filesystem::path::canonicalize; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{CommandAction, ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; @@ -101,8 +101,7 @@ impl WholeStreamCommand for SubCommand { } Ok(OutputStream::one(ReturnSuccess::value( - UntaggedValue::string(crate::commands::help::get_help(&SubCommand, &scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&SubCommand, &scope)).into_value(Tag::unknown()), ))) } } diff --git a/crates/nu-cli/src/commands/open.rs b/crates/nu-cli/src/commands/open.rs index b77db75fb4..4dc414d6d0 100644 --- a/crates/nu-cli/src/commands/open.rs +++ b/crates/nu-cli/src/commands/open.rs @@ -1,10 +1,10 @@ -use crate::commands::classified::maybe_text_codec::StringOrBinary; use crate::commands::constants::BAT_LANGUAGES; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use encoding_rs::{Encoding, UTF_8}; use futures_util::StreamExt; use log::debug; +use nu_engine::StringOrBinary; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{CommandAction, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::{AnchorLocation, Span, Tagged}; diff --git a/crates/nu-cli/src/commands/parse/command.rs b/crates/nu-cli/src/commands/parse/command.rs index 981a5a2577..dae4c3caaf 100644 --- a/crates/nu-cli/src/commands/parse/command.rs +++ b/crates/nu-cli/src/commands/parse/command.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/path/basename.rs b/crates/nu-cli/src/commands/path/basename.rs index fdadb208a4..b8e7aa8ddf 100644 --- a/crates/nu-cli/src/commands/path/basename.rs +++ b/crates/nu-cli/src/commands/path/basename.rs @@ -1,6 +1,6 @@ use super::{operate, DefaultArguments}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ColumnPath, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/path/command.rs b/crates/nu-cli/src/commands/path/command.rs index ff7c8d5be1..f12fca07a6 100644 --- a/crates/nu-cli/src/commands/path/command.rs +++ b/crates/nu-cli/src/commands/path/command.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; @@ -21,8 +21,7 @@ impl WholeStreamCommand for Path { async fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( - UntaggedValue::string(crate::commands::help::get_help(&Path, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&Path, &args.scope)).into_value(Tag::unknown()), ))) } } diff --git a/crates/nu-cli/src/commands/path/dirname.rs b/crates/nu-cli/src/commands/path/dirname.rs index ac0eafbb63..9c1d7d4272 100644 --- a/crates/nu-cli/src/commands/path/dirname.rs +++ b/crates/nu-cli/src/commands/path/dirname.rs @@ -1,6 +1,6 @@ use super::{operate, DefaultArguments}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ColumnPath, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/path/exists.rs b/crates/nu-cli/src/commands/path/exists.rs index ca1d5c0c91..2eac3df003 100644 --- a/crates/nu-cli/src/commands/path/exists.rs +++ b/crates/nu-cli/src/commands/path/exists.rs @@ -1,6 +1,6 @@ use super::{operate, DefaultArguments}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ColumnPath, Signature, SyntaxShape, UntaggedValue, Value}; use std::path::Path; diff --git a/crates/nu-cli/src/commands/path/expand.rs b/crates/nu-cli/src/commands/path/expand.rs index 5ee42d1828..849d1ed262 100644 --- a/crates/nu-cli/src/commands/path/expand.rs +++ b/crates/nu-cli/src/commands/path/expand.rs @@ -1,6 +1,6 @@ use super::{operate, DefaultArguments}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ColumnPath, Signature, SyntaxShape, UntaggedValue}; use std::path::{Path, PathBuf}; diff --git a/crates/nu-cli/src/commands/path/extension.rs b/crates/nu-cli/src/commands/path/extension.rs index 04247f8a75..490bec6662 100644 --- a/crates/nu-cli/src/commands/path/extension.rs +++ b/crates/nu-cli/src/commands/path/extension.rs @@ -1,6 +1,6 @@ use super::{operate, DefaultArguments}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ColumnPath, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/path/filestem.rs b/crates/nu-cli/src/commands/path/filestem.rs index 006d7450b2..6e70375d0a 100644 --- a/crates/nu-cli/src/commands/path/filestem.rs +++ b/crates/nu-cli/src/commands/path/filestem.rs @@ -1,6 +1,6 @@ use super::{operate, DefaultArguments}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ColumnPath, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/path/type.rs b/crates/nu-cli/src/commands/path/type.rs index 3ba41e6579..bbb74b51a4 100644 --- a/crates/nu-cli/src/commands/path/type.rs +++ b/crates/nu-cli/src/commands/path/type.rs @@ -1,7 +1,7 @@ use super::{operate, DefaultArguments}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; -use crate::shell::filesystem_shell::get_file_type; +use nu_engine::filesystem::filesystem_shell::get_file_type; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ColumnPath, Signature, SyntaxShape, UntaggedValue, Value}; use std::path::Path; diff --git a/crates/nu-cli/src/commands/pivot.rs b/crates/nu-cli/src/commands/pivot.rs index bb1a6965c0..76473fecef 100644 --- a/crates/nu-cli/src/commands/pivot.rs +++ b/crates/nu-cli/src/commands/pivot.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ merge_descriptors, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, diff --git a/crates/nu-cli/src/commands/prepend.rs b/crates/nu-cli/src/commands/prepend.rs index 458de563ee..47b09466f5 100644 --- a/crates/nu-cli/src/commands/prepend.rs +++ b/crates/nu-cli/src/commands/prepend.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/prev.rs b/crates/nu-cli/src/commands/prev.rs index 24e60d9e10..8e8201bccc 100644 --- a/crates/nu-cli/src/commands/prev.rs +++ b/crates/nu-cli/src/commands/prev.rs @@ -2,7 +2,7 @@ use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{CommandAction, ReturnSuccess, Signature}; -use crate::commands::WholeStreamCommand; +use nu_engine::WholeStreamCommand; pub struct Previous; diff --git a/crates/nu-cli/src/commands/pwd.rs b/crates/nu-cli/src/commands/pwd.rs index 3d59d456f5..da3fa4c002 100644 --- a/crates/nu-cli/src/commands/pwd.rs +++ b/crates/nu-cli/src/commands/pwd.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::Signature; diff --git a/crates/nu-cli/src/commands/random/bool.rs b/crates/nu-cli/src/commands/random/bool.rs index 4c3f9717cb..ab6fe7f1c5 100644 --- a/crates/nu-cli/src/commands/random/bool.rs +++ b/crates/nu-cli/src/commands/random/bool.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/random/chars.rs b/crates/nu-cli/src/commands/random/chars.rs index 904535bb18..65885e3763 100644 --- a/crates/nu-cli/src/commands/random/chars.rs +++ b/crates/nu-cli/src/commands/random/chars.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/random/command.rs b/crates/nu-cli/src/commands/random/command.rs index 510fb536f9..f11b1fb707 100644 --- a/crates/nu-cli/src/commands/random/command.rs +++ b/crates/nu-cli/src/commands/random/command.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; @@ -21,8 +21,7 @@ impl WholeStreamCommand for Command { async fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(Ok(ReturnSuccess::Value( - UntaggedValue::string(crate::commands::help::get_help(&Command, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&Command, &args.scope)).into_value(Tag::unknown()), )))) } } diff --git a/crates/nu-cli/src/commands/random/decimal.rs b/crates/nu-cli/src/commands/random/decimal.rs index d0f2f0e412..65cee4b0a1 100644 --- a/crates/nu-cli/src/commands/random/decimal.rs +++ b/crates/nu-cli/src/commands/random/decimal.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; -use crate::deserializer::NumericRange; use crate::prelude::*; +use nu_engine::deserializer::NumericRange; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/random/dice.rs b/crates/nu-cli/src/commands/random/dice.rs index 80542c0cf8..1bc56f8e16 100644 --- a/crates/nu-cli/src/commands/random/dice.rs +++ b/crates/nu-cli/src/commands/random/dice.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/random/integer.rs b/crates/nu-cli/src/commands/random/integer.rs index 979bda9f87..80ffb4d89d 100644 --- a/crates/nu-cli/src/commands/random/integer.rs +++ b/crates/nu-cli/src/commands/random/integer.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; -use crate::deserializer::NumericRange; use crate::prelude::*; +use nu_engine::deserializer::NumericRange; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/random/uuid.rs b/crates/nu-cli/src/commands/random/uuid.rs index 943fb97c73..0bd060935f 100644 --- a/crates/nu-cli/src/commands/random/uuid.rs +++ b/crates/nu-cli/src/commands/random/uuid.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature}; use uuid_crate::Uuid; diff --git a/crates/nu-cli/src/commands/range.rs b/crates/nu-cli/src/commands/range.rs index 28d4b47dd8..3f60ada122 100644 --- a/crates/nu-cli/src/commands/range.rs +++ b/crates/nu-cli/src/commands/range.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; -use crate::deserializer::NumericRange; use crate::prelude::*; +use nu_engine::deserializer::NumericRange; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{RangeInclusion, ReturnSuccess, Signature, SyntaxShape}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/reduce.rs b/crates/nu-cli/src/commands/reduce.rs index f92f7a144b..39929feb07 100644 --- a/crates/nu-cli/src/commands/reduce.rs +++ b/crates/nu-cli/src/commands/reduce.rs @@ -1,13 +1,14 @@ -use crate::commands::classified::block::run_block; use crate::commands::each; -use crate::commands::WholeStreamCommand; use crate::prelude::*; -use crate::{CommandArgs, Example, OutputStream}; use futures::stream::once; +use nu_engine::run_block; +use nu_engine::WholeStreamCommand; +use nu_engine::{CommandArgs, Example}; use nu_errors::ShellError; use nu_parser::ParserScope; use nu_protocol::{hir::CapturedBlock, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; +use nu_stream::OutputStream; pub struct Reduce; diff --git a/crates/nu-cli/src/commands/reject.rs b/crates/nu-cli/src/commands/reject.rs index 2248d14bfc..b58c22f2aa 100644 --- a/crates/nu-cli/src/commands/reject.rs +++ b/crates/nu-cli/src/commands/reject.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_data::base::reject_fields; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/rename.rs b/crates/nu-cli/src/commands/rename.rs index 5051e935b6..95d83c2bc3 100644 --- a/crates/nu-cli/src/commands/rename.rs +++ b/crates/nu-cli/src/commands/rename.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use indexmap::indexmap; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/reverse.rs b/crates/nu-cli/src/commands/reverse.rs index a65e1b471a..8061af7665 100644 --- a/crates/nu-cli/src/commands/reverse.rs +++ b/crates/nu-cli/src/commands/reverse.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/rm.rs b/crates/nu-cli/src/commands/rm.rs index 5e31a487bd..77bef9fe55 100644 --- a/crates/nu-cli/src/commands/rm.rs +++ b/crates/nu-cli/src/commands/rm.rs @@ -1,23 +1,11 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::shell::RemoveArgs; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; -use nu_source::Tagged; -use std::path::PathBuf; pub struct Remove; -#[derive(Deserialize)] -pub struct RemoveArgs { - pub rest: Vec>, - pub recursive: Tagged, - #[allow(unused)] - pub trash: Tagged, - #[allow(unused)] - pub permanent: Tagged, - pub force: Tagged, -} - #[async_trait] impl WholeStreamCommand for Remove { fn name(&self) -> &str { diff --git a/crates/nu-cli/src/commands/run_external.rs b/crates/nu-cli/src/commands/run_external.rs index 8875542b77..1aa2db7653 100644 --- a/crates/nu-cli/src/commands/run_external.rs +++ b/crates/nu-cli/src/commands/run_external.rs @@ -1,12 +1,12 @@ -use crate::commands::cd::CdArgs; use crate::commands::classified::external; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use derive_new::new; use parking_lot::Mutex; use std::path::PathBuf; +use nu_engine::shell::CdArgs; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::hir::{Expression, ExternalArgs, ExternalCommand, Literal, SpannedExpression}; use nu_protocol::{Signature, SyntaxShape}; diff --git a/crates/nu-cli/src/commands/save.rs b/crates/nu-cli/src/commands/save.rs index 76e973480d..0233b6f200 100644 --- a/crates/nu-cli/src/commands/save.rs +++ b/crates/nu-cli/src/commands/save.rs @@ -1,5 +1,5 @@ -use crate::commands::{UnevaluatedCallInfo, WholeStreamCommand}; use crate::prelude::*; +use nu_engine::{UnevaluatedCallInfo, WholeStreamCommand}; use nu_errors::ShellError; use nu_protocol::{ hir::ExternalRedirection, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, diff --git a/crates/nu-cli/src/commands/select.rs b/crates/nu-cli/src/commands/select.rs index 8132a40d41..6b89e4494e 100644 --- a/crates/nu-cli/src/commands/select.rs +++ b/crates/nu-cli/src/commands/select.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ ColumnPath, PathMember, Primitive, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, diff --git a/crates/nu-cli/src/commands/seq.rs b/crates/nu-cli/src/commands/seq.rs index 07b7333c3a..5f65fa08e3 100644 --- a/crates/nu-cli/src/commands/seq.rs +++ b/crates/nu-cli/src/commands/seq.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::value::StrExt; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/seq_dates.rs b/crates/nu-cli/src/commands/seq_dates.rs index 31649a1a5b..9297d22e5a 100644 --- a/crates/nu-cli/src/commands/seq_dates.rs +++ b/crates/nu-cli/src/commands/seq_dates.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use chrono::naive::NaiveDate; use chrono::{Duration, Local}; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{value::I64Ext, value::StrExt, value::StringExt, value::U64Ext}; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/shells.rs b/crates/nu-cli/src/commands/shells.rs index 0f53c4e6ba..00b7988d8f 100644 --- a/crates/nu-cli/src/commands/shells.rs +++ b/crates/nu-cli/src/commands/shells.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, TaggedDictBuilder}; use std::sync::atomic::Ordering; diff --git a/crates/nu-cli/src/commands/shuffle.rs b/crates/nu-cli/src/commands/shuffle.rs index 5e53f2dfe7..f47d41150a 100644 --- a/crates/nu-cli/src/commands/shuffle.rs +++ b/crates/nu-cli/src/commands/shuffle.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Value}; diff --git a/crates/nu-cli/src/commands/size.rs b/crates/nu-cli/src/commands/size.rs index fd1fbbc5b9..897cf0d691 100644 --- a/crates/nu-cli/src/commands/size.rs +++ b/crates/nu-cli/src/commands/size.rs @@ -1,8 +1,8 @@ extern crate unicode_segmentation; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use indexmap::indexmap; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, TaggedDictBuilder, UntaggedValue, Value}; use unicode_segmentation::UnicodeSegmentation; diff --git a/crates/nu-cli/src/commands/skip/command.rs b/crates/nu-cli/src/commands/skip/command.rs index 5bc0ac51c4..e057aa4485 100644 --- a/crates/nu-cli/src/commands/skip/command.rs +++ b/crates/nu-cli/src/commands/skip/command.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/skip/until.rs b/crates/nu-cli/src/commands/skip/until.rs index f8fb830934..97c2f923d9 100644 --- a/crates/nu-cli/src/commands/skip/until.rs +++ b/crates/nu-cli/src/commands/skip/until.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; -use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; use log::trace; +use nu_engine::evaluate_baseline_expr; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/skip/while_.rs b/crates/nu-cli/src/commands/skip/while_.rs index 9d9c112fbd..2ee782377c 100644 --- a/crates/nu-cli/src/commands/skip/while_.rs +++ b/crates/nu-cli/src/commands/skip/while_.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; -use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; use log::trace; +use nu_engine::evaluate_baseline_expr; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/sleep.rs b/crates/nu-cli/src/commands/sleep.rs index 4bf982bb00..546f03fc44 100644 --- a/crates/nu-cli/src/commands/sleep.rs +++ b/crates/nu-cli/src/commands/sleep.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/sort_by.rs b/crates/nu-cli/src/commands/sort_by.rs index fc842facc9..128f3377db 100644 --- a/crates/nu-cli/src/commands/sort_by.rs +++ b/crates/nu-cli/src/commands/sort_by.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_data::base::coerce_compare; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/source.rs b/crates/nu-cli/src/commands/source.rs index 72ae45a6bf..3f2a99c9b8 100644 --- a/crates/nu-cli/src/commands/source.rs +++ b/crates/nu-cli/src/commands/source.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; diff --git a/crates/nu-cli/src/commands/split/chars.rs b/crates/nu-cli/src/commands/split/chars.rs index 34a6eeaa7b..268b9776f8 100644 --- a/crates/nu-cli/src/commands/split/chars.rs +++ b/crates/nu-cli/src/commands/split/chars.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, Value}; diff --git a/crates/nu-cli/src/commands/split/column.rs b/crates/nu-cli/src/commands/split/column.rs index a5699b4e53..4d98c9906d 100644 --- a/crates/nu-cli/src/commands/split/column.rs +++ b/crates/nu-cli/src/commands/split/column.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use log::trace; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ Primitive, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue, diff --git a/crates/nu-cli/src/commands/split/command.rs b/crates/nu-cli/src/commands/split/command.rs index b6119f2946..c3befbe8ba 100644 --- a/crates/nu-cli/src/commands/split/command.rs +++ b/crates/nu-cli/src/commands/split/command.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; @@ -22,8 +22,7 @@ impl WholeStreamCommand for Command { async fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(Ok(ReturnSuccess::Value( - UntaggedValue::string(crate::commands::help::get_help(&Command, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&Command, &args.scope)).into_value(Tag::unknown()), )))) } } diff --git a/crates/nu-cli/src/commands/split/row.rs b/crates/nu-cli/src/commands/split/row.rs index 30014299f0..4981fa5bc5 100644 --- a/crates/nu-cli/src/commands/split/row.rs +++ b/crates/nu-cli/src/commands/split/row.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use log::trace; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/split_by.rs b/crates/nu-cli/src/commands/split_by.rs index 3e0b173a57..10c93c0d2e 100644 --- a/crates/nu-cli/src/commands/split_by.rs +++ b/crates/nu-cli/src/commands/split_by.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use crate::utils::suggestions::suggestions; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/str_/capitalize.rs b/crates/nu-cli/src/commands/str_/capitalize.rs index 72867af55e..7f6caa5aab 100644 --- a/crates/nu-cli/src/commands/str_/capitalize.rs +++ b/crates/nu-cli/src/commands/str_/capitalize.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/case/camel_case.rs b/crates/nu-cli/src/commands/str_/case/camel_case.rs index e0abba4cbf..c197842e1b 100644 --- a/crates/nu-cli/src/commands/str_/case/camel_case.rs +++ b/crates/nu-cli/src/commands/str_/case/camel_case.rs @@ -1,7 +1,7 @@ use super::operate; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use inflector::cases::camelcase::to_camel_case; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/str_/case/kebab_case.rs b/crates/nu-cli/src/commands/str_/case/kebab_case.rs index 4112a9e0d6..1a4b03dd95 100644 --- a/crates/nu-cli/src/commands/str_/case/kebab_case.rs +++ b/crates/nu-cli/src/commands/str_/case/kebab_case.rs @@ -1,7 +1,7 @@ use super::operate; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use inflector::cases::kebabcase::to_kebab_case; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/str_/case/pascal_case.rs b/crates/nu-cli/src/commands/str_/case/pascal_case.rs index c354c7ff97..fa43143121 100644 --- a/crates/nu-cli/src/commands/str_/case/pascal_case.rs +++ b/crates/nu-cli/src/commands/str_/case/pascal_case.rs @@ -1,7 +1,7 @@ use super::operate; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use inflector::cases::pascalcase::to_pascal_case; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/str_/case/screaming_snake_case.rs b/crates/nu-cli/src/commands/str_/case/screaming_snake_case.rs index ee250b3d28..94a00fbb3a 100644 --- a/crates/nu-cli/src/commands/str_/case/screaming_snake_case.rs +++ b/crates/nu-cli/src/commands/str_/case/screaming_snake_case.rs @@ -1,7 +1,7 @@ use super::operate; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use inflector::cases::screamingsnakecase::to_screaming_snake_case; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/str_/case/snake_case.rs b/crates/nu-cli/src/commands/str_/case/snake_case.rs index 9151612eac..7c94baadfe 100644 --- a/crates/nu-cli/src/commands/str_/case/snake_case.rs +++ b/crates/nu-cli/src/commands/str_/case/snake_case.rs @@ -1,7 +1,7 @@ use super::operate; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use inflector::cases::snakecase::to_snake_case; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/str_/collect.rs b/crates/nu-cli/src/commands/str_/collect.rs index a858ba54f3..662803399d 100644 --- a/crates/nu-cli/src/commands/str_/collect.rs +++ b/crates/nu-cli/src/commands/str_/collect.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/str_/command.rs b/crates/nu-cli/src/commands/str_/command.rs index 7c9413de79..a8688582a1 100644 --- a/crates/nu-cli/src/commands/str_/command.rs +++ b/crates/nu-cli/src/commands/str_/command.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue}; @@ -24,8 +24,7 @@ impl WholeStreamCommand for Command { async fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( - UntaggedValue::string(crate::commands::help::get_help(&Command, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&Command, &args.scope)).into_value(Tag::unknown()), ))) } } diff --git a/crates/nu-cli/src/commands/str_/contains.rs b/crates/nu-cli/src/commands/str_/contains.rs index d777870ad5..a8ecf4a40b 100644 --- a/crates/nu-cli/src/commands/str_/contains.rs +++ b/crates/nu-cli/src/commands/str_/contains.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/downcase.rs b/crates/nu-cli/src/commands/str_/downcase.rs index 6ddf4af6d9..1f6d6f325f 100644 --- a/crates/nu-cli/src/commands/str_/downcase.rs +++ b/crates/nu-cli/src/commands/str_/downcase.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/ends_with.rs b/crates/nu-cli/src/commands/str_/ends_with.rs index e4b0fe97c9..3f03f4b27f 100644 --- a/crates/nu-cli/src/commands/str_/ends_with.rs +++ b/crates/nu-cli/src/commands/str_/ends_with.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/find_replace.rs b/crates/nu-cli/src/commands/str_/find_replace.rs index 99569374fb..8b6de45f31 100644 --- a/crates/nu-cli/src/commands/str_/find_replace.rs +++ b/crates/nu-cli/src/commands/str_/find_replace.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/from.rs b/crates/nu-cli/src/commands/str_/from.rs index 1f7144809a..32731db74a 100644 --- a/crates/nu-cli/src/commands/str_/from.rs +++ b/crates/nu-cli/src/commands/str_/from.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, diff --git a/crates/nu-cli/src/commands/str_/index_of.rs b/crates/nu-cli/src/commands/str_/index_of.rs index e1281eb2f7..5306bd3bc0 100644 --- a/crates/nu-cli/src/commands/str_/index_of.rs +++ b/crates/nu-cli/src/commands/str_/index_of.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/length.rs b/crates/nu-cli/src/commands/str_/length.rs index 417763ed51..6dec270242 100644 --- a/crates/nu-cli/src/commands/str_/length.rs +++ b/crates/nu-cli/src/commands/str_/length.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/str_/lpad.rs b/crates/nu-cli/src/commands/str_/lpad.rs index 0d55ad29ce..a12f2c1df0 100644 --- a/crates/nu-cli/src/commands/str_/lpad.rs +++ b/crates/nu-cli/src/commands/str_/lpad.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/reverse.rs b/crates/nu-cli/src/commands/str_/reverse.rs index 8fbf5c0c89..3605958d74 100644 --- a/crates/nu-cli/src/commands/str_/reverse.rs +++ b/crates/nu-cli/src/commands/str_/reverse.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/str_/rpad.rs b/crates/nu-cli/src/commands/str_/rpad.rs index f2740732d5..1ded3116a8 100644 --- a/crates/nu-cli/src/commands/str_/rpad.rs +++ b/crates/nu-cli/src/commands/str_/rpad.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/set.rs b/crates/nu-cli/src/commands/str_/set.rs index 171bd303a1..70ba7917bd 100644 --- a/crates/nu-cli/src/commands/str_/set.rs +++ b/crates/nu-cli/src/commands/str_/set.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ColumnPath, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::{Tag, Tagged}; diff --git a/crates/nu-cli/src/commands/str_/starts_with.rs b/crates/nu-cli/src/commands/str_/starts_with.rs index 2a8c21e3df..2176d04e26 100644 --- a/crates/nu-cli/src/commands/str_/starts_with.rs +++ b/crates/nu-cli/src/commands/str_/starts_with.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/substring.rs b/crates/nu-cli/src/commands/str_/substring.rs index cbde75dcea..cfc26714e9 100644 --- a/crates/nu-cli/src/commands/str_/substring.rs +++ b/crates/nu-cli/src/commands/str_/substring.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/to_datetime.rs b/crates/nu-cli/src/commands/str_/to_datetime.rs index d3ee6e3059..d496284b18 100644 --- a/crates/nu-cli/src/commands/str_/to_datetime.rs +++ b/crates/nu-cli/src/commands/str_/to_datetime.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ ColumnPath, Primitive, ReturnSuccess, ShellTypeName, Signature, SyntaxShape, UntaggedValue, diff --git a/crates/nu-cli/src/commands/str_/to_decimal.rs b/crates/nu-cli/src/commands/str_/to_decimal.rs index b5e672a6a9..ef836d8d57 100644 --- a/crates/nu-cli/src/commands/str_/to_decimal.rs +++ b/crates/nu-cli/src/commands/str_/to_decimal.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/to_integer.rs b/crates/nu-cli/src/commands/str_/to_integer.rs index 554bb7a95d..f1b29bfb4d 100644 --- a/crates/nu-cli/src/commands/str_/to_integer.rs +++ b/crates/nu-cli/src/commands/str_/to_integer.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs b/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs index d72f0348ed..c18dd8eaf0 100644 --- a/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs +++ b/crates/nu-cli/src/commands/str_/trim/trim_both_ends.rs @@ -1,6 +1,6 @@ use super::operate; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/str_/trim/trim_left.rs b/crates/nu-cli/src/commands/str_/trim/trim_left.rs index b50609560d..978ddc303b 100644 --- a/crates/nu-cli/src/commands/str_/trim/trim_left.rs +++ b/crates/nu-cli/src/commands/str_/trim/trim_left.rs @@ -1,6 +1,6 @@ use super::operate; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/str_/trim/trim_right.rs b/crates/nu-cli/src/commands/str_/trim/trim_right.rs index 5fd2ed0a74..513e178822 100644 --- a/crates/nu-cli/src/commands/str_/trim/trim_right.rs +++ b/crates/nu-cli/src/commands/str_/trim/trim_right.rs @@ -1,6 +1,6 @@ use super::operate; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/str_/upcase.rs b/crates/nu-cli/src/commands/str_/upcase.rs index a46e87af7d..18882f5a26 100644 --- a/crates/nu-cli/src/commands/str_/upcase.rs +++ b/crates/nu-cli/src/commands/str_/upcase.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::ShellTypeName; use nu_protocol::{ diff --git a/crates/nu-cli/src/commands/table/command.rs b/crates/nu-cli/src/commands/table/command.rs index ad2772f15a..1f3fd75d2e 100644 --- a/crates/nu-cli/src/commands/table/command.rs +++ b/crates/nu-cli/src/commands/table/command.rs @@ -1,8 +1,8 @@ use crate::commands::table::options::{ConfigExtensions, NuConfig as TableConfiguration}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; use crate::primitive::get_color_config; use nu_data::value::{format_leaf, style_leaf}; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value}; use nu_table::{draw_table, Alignment, StyledString, TextStyle}; diff --git a/crates/nu-cli/src/commands/tags.rs b/crates/nu-cli/src/commands/tags.rs index 2d066d3997..d8c0a360b3 100644 --- a/crates/nu-cli/src/commands/tags.rs +++ b/crates/nu-cli/src/commands/tags.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, TaggedDictBuilder, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/to.rs b/crates/nu-cli/src/commands/to.rs index 3b5305b01d..ee6a4a74ff 100644 --- a/crates/nu-cli/src/commands/to.rs +++ b/crates/nu-cli/src/commands/to.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; @@ -22,8 +22,7 @@ impl WholeStreamCommand for To { async fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( - UntaggedValue::string(crate::commands::help::get_help(&To, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&To, &args.scope)).into_value(Tag::unknown()), ))) } } diff --git a/crates/nu-cli/src/commands/to_csv.rs b/crates/nu-cli/src/commands/to_csv.rs index db61051942..201954dcb9 100644 --- a/crates/nu-cli/src/commands/to_csv.rs +++ b/crates/nu-cli/src/commands/to_csv.rs @@ -1,6 +1,6 @@ use crate::commands::to_delimited_data::to_delimited_data; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/to_html.rs b/crates/nu-cli/src/commands/to_html.rs index 6493764e61..9aaa1ee091 100644 --- a/crates/nu-cli/src/commands/to_html.rs +++ b/crates/nu-cli/src/commands/to_html.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use futures::StreamExt; use nu_data::value::format_leaf; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::{AnchorLocation, Tagged}; diff --git a/crates/nu-cli/src/commands/to_json.rs b/crates/nu-cli/src/commands/to_json.rs index e95ff64ce5..8cebc1b3a2 100644 --- a/crates/nu-cli/src/commands/to_json.rs +++ b/crates/nu-cli/src/commands/to_json.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::{CoerceInto, ShellError}; use nu_protocol::{ Primitive, ReturnSuccess, Signature, SyntaxShape, UnspannedPathMember, UntaggedValue, Value, diff --git a/crates/nu-cli/src/commands/to_md.rs b/crates/nu-cli/src/commands/to_md.rs index 7a407541ed..c660f97265 100644 --- a/crates/nu-cli/src/commands/to_md.rs +++ b/crates/nu-cli/src/commands/to_md.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use futures::StreamExt; use nu_data::value::format_leaf; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/to_toml.rs b/crates/nu-cli/src/commands/to_toml.rs index 7dbaa7760d..88af79ac9e 100644 --- a/crates/nu-cli/src/commands/to_toml.rs +++ b/crates/nu-cli/src/commands/to_toml.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::{CoerceInto, ShellError}; use nu_protocol::{Primitive, ReturnSuccess, Signature, UnspannedPathMember, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/to_tsv.rs b/crates/nu-cli/src/commands/to_tsv.rs index 7a988327e9..71c39250bd 100644 --- a/crates/nu-cli/src/commands/to_tsv.rs +++ b/crates/nu-cli/src/commands/to_tsv.rs @@ -1,6 +1,6 @@ use crate::commands::to_delimited_data::to_delimited_data; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::Signature; diff --git a/crates/nu-cli/src/commands/to_url.rs b/crates/nu-cli/src/commands/to_url.rs index 3831bca46c..0d44afd025 100644 --- a/crates/nu-cli/src/commands/to_url.rs +++ b/crates/nu-cli/src/commands/to_url.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/to_xml.rs b/crates/nu-cli/src/commands/to_xml.rs index f40a213136..d3e3a2d0e9 100644 --- a/crates/nu-cli/src/commands/to_xml.rs +++ b/crates/nu-cli/src/commands/to_xml.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use indexmap::IndexMap; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use quick_xml::events::{BytesEnd, BytesStart, BytesText, Event}; diff --git a/crates/nu-cli/src/commands/to_yaml.rs b/crates/nu-cli/src/commands/to_yaml.rs index 12fe760cf7..b445e08f0a 100644 --- a/crates/nu-cli/src/commands/to_yaml.rs +++ b/crates/nu-cli/src/commands/to_yaml.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::{CoerceInto, ShellError}; use nu_protocol::{Primitive, ReturnSuccess, Signature, UnspannedPathMember, UntaggedValue, Value}; diff --git a/crates/nu-cli/src/commands/touch.rs b/crates/nu-cli/src/commands/touch.rs index 4e38757b96..a4db691ebc 100644 --- a/crates/nu-cli/src/commands/touch.rs +++ b/crates/nu-cli/src/commands/touch.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/uniq.rs b/crates/nu-cli/src/commands/uniq.rs index 1865b0f2c0..63f70f9731 100644 --- a/crates/nu-cli/src/commands/uniq.rs +++ b/crates/nu-cli/src/commands/uniq.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/update.rs b/crates/nu-cli/src/commands/update.rs index cd5ff47008..ad0f68bf6b 100644 --- a/crates/nu-cli/src/commands/update.rs +++ b/crates/nu-cli/src/commands/update.rs @@ -1,6 +1,6 @@ -use crate::commands::classified::block::run_block; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::run_block; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, diff --git a/crates/nu-cli/src/commands/url_/command.rs b/crates/nu-cli/src/commands/url_/command.rs index 0b42d770a0..a42dc498d9 100644 --- a/crates/nu-cli/src/commands/url_/command.rs +++ b/crates/nu-cli/src/commands/url_/command.rs @@ -1,5 +1,5 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, UntaggedValue}; @@ -21,8 +21,7 @@ impl WholeStreamCommand for Url { async fn run(&self, args: CommandArgs) -> Result { Ok(OutputStream::one(ReturnSuccess::value( - UntaggedValue::string(crate::commands::help::get_help(&Url, &args.scope)) - .into_value(Tag::unknown()), + UntaggedValue::string(get_help(&Url, &args.scope)).into_value(Tag::unknown()), ))) } } diff --git a/crates/nu-cli/src/commands/url_/host.rs b/crates/nu-cli/src/commands/url_/host.rs index 55344e4383..9e9bd04388 100644 --- a/crates/nu-cli/src/commands/url_/host.rs +++ b/crates/nu-cli/src/commands/url_/host.rs @@ -1,8 +1,8 @@ use url::Url; use super::{operate, DefaultArguments}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/url_/path.rs b/crates/nu-cli/src/commands/url_/path.rs index 456aa21d49..38a4df2187 100644 --- a/crates/nu-cli/src/commands/url_/path.rs +++ b/crates/nu-cli/src/commands/url_/path.rs @@ -1,8 +1,8 @@ use url::Url; use super::{operate, DefaultArguments}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/url_/query.rs b/crates/nu-cli/src/commands/url_/query.rs index 23d1c7381e..e80df0466e 100644 --- a/crates/nu-cli/src/commands/url_/query.rs +++ b/crates/nu-cli/src/commands/url_/query.rs @@ -1,8 +1,8 @@ use url::Url; use super::{operate, DefaultArguments}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/url_/scheme.rs b/crates/nu-cli/src/commands/url_/scheme.rs index f0c6982483..5fb987771f 100644 --- a/crates/nu-cli/src/commands/url_/scheme.rs +++ b/crates/nu-cli/src/commands/url_/scheme.rs @@ -1,8 +1,8 @@ use url::Url; use super::{operate, DefaultArguments}; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, Value}; diff --git a/crates/nu-cli/src/commands/version.rs b/crates/nu-cli/src/commands/version.rs index 650b19ae68..c6de617ff7 100644 --- a/crates/nu-cli/src/commands/version.rs +++ b/crates/nu-cli/src/commands/version.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use indexmap::IndexMap; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{value::StrExt, value::StringExt, Dictionary, Signature, UntaggedValue}; diff --git a/crates/nu-cli/src/commands/where_.rs b/crates/nu-cli/src/commands/where_.rs index 661e9374a3..08525f84e8 100644 --- a/crates/nu-cli/src/commands/where_.rs +++ b/crates/nu-cli/src/commands/where_.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; -use crate::evaluate::evaluate_baseline_expr; use crate::prelude::*; +use nu_engine::evaluate_baseline_expr; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ hir::CapturedBlock, hir::ClassifiedCommand, ReturnSuccess, Signature, SyntaxShape, diff --git a/crates/nu-cli/src/commands/which_.rs b/crates/nu-cli/src/commands/which_.rs index 124ce424e6..76d5a471ee 100644 --- a/crates/nu-cli/src/commands/which_.rs +++ b/crates/nu-cli/src/commands/which_.rs @@ -1,7 +1,7 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use indexmap::map::IndexMap; use log::trace; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/commands/with_env.rs b/crates/nu-cli/src/commands/with_env.rs index 0311986e07..427b3f76f1 100644 --- a/crates/nu-cli/src/commands/with_env.rs +++ b/crates/nu-cli/src/commands/with_env.rs @@ -1,6 +1,6 @@ -use crate::commands::classified::block::run_block; -use crate::commands::WholeStreamCommand; use crate::prelude::*; +use nu_engine::run_block; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ hir::CapturedBlock, Signature, SpannedTypeName, SyntaxShape, UntaggedValue, Value, diff --git a/crates/nu-cli/src/commands/wrap.rs b/crates/nu-cli/src/commands/wrap.rs index 8e42058fc7..7e4682b8ac 100644 --- a/crates/nu-cli/src/commands/wrap.rs +++ b/crates/nu-cli/src/commands/wrap.rs @@ -1,6 +1,6 @@ -use crate::commands::WholeStreamCommand; use crate::prelude::*; use indexmap::{indexmap, IndexMap}; +use nu_engine::WholeStreamCommand; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_source::Tagged; diff --git a/crates/nu-cli/src/completion/command.rs b/crates/nu-cli/src/completion/command.rs index e056ee904d..ce0efd7ead 100644 --- a/crates/nu-cli/src/completion/command.rs +++ b/crates/nu-cli/src/completion/command.rs @@ -5,7 +5,7 @@ use indexmap::set::IndexSet; use super::matchers::Matcher; use crate::completion::{Completer, CompletionContext, Suggestion}; -use crate::evaluation_context::EvaluationContext; +use nu_engine::EvaluationContext; pub struct CommandCompleter; diff --git a/crates/nu-cli/src/completion/flag.rs b/crates/nu-cli/src/completion/flag.rs index 9073928864..e4d707ef2c 100644 --- a/crates/nu-cli/src/completion/flag.rs +++ b/crates/nu-cli/src/completion/flag.rs @@ -1,6 +1,6 @@ use super::matchers::Matcher; use crate::completion::{Completer, CompletionContext, Suggestion}; -use crate::evaluation_context::EvaluationContext; +use nu_engine::EvaluationContext; pub struct FlagCompleter { pub(crate) cmd: String, diff --git a/crates/nu-cli/src/completion/mod.rs b/crates/nu-cli/src/completion/mod.rs index 145339be46..c66d959f14 100644 --- a/crates/nu-cli/src/completion/mod.rs +++ b/crates/nu-cli/src/completion/mod.rs @@ -4,8 +4,8 @@ pub(crate) mod flag; pub(crate) mod matchers; pub(crate) mod path; -use crate::evaluation_context::EvaluationContext; use matchers::Matcher; +use nu_engine::EvaluationContext; #[derive(Debug, Eq, PartialEq)] pub struct Suggestion { diff --git a/crates/nu-cli/src/env.rs b/crates/nu-cli/src/env.rs index 74ae277ab5..d524f89fca 100644 --- a/crates/nu-cli/src/env.rs +++ b/crates/nu-cli/src/env.rs @@ -1,6 +1,4 @@ +pub(crate) mod basic_host; pub(crate) mod directory_specific_environment; pub(crate) mod environment; pub(crate) mod environment_syncer; -pub(crate) mod host; - -pub(crate) use self::host::Host; diff --git a/crates/nu-cli/src/env/basic_host.rs b/crates/nu-cli/src/env/basic_host.rs new file mode 100644 index 0000000000..864d2c8719 --- /dev/null +++ b/crates/nu-cli/src/env/basic_host.rs @@ -0,0 +1,77 @@ +use nu_engine::Host; +use nu_protocol::{errln, outln}; +use std::ffi::OsString; + +#[derive(Debug)] +pub struct BasicHost; + +impl Host for BasicHost { + fn stdout(&mut self, out: &str) { + match out { + "\n" => outln!(""), + other => outln!("{}", other), + } + } + + fn stderr(&mut self, out: &str) { + match out { + "\n" => errln!(""), + other => errln!("{}", other), + } + } + + #[allow(unused_variables)] + fn vars(&mut self) -> Vec<(String, String)> { + #[cfg(not(target_arch = "wasm32"))] + { + std::env::vars().collect::>() + } + + #[cfg(target_arch = "wasm32")] + { + vec![] + } + } + + #[allow(unused_variables)] + fn env_get(&mut self, key: OsString) -> Option { + #[cfg(not(target_arch = "wasm32"))] + { + std::env::var_os(key) + } + #[cfg(target_arch = "wasm32")] + { + None + } + } + + #[allow(unused_variables)] + fn env_set(&mut self, key: OsString, value: OsString) { + #[cfg(not(target_arch = "wasm32"))] + { + std::env::set_var(key, value); + } + } + + #[allow(unused_variables)] + fn env_rm(&mut self, key: OsString) { + #[cfg(not(target_arch = "wasm32"))] + { + std::env::remove_var(key); + } + } + + fn out_termcolor(&self) -> termcolor::StandardStream { + termcolor::StandardStream::stdout(termcolor::ColorChoice::Auto) + } + + fn err_termcolor(&self) -> termcolor::StandardStream { + termcolor::StandardStream::stderr(termcolor::ColorChoice::Auto) + } + + fn width(&self) -> usize { + let (mut term_width, _) = term_size::dimensions().unwrap_or((80, 20)); + term_width -= 1; + term_width + } +} diff --git a/crates/nu-cli/src/env/environment.rs b/crates/nu-cli/src/env/environment.rs index 3bdbbc7eac..37bf638ad5 100644 --- a/crates/nu-cli/src/env/environment.rs +++ b/crates/nu-cli/src/env/environment.rs @@ -1,38 +1,9 @@ use crate::env::directory_specific_environment::*; use indexmap::{indexmap, IndexSet}; use nu_data::config::Conf; +use nu_engine::Env; use nu_errors::ShellError; use nu_protocol::{UntaggedValue, Value}; -use std::env::*; -use std::ffi::OsString; - -use std::fmt::Debug; - -pub trait Env: Debug + Send { - fn env(&self) -> Option; - fn path(&self) -> Option; - - fn add_env(&mut self, key: &str, value: &str); - fn add_path(&mut self, new_path: OsString); -} - -impl Env for Box { - fn env(&self) -> Option { - (**self).env() - } - - fn path(&self) -> Option { - (**self).path() - } - - fn add_env(&mut self, key: &str, value: &str) { - (**self).add_env(key, value); - } - - fn add_path(&mut self, new_path: OsString) { - (**self).add_path(new_path); - } -} #[derive(Debug, Default)] pub struct Environment { @@ -128,7 +99,7 @@ impl Env for Environment { { let mut new_paths = current_paths.clone(); - let new_path_candidates = split_paths(&paths).map(|path| { + let new_path_candidates = std::env::split_paths(&paths).map(|path| { UntaggedValue::string(path.to_string_lossy()).into_value(tag.clone()) }); diff --git a/crates/nu-cli/src/env/environment_syncer.rs b/crates/nu-cli/src/env/environment_syncer.rs index 9b46044498..57c2ffbb93 100644 --- a/crates/nu-cli/src/env/environment_syncer.rs +++ b/crates/nu-cli/src/env/environment_syncer.rs @@ -1,6 +1,7 @@ -use crate::env::environment::{Env, Environment}; -use crate::evaluation_context::EvaluationContext; +use crate::env::environment::Environment; use nu_data::config::{Conf, NuConfig}; +use nu_engine::Env; +use nu_engine::EvaluationContext; use nu_errors::ShellError; use parking_lot::Mutex; use std::sync::{atomic::Ordering, Arc}; @@ -152,10 +153,9 @@ impl EnvironmentSyncer { #[cfg(test)] mod tests { use super::EnvironmentSyncer; - use crate::env::environment::Env; - use crate::evaluation_context::EvaluationContext; use indexmap::IndexMap; use nu_data::config::tests::FakeConfig; + use nu_engine::Env; use nu_errors::ShellError; use nu_test_support::fs::Stub::FileWithContent; use nu_test_support::playground::Playground; @@ -170,8 +170,8 @@ mod tests { #[test] fn syncs_env_if_new_env_entry_is_added_to_an_existing_configuration() -> Result<(), ShellError> { - let mut ctx = EvaluationContext::basic()?; - ctx.host = Arc::new(Mutex::new(Box::new(crate::env::host::FakeHost::new()))); + let mut ctx = crate::cli::basic_evaluation_context()?; + ctx.host = Arc::new(Mutex::new(Box::new(nu_engine::FakeHost::new()))); let mut expected = IndexMap::new(); expected.insert( @@ -273,8 +273,8 @@ mod tests { #[test] fn syncs_env_if_new_env_entry_in_session_is_not_in_configuration_file() -> Result<(), ShellError> { - let mut ctx = EvaluationContext::basic()?; - ctx.host = Arc::new(Mutex::new(Box::new(crate::env::host::FakeHost::new()))); + let mut ctx = crate::cli::basic_evaluation_context()?; + ctx.host = Arc::new(Mutex::new(Box::new(nu_engine::FakeHost::new()))); let mut expected = IndexMap::new(); expected.insert( @@ -372,8 +372,8 @@ mod tests { #[test] fn nu_envs_have_higher_priority_and_does_not_get_overwritten() -> Result<(), ShellError> { - let mut ctx = EvaluationContext::basic()?; - ctx.host = Arc::new(Mutex::new(Box::new(crate::env::host::FakeHost::new()))); + let mut ctx = crate::cli::basic_evaluation_context()?; + ctx.host = Arc::new(Mutex::new(Box::new(nu_engine::FakeHost::new()))); let mut expected = IndexMap::new(); expected.insert( @@ -448,8 +448,8 @@ mod tests { #[test] fn syncs_path_if_new_path_entry_in_session_is_not_in_configuration_file( ) -> Result<(), ShellError> { - let mut ctx = EvaluationContext::basic()?; - ctx.host = Arc::new(Mutex::new(Box::new(crate::env::host::FakeHost::new()))); + let mut ctx = crate::cli::basic_evaluation_context()?; + ctx.host = Arc::new(Mutex::new(Box::new(nu_engine::FakeHost::new()))); let expected = std::env::join_paths(vec![ PathBuf::from("/Users/andresrobalino/.volta/bin"), @@ -535,8 +535,8 @@ mod tests { #[test] fn nu_paths_have_higher_priority_and_new_paths_get_appended_to_the_end( ) -> Result<(), ShellError> { - let mut ctx = EvaluationContext::basic()?; - ctx.host = Arc::new(Mutex::new(Box::new(crate::env::host::FakeHost::new()))); + let mut ctx = crate::cli::basic_evaluation_context()?; + ctx.host = Arc::new(Mutex::new(Box::new(nu_engine::FakeHost::new()))); let expected = std::env::join_paths(vec![ PathBuf::from("/Users/andresrobalino/.volta/bin"), diff --git a/crates/nu-cli/src/examples.rs b/crates/nu-cli/src/examples.rs index 111fe1c23b..91db772b12 100644 --- a/crates/nu-cli/src/examples.rs +++ b/crates/nu-cli/src/examples.rs @@ -10,13 +10,12 @@ use crate::prelude::*; use num_bigint::BigInt; -use crate::commands::classified::block::run_block; -use crate::commands::command::CommandArgs; use crate::commands::{ - whole_stream_command, BuildString, Command, Each, Echo, First, Get, Keep, Last, Let, Nth, - StrCollect, WholeStreamCommand, Wrap, + BuildString, Each, Echo, First, Get, Keep, Last, Let, Nth, StrCollect, Wrap, +}; +use nu_engine::{ + run_block, whole_stream_command, Command, CommandArgs, EvaluationContext, WholeStreamCommand, }; -use crate::evaluation_context::EvaluationContext; use nu_stream::{InputStream, OutputStream}; use async_trait::async_trait; @@ -26,7 +25,7 @@ use serde::Deserialize; pub fn test_examples(cmd: Command) -> Result<(), ShellError> { let examples = cmd.examples(); - let base_context = EvaluationContext::basic()?; + let base_context = crate::cli::basic_evaluation_context()?; base_context.add_commands(vec![ // Mocks @@ -90,7 +89,7 @@ pub fn test_examples(cmd: Command) -> Result<(), ShellError> { pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> { let examples = cmd.examples(); - let base_context = EvaluationContext::basic()?; + let base_context = crate::cli::basic_evaluation_context()?; base_context.add_commands(vec![ whole_stream_command(Echo {}), @@ -146,7 +145,7 @@ pub fn test(cmd: impl WholeStreamCommand + 'static) -> Result<(), ShellError> { pub fn test_anchors(cmd: Command) -> Result<(), ShellError> { let examples = cmd.examples(); - let base_context = EvaluationContext::basic()?; + let base_context = crate::cli::basic_evaluation_context()?; base_context.add_commands(vec![ // Minimal restricted commands to aid in testing diff --git a/crates/nu-cli/src/lib.rs b/crates/nu-cli/src/lib.rs index b3d5b7affa..b67d30e388 100644 --- a/crates/nu-cli/src/lib.rs +++ b/crates/nu-cli/src/lib.rs @@ -17,11 +17,7 @@ mod cli; mod commands; #[cfg(feature = "rustyline-support")] mod completion; -mod deserializer; -mod documentation; mod env; -mod evaluate; -mod evaluation_context; mod format; mod futures; #[cfg(feature = "rustyline-support")] @@ -29,8 +25,6 @@ mod git; #[cfg(feature = "rustyline-support")] mod keybinding; mod line_editor; -mod path; -mod plugin; pub mod script; mod shell; pub mod types; @@ -43,17 +37,9 @@ mod examples; pub use crate::cli::cli; pub use crate::cli::{parse_and_eval, register_plugins, run_script_file}; -pub use crate::commands::classified::block::run_block; -pub use crate::commands::command::{ - whole_stream_command, CommandArgs, EvaluatedWholeStreamCommandArgs, Example, WholeStreamCommand, -}; pub use crate::commands::default_context::create_default_context; -pub use crate::commands::help::get_help; pub use crate::env::environment_syncer::EnvironmentSyncer; -pub use crate::env::host::BasicHost; -pub use crate::evaluation_context::EvaluationContext; -pub use crate::prelude::ToOutputStream; pub use nu_data::config; pub use nu_data::dict::TaggedListBuilder; pub use nu_data::primitive; diff --git a/crates/nu-cli/src/line_editor.rs b/crates/nu-cli/src/line_editor.rs index 4c105e96a5..3f46ad5e74 100644 --- a/crates/nu-cli/src/line_editor.rs +++ b/crates/nu-cli/src/line_editor.rs @@ -1,4 +1,4 @@ -use crate::evaluation_context::EvaluationContext; +use nu_engine::EvaluationContext; use std::error::Error; #[allow(unused_imports)] diff --git a/crates/nu-cli/src/prelude.rs b/crates/nu-cli/src/prelude.rs index d4d4cd8daf..b81bbb328b 100644 --- a/crates/nu-cli/src/prelude.rs +++ b/crates/nu-cli/src/prelude.rs @@ -21,28 +21,6 @@ macro_rules! stream { }} } -#[macro_export] -macro_rules! trace_stream { - (target: $target:tt, $desc:tt = $expr:expr) => {{ - if log::log_enabled!(target: $target, log::Level::Trace) { - use futures::stream::StreamExt; - - let objects = $expr.inspect(move |o| { - trace!( - target: $target, - "{} = {}", - $desc, - nu_source::PrettyDebug::plain_string(o, 70) - ); - }); - - nu_stream::InputStream::from_stream(objects.boxed()) - } else { - $expr - } - }}; -} - #[macro_export] macro_rules! trace_out_stream { (target: $target:tt, $desc:tt = $expr:expr) => {{ @@ -68,32 +46,28 @@ macro_rules! trace_out_stream { }}; } -pub(crate) use nu_protocol::{errln, out, outln, row}; -use nu_source::HasFallibleSpan; +pub(crate) use nu_protocol::{out, outln, row}; -pub(crate) use crate::commands::command::{CommandArgs, RawCommandArgs, RunnableContext}; -pub(crate) use crate::commands::Example; -pub(crate) use crate::evaluation_context::EvaluationContext; +pub(crate) use crate::cli::maybe_print_errors; +pub(crate) use crate::commands::command::RunnableContext; +pub(crate) use async_trait::async_trait; +pub(crate) use bigdecimal::BigDecimal; +pub(crate) use futures::{Stream, StreamExt}; +pub(crate) use indexmap::{indexmap, IndexMap}; +pub(crate) use itertools::Itertools; pub(crate) use nu_data::config; pub(crate) use nu_data::value; -// pub(crate) use crate::env::host::handle_unexpected; -pub(crate) use crate::env::Host; -pub(crate) use crate::evaluate::scope::Scope; - -pub(crate) use crate::shell::filesystem_shell::FilesystemShell; -pub(crate) use crate::shell::help_shell::HelpShell; -pub(crate) use crate::shell::shell_manager::ShellManager; -pub(crate) use crate::shell::value_shell::ValueShell; -pub(crate) use bigdecimal::BigDecimal; -pub(crate) use futures::stream::BoxStream; -pub(crate) use futures::{Stream, StreamExt}; +pub(crate) use nu_engine::EvaluationContext; +pub(crate) use nu_engine::Example; +pub(crate) use nu_engine::Host; +pub(crate) use nu_engine::RawCommandArgs; +pub(crate) use nu_engine::ShellManager; +pub(crate) use nu_engine::{get_help, CommandArgs, Scope, WholeStreamCommand}; pub(crate) use nu_errors::ShellError; pub(crate) use nu_parser::ParserScope; -pub(crate) use nu_source::{ - b, AnchorLocation, DebugDocBuilder, PrettyDebug, PrettyDebugWithSource, Span, SpannedItem, Tag, - TaggedItem, Text, -}; -pub(crate) use nu_stream::{InputStream, InterruptibleStream, OutputStream}; +pub(crate) use nu_source::{AnchorLocation, PrettyDebug, Span, SpannedItem, Tag, TaggedItem, Text}; +pub(crate) use nu_stream::ToInputStream; +pub(crate) use nu_stream::{InputStream, Interruptible, OutputStream}; pub(crate) use nu_value_ext::ValueExt; pub(crate) use num_bigint::BigInt; pub(crate) use num_traits::cast::ToPrimitive; @@ -103,10 +77,6 @@ pub(crate) use std::future::Future; pub(crate) use std::sync::atomic::{AtomicBool, Ordering}; pub(crate) use std::sync::Arc; -pub(crate) use async_trait::async_trait; -pub(crate) use indexmap::{indexmap, IndexMap}; -pub(crate) use itertools::Itertools; - pub trait FromInputStream { fn from_input_stream(self) -> OutputStream; } @@ -122,26 +92,6 @@ where } } -pub trait ToInputStream { - fn to_input_stream(self) -> InputStream; -} - -impl ToInputStream for T -where - T: Stream + Send + 'static, - U: Into>, -{ - fn to_input_stream(self) -> InputStream { - InputStream::from_stream(self.map(|item| match item.into() { - Ok(result) => result, - Err(err) => match HasFallibleSpan::maybe_span(&err) { - Some(span) => nu_protocol::UntaggedValue::Error(err).into_value(span), - None => nu_protocol::UntaggedValue::Error(err).into_untagged_value(), - }, - })) - } -} - pub trait ToOutputStream { fn to_output_stream(self) -> OutputStream; } @@ -157,16 +107,3 @@ where } } } - -pub trait Interruptible { - fn interruptible(self, ctrl_c: Arc) -> InterruptibleStream; -} - -impl Interruptible for S -where - S: Stream + Send + 'static, -{ - fn interruptible(self, ctrl_c: Arc) -> InterruptibleStream { - InterruptibleStream::new(self, ctrl_c) - } -} diff --git a/crates/nu-cli/src/script.rs b/crates/nu-cli/src/script.rs index 6151cfa824..91311f9da2 100644 --- a/crates/nu-cli/src/script.rs +++ b/crates/nu-cli/src/script.rs @@ -1,12 +1,13 @@ -use crate::commands::classified::block::run_block; -use crate::commands::classified::maybe_text_codec::{MaybeTextCodec, StringOrBinary}; -use crate::evaluation_context::EvaluationContext; -use crate::path::canonicalize; use crate::prelude::*; use futures_codec::FramedRead; +use nu_engine::path::canonicalize; +use nu_engine::run_block; +use nu_engine::EvaluationContext; +use nu_engine::{MaybeTextCodec, StringOrBinary}; use nu_errors::ShellError; use nu_protocol::hir::{ClassifiedCommand, Expression, InternalCommand, Literal, NamedArguments}; use nu_protocol::{Primitive, ReturnSuccess, UntaggedValue, Value}; +use nu_stream::ToInputStream; use log::{debug, trace}; use std::error::Error; @@ -250,7 +251,7 @@ pub async fn run_script_standalone( } }; - context.maybe_print_errors(Text::from(line)); + maybe_print_errors(&context, Text::from(line)); if error_code != 0 && exit_on_error { std::process::exit(error_code); } @@ -261,7 +262,7 @@ pub async fn run_script_standalone( print_err(err, &Text::from(line.clone())); }); - context.maybe_print_errors(Text::from(line)); + maybe_print_errors(&context, Text::from(line)); if exit_on_error { std::process::exit(1); } diff --git a/crates/nu-cli/src/shell.rs b/crates/nu-cli/src/shell.rs index 36baf3daad..3c8930606d 100644 --- a/crates/nu-cli/src/shell.rs +++ b/crates/nu-cli/src/shell.rs @@ -2,15 +2,8 @@ #[cfg(feature = "rustyline-support")] pub(crate) mod completer; -pub(crate) mod filesystem_shell; -pub(crate) mod help_shell; #[cfg(feature = "rustyline-support")] pub(crate) mod helper; -pub(crate) mod painter; -pub(crate) mod palette; -pub(crate) mod shell; -pub(crate) mod shell_manager; -pub(crate) mod value_shell; #[cfg(feature = "rustyline-support")] pub(crate) use helper::Helper; diff --git a/crates/nu-cli/src/shell/completer.rs b/crates/nu-cli/src/shell/completer.rs index 63d63405cf..ad4e17ae0c 100644 --- a/crates/nu-cli/src/shell/completer.rs +++ b/crates/nu-cli/src/shell/completer.rs @@ -4,7 +4,7 @@ use crate::completion::matchers; use crate::completion::matchers::Matcher; use crate::completion::path::{PathCompleter, PathSuggestion}; use crate::completion::{self, Completer, Suggestion}; -use crate::evaluation_context::EvaluationContext; +use nu_engine::EvaluationContext; use nu_parser::ParserScope; use nu_source::Tag; diff --git a/crates/nu-cli/src/shell/helper.rs b/crates/nu-cli/src/shell/helper.rs index 348178c9f8..0c4c5cd305 100644 --- a/crates/nu-cli/src/shell/helper.rs +++ b/crates/nu-cli/src/shell/helper.rs @@ -1,12 +1,8 @@ -use std::borrow::Cow::{self, Owned}; - -use nu_source::{Tag, Tagged}; - use crate::completion; -use crate::evaluation_context::EvaluationContext; use crate::shell::completer::NuCompleter; -use crate::shell::painter::Painter; -use crate::shell::palette::DefaultPalette; +use nu_engine::{DefaultPalette, EvaluationContext, Painter}; +use nu_source::{Tag, Tagged}; +use std::borrow::Cow::{self, Owned}; pub struct Helper { completer: NuCompleter, diff --git a/crates/nu-cli/src/types/deduction.rs b/crates/nu-cli/src/types/deduction.rs index d662f7d41d..88d99f0f8a 100644 --- a/crates/nu-cli/src/types/deduction.rs +++ b/crates/nu-cli/src/types/deduction.rs @@ -1,6 +1,8 @@ #![allow(dead_code)] -use crate::prelude::*; +use itertools::{merge_join_by, EitherOrBoth, Itertools}; use lazy_static::lazy_static; +use log::trace; +use nu_engine::Scope; use nu_errors::ShellError; use nu_parser::ParserScope; use nu_protocol::{ @@ -14,9 +16,6 @@ use nu_source::Span; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, hash::Hash}; -use itertools::{merge_join_by, EitherOrBoth, Itertools}; -use log::trace; - #[derive(Debug, Clone, Serialize, Deserialize)] pub struct VarDeclaration { pub name: String, diff --git a/crates/nu-cli/src/utils.rs b/crates/nu-cli/src/utils.rs index 1c049b3b9d..ed6dc62fcf 100644 --- a/crates/nu-cli/src/utils.rs +++ b/crates/nu-cli/src/utils.rs @@ -1,291 +1,2 @@ pub mod suggestions; pub mod test_bins; - -use crate::path::canonicalize; -use nu_errors::ShellError; -use nu_protocol::{UntaggedValue, Value}; -use std::path::{Component, Path, PathBuf}; - -fn is_value_tagged_dir(value: &Value) -> bool { - matches!(&value.value, UntaggedValue::Row(_) | UntaggedValue::Table(_)) -} - -#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)] -pub struct ValueResource { - pub at: usize, - pub loc: PathBuf, -} - -impl ValueResource {} - -#[derive(Default)] -pub struct ValueStructure { - pub resources: Vec, -} - -impl ValueStructure { - pub fn new() -> ValueStructure { - ValueStructure { - resources: Vec::::new(), - } - } - - pub fn exists(&self, path: &Path) -> bool { - if path == Path::new("/") { - return true; - } - - let path = if path.starts_with("/") { - path.strip_prefix("/").unwrap_or(path) - } else { - path - }; - - let comps: Vec<_> = path.components().map(Component::as_os_str).collect(); - - let mut is_there = true; - - for (at, fragment) in comps.iter().enumerate() { - is_there = is_there - && self - .resources - .iter() - .any(|resource| at == resource.at && *fragment == resource.loc.as_os_str()); - } - - is_there - } - - pub fn walk_decorate(&mut self, start: &Value) -> Result<(), ShellError> { - self.resources = Vec::::new(); - self.build(start, 0)?; - self.resources.sort(); - - Ok(()) - } - - fn build(&mut self, src: &Value, lvl: usize) -> Result<(), ShellError> { - for entry in src.row_entries() { - let value = entry.1; - let path = entry.0; - - self.resources.push(ValueResource { - at: lvl, - loc: PathBuf::from(path), - }); - - if is_value_tagged_dir(value) { - self.build(value, lvl + 1)?; - } - } - - Ok(()) - } -} - -#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)] -pub struct Res { - pub at: usize, - pub loc: PathBuf, -} - -impl Res {} - -#[derive(Default)] -pub struct FileStructure { - pub resources: Vec, -} - -impl FileStructure { - pub fn new() -> FileStructure { - FileStructure { - resources: Vec::::new(), - } - } - - #[allow(dead_code)] - pub fn contains_more_than_one_file(&self) -> bool { - self.resources.len() > 1 - } - - #[allow(dead_code)] - pub fn contains_files(&self) -> bool { - !self.resources.is_empty() - } - - pub fn paths_applying_with( - &mut self, - to: F, - ) -> Result, Box> - where - F: Fn((PathBuf, usize)) -> Result<(PathBuf, PathBuf), Box>, - { - self.resources - .iter() - .map(|f| (PathBuf::from(&f.loc), f.at)) - .map(|f| to(f)) - .collect() - } - - pub fn walk_decorate(&mut self, start_path: &Path) -> Result<(), ShellError> { - self.resources = Vec::::new(); - self.build(start_path, 0)?; - self.resources.sort(); - - Ok(()) - } - - fn build(&mut self, src: &Path, lvl: usize) -> Result<(), ShellError> { - let source = canonicalize(std::env::current_dir()?, src)?; - - if source.is_dir() { - for entry in std::fs::read_dir(src)? { - let entry = entry?; - let path = entry.path(); - - if path.is_dir() { - self.build(&path, lvl + 1)?; - } - - self.resources.push(Res { - loc: path.to_path_buf(), - at: lvl, - }); - } - } else { - self.resources.push(Res { - loc: source, - at: lvl, - }); - } - - Ok(()) - } -} - -#[cfg(test)] -mod tests { - use super::{FileStructure, Res, ValueResource, ValueStructure}; - use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value}; - use nu_source::Tag; - use nu_test_support::{fs::Stub::EmptyFile, playground::Playground}; - use std::path::PathBuf; - - fn structured_sample_record(key: &str, value: &str) -> Value { - let mut record = TaggedDictBuilder::new(Tag::unknown()); - record.insert_untagged(key, UntaggedValue::string(value)); - record.into_value() - } - - fn sample_nushell_source_code() -> Value { - /* - src - commands - plugins => "sys.rs" - tests - helpers => "mod.rs" - */ - - let mut src = TaggedDictBuilder::new(Tag::unknown()); - let mut record = TaggedDictBuilder::new(Tag::unknown()); - - record.insert_value("commands", structured_sample_record("plugins", "sys.rs")); - record.insert_value("tests", structured_sample_record("helpers", "mod.rs")); - src.insert_value("src", record.into_value()); - - src.into_value() - } - - #[test] - fn prepares_and_decorates_value_filesystemlike_sources() { - let mut res = ValueStructure::new(); - - res.walk_decorate(&sample_nushell_source_code()) - .expect("Can not decorate values traversal."); - - assert_eq!( - res.resources, - vec![ - ValueResource { - loc: PathBuf::from("src"), - at: 0, - }, - ValueResource { - loc: PathBuf::from("commands"), - at: 1, - }, - ValueResource { - loc: PathBuf::from("tests"), - at: 1, - }, - ValueResource { - loc: PathBuf::from("helpers"), - at: 2, - }, - ValueResource { - loc: PathBuf::from("plugins"), - at: 2, - }, - ] - ); - } - - #[test] - fn recognizes_if_path_exists_in_value_filesystemlike_sources() { - let mut res = ValueStructure::new(); - - res.walk_decorate(&sample_nushell_source_code()) - .expect("Can not decorate values traversal."); - - assert!(res.exists(&PathBuf::from("/"))); - - assert!(res.exists(&PathBuf::from("src/commands/plugins"))); - assert!(res.exists(&PathBuf::from("src/commands"))); - assert!(res.exists(&PathBuf::from("src/tests"))); - assert!(res.exists(&PathBuf::from("src/tests/helpers"))); - assert!(res.exists(&PathBuf::from("src"))); - - assert!(res.exists(&PathBuf::from("/src/commands/plugins"))); - assert!(res.exists(&PathBuf::from("/src/commands"))); - assert!(res.exists(&PathBuf::from("/src/tests"))); - assert!(res.exists(&PathBuf::from("/src/tests/helpers"))); - assert!(res.exists(&PathBuf::from("/src"))); - - assert!(!res.exists(&PathBuf::from("/not_valid"))); - assert!(!res.exists(&PathBuf::from("/src/not_valid"))); - } - - #[test] - fn prepares_and_decorates_filesystem_source_files() { - Playground::setup("file_structure_test", |dirs, sandbox| { - sandbox.with_files(vec![ - EmptyFile("sample.ini"), - EmptyFile("sample.eml"), - EmptyFile("cargo_sample.toml"), - ]); - - let mut res = FileStructure::new(); - - res.walk_decorate(&dirs.test()) - .expect("Can not decorate files traversal."); - - assert_eq!( - res.resources, - vec![ - Res { - loc: dirs.test().join("cargo_sample.toml"), - at: 0 - }, - Res { - loc: dirs.test().join("sample.eml"), - at: 0 - }, - Res { - loc: dirs.test().join("sample.ini"), - at: 0 - } - ] - ); - }) - } -} diff --git a/crates/nu-engine/Cargo.toml b/crates/nu-engine/Cargo.toml new file mode 100644 index 0000000000..bba5919771 --- /dev/null +++ b/crates/nu-engine/Cargo.toml @@ -0,0 +1,48 @@ +[package] +authors = ["The Nu Project Contributors"] +description = "Core commands for nushell" +edition = "2018" +license = "MIT" +name = "nu-engine" +version = "0.25.1" + +[dependencies] +nu-data = {version = "0.25.1", path = "../nu-data"} +nu-errors = {version = "0.25.1", path = "../nu-errors"} +nu-parser = {version = "0.25.1", path = "../nu-parser"} +nu-plugin = {version = "0.25.1", path = "../nu-plugin"} +nu-protocol = {version = "0.25.1", path = "../nu-protocol"} +nu-source = {version = "0.25.1", path = "../nu-source"} +nu-stream = {version = "0.25.1", path = "../nu-stream"} +nu-value-ext = {version = "0.25.1", path = "../nu-value-ext"} + +ansi_term = "0.12.1" +async-recursion = "0.3.1" +async-trait = "0.1.40" +bytes = "0.5.6" +derive-new = "0.5.8" +dunce = "1.0.1" +encoding_rs = "0.8.24" +filesize = "0.2.0" +fs_extra = "1.2.0" +futures = {version = "0.3.5", features = ["compat", "io-compat"]} +futures-util = "0.3.8" +futures_codec = "0.4.1" +getset = "0.1.1" +glob = "0.3.0" +indexmap = {version = "1.6.0", features = ["serde-1"]} +itertools = "0.10.0" +log = "0.4.11" +parking_lot = "0.11.0" +rayon = "1.4.0" +serde = {version = "1.0.115", features = ["derive"]} +serde_json = "1.0.57" +tempfile = "3.1.0" +termcolor = "1.1.0" + +[target.'cfg(unix)'.dependencies] +umask = "1.0.0" +users = "0.10.0" + +[dev-dependencies] +nu-test-support = {version = "0.25.1", path = "../nu-test-support"} diff --git a/crates/nu-engine/src/call_info.rs b/crates/nu-engine/src/call_info.rs new file mode 100644 index 0000000000..25151e6b4b --- /dev/null +++ b/crates/nu-engine/src/call_info.rs @@ -0,0 +1,27 @@ +use crate::evaluate::evaluate_args::evaluate_args; +use crate::evaluation_context::EvaluationContext; +use nu_errors::ShellError; +use nu_protocol::hir; +use nu_protocol::CallInfo; +use nu_source::Tag; + +#[derive(Debug, Clone)] +pub struct UnevaluatedCallInfo { + pub args: hir::Call, + pub name_tag: Tag, +} + +impl UnevaluatedCallInfo { + pub async fn evaluate(self, ctx: &EvaluationContext) -> Result { + let args = evaluate_args(&self.args, ctx).await?; + + Ok(CallInfo { + args, + name_tag: self.name_tag, + }) + } + + pub fn switch_present(&self, switch: &str) -> bool { + self.args.switch_preset(switch) + } +} diff --git a/crates/nu-engine/src/command_args.rs b/crates/nu-engine/src/command_args.rs new file mode 100644 index 0000000000..0f071a8bf8 --- /dev/null +++ b/crates/nu-engine/src/command_args.rs @@ -0,0 +1,173 @@ +use crate::call_info::UnevaluatedCallInfo; +use crate::deserializer::ConfigDeserializer; +use crate::env::host::Host; +use crate::evaluate::scope::Scope; +use crate::evaluation_context::EvaluationContext; +use crate::shell::shell_manager::ShellManager; +use derive_new::new; +use getset::Getters; +use nu_errors::ShellError; +use nu_protocol::EvaluatedArgs; +use nu_protocol::{CallInfo, Value}; +use nu_source::Tag; +use nu_stream::InputStream; +use parking_lot::Mutex; +use serde::Deserialize; +use std::ops::Deref; +use std::sync::atomic::AtomicBool; +use std::sync::Arc; + +#[derive(Getters)] +#[get = "pub"] +pub struct CommandArgs { + pub host: Arc>>, + pub ctrl_c: Arc, + pub current_errors: Arc>>, + pub shell_manager: ShellManager, + pub call_info: UnevaluatedCallInfo, + pub scope: Scope, + pub input: InputStream, +} + +#[derive(Getters, Clone)] +#[get = "pub"] +pub struct RawCommandArgs { + pub host: Arc>>, + pub ctrl_c: Arc, + pub current_errors: Arc>>, + pub shell_manager: ShellManager, + pub scope: Scope, + pub call_info: UnevaluatedCallInfo, +} + +impl RawCommandArgs { + pub fn with_input(self, input: impl Into) -> CommandArgs { + CommandArgs { + host: self.host, + ctrl_c: self.ctrl_c, + current_errors: self.current_errors, + shell_manager: self.shell_manager, + call_info: self.call_info, + scope: self.scope, + input: input.into(), + } + } +} + +impl std::fmt::Debug for CommandArgs { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self.call_info.fmt(f) + } +} + +impl CommandArgs { + pub async fn evaluate_once(self) -> Result { + 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(&ctx).await?; + let scope = self.scope.clone(); + + Ok(EvaluatedWholeStreamCommandArgs::new( + host, + ctrl_c, + shell_manager, + call_info, + input, + scope, + )) + } + + 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); + + Ok((T::deserialize(&mut deserializer)?, args.input)) + } +} + +pub struct EvaluatedWholeStreamCommandArgs { + pub args: EvaluatedCommandArgs, + pub input: InputStream, +} + +impl Deref for EvaluatedWholeStreamCommandArgs { + type Target = EvaluatedCommandArgs; + fn deref(&self) -> &Self::Target { + &self.args + } +} + +impl EvaluatedWholeStreamCommandArgs { + pub fn new( + host: Arc>, + ctrl_c: Arc, + shell_manager: ShellManager, + call_info: CallInfo, + input: impl Into, + scope: Scope, + ) -> EvaluatedWholeStreamCommandArgs { + EvaluatedWholeStreamCommandArgs { + args: EvaluatedCommandArgs { + host, + ctrl_c, + shell_manager, + call_info, + scope, + }, + input: input.into(), + } + } + + pub fn name_tag(&self) -> Tag { + self.args.call_info.name_tag.clone() + } + + pub fn parts(self) -> (InputStream, EvaluatedArgs) { + let EvaluatedWholeStreamCommandArgs { args, input } = self; + + (input, args.call_info.args) + } + + pub fn split(self) -> (InputStream, EvaluatedCommandArgs) { + let EvaluatedWholeStreamCommandArgs { args, input } = self; + + (input, args) + } +} + +#[derive(Getters, new)] +#[get = "pub(crate)"] +pub struct EvaluatedCommandArgs { + pub host: Arc>, + pub ctrl_c: Arc, + pub shell_manager: ShellManager, + pub call_info: CallInfo, + pub scope: Scope, +} + +impl EvaluatedCommandArgs { + pub fn nth(&self, pos: usize) -> Option<&Value> { + self.call_info.args.nth(pos) + } + + /// Get the nth positional argument, error if not possible + pub fn expect_nth(&self, pos: usize) -> Result<&Value, ShellError> { + self.call_info + .args + .nth(pos) + .ok_or_else(|| ShellError::unimplemented("Better error: expect_nth")) + } + + pub fn get(&self, name: &str) -> Option<&Value> { + self.call_info.args.get(name) + } + + pub fn has(&self, name: &str) -> bool { + self.call_info.args.has(name) + } +} diff --git a/crates/nu-cli/src/deserializer.rs b/crates/nu-engine/src/deserializer.rs similarity index 100% rename from crates/nu-cli/src/deserializer.rs rename to crates/nu-engine/src/deserializer.rs diff --git a/crates/nu-cli/src/documentation.rs b/crates/nu-engine/src/documentation.rs similarity index 96% rename from crates/nu-cli/src/documentation.rs rename to crates/nu-engine/src/documentation.rs index 3123f34c46..76e00b5c05 100644 --- a/crates/nu-cli/src/documentation.rs +++ b/crates/nu-engine/src/documentation.rs @@ -1,8 +1,9 @@ -use crate::commands::WholeStreamCommand; - -use crate::prelude::*; +use crate::evaluate::scope::Scope; +use crate::whole_stream_command::WholeStreamCommand; +use indexmap::IndexMap; +use itertools::Itertools; use nu_protocol::{NamedType, PositionalType, Signature, UntaggedValue, Value}; - +use nu_source::PrettyDebug; use std::collections::HashMap; const COMMANDS_DOCS_DIR: &str = "docs/commands"; @@ -289,3 +290,7 @@ fn get_flags_section(signature: &Signature) -> String { } long_desc } + +pub fn get_help(cmd: &dyn WholeStreamCommand, scope: &Scope) -> String { + get_documentation(cmd, scope, &DocumentationConfig::default()) +} diff --git a/crates/nu-engine/src/env/environment.rs b/crates/nu-engine/src/env/environment.rs new file mode 100644 index 0000000000..5d5fff6a57 --- /dev/null +++ b/crates/nu-engine/src/env/environment.rs @@ -0,0 +1,30 @@ +use nu_protocol::Value; +use std::ffi::OsString; + +use std::fmt::Debug; + +pub trait Env: Debug + Send { + fn env(&self) -> Option; + fn path(&self) -> Option; + + fn add_env(&mut self, key: &str, value: &str); + fn add_path(&mut self, new_path: OsString); +} + +impl Env for Box { + fn env(&self) -> Option { + (**self).env() + } + + fn path(&self) -> Option { + (**self).path() + } + + fn add_env(&mut self, key: &str, value: &str) { + (**self).add_env(key, value); + } + + fn add_path(&mut self, new_path: OsString) { + (**self).add_path(new_path); + } +} diff --git a/crates/nu-cli/src/env/host.rs b/crates/nu-engine/src/env/host.rs similarity index 58% rename from crates/nu-cli/src/env/host.rs rename to crates/nu-engine/src/env/host.rs index 11ba0ea0f8..6496546ba3 100644 --- a/crates/nu-cli/src/env/host.rs +++ b/crates/nu-engine/src/env/host.rs @@ -1,7 +1,4 @@ -use crate::prelude::*; -#[cfg(test)] use indexmap::IndexMap; -// use nu_errors::ShellError; use std::ffi::OsString; use std::fmt::Debug; @@ -58,88 +55,12 @@ impl Host for Box { } } -#[derive(Debug)] -pub struct BasicHost; - -impl Host for BasicHost { - fn stdout(&mut self, out: &str) { - match out { - "\n" => outln!(""), - other => outln!("{}", other), - } - } - - fn stderr(&mut self, out: &str) { - match out { - "\n" => errln!(""), - other => errln!("{}", other), - } - } - - #[allow(unused_variables)] - fn vars(&mut self) -> Vec<(String, String)> { - #[cfg(not(target_arch = "wasm32"))] - { - std::env::vars().collect::>() - } - - #[cfg(target_arch = "wasm32")] - { - vec![] - } - } - - #[allow(unused_variables)] - fn env_get(&mut self, key: OsString) -> Option { - #[cfg(not(target_arch = "wasm32"))] - { - std::env::var_os(key) - } - #[cfg(target_arch = "wasm32")] - { - None - } - } - - #[allow(unused_variables)] - fn env_set(&mut self, key: OsString, value: OsString) { - #[cfg(not(target_arch = "wasm32"))] - { - std::env::set_var(key, value); - } - } - - #[allow(unused_variables)] - fn env_rm(&mut self, key: OsString) { - #[cfg(not(target_arch = "wasm32"))] - { - std::env::remove_var(key); - } - } - - fn out_termcolor(&self) -> termcolor::StandardStream { - termcolor::StandardStream::stdout(termcolor::ColorChoice::Auto) - } - - fn err_termcolor(&self) -> termcolor::StandardStream { - termcolor::StandardStream::stderr(termcolor::ColorChoice::Auto) - } - - fn width(&self) -> usize { - let (mut term_width, _) = term_size::dimensions().unwrap_or((80, 20)); - term_width -= 1; - term_width - } -} - -#[cfg(test)] #[derive(Debug)] pub struct FakeHost { line_written: String, env_vars: IndexMap, } -#[cfg(test)] impl FakeHost { pub fn new() -> FakeHost { FakeHost { @@ -149,7 +70,12 @@ impl FakeHost { } } -#[cfg(test)] +impl Default for FakeHost { + fn default() -> Self { + FakeHost::new() + } +} + impl Host for FakeHost { fn stdout(&mut self, out: &str) { self.line_written = out.to_string(); @@ -199,14 +125,3 @@ impl Host for FakeHost { 1 } } - -// pub(crate) fn handle_unexpected( -// host: &mut dyn Host, -// func: impl FnOnce(&mut dyn Host) -> Result, -// ) { -// let result = func(host); - -// if let Err(err) = result { -// host.stderr(&format!("Something unexpected happened:\n{:?}", err)); -// } -// } diff --git a/crates/nu-engine/src/env/mod.rs b/crates/nu-engine/src/env/mod.rs new file mode 100644 index 0000000000..99390642d1 --- /dev/null +++ b/crates/nu-engine/src/env/mod.rs @@ -0,0 +1,2 @@ +pub(crate) mod environment; +pub(crate) mod host; diff --git a/crates/nu-cli/src/commands/classified/block.rs b/crates/nu-engine/src/evaluate/block.rs similarity index 97% rename from crates/nu-cli/src/commands/classified/block.rs rename to crates/nu-engine/src/evaluate/block.rs index 5c73b86d21..e2f4eab34e 100644 --- a/crates/nu-cli/src/commands/classified/block.rs +++ b/crates/nu-engine/src/evaluate/block.rs @@ -1,15 +1,17 @@ -use crate::commands::classified::expr::run_expression_block; -use crate::commands::classified::internal::run_internal_command; +use crate::evaluate::expr::run_expression_block; +use crate::evaluate::internal::run_internal_command; use crate::evaluation_context::EvaluationContext; -use crate::prelude::*; use async_recursion::async_recursion; use futures::stream::TryStreamExt; use nu_errors::ShellError; +use nu_parser::ParserScope; use nu_protocol::hir::{ Block, Call, ClassifiedCommand, Expression, Pipeline, SpannedExpression, Synthetic, }; use nu_protocol::{ReturnSuccess, UntaggedValue, Value}; +use nu_source::{Span, Tag}; use nu_stream::InputStream; +use nu_stream::ToOutputStream; use std::sync::atomic::Ordering; #[async_recursion] diff --git a/crates/nu-cli/src/evaluate/evaluate_args.rs b/crates/nu-engine/src/evaluate/evaluate_args.rs similarity index 89% rename from crates/nu-cli/src/evaluate/evaluate_args.rs rename to crates/nu-engine/src/evaluate/evaluate_args.rs index 4bfac6f694..95dd57cbd3 100644 --- a/crates/nu-cli/src/evaluate/evaluate_args.rs +++ b/crates/nu-engine/src/evaluate/evaluate_args.rs @@ -1,6 +1,8 @@ // TODO: Temporary redirect -use crate::evaluate::evaluate_baseline_expr; -use crate::prelude::*; +use crate::evaluate::evaluator::evaluate_baseline_expr; +use crate::evaluation_context::EvaluationContext; +use indexmap::IndexMap; +use nu_errors::ShellError; use nu_protocol::{hir, EvaluatedArgs, UntaggedValue, Value}; pub(crate) async fn evaluate_args( diff --git a/crates/nu-cli/src/evaluate/evaluator.rs b/crates/nu-engine/src/evaluate/evaluator.rs similarity index 97% rename from crates/nu-cli/src/evaluate/evaluator.rs rename to crates/nu-engine/src/evaluate/evaluator.rs index 2e336d3c11..b10ab05c20 100644 --- a/crates/nu-cli/src/evaluate/evaluator.rs +++ b/crates/nu-engine/src/evaluate/evaluator.rs @@ -1,10 +1,11 @@ -use crate::commands::classified::block::run_block; -use crate::did_you_mean; +use crate::evaluate::block::run_block; use crate::evaluate::operator::apply_operator; -use crate::prelude::*; +use crate::evaluation_context::EvaluationContext; use async_recursion::async_recursion; +use indexmap::IndexMap; use log::trace; use nu_errors::{ArgumentError, ShellError}; +use nu_protocol::did_you_mean; use nu_protocol::{ hir::{self, CapturedBlock, Expression, ExternalRedirection, RangeOperator, SpannedExpression}, Dictionary, @@ -12,9 +13,12 @@ use nu_protocol::{ use nu_protocol::{ ColumnPath, Primitive, RangeInclusion, UnspannedPathMember, UntaggedValue, Value, }; +use nu_source::{Span, SpannedItem, Tag}; +use nu_stream::InputStream; +use nu_value_ext::ValueExt; #[async_recursion] -pub(crate) async fn evaluate_baseline_expr( +pub async fn evaluate_baseline_expr( expr: &SpannedExpression, ctx: &EvaluationContext, ) -> Result { diff --git a/crates/nu-cli/src/commands/classified/expr.rs b/crates/nu-engine/src/evaluate/expr.rs similarity index 80% rename from crates/nu-cli/src/commands/classified/expr.rs rename to crates/nu-engine/src/evaluate/expr.rs index b48adbfd01..4b25437053 100644 --- a/crates/nu-cli/src/commands/classified/expr.rs +++ b/crates/nu-engine/src/evaluate/expr.rs @@ -1,11 +1,12 @@ -use crate::evaluate::evaluate_baseline_expr; -use crate::prelude::*; +use crate::evaluate_baseline_expr; use log::{log_enabled, trace}; +use crate::evaluation_context::EvaluationContext; use futures::stream::once; use nu_errors::ShellError; use nu_protocol::hir::SpannedExpression; +use nu_stream::{InputStream, ToInputStream}; pub(crate) async fn run_expression_block( expr: &SpannedExpression, diff --git a/crates/nu-cli/src/commands/classified/internal.rs b/crates/nu-engine/src/evaluate/internal.rs similarity index 95% rename from crates/nu-cli/src/commands/classified/internal.rs rename to crates/nu-engine/src/evaluate/internal.rs index 413ac9eeff..98e12e12ab 100644 --- a/crates/nu-cli/src/commands/classified/internal.rs +++ b/crates/nu-engine/src/evaluate/internal.rs @@ -1,11 +1,18 @@ -use std::sync::atomic::Ordering; - -use crate::commands::UnevaluatedCallInfo; -use crate::prelude::*; +use crate::call_info::UnevaluatedCallInfo; +use crate::command_args::RawCommandArgs; +use crate::evaluation_context::EvaluationContext; +use crate::filesystem::filesystem_shell::FilesystemShell; +use crate::shell::help_shell::HelpShell; +use crate::shell::value_shell::ValueShell; +use futures::StreamExt; use log::{log_enabled, trace}; use nu_errors::ShellError; use nu_protocol::hir::{ExternalRedirection, InternalCommand}; use nu_protocol::{CommandAction, Primitive, ReturnSuccess, UntaggedValue, Value}; +use nu_source::{PrettyDebug, Span, Tag}; +use nu_stream::{trace_stream, InputStream, ToInputStream}; +use std::sync::atomic::Ordering; +use std::sync::Arc; pub(crate) async fn run_internal_command( command: InternalCommand, @@ -177,7 +184,9 @@ pub(crate) async fn run_internal_command( InputStream::from_stream(futures::stream::iter(vec![])) } CommandAction::AddPlugins(path) => { - match crate::plugin::scan(vec![std::path::PathBuf::from(path)]) { + match crate::plugin::build_plugin::scan(vec![ + std::path::PathBuf::from(path), + ]) { Ok(plugins) => { context.add_commands( plugins diff --git a/crates/nu-cli/src/evaluate/mod.rs b/crates/nu-engine/src/evaluate/mod.rs similarity index 65% rename from crates/nu-cli/src/evaluate/mod.rs rename to crates/nu-engine/src/evaluate/mod.rs index cf784acc17..e383e4fc59 100644 --- a/crates/nu-cli/src/evaluate/mod.rs +++ b/crates/nu-engine/src/evaluate/mod.rs @@ -1,7 +1,8 @@ +pub(crate) mod block; pub(crate) mod evaluate_args; pub(crate) mod evaluator; +pub(crate) mod expr; +pub(crate) mod internal; pub(crate) mod operator; pub(crate) mod scope; pub(crate) mod variables; - -pub(crate) use evaluator::evaluate_baseline_expr; diff --git a/crates/nu-cli/src/evaluate/operator.rs b/crates/nu-engine/src/evaluate/operator.rs similarity index 100% rename from crates/nu-cli/src/evaluate/operator.rs rename to crates/nu-engine/src/evaluate/operator.rs diff --git a/crates/nu-cli/src/evaluate/scope.rs b/crates/nu-engine/src/evaluate/scope.rs similarity index 97% rename from crates/nu-cli/src/evaluate/scope.rs rename to crates/nu-engine/src/evaluate/scope.rs index 55c7a371c7..bbc3408d56 100644 --- a/crates/nu-cli/src/evaluate/scope.rs +++ b/crates/nu-engine/src/evaluate/scope.rs @@ -1,8 +1,10 @@ -use crate::prelude::*; -use crate::{commands::Command, whole_stream_command}; +use crate::whole_stream_command::{whole_stream_command, Command}; +use indexmap::IndexMap; +use nu_errors::ShellError; use nu_parser::ParserScope; use nu_protocol::{hir::Block, Value}; use nu_source::Spanned; +use std::sync::Arc; #[derive(Debug, Clone)] pub struct Scope { @@ -87,6 +89,10 @@ impl Scope { self.frames.lock().len() } + pub fn is_empty(&self) -> bool { + self.frames.lock().is_empty() + } + fn has_cmd_helper(&self, name: &str, f: fn(&ScopeFrame, &str) -> bool) -> bool { self.frames.lock().iter().any(|frame| f(frame, name)) } diff --git a/crates/nu-cli/src/evaluate/variables.rs b/crates/nu-engine/src/evaluate/variables.rs similarity index 91% rename from crates/nu-cli/src/evaluate/variables.rs rename to crates/nu-engine/src/evaluate/variables.rs index 6cf908cb8a..34f79e7887 100644 --- a/crates/nu-cli/src/evaluate/variables.rs +++ b/crates/nu-engine/src/evaluate/variables.rs @@ -1,4 +1,5 @@ -use crate::prelude::*; +use crate::history_path::history_path; +use indexmap::IndexMap; use nu_errors::ShellError; use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value}; use nu_source::Tag; @@ -31,7 +32,7 @@ pub fn nu(env: &IndexMap, tag: impl Into) -> Result, tag: impl Into) -> Result = Box::new(nu_data::config::NuConfig::new()); - let history = crate::commands::history::history_path(&config); + let history = history_path(&config); nu_dict.insert_value( "history-path", UntaggedValue::filepath(history).into_value(&tag), diff --git a/crates/nu-cli/src/evaluation_context.rs b/crates/nu-engine/src/evaluation_context.rs similarity index 67% rename from crates/nu-cli/src/evaluation_context.rs rename to crates/nu-engine/src/evaluation_context.rs index 8dfe3328f4..6c24f6da5e 100644 --- a/crates/nu-cli/src/evaluation_context.rs +++ b/crates/nu-engine/src/evaluation_context.rs @@ -1,12 +1,15 @@ -use crate::commands::{command::CommandArgs, Command, UnevaluatedCallInfo}; +use crate::call_info::UnevaluatedCallInfo; +use crate::command_args::CommandArgs; use crate::env::host::Host; -use crate::prelude::*; +use crate::evaluate::scope::Scope; use crate::shell::shell_manager::ShellManager; +use crate::whole_stream_command::Command; +use indexmap::IndexMap; +use nu_errors::ShellError; use nu_protocol::hir; -use nu_source::{Tag, Text}; +use nu_source::Tag; use nu_stream::{InputStream, OutputStream}; use parking_lot::Mutex; -use std::error::Error; use std::sync::atomic::AtomicBool; use std::sync::Arc; @@ -17,14 +20,14 @@ pub struct EvaluationContext { pub current_errors: Arc>>, pub ctrl_c: Arc, pub user_recently_used_autoenv_untrust: Arc, - pub(crate) shell_manager: ShellManager, + pub shell_manager: ShellManager, /// Windows-specific: keep track of previous cwd on each drive pub windows_drives_previous_cwd: Arc>>, } impl EvaluationContext { - pub(crate) fn from_raw(raw_args: &CommandArgs) -> EvaluationContext { + pub fn from_raw(raw_args: &CommandArgs) -> EvaluationContext { EvaluationContext { scope: raw_args.scope.clone(), host: raw_args.host.clone(), @@ -36,7 +39,7 @@ impl EvaluationContext { } } - pub(crate) fn from_args(args: &CommandArgs) -> EvaluationContext { + pub fn from_args(args: &CommandArgs) -> EvaluationContext { EvaluationContext { scope: args.scope.clone(), host: args.host.clone(), @@ -48,48 +51,19 @@ impl EvaluationContext { } } - pub fn basic() -> Result> { - Ok(EvaluationContext { - scope: Scope::new(), - host: Arc::new(parking_lot::Mutex::new(Box::new( - crate::env::host::BasicHost, - ))), - current_errors: Arc::new(Mutex::new(vec![])), - ctrl_c: Arc::new(AtomicBool::new(false)), - user_recently_used_autoenv_untrust: Arc::new(AtomicBool::new(false)), - shell_manager: ShellManager::basic()?, - windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())), - }) - } - - pub(crate) fn error(&self, error: ShellError) { + pub fn error(&self, error: ShellError) { self.with_errors(|errors| errors.push(error)) } - pub(crate) fn clear_errors(&self) { + pub fn clear_errors(&self) { self.current_errors.lock().clear() } - pub(crate) fn get_errors(&self) -> Vec { + pub fn get_errors(&self) -> Vec { self.current_errors.lock().clone() } - pub(crate) fn maybe_print_errors(&self, source: Text) -> bool { - let errors = self.current_errors.clone(); - let mut errors = errors.lock(); - - if errors.len() > 0 { - let error = errors[0].clone(); - *errors = vec![]; - - crate::script::print_err(error, &source); - true - } else { - false - } - } - - pub(crate) fn configure( + pub fn configure( &mut self, config: &dyn nu_data::config::Conf, block: impl FnOnce(&dyn nu_data::config::Conf, &mut Self) -> T, @@ -97,13 +71,13 @@ impl EvaluationContext { block(config, &mut *self); } - pub(crate) fn with_host(&self, block: impl FnOnce(&mut dyn Host) -> T) -> T { + pub fn with_host(&self, block: impl FnOnce(&mut dyn Host) -> T) -> T { let mut host = self.host.lock(); block(&mut *host) } - pub(crate) fn with_errors(&self, block: impl FnOnce(&mut Vec) -> T) -> T { + pub fn with_errors(&self, block: impl FnOnce(&mut Vec) -> T) -> T { let mut errors = self.current_errors.lock(); block(&mut *errors) @@ -120,7 +94,7 @@ impl EvaluationContext { self.scope.get_command(name) } - pub(crate) fn is_command_registered(&self, name: &str) -> bool { + pub fn is_command_registered(&self, name: &str) -> bool { self.scope.has_command(name) } diff --git a/crates/nu-engine/src/example.rs b/crates/nu-engine/src/example.rs new file mode 100644 index 0000000000..41a93ea5a6 --- /dev/null +++ b/crates/nu-engine/src/example.rs @@ -0,0 +1,7 @@ +use nu_protocol::Value; + +pub struct Example { + pub example: &'static str, + pub description: &'static str, + pub result: Option>, +} diff --git a/crates/nu-engine/src/filesystem/dir_info.rs b/crates/nu-engine/src/filesystem/dir_info.rs new file mode 100644 index 0000000000..905ab57f89 --- /dev/null +++ b/crates/nu-engine/src/filesystem/dir_info.rs @@ -0,0 +1,267 @@ +use filesize::file_real_size_fast; +use glob::Pattern; +use indexmap::IndexMap; +use nu_errors::ShellError; +use nu_protocol::{UntaggedValue, Value}; +use nu_source::Tag; +use std::path::PathBuf; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::Arc; + +pub struct DirBuilder { + pub tag: Tag, + pub min: Option, + pub deref: bool, + pub exclude: Option, + pub all: bool, +} + +impl DirBuilder { + pub fn new( + tag: Tag, + min: Option, + deref: bool, + exclude: Option, + all: bool, + ) -> DirBuilder { + DirBuilder { + tag, + min, + deref, + exclude, + all, + } + } +} + +pub struct DirInfo { + dirs: Vec, + files: Vec, + errors: Vec, + size: u64, + blocks: u64, + path: PathBuf, + tag: Tag, +} + +pub struct FileInfo { + path: PathBuf, + size: u64, + blocks: Option, + tag: Tag, +} + +impl FileInfo { + pub fn new(path: impl Into, deref: bool, tag: Tag) -> Result { + let path = path.into(); + let m = if deref { + std::fs::metadata(&path) + } else { + std::fs::symlink_metadata(&path) + }; + + match m { + Ok(d) => { + let block_size = file_real_size_fast(&path, &d).ok(); + + Ok(FileInfo { + path, + blocks: block_size, + size: d.len(), + tag, + }) + } + Err(e) => Err(e.into()), + } + } +} + +impl DirInfo { + pub fn new( + path: impl Into, + params: &DirBuilder, + depth: Option, + ctrl_c: Arc, + ) -> Self { + let path = path.into(); + + let mut s = Self { + dirs: Vec::new(), + errors: Vec::new(), + files: Vec::new(), + size: 0, + blocks: 0, + tag: params.tag.clone(), + path, + }; + + match std::fs::read_dir(&s.path) { + Ok(d) => { + for f in d { + if ctrl_c.load(Ordering::SeqCst) { + break; + } + + match f { + Ok(i) => match i.file_type() { + Ok(t) if t.is_dir() => { + s = s.add_dir(i.path(), depth, ¶ms, ctrl_c.clone()) + } + Ok(_t) => s = s.add_file(i.path(), ¶ms), + Err(e) => s = s.add_error(e.into()), + }, + Err(e) => s = s.add_error(e.into()), + } + } + } + Err(e) => s = s.add_error(e.into()), + } + s + } + + fn add_dir( + mut self, + path: impl Into, + mut depth: Option, + params: &DirBuilder, + ctrl_c: Arc, + ) -> Self { + if let Some(current) = depth { + if let Some(new) = current.checked_sub(1) { + depth = Some(new); + } else { + return self; + } + } + + let d = DirInfo::new(path, ¶ms, depth, ctrl_c); + self.size += d.size; + self.blocks += d.blocks; + self.dirs.push(d); + self + } + + fn add_file(mut self, f: impl Into, params: &DirBuilder) -> Self { + let f = f.into(); + let include = params + .exclude + .as_ref() + .map_or(true, |x| !x.matches_path(&f)); + if include { + match FileInfo::new(f, params.deref, self.tag.clone()) { + Ok(file) => { + let inc = params.min.map_or(true, |s| file.size >= s); + if inc { + self.size += file.size; + self.blocks += file.blocks.unwrap_or(0); + if params.all { + self.files.push(file); + } + } + } + Err(e) => self = self.add_error(e), + } + } + self + } + + fn add_error(mut self, e: ShellError) -> Self { + self.errors.push(e); + self + } + + pub fn get_size(&self) -> u64 { + self.size + } +} + +impl From for Value { + fn from(d: DirInfo) -> Self { + let mut r: IndexMap = IndexMap::new(); + + r.insert( + "path".to_string(), + UntaggedValue::filepath(d.path).into_value(&d.tag), + ); + + r.insert( + "apparent".to_string(), + UntaggedValue::filesize(d.size).into_value(&d.tag), + ); + + r.insert( + "physical".to_string(), + UntaggedValue::filesize(d.blocks).into_value(&d.tag), + ); + + r.insert("directories".to_string(), value_from_vec(d.dirs, &d.tag)); + + r.insert("files".to_string(), value_from_vec(d.files, &d.tag)); + + if !d.errors.is_empty() { + let v = UntaggedValue::Table( + d.errors + .into_iter() + .map(move |e| UntaggedValue::Error(e).into_untagged_value()) + .collect::>(), + ) + .into_value(&d.tag); + + r.insert("errors".to_string(), v); + } + + Value { + value: UntaggedValue::row(r), + tag: d.tag, + } + } +} + +impl From for Value { + fn from(f: FileInfo) -> Self { + let mut r: IndexMap = IndexMap::new(); + + r.insert( + "path".to_string(), + UntaggedValue::filepath(f.path).into_value(&f.tag), + ); + + r.insert( + "apparent".to_string(), + UntaggedValue::filesize(f.size).into_value(&f.tag), + ); + + let b = f + .blocks + .map(UntaggedValue::filesize) + .unwrap_or_else(UntaggedValue::nothing) + .into_value(&f.tag); + + r.insert("physical".to_string(), b); + + r.insert( + "directories".to_string(), + UntaggedValue::nothing().into_value(&f.tag), + ); + + r.insert( + "files".to_string(), + UntaggedValue::nothing().into_value(&f.tag), + ); + + UntaggedValue::row(r).into_value(&f.tag) + } +} + +fn value_from_vec(vec: Vec, tag: &Tag) -> Value +where + V: Into, +{ + if vec.is_empty() { + UntaggedValue::nothing() + } else { + let values = vec.into_iter().map(Into::into).collect::>(); + UntaggedValue::Table(values) + } + .into_value(tag) +} diff --git a/crates/nu-cli/src/shell/filesystem_shell.rs b/crates/nu-engine/src/filesystem/filesystem_shell.rs similarity index 98% rename from crates/nu-cli/src/shell/filesystem_shell.rs rename to crates/nu-engine/src/filesystem/filesystem_shell.rs index d947ad00d1..fe8d461c68 100644 --- a/crates/nu-cli/src/shell/filesystem_shell.rs +++ b/crates/nu-engine/src/filesystem/filesystem_shell.rs @@ -1,25 +1,24 @@ -use crate::commands::cd::CdArgs; -use crate::commands::command::EvaluatedWholeStreamCommandArgs; -use crate::commands::cp::CopyArgs; -use crate::commands::du::{DirBuilder, DirInfo}; -use crate::commands::ls::LsArgs; -use crate::commands::mkdir::MkdirArgs; -use crate::commands::move_::mv::Arguments as MvArgs; -use crate::commands::rm::RemoveArgs; -use crate::path::canonicalize; -use crate::prelude::*; -use crate::shell::shell::Shell; -use crate::utils::FileStructure; -use nu_protocol::{TaggedDictBuilder, Value}; - -use std::collections::HashMap; -use std::io::{Error, ErrorKind}; -use std::path::{Path, PathBuf}; - -use crate::commands::classified::maybe_text_codec::{MaybeTextCodec, StringOrBinary}; +use crate::command_args::EvaluatedWholeStreamCommandArgs; +use crate::filesystem::dir_info::{DirBuilder, DirInfo}; +use crate::filesystem::path::canonicalize; +use crate::filesystem::utils::FileStructure; +use crate::maybe_text_codec::{MaybeTextCodec, StringOrBinary}; +use crate::shell::shell_args::{CdArgs, CopyArgs, LsArgs, MkdirArgs, MvArgs, RemoveArgs}; +use crate::shell::Shell; use encoding_rs::Encoding; +use futures::stream::BoxStream; +use futures::StreamExt; use futures_codec::FramedRead; use futures_util::TryStreamExt; +use nu_protocol::{TaggedDictBuilder, Value}; +use nu_source::{Span, Tag}; +use nu_stream::{Interruptible, OutputStream, ToOutputStream}; +use std::collections::HashMap; +use std::collections::VecDeque; +use std::io::{Error, ErrorKind}; +use std::path::{Path, PathBuf}; +use std::sync::atomic::AtomicBool; +use std::sync::Arc; #[cfg(unix)] use std::os::unix::fs::PermissionsExt; @@ -884,7 +883,7 @@ fn is_hidden_dir(dir: impl AsRef) -> bool { #[cfg(unix)] use std::os::unix::fs::FileTypeExt; -pub(crate) fn get_file_type(md: &std::fs::Metadata) -> &str { +pub fn get_file_type(md: &std::fs::Metadata) -> &str { let ft = md.file_type(); let mut file_type = "Unknown"; if ft.is_dir() { diff --git a/crates/nu-engine/src/filesystem/mod.rs b/crates/nu-engine/src/filesystem/mod.rs new file mode 100644 index 0000000000..105462f27f --- /dev/null +++ b/crates/nu-engine/src/filesystem/mod.rs @@ -0,0 +1,4 @@ +pub(crate) mod dir_info; +pub mod filesystem_shell; +pub mod path; +pub(crate) mod utils; diff --git a/crates/nu-cli/src/path.rs b/crates/nu-engine/src/filesystem/path.rs similarity index 100% rename from crates/nu-cli/src/path.rs rename to crates/nu-engine/src/filesystem/path.rs diff --git a/crates/nu-engine/src/filesystem/utils.rs b/crates/nu-engine/src/filesystem/utils.rs new file mode 100644 index 0000000000..17ca009582 --- /dev/null +++ b/crates/nu-engine/src/filesystem/utils.rs @@ -0,0 +1,210 @@ +use crate::filesystem::path::canonicalize; +use nu_errors::ShellError; +use std::path::{Path, PathBuf}; + +#[derive(Default)] +pub struct FileStructure { + pub resources: Vec, +} + +impl FileStructure { + pub fn new() -> FileStructure { + FileStructure { + resources: Vec::::new(), + } + } + + #[allow(dead_code)] + pub fn contains_more_than_one_file(&self) -> bool { + self.resources.len() > 1 + } + + #[allow(dead_code)] + pub fn contains_files(&self) -> bool { + !self.resources.is_empty() + } + + pub fn paths_applying_with( + &mut self, + to: F, + ) -> Result, Box> + where + F: Fn((PathBuf, usize)) -> Result<(PathBuf, PathBuf), Box>, + { + self.resources + .iter() + .map(|f| (PathBuf::from(&f.loc), f.at)) + .map(|f| to(f)) + .collect() + } + + pub fn walk_decorate(&mut self, start_path: &Path) -> Result<(), ShellError> { + self.resources = Vec::::new(); + self.build(start_path, 0)?; + self.resources.sort(); + + Ok(()) + } + + fn build(&mut self, src: &Path, lvl: usize) -> Result<(), ShellError> { + let source = canonicalize(std::env::current_dir()?, src)?; + + if source.is_dir() { + for entry in std::fs::read_dir(src)? { + let entry = entry?; + let path = entry.path(); + + if path.is_dir() { + self.build(&path, lvl + 1)?; + } + + self.resources.push(Res { + loc: path.to_path_buf(), + at: lvl, + }); + } + } else { + self.resources.push(Res { + loc: source, + at: lvl, + }); + } + + Ok(()) + } +} + +#[derive(Debug, Eq, Ord, PartialEq, PartialOrd)] +pub struct Res { + pub at: usize, + pub loc: PathBuf, +} + +impl Res {} + +#[cfg(test)] +mod tests { + use super::{FileStructure, Res}; + use nu_protocol::{TaggedDictBuilder, UntaggedValue, Value, ValueResource, ValueStructure}; + use nu_source::Tag; + use nu_test_support::{fs::Stub::EmptyFile, playground::Playground}; + use std::path::PathBuf; + + fn structured_sample_record(key: &str, value: &str) -> Value { + let mut record = TaggedDictBuilder::new(Tag::unknown()); + record.insert_untagged(key, UntaggedValue::string(value)); + record.into_value() + } + + fn sample_nushell_source_code() -> Value { + /* + src + commands + plugins => "sys.rs" + tests + helpers => "mod.rs" + */ + + let mut src = TaggedDictBuilder::new(Tag::unknown()); + let mut record = TaggedDictBuilder::new(Tag::unknown()); + + record.insert_value("commands", structured_sample_record("plugins", "sys.rs")); + record.insert_value("tests", structured_sample_record("helpers", "mod.rs")); + src.insert_value("src", record.into_value()); + + src.into_value() + } + + #[test] + fn prepares_and_decorates_value_filesystemlike_sources() { + let mut res = ValueStructure::new(); + + res.walk_decorate(&sample_nushell_source_code()) + .expect("Can not decorate values traversal."); + + assert_eq!( + res.resources, + vec![ + ValueResource { + loc: PathBuf::from("src"), + at: 0, + }, + ValueResource { + loc: PathBuf::from("commands"), + at: 1, + }, + ValueResource { + loc: PathBuf::from("tests"), + at: 1, + }, + ValueResource { + loc: PathBuf::from("helpers"), + at: 2, + }, + ValueResource { + loc: PathBuf::from("plugins"), + at: 2, + }, + ] + ); + } + + #[test] + fn recognizes_if_path_exists_in_value_filesystemlike_sources() { + let mut res = ValueStructure::new(); + + res.walk_decorate(&sample_nushell_source_code()) + .expect("Can not decorate values traversal."); + + assert!(res.exists(&PathBuf::from("/"))); + + assert!(res.exists(&PathBuf::from("src/commands/plugins"))); + assert!(res.exists(&PathBuf::from("src/commands"))); + assert!(res.exists(&PathBuf::from("src/tests"))); + assert!(res.exists(&PathBuf::from("src/tests/helpers"))); + assert!(res.exists(&PathBuf::from("src"))); + + assert!(res.exists(&PathBuf::from("/src/commands/plugins"))); + assert!(res.exists(&PathBuf::from("/src/commands"))); + assert!(res.exists(&PathBuf::from("/src/tests"))); + assert!(res.exists(&PathBuf::from("/src/tests/helpers"))); + assert!(res.exists(&PathBuf::from("/src"))); + + assert!(!res.exists(&PathBuf::from("/not_valid"))); + assert!(!res.exists(&PathBuf::from("/src/not_valid"))); + } + + #[test] + fn prepares_and_decorates_filesystem_source_files() { + Playground::setup("file_structure_test", |dirs, sandbox| { + sandbox.with_files(vec![ + EmptyFile("sample.ini"), + EmptyFile("sample.eml"), + EmptyFile("cargo_sample.toml"), + ]); + + let mut res = FileStructure::new(); + + res.walk_decorate(&dirs.test()) + .expect("Can not decorate files traversal."); + + assert_eq!( + res.resources, + vec![ + Res { + loc: dirs.test().join("cargo_sample.toml"), + at: 0 + }, + Res { + loc: dirs.test().join("sample.eml"), + at: 0 + }, + Res { + loc: dirs.test().join("sample.ini"), + at: 0 + } + ] + ); + }) + } +} diff --git a/crates/nu-engine/src/history_path.rs b/crates/nu-engine/src/history_path.rs new file mode 100644 index 0000000000..2fc1529b89 --- /dev/null +++ b/crates/nu-engine/src/history_path.rs @@ -0,0 +1,22 @@ +use nu_data::config::Conf; +use std::path::PathBuf; + +const DEFAULT_LOCATION: &str = "history.txt"; + +pub fn history_path(config: &dyn Conf) -> PathBuf { + let default_path = nu_data::config::user_data() + .map(|mut p| { + p.push(DEFAULT_LOCATION); + p + }) + .unwrap_or_else(|_| PathBuf::from(DEFAULT_LOCATION)); + + config + .var("history-path") + .map_or(default_path.clone(), |custom_path| { + match custom_path.as_string() { + Ok(path) => PathBuf::from(path), + Err(_) => default_path, + } + }) +} diff --git a/crates/nu-engine/src/lib.rs b/crates/nu-engine/src/lib.rs new file mode 100644 index 0000000000..7c9065ff53 --- /dev/null +++ b/crates/nu-engine/src/lib.rs @@ -0,0 +1,39 @@ +mod call_info; +mod command_args; +pub mod deserializer; +pub mod documentation; +mod env; +mod evaluate; +mod evaluation_context; +mod example; +pub mod filesystem; +mod history_path; +mod maybe_text_codec; +pub mod plugin; +pub mod shell; +mod whole_stream_command; + +pub use crate::call_info::UnevaluatedCallInfo; +pub use crate::command_args::{ + CommandArgs, EvaluatedCommandArgs, EvaluatedWholeStreamCommandArgs, RawCommandArgs, +}; +pub use crate::documentation::{generate_docs, get_documentation, get_help}; +pub use crate::env::environment::Env; +pub use crate::env::host::FakeHost; +pub use crate::env::host::Host; +pub use crate::evaluate::block::run_block; +pub use crate::evaluate::evaluator::evaluate_baseline_expr; +pub use crate::evaluate::scope::Scope; +pub use crate::evaluation_context::EvaluationContext; +pub use crate::example::Example; +pub use crate::filesystem::dir_info::{DirBuilder, DirInfo, FileInfo}; +pub use crate::filesystem::filesystem_shell::FilesystemShell; +pub use crate::filesystem::path; +pub use crate::history_path::history_path; +pub use crate::maybe_text_codec::{MaybeTextCodec, StringOrBinary}; +pub use crate::shell::help_shell::{command_dict, HelpShell}; +pub use crate::shell::painter::Painter; +pub use crate::shell::palette::{DefaultPalette, Palette}; +pub use crate::shell::shell_manager::ShellManager; +pub use crate::shell::value_shell::ValueShell; +pub use crate::whole_stream_command::{whole_stream_command, Command, WholeStreamCommand}; diff --git a/crates/nu-cli/src/commands/classified/maybe_text_codec.rs b/crates/nu-engine/src/maybe_text_codec.rs similarity index 99% rename from crates/nu-cli/src/commands/classified/maybe_text_codec.rs rename to crates/nu-engine/src/maybe_text_codec.rs index 8aba39b2e9..3cffef2119 100644 --- a/crates/nu-cli/src/commands/classified/maybe_text_codec.rs +++ b/crates/nu-engine/src/maybe_text_codec.rs @@ -2,7 +2,6 @@ use bytes::{BufMut, Bytes, BytesMut}; use nu_errors::ShellError; -extern crate encoding_rs; use encoding_rs::{CoderResult, Decoder, Encoding, UTF_8}; #[cfg(not(test))] diff --git a/crates/nu-cli/src/plugin.rs b/crates/nu-engine/src/plugin/build_plugin.rs similarity index 95% rename from crates/nu-cli/src/plugin.rs rename to crates/nu-engine/src/plugin/build_plugin.rs index c8dd67bf0a..879544dc94 100644 --- a/crates/nu-cli/src/plugin.rs +++ b/crates/nu-engine/src/plugin/build_plugin.rs @@ -1,4 +1,4 @@ -use crate::commands::classified::plugin::PluginCommandBuilder; +use crate::plugin::run_plugin::PluginCommandBuilder; use log::trace; use nu_errors::ShellError; use nu_plugin::jsonrpc::JsonRpc; @@ -93,7 +93,9 @@ pub fn build_plugin_command( result } -pub fn scan(paths: Vec) -> Result, ShellError> { +pub fn scan( + paths: Vec, +) -> Result, ShellError> { let mut plugins = vec![]; let opts = glob::MatchOptions { @@ -162,7 +164,7 @@ pub fn scan(paths: Vec) -> Result>(); + .collect::>(); plugins.extend(plugs); } diff --git a/crates/nu-engine/src/plugin/mod.rs b/crates/nu-engine/src/plugin/mod.rs new file mode 100644 index 0000000000..1d1650c40c --- /dev/null +++ b/crates/nu-engine/src/plugin/mod.rs @@ -0,0 +1,2 @@ +pub mod build_plugin; +pub(crate) mod run_plugin; diff --git a/crates/nu-cli/src/commands/classified/plugin.rs b/crates/nu-engine/src/plugin/run_plugin.rs similarity index 97% rename from crates/nu-cli/src/commands/classified/plugin.rs rename to crates/nu-engine/src/plugin/run_plugin.rs index 4bc35bd90d..89d2773651 100644 --- a/crates/nu-cli/src/commands/classified/plugin.rs +++ b/crates/nu-engine/src/plugin/run_plugin.rs @@ -1,11 +1,15 @@ -use crate::commands::command::{whole_stream_command, WholeStreamCommand}; -use crate::prelude::*; +use crate::command_args::CommandArgs; +use crate::whole_stream_command::{whole_stream_command, WholeStreamCommand}; +use async_trait::async_trait; use derive_new::new; +use futures::StreamExt; use log::trace; use nu_errors::ShellError; use nu_plugin::jsonrpc::JsonRpc; use nu_protocol::{Primitive, ReturnValue, Signature, UntaggedValue, Value}; +use nu_stream::{OutputStream, ToOutputStream}; use serde::{self, Deserialize, Serialize}; +use std::collections::VecDeque; use std::io::prelude::*; use std::io::BufReader; use std::io::Write; @@ -27,7 +31,7 @@ enum PluginCommand { } impl PluginCommand { - fn command(self) -> Result { + fn command(self) -> Result { match self { PluginCommand::Filter(cmd) => Ok(whole_stream_command(cmd)), PluginCommand::Sink(cmd) => Ok(whole_stream_command(cmd)), @@ -67,7 +71,7 @@ impl PluginCommandBuilder { } } - pub fn build(&self) -> Result { + pub fn build(&self) -> Result { let mode = &self.mode; let name = self.name.clone(); diff --git a/crates/nu-cli/src/shell/help_shell.rs b/crates/nu-engine/src/shell/help_shell.rs similarity index 85% rename from crates/nu-cli/src/shell/help_shell.rs rename to crates/nu-engine/src/shell/help_shell.rs index 2dfac43252..bfb68346e4 100644 --- a/crates/nu-cli/src/shell/help_shell.rs +++ b/crates/nu-engine/src/shell/help_shell.rs @@ -1,24 +1,40 @@ -use crate::commands::cd::CdArgs; -use crate::commands::command::EvaluatedWholeStreamCommandArgs; -use crate::commands::cp::CopyArgs; -use crate::commands::help::command_dict; -use crate::commands::ls::LsArgs; -use crate::commands::mkdir::MkdirArgs; -use crate::commands::move_::mv::Arguments as MvArgs; -use crate::commands::rm::RemoveArgs; -use crate::prelude::*; -use crate::shell::shell::Shell; - -use std::ffi::OsStr; -use std::path::PathBuf; - -use crate::commands::classified::maybe_text_codec::StringOrBinary; +use crate::command_args::EvaluatedWholeStreamCommandArgs; +use crate::evaluate::scope::Scope; +use crate::maybe_text_codec::StringOrBinary; +use crate::shell::shell_args::{CdArgs, CopyArgs, LsArgs, MkdirArgs, MvArgs, RemoveArgs}; +use crate::shell::Shell; +use crate::whole_stream_command::Command; use encoding_rs::Encoding; +use futures::stream::BoxStream; +use nu_data::command::signature_dict; use nu_errors::ShellError; use nu_protocol::{ Primitive, ReturnSuccess, ShellTypeName, TaggedDictBuilder, UntaggedValue, Value, }; use nu_source::Tagged; +use nu_source::{Span, SpannedItem, Tag}; +use nu_stream::OutputStream; +use nu_value_ext::ValueExt; +use std::collections::VecDeque; +use std::ffi::OsStr; +use std::path::PathBuf; +use std::sync::atomic::AtomicBool; +use std::sync::Arc; + +pub fn command_dict(command: Command, tag: impl Into) -> Value { + let tag = tag.into(); + + let mut cmd_dict = TaggedDictBuilder::new(&tag); + + cmd_dict.insert_untagged("name", UntaggedValue::string(command.name())); + + cmd_dict.insert_untagged("type", UntaggedValue::string("Command")); + + cmd_dict.insert_value("signature", signature_dict(command.signature(), tag)); + cmd_dict.insert_untagged("usage", UntaggedValue::string(command.usage())); + + cmd_dict.into_value() +} #[derive(Clone, Debug)] pub struct HelpShell { diff --git a/crates/nu-cli/src/shell/shell.rs b/crates/nu-engine/src/shell/mod.rs similarity index 73% rename from crates/nu-cli/src/shell/shell.rs rename to crates/nu-engine/src/shell/mod.rs index c32312afc9..2b5d367f5b 100644 --- a/crates/nu-cli/src/shell/shell.rs +++ b/crates/nu-engine/src/shell/mod.rs @@ -1,17 +1,22 @@ -use crate::commands::cd::CdArgs; -use crate::commands::classified::maybe_text_codec::StringOrBinary; -use crate::commands::command::EvaluatedWholeStreamCommandArgs; -use crate::commands::cp::CopyArgs; -use crate::commands::ls::LsArgs; -use crate::commands::mkdir::MkdirArgs; -use crate::commands::move_::mv::Arguments as MvArgs; -use crate::commands::rm::RemoveArgs; -use crate::prelude::*; use nu_stream::OutputStream; +use crate::command_args::EvaluatedWholeStreamCommandArgs; +use crate::maybe_text_codec::StringOrBinary; +pub use crate::shell::shell_args::{CdArgs, CopyArgs, LsArgs, MkdirArgs, MvArgs, RemoveArgs}; use encoding_rs::Encoding; +use futures::stream::BoxStream; use nu_errors::ShellError; +use nu_source::{Span, Tag}; use std::path::PathBuf; +use std::sync::atomic::AtomicBool; +use std::sync::Arc; + +pub(crate) mod help_shell; +pub(crate) mod painter; +pub(crate) mod palette; +pub(crate) mod shell_args; +pub(crate) mod shell_manager; +pub(crate) mod value_shell; pub trait Shell: std::fmt::Debug { fn name(&self) -> String; diff --git a/crates/nu-cli/src/shell/painter.rs b/crates/nu-engine/src/shell/painter.rs similarity index 95% rename from crates/nu-cli/src/shell/painter.rs rename to crates/nu-engine/src/shell/painter.rs index 03bab96cbc..d6de9f9510 100644 --- a/crates/nu-cli/src/shell/painter.rs +++ b/crates/nu-engine/src/shell/painter.rs @@ -1,4 +1,4 @@ -use crate::prelude::*; +use crate::evaluate::scope::Scope; use crate::shell::palette::Palette; use ansi_term::{Color, Style}; use nu_parser::ParserScope; @@ -6,6 +6,7 @@ use nu_protocol::hir::FlatShape; use nu_source::Spanned; use std::borrow::Cow; +// FIXME: find a good home, as nu-engine may be too core for styling pub struct Painter { original: Vec, styles: Vec