mirror of
https://github.com/nushell/nushell.git
synced 2025-08-09 23:37:48 +02:00
Runnable contexts move. (#3283)
* Engine extract first steps. * Don't depend on ShellManager.
This commit is contained in:
committed by
GitHub
parent
09a1f5acb9
commit
2880109f31
@ -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();
|
||||
|
||||
|
@ -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> {
|
||||
|
@ -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> {
|
||||
|
@ -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,
|
||||
|
@ -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> {
|
||||
|
@ -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> {
|
||||
|
@ -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> {
|
||||
|
@ -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> {
|
||||
|
@ -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> {
|
||||
|
@ -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> {
|
||||
|
@ -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,
|
||||
|
Reference in New Issue
Block a user