diff --git a/crates/nu-command/src/commands/autoview/command.rs b/crates/nu-command/src/commands/autoview/command.rs index ddb24c98f4..1bc3570739 100644 --- a/crates/nu-command/src/commands/autoview/command.rs +++ b/crates/nu-command/src/commands/autoview/command.rs @@ -2,13 +2,11 @@ use crate::commands::autoview::options::{ConfigExtensions, NuConfig as AutoViewC use crate::prelude::*; use crate::primitive::get_color_config; use nu_data::value::format_leaf; -use nu_engine::{ConfigHolder, RunnableContext, UnevaluatedCallInfo, WholeStreamCommand}; +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}; use nu_table::TextStyle; -use parking_lot::Mutex; -use std::sync::atomic::AtomicBool; pub struct Command; @@ -26,7 +24,7 @@ impl WholeStreamCommand for Command { } fn run(&self, args: CommandArgs) -> Result { - autoview(RunnableContext::from_command_args(args)) + autoview(args) } fn examples(&self) -> Vec { @@ -45,41 +43,17 @@ impl WholeStreamCommand for Command { } } -pub struct RunnableContextWithoutInput { - pub shell_manager: ShellManager, - pub host: Arc>>, - pub current_errors: Arc>>, - pub ctrl_c: Arc, - pub configs: Arc>, - pub scope: Scope, - pub name: Tag, -} - -impl RunnableContextWithoutInput { - pub fn convert(context: RunnableContext) -> (InputStream, RunnableContextWithoutInput) { - let new_context = RunnableContextWithoutInput { - shell_manager: context.shell_manager, - host: context.host, - ctrl_c: context.ctrl_c, - configs: context.configs, - current_errors: context.current_errors, - scope: context.scope, - name: context.name, - }; - (context.input, new_context) - } -} - -pub fn autoview(context: RunnableContext) -> Result { +pub fn autoview(context: CommandArgs) -> Result { let configuration = AutoViewConfiguration::new(); - let binary = context.get_command("binaryview"); - let text = context.get_command("textview"); - let table = context.get_command("table"); + let binary = context.scope.get_command("binaryview"); + let text = context.scope.get_command("textview"); + let table = context.scope.get_command("table"); let pivot_mode = configuration.pivot_mode(); - let (mut input_stream, context) = RunnableContextWithoutInput::convert(context); + let (mut input_stream, context) = context.split(); + let term_width = context.host.lock().width(); let color_hm = get_color_config(); diff --git a/crates/nu-command/src/commands/math/avg.rs b/crates/nu-command/src/commands/math/avg.rs index 3617d69c45..13f7ccd74c 100644 --- a/crates/nu-command/src/commands/math/avg.rs +++ b/crates/nu-command/src/commands/math/avg.rs @@ -28,7 +28,7 @@ impl WholeStreamCommand for SubCommand { } fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), average) + run_with_function(args, average) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/ceil.rs b/crates/nu-command/src/commands/math/ceil.rs index 27510ecb17..c981592a50 100644 --- a/crates/nu-command/src/commands/math/ceil.rs +++ b/crates/nu-command/src/commands/math/ceil.rs @@ -21,12 +21,9 @@ impl WholeStreamCommand for SubCommand { } fn run(&self, args: CommandArgs) -> Result { - run_with_numerical_functions_on_stream( - RunnableContext::from_command_args(args), - ceil_big_int, - ceil_big_decimal, - ceil_default, - ) + let input = args.input; + + run_with_numerical_functions_on_stream(input, ceil_big_int, ceil_big_decimal, ceil_default) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/floor.rs b/crates/nu-command/src/commands/math/floor.rs index e5dd027d48..d5da9f6a05 100644 --- a/crates/nu-command/src/commands/math/floor.rs +++ b/crates/nu-command/src/commands/math/floor.rs @@ -21,8 +21,10 @@ impl WholeStreamCommand for SubCommand { } fn run(&self, args: CommandArgs) -> Result { + let input = args.input; + run_with_numerical_functions_on_stream( - RunnableContext::from_command_args(args), + input, floor_big_int, floor_big_decimal, floor_default, diff --git a/crates/nu-command/src/commands/math/max.rs b/crates/nu-command/src/commands/math/max.rs index 0ce405ea79..0facfafcc0 100644 --- a/crates/nu-command/src/commands/math/max.rs +++ b/crates/nu-command/src/commands/math/max.rs @@ -21,7 +21,7 @@ impl WholeStreamCommand for SubCommand { } fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), maximum) + run_with_function(args, maximum) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/median.rs b/crates/nu-command/src/commands/math/median.rs index 78c3ae98f9..de7a4a9b76 100644 --- a/crates/nu-command/src/commands/math/median.rs +++ b/crates/nu-command/src/commands/math/median.rs @@ -25,7 +25,7 @@ impl WholeStreamCommand for SubCommand { } fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), median) + run_with_function(args, median) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/min.rs b/crates/nu-command/src/commands/math/min.rs index 71918c5827..2ed64a0c44 100644 --- a/crates/nu-command/src/commands/math/min.rs +++ b/crates/nu-command/src/commands/math/min.rs @@ -21,7 +21,7 @@ impl WholeStreamCommand for SubCommand { } fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), minimum) + run_with_function(args, minimum) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/mode.rs b/crates/nu-command/src/commands/math/mode.rs index e765b2649b..112808ebf2 100644 --- a/crates/nu-command/src/commands/math/mode.rs +++ b/crates/nu-command/src/commands/math/mode.rs @@ -21,7 +21,7 @@ impl WholeStreamCommand for SubCommand { } fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), mode) + run_with_function(args, mode) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/product.rs b/crates/nu-command/src/commands/math/product.rs index 66773813cb..62decd9ba4 100644 --- a/crates/nu-command/src/commands/math/product.rs +++ b/crates/nu-command/src/commands/math/product.rs @@ -21,7 +21,7 @@ impl WholeStreamCommand for SubCommand { } fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), product) + run_with_function(args, product) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/sum.rs b/crates/nu-command/src/commands/math/sum.rs index c94a4b71c2..0f09a19831 100644 --- a/crates/nu-command/src/commands/math/sum.rs +++ b/crates/nu-command/src/commands/math/sum.rs @@ -22,7 +22,7 @@ impl WholeStreamCommand for SubCommand { } fn run(&self, args: CommandArgs) -> Result { - run_with_function(RunnableContext::from_command_args(args), summation) + run_with_function(args, summation) } fn examples(&self) -> Vec { diff --git a/crates/nu-command/src/commands/math/utils.rs b/crates/nu-command/src/commands/math/utils.rs index ab53854c10..e9108d5878 100644 --- a/crates/nu-command/src/commands/math/utils.rs +++ b/crates/nu-command/src/commands/math/utils.rs @@ -7,11 +7,16 @@ use indexmap::map::IndexMap; pub type MathFunction = fn(values: &[Value], tag: &Tag) -> Result; pub fn run_with_function( - RunnableContext { - mut input, name, .. - }: RunnableContext, + args: impl Into, mf: MathFunction, ) -> Result { + let RunnableContext { + mut input, + call_info, + .. + } = args.into(); + let name = call_info.name_tag; + let values: Vec = input.drain_vec(); let res = calculate(&values, &name, mf); @@ -38,7 +43,7 @@ pub type DecimalFunction = fn(val: BigDecimal) -> Value; pub type DefaultFunction = fn(val: UntaggedValue) -> Value; pub fn run_with_numerical_functions_on_stream( - RunnableContext { input, .. }: RunnableContext, + input: InputStream, int_function: IntFunction, decimal_function: DecimalFunction, default_function: DefaultFunction, diff --git a/crates/nu-command/src/prelude.rs b/crates/nu-command/src/prelude.rs index 52017bb81a..daa2d399cb 100644 --- a/crates/nu-command/src/prelude.rs +++ b/crates/nu-command/src/prelude.rs @@ -30,9 +30,8 @@ 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::RunnableContext; -pub(crate) use nu_engine::ShellManager; pub(crate) use nu_engine::{get_full_help, CommandArgs, Scope, WholeStreamCommand}; +pub(crate) use nu_engine::{RunnableContext, RunnableContextWithoutInput}; pub(crate) use nu_parser::ParserScope; pub(crate) use nu_protocol::{out, row}; pub(crate) use nu_source::{AnchorLocation, PrettyDebug, Span, SpannedItem, Tag, TaggedItem}; diff --git a/crates/nu-engine/src/command_args.rs b/crates/nu-engine/src/command_args.rs index 9101604cb7..88b546482a 100644 --- a/crates/nu-engine/src/command_args.rs +++ b/crates/nu-engine/src/command_args.rs @@ -31,6 +31,18 @@ pub struct CommandArgs { pub input: InputStream, } +pub type RunnableContext = CommandArgs; + +pub struct RunnableContextWithoutInput { + pub shell_manager: ShellManager, + pub host: Arc>>, + pub current_errors: Arc>>, + pub ctrl_c: Arc, + pub configs: Arc>, + pub scope: Scope, + pub name: Tag, +} + #[derive(Getters, Clone)] #[get = "pub"] pub struct RawCommandArgs { @@ -86,6 +98,20 @@ impl CommandArgs { )) } + pub fn split(self) -> (InputStream, RunnableContextWithoutInput) { + let new_context = RunnableContextWithoutInput { + shell_manager: self.shell_manager, + host: self.host, + ctrl_c: self.ctrl_c, + configs: self.configs, + current_errors: self.current_errors, + scope: self.scope, + name: self.call_info.name_tag, + }; + + (self.input, new_context) + } + pub fn process<'de, T: Deserialize<'de>>(self) -> Result<(T, InputStream), ShellError> { let args = self.evaluate_once()?; let call_info = args.call_info.clone(); diff --git a/crates/nu-engine/src/lib.rs b/crates/nu-engine/src/lib.rs index b66fe1408f..0ec3a07a2b 100644 --- a/crates/nu-engine/src/lib.rs +++ b/crates/nu-engine/src/lib.rs @@ -14,7 +14,6 @@ mod from_value; mod maybe_text_codec; pub mod plugin; mod print; -mod runnable_context; pub mod script; pub mod shell; mod whole_stream_command; @@ -24,6 +23,7 @@ pub use crate::basic_shell_manager::basic_shell_manager; pub use crate::call_info::UnevaluatedCallInfo; pub use crate::command_args::{ CommandArgs, EvaluatedCommandArgs, EvaluatedWholeStreamCommandArgs, RawCommandArgs, + RunnableContext, RunnableContextWithoutInput, }; pub use crate::config_holder::ConfigHolder; pub use crate::documentation::{generate_docs, get_brief_help, get_documentation, get_full_help}; @@ -40,7 +40,6 @@ pub use crate::filesystem::path; pub use crate::from_value::FromValue; pub use crate::maybe_text_codec::{BufCodecReader, MaybeTextCodec, StringOrBinary}; pub use crate::print::maybe_print_errors; -pub use crate::runnable_context::RunnableContext; pub use crate::shell::painter::Painter; pub use crate::shell::palette::{DefaultPalette, Palette}; pub use crate::shell::shell_manager::ShellManager; diff --git a/crates/nu-engine/src/runnable_context.rs b/crates/nu-engine/src/runnable_context.rs deleted file mode 100644 index 5db935888b..0000000000 --- a/crates/nu-engine/src/runnable_context.rs +++ /dev/null @@ -1,50 +0,0 @@ -use crate::{Command, CommandArgs, EvaluationContext}; -use crate::{ConfigHolder, Host, Scope, ShellManager}; -use nu_errors::ShellError; -use nu_source::Tag; -use nu_stream::InputStream; -use parking_lot::Mutex; -use std::sync::{atomic::AtomicBool, Arc}; - -pub struct RunnableContext { - pub input: InputStream, - pub shell_manager: ShellManager, - pub host: Arc>>, - pub ctrl_c: Arc, - pub configs: Arc>, - pub current_errors: Arc>>, - pub scope: Scope, - pub name: Tag, -} - -impl RunnableContext { - pub fn from_command_args(args: CommandArgs) -> Self { - RunnableContext { - input: args.input, - scope: args.scope.clone(), - shell_manager: args.shell_manager, - host: args.host, - ctrl_c: args.ctrl_c, - configs: args.configs, - current_errors: args.current_errors, - name: args.call_info.name_tag, - } - } - - pub fn from_evaluation_context(input: InputStream, ctx: &EvaluationContext) -> Self { - RunnableContext { - input, - shell_manager: ctx.shell_manager.clone(), - host: ctx.host.clone(), - ctrl_c: ctx.ctrl_c.clone(), - configs: ctx.configs.clone(), - current_errors: ctx.current_errors.clone(), - scope: ctx.scope.clone(), - name: Tag::unknown(), - } - } - - pub fn get_command(&self, name: &str) -> Option { - self.scope.get_command(name) - } -}