From 3ef53fe2cdc54f8fd095d67e5b155abe66f14216 Mon Sep 17 00:00:00 2001 From: Michael Angerman Date: Thu, 31 Dec 2020 18:12:16 -0800 Subject: [PATCH] move create_default_context out of cli.rs and into its own mod (#2833) --- crates/nu-cli/src/cli.rs | 247 +---------------- crates/nu-cli/src/commands.rs | 1 + crates/nu-cli/src/commands/default_context.rs | 248 ++++++++++++++++++ crates/nu-cli/src/lib.rs | 4 +- 4 files changed, 253 insertions(+), 247 deletions(-) create mode 100644 crates/nu-cli/src/commands/default_context.rs diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 01041322b9..6f41e847ad 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -1,4 +1,5 @@ use crate::commands::classified::block::run_block; +use crate::commands::default_context::create_default_context; use crate::evaluation_context::EvaluationContext; use crate::prelude::*; use crate::script::{print_err, run_script_standalone}; @@ -56,252 +57,6 @@ pub fn search_paths() -> Vec { search_paths } -pub fn create_default_context(interactive: bool) -> Result> { - let context = EvaluationContext::basic()?; - - { - use crate::commands::*; - - context.add_commands(vec![ - // Fundamentals - whole_stream_command(NuPlugin), - whole_stream_command(Set), - whole_stream_command(SetEnv), - whole_stream_command(Def), - whole_stream_command(Source), - // System/file operations - whole_stream_command(Exec), - whole_stream_command(Pwd), - whole_stream_command(Ls), - whole_stream_command(Du), - whole_stream_command(Cd), - whole_stream_command(Remove), - whole_stream_command(Open), - whole_stream_command(Config), - whole_stream_command(ConfigGet), - whole_stream_command(ConfigSet), - whole_stream_command(ConfigSetInto), - whole_stream_command(ConfigClear), - whole_stream_command(ConfigLoad), - whole_stream_command(ConfigRemove), - whole_stream_command(ConfigPath), - whole_stream_command(Help), - whole_stream_command(History), - whole_stream_command(Save), - whole_stream_command(Touch), - whole_stream_command(Cpy), - whole_stream_command(Date), - whole_stream_command(DateListTimeZone), - whole_stream_command(DateNow), - whole_stream_command(DateToTable), - whole_stream_command(DateToTimeZone), - whole_stream_command(DateFormat), - whole_stream_command(Cal), - whole_stream_command(Mkdir), - whole_stream_command(Mv), - whole_stream_command(Kill), - whole_stream_command(Version), - whole_stream_command(Clear), - whole_stream_command(Describe), - whole_stream_command(Which), - whole_stream_command(Debug), - whole_stream_command(WithEnv), - whole_stream_command(Do), - whole_stream_command(Sleep), - // Statistics - whole_stream_command(Size), - whole_stream_command(Count), - whole_stream_command(Benchmark), - // Metadata - whole_stream_command(Tags), - // Shells - whole_stream_command(Next), - whole_stream_command(Previous), - whole_stream_command(Shells), - whole_stream_command(Enter), - whole_stream_command(Exit), - // Viz - whole_stream_command(Chart), - // Viewers - whole_stream_command(Autoview), - whole_stream_command(Table), - // Text manipulation - whole_stream_command(Hash), - whole_stream_command(HashBase64), - whole_stream_command(Split), - whole_stream_command(SplitColumn), - whole_stream_command(SplitRow), - whole_stream_command(SplitChars), - whole_stream_command(Lines), - whole_stream_command(Echo), - whole_stream_command(Parse), - whole_stream_command(Str), - whole_stream_command(StrToDecimal), - whole_stream_command(StrToInteger), - whole_stream_command(StrDowncase), - whole_stream_command(StrUpcase), - whole_stream_command(StrCapitalize), - whole_stream_command(StrFindReplace), - whole_stream_command(StrFrom), - whole_stream_command(StrSubstring), - whole_stream_command(StrSet), - whole_stream_command(StrToDatetime), - whole_stream_command(StrContains), - whole_stream_command(StrIndexOf), - whole_stream_command(StrTrim), - whole_stream_command(StrTrimLeft), - whole_stream_command(StrTrimRight), - whole_stream_command(StrStartsWith), - whole_stream_command(StrEndsWith), - whole_stream_command(StrCollect), - whole_stream_command(StrLength), - whole_stream_command(StrLPad), - whole_stream_command(StrReverse), - whole_stream_command(StrRPad), - whole_stream_command(StrCamelCase), - whole_stream_command(StrPascalCase), - whole_stream_command(StrKebabCase), - whole_stream_command(StrSnakeCase), - whole_stream_command(StrScreamingSnakeCase), - whole_stream_command(BuildString), - whole_stream_command(Ansi), - whole_stream_command(Char), - // Column manipulation - whole_stream_command(Move), - whole_stream_command(Reject), - whole_stream_command(Select), - whole_stream_command(Get), - whole_stream_command(Update), - whole_stream_command(Insert), - whole_stream_command(IntoInt), - whole_stream_command(SplitBy), - // Row manipulation - whole_stream_command(Reverse), - whole_stream_command(Append), - whole_stream_command(Prepend), - whole_stream_command(SortBy), - whole_stream_command(GroupBy), - whole_stream_command(GroupByDate), - whole_stream_command(First), - whole_stream_command(Last), - whole_stream_command(Every), - whole_stream_command(Nth), - whole_stream_command(Drop), - whole_stream_command(Format), - whole_stream_command(FileSize), - whole_stream_command(Where), - whole_stream_command(If), - whole_stream_command(Compact), - whole_stream_command(Default), - whole_stream_command(Skip), - whole_stream_command(SkipUntil), - whole_stream_command(SkipWhile), - whole_stream_command(Keep), - whole_stream_command(KeepUntil), - whole_stream_command(KeepWhile), - whole_stream_command(Range), - whole_stream_command(Rename), - whole_stream_command(Uniq), - whole_stream_command(Each), - whole_stream_command(EachGroup), - whole_stream_command(EachWindow), - whole_stream_command(Empty), - // Table manipulation - whole_stream_command(Flatten), - whole_stream_command(Move), - whole_stream_command(Merge), - whole_stream_command(Shuffle), - whole_stream_command(Wrap), - whole_stream_command(Pivot), - whole_stream_command(Headers), - whole_stream_command(Reduce), - // Data processing - whole_stream_command(Histogram), - whole_stream_command(Autoenv), - whole_stream_command(AutoenvTrust), - whole_stream_command(AutoenvUnTrust), - whole_stream_command(Math), - whole_stream_command(MathAbs), - whole_stream_command(MathAverage), - whole_stream_command(MathEval), - whole_stream_command(MathMedian), - whole_stream_command(MathMinimum), - whole_stream_command(MathMode), - whole_stream_command(MathMaximum), - whole_stream_command(MathStddev), - whole_stream_command(MathSummation), - whole_stream_command(MathVariance), - whole_stream_command(MathProduct), - whole_stream_command(MathRound), - whole_stream_command(MathFloor), - whole_stream_command(MathCeil), - // File format output - whole_stream_command(To), - whole_stream_command(ToCSV), - whole_stream_command(ToHTML), - whole_stream_command(ToJSON), - whole_stream_command(ToMarkdown), - whole_stream_command(ToTOML), - whole_stream_command(ToTSV), - whole_stream_command(ToURL), - whole_stream_command(ToYAML), - whole_stream_command(ToXML), - // File format input - whole_stream_command(From), - whole_stream_command(FromCSV), - whole_stream_command(FromEML), - whole_stream_command(FromTSV), - whole_stream_command(FromSSV), - whole_stream_command(FromINI), - whole_stream_command(FromJSON), - whole_stream_command(FromODS), - whole_stream_command(FromTOML), - whole_stream_command(FromURL), - whole_stream_command(FromXLSX), - whole_stream_command(FromXML), - whole_stream_command(FromYAML), - whole_stream_command(FromYML), - whole_stream_command(FromIcs), - whole_stream_command(FromVcf), - // "Private" commands (not intended to be accessed directly) - whole_stream_command(RunExternalCommand { interactive }), - // Random value generation - whole_stream_command(Random), - whole_stream_command(RandomBool), - whole_stream_command(RandomDice), - #[cfg(feature = "uuid_crate")] - whole_stream_command(RandomUUID), - whole_stream_command(RandomInteger), - whole_stream_command(RandomDecimal), - whole_stream_command(RandomChars), - // Path - whole_stream_command(PathBasename), - whole_stream_command(PathCommand), - whole_stream_command(PathDirname), - whole_stream_command(PathExists), - whole_stream_command(PathExpand), - whole_stream_command(PathExtension), - whole_stream_command(PathFilestem), - whole_stream_command(PathType), - // Url - whole_stream_command(UrlCommand), - whole_stream_command(UrlScheme), - whole_stream_command(UrlPath), - whole_stream_command(UrlHost), - whole_stream_command(UrlQuery), - whole_stream_command(Seq), - whole_stream_command(SeqDates), - ]); - - #[cfg(feature = "clipboard-cli")] - { - context.add_commands(vec![whole_stream_command(crate::commands::clip::Clip)]); - } - } - - Ok(context) -} - pub async fn run_script_file( file_contents: String, redirect_stdin: bool, diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index 816b51647c..7172a146e1 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -30,6 +30,7 @@ pub(crate) mod date; pub(crate) mod debug; pub(crate) mod def; pub(crate) mod default; +pub(crate) mod default_context; pub(crate) mod describe; pub(crate) mod do_; pub(crate) mod drop; diff --git a/crates/nu-cli/src/commands/default_context.rs b/crates/nu-cli/src/commands/default_context.rs new file mode 100644 index 0000000000..c04425bb45 --- /dev/null +++ b/crates/nu-cli/src/commands/default_context.rs @@ -0,0 +1,248 @@ +use crate::prelude::*; +use std::error::Error; + +pub fn create_default_context(interactive: bool) -> Result> { + let context = EvaluationContext::basic()?; + + { + use crate::commands::*; + + context.add_commands(vec![ + // Fundamentals + whole_stream_command(NuPlugin), + whole_stream_command(Set), + whole_stream_command(SetEnv), + whole_stream_command(Def), + whole_stream_command(Source), + // System/file operations + whole_stream_command(Exec), + whole_stream_command(Pwd), + whole_stream_command(Ls), + whole_stream_command(Du), + whole_stream_command(Cd), + whole_stream_command(Remove), + whole_stream_command(Open), + whole_stream_command(Config), + whole_stream_command(ConfigGet), + whole_stream_command(ConfigSet), + whole_stream_command(ConfigSetInto), + whole_stream_command(ConfigClear), + whole_stream_command(ConfigLoad), + whole_stream_command(ConfigRemove), + whole_stream_command(ConfigPath), + whole_stream_command(Help), + whole_stream_command(History), + whole_stream_command(Save), + whole_stream_command(Touch), + whole_stream_command(Cpy), + whole_stream_command(Date), + whole_stream_command(DateListTimeZone), + whole_stream_command(DateNow), + whole_stream_command(DateToTable), + whole_stream_command(DateToTimeZone), + whole_stream_command(DateFormat), + whole_stream_command(Cal), + whole_stream_command(Mkdir), + whole_stream_command(Mv), + whole_stream_command(Kill), + whole_stream_command(Version), + whole_stream_command(Clear), + whole_stream_command(Describe), + whole_stream_command(Which), + whole_stream_command(Debug), + whole_stream_command(WithEnv), + whole_stream_command(Do), + whole_stream_command(Sleep), + // Statistics + whole_stream_command(Size), + whole_stream_command(Count), + whole_stream_command(Benchmark), + // Metadata + whole_stream_command(Tags), + // Shells + whole_stream_command(Next), + whole_stream_command(Previous), + whole_stream_command(Shells), + whole_stream_command(Enter), + whole_stream_command(Exit), + // Viz + whole_stream_command(Chart), + // Viewers + whole_stream_command(Autoview), + whole_stream_command(Table), + // Text manipulation + whole_stream_command(Hash), + whole_stream_command(HashBase64), + whole_stream_command(Split), + whole_stream_command(SplitColumn), + whole_stream_command(SplitRow), + whole_stream_command(SplitChars), + whole_stream_command(Lines), + whole_stream_command(Echo), + whole_stream_command(Parse), + whole_stream_command(Str), + whole_stream_command(StrToDecimal), + whole_stream_command(StrToInteger), + whole_stream_command(StrDowncase), + whole_stream_command(StrUpcase), + whole_stream_command(StrCapitalize), + whole_stream_command(StrFindReplace), + whole_stream_command(StrFrom), + whole_stream_command(StrSubstring), + whole_stream_command(StrSet), + whole_stream_command(StrToDatetime), + whole_stream_command(StrContains), + whole_stream_command(StrIndexOf), + whole_stream_command(StrTrim), + whole_stream_command(StrTrimLeft), + whole_stream_command(StrTrimRight), + whole_stream_command(StrStartsWith), + whole_stream_command(StrEndsWith), + whole_stream_command(StrCollect), + whole_stream_command(StrLength), + whole_stream_command(StrLPad), + whole_stream_command(StrReverse), + whole_stream_command(StrRPad), + whole_stream_command(StrCamelCase), + whole_stream_command(StrPascalCase), + whole_stream_command(StrKebabCase), + whole_stream_command(StrSnakeCase), + whole_stream_command(StrScreamingSnakeCase), + whole_stream_command(BuildString), + whole_stream_command(Ansi), + whole_stream_command(Char), + // Column manipulation + whole_stream_command(Move), + whole_stream_command(Reject), + whole_stream_command(Select), + whole_stream_command(Get), + whole_stream_command(Update), + whole_stream_command(Insert), + whole_stream_command(IntoInt), + whole_stream_command(SplitBy), + // Row manipulation + whole_stream_command(Reverse), + whole_stream_command(Append), + whole_stream_command(Prepend), + whole_stream_command(SortBy), + whole_stream_command(GroupBy), + whole_stream_command(GroupByDate), + whole_stream_command(First), + whole_stream_command(Last), + whole_stream_command(Every), + whole_stream_command(Nth), + whole_stream_command(Drop), + whole_stream_command(Format), + whole_stream_command(FileSize), + whole_stream_command(Where), + whole_stream_command(If), + whole_stream_command(Compact), + whole_stream_command(Default), + whole_stream_command(Skip), + whole_stream_command(SkipUntil), + whole_stream_command(SkipWhile), + whole_stream_command(Keep), + whole_stream_command(KeepUntil), + whole_stream_command(KeepWhile), + whole_stream_command(Range), + whole_stream_command(Rename), + whole_stream_command(Uniq), + whole_stream_command(Each), + whole_stream_command(EachGroup), + whole_stream_command(EachWindow), + whole_stream_command(Empty), + // Table manipulation + whole_stream_command(Flatten), + whole_stream_command(Move), + whole_stream_command(Merge), + whole_stream_command(Shuffle), + whole_stream_command(Wrap), + whole_stream_command(Pivot), + whole_stream_command(Headers), + whole_stream_command(Reduce), + // Data processing + whole_stream_command(Histogram), + whole_stream_command(Autoenv), + whole_stream_command(AutoenvTrust), + whole_stream_command(AutoenvUnTrust), + whole_stream_command(Math), + whole_stream_command(MathAbs), + whole_stream_command(MathAverage), + whole_stream_command(MathEval), + whole_stream_command(MathMedian), + whole_stream_command(MathMinimum), + whole_stream_command(MathMode), + whole_stream_command(MathMaximum), + whole_stream_command(MathStddev), + whole_stream_command(MathSummation), + whole_stream_command(MathVariance), + whole_stream_command(MathProduct), + whole_stream_command(MathRound), + whole_stream_command(MathFloor), + whole_stream_command(MathCeil), + // File format output + whole_stream_command(To), + whole_stream_command(ToCSV), + whole_stream_command(ToHTML), + whole_stream_command(ToJSON), + whole_stream_command(ToMarkdown), + whole_stream_command(ToTOML), + whole_stream_command(ToTSV), + whole_stream_command(ToURL), + whole_stream_command(ToYAML), + whole_stream_command(ToXML), + // File format input + whole_stream_command(From), + whole_stream_command(FromCSV), + whole_stream_command(FromEML), + whole_stream_command(FromTSV), + whole_stream_command(FromSSV), + whole_stream_command(FromINI), + whole_stream_command(FromJSON), + whole_stream_command(FromODS), + whole_stream_command(FromTOML), + whole_stream_command(FromURL), + whole_stream_command(FromXLSX), + whole_stream_command(FromXML), + whole_stream_command(FromYAML), + whole_stream_command(FromYML), + whole_stream_command(FromIcs), + whole_stream_command(FromVcf), + // "Private" commands (not intended to be accessed directly) + whole_stream_command(RunExternalCommand { interactive }), + // Random value generation + whole_stream_command(Random), + whole_stream_command(RandomBool), + whole_stream_command(RandomDice), + #[cfg(feature = "uuid_crate")] + whole_stream_command(RandomUUID), + whole_stream_command(RandomInteger), + whole_stream_command(RandomDecimal), + whole_stream_command(RandomChars), + // Path + whole_stream_command(PathBasename), + whole_stream_command(PathCommand), + whole_stream_command(PathDirname), + whole_stream_command(PathExists), + whole_stream_command(PathExpand), + whole_stream_command(PathExtension), + whole_stream_command(PathFilestem), + whole_stream_command(PathType), + // Url + whole_stream_command(UrlCommand), + whole_stream_command(UrlScheme), + whole_stream_command(UrlPath), + whole_stream_command(UrlHost), + whole_stream_command(UrlQuery), + whole_stream_command(Seq), + whole_stream_command(SeqDates), + ]); + + #[cfg(feature = "clipboard-cli")] + { + context.add_commands(vec![whole_stream_command(crate::commands::clip::Clip)]); + } + } + + Ok(context) +} diff --git a/crates/nu-cli/src/lib.rs b/crates/nu-cli/src/lib.rs index df64e20355..4d1ec02fc9 100644 --- a/crates/nu-cli/src/lib.rs +++ b/crates/nu-cli/src/lib.rs @@ -41,11 +41,13 @@ mod examples; #[cfg(feature = "rustyline-support")] pub use crate::cli::cli; -pub use crate::cli::{create_default_context, parse_and_eval, register_plugins, run_script_file}; +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;