Runnable contexts move. (#3283)

* Engine extract first steps.

* Don't depend on ShellManager.
This commit is contained in:
Andrés N. Robalino 2021-04-08 13:51:12 -05:00 committed by GitHub
parent 09a1f5acb9
commit 2880109f31
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 58 additions and 106 deletions

View file

@ -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<OutputStream, ShellError> {
autoview(RunnableContext::from_command_args(args))
autoview(args)
}
fn examples(&self) -> Vec<Example> {
@ -45,41 +43,17 @@ impl WholeStreamCommand for Command {
}
}
pub struct RunnableContextWithoutInput {
pub shell_manager: ShellManager,
pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>,
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
pub ctrl_c: Arc<AtomicBool>,
pub configs: Arc<Mutex<ConfigHolder>>,
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<OutputStream, ShellError> {
pub fn autoview(context: CommandArgs) -> Result<OutputStream, ShellError> {
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();

View file

@ -28,7 +28,7 @@ impl WholeStreamCommand for SubCommand {
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), average)
run_with_function(args, average)
}
fn examples(&self) -> Vec<Example> {

View file

@ -21,12 +21,9 @@ impl WholeStreamCommand for SubCommand {
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
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<Example> {

View file

@ -21,8 +21,10 @@ impl WholeStreamCommand for SubCommand {
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let input = args.input;
run_with_numerical_functions_on_stream(
RunnableContext::from_command_args(args),
input,
floor_big_int,
floor_big_decimal,
floor_default,

View file

@ -21,7 +21,7 @@ impl WholeStreamCommand for SubCommand {
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), maximum)
run_with_function(args, maximum)
}
fn examples(&self) -> Vec<Example> {

View file

@ -25,7 +25,7 @@ impl WholeStreamCommand for SubCommand {
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), median)
run_with_function(args, median)
}
fn examples(&self) -> Vec<Example> {

View file

@ -21,7 +21,7 @@ impl WholeStreamCommand for SubCommand {
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), minimum)
run_with_function(args, minimum)
}
fn examples(&self) -> Vec<Example> {

View file

@ -21,7 +21,7 @@ impl WholeStreamCommand for SubCommand {
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), mode)
run_with_function(args, mode)
}
fn examples(&self) -> Vec<Example> {

View file

@ -21,7 +21,7 @@ impl WholeStreamCommand for SubCommand {
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), product)
run_with_function(args, product)
}
fn examples(&self) -> Vec<Example> {

View file

@ -22,7 +22,7 @@ impl WholeStreamCommand for SubCommand {
}
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
run_with_function(RunnableContext::from_command_args(args), summation)
run_with_function(args, summation)
}
fn examples(&self) -> Vec<Example> {

View file

@ -7,11 +7,16 @@ use indexmap::map::IndexMap;
pub type MathFunction = fn(values: &[Value], tag: &Tag) -> Result<Value, ShellError>;
pub fn run_with_function(
RunnableContext {
mut input, name, ..
}: RunnableContext,
args: impl Into<RunnableContext>,
mf: MathFunction,
) -> Result<OutputStream, ShellError> {
let RunnableContext {
mut input,
call_info,
..
} = args.into();
let name = call_info.name_tag;
let values: Vec<Value> = 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,

View file

@ -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};

View file

@ -31,6 +31,18 @@ pub struct CommandArgs {
pub input: InputStream,
}
pub type RunnableContext = CommandArgs;
pub struct RunnableContextWithoutInput {
pub shell_manager: ShellManager,
pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>,
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
pub ctrl_c: Arc<AtomicBool>,
pub configs: Arc<Mutex<ConfigHolder>>,
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();

View file

@ -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;

View file

@ -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<Mutex<Box<dyn Host>>>,
pub ctrl_c: Arc<AtomicBool>,
pub configs: Arc<Mutex<ConfigHolder>>,
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
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<Command> {
self.scope.get_command(name)
}
}