Remove some clones and improve when autoview reads config (#3285)

This commit is contained in:
Jonathan Turner 2021-04-09 07:47:41 +12:00 committed by GitHub
parent 2880109f31
commit 81160bcefb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 60 additions and 51 deletions

View File

@ -50,13 +50,8 @@ pub fn autoview(context: CommandArgs) -> Result<OutputStream, ShellError> {
let text = context.scope.get_command("textview"); let text = context.scope.get_command("textview");
let table = context.scope.get_command("table"); let table = context.scope.get_command("table");
let pivot_mode = configuration.pivot_mode();
let (mut input_stream, context) = context.split(); let (mut input_stream, context) = context.split();
let term_width = context.host.lock().width();
let color_hm = get_color_config();
if let Some(x) = input_stream.next() { if let Some(x) = input_stream.next() {
match input_stream.next() { match input_stream.next() {
Some(y) => { Some(y) => {
@ -183,9 +178,13 @@ pub fn autoview(context: CommandArgs) -> Result<OutputStream, ShellError> {
} }
Value { Value {
value: UntaggedValue::Row(row), value: UntaggedValue::Row(ref row),
.. ..
} if pivot_mode.is_always() } => {
let pivot_mode = configuration.pivot_mode();
let term_width = context.host.lock().width();
if pivot_mode.is_always()
|| (pivot_mode.is_auto() || (pivot_mode.is_auto()
&& (row && (row
.entries .entries
@ -195,7 +194,7 @@ pub fn autoview(context: CommandArgs) -> Result<OutputStream, ShellError> {
.iter() .iter()
.fold(0usize, |acc, len| acc + len.len()) .fold(0usize, |acc, len| acc + len.len())
+ row.entries.iter().count() * 2) + row.entries.iter().count() * 2)
> term_width) => > term_width)
{ {
let mut entries = vec![]; let mut entries = vec![];
for (key, value) in row.entries.iter() { for (key, value) in row.entries.iter() {
@ -214,10 +213,22 @@ pub fn autoview(context: CommandArgs) -> Result<OutputStream, ShellError> {
]); ]);
} }
let color_hm = get_color_config();
let table = let table =
nu_table::Table::new(vec![], entries, nu_table::Theme::compact()); nu_table::Table::new(vec![], entries, nu_table::Theme::compact());
println!("{}", nu_table::draw_table(&table, term_width, &color_hm)); println!("{}", nu_table::draw_table(&table, term_width, &color_hm));
} else if let Some(table) = table {
let mut stream = VecDeque::new();
stream.push_back(x);
let command_args =
create_default_command_args(&context).with_input(stream);
let result = table.run(command_args)?;
let _ = result.collect::<Vec<_>>();
} else {
out!("{:?}", row);
}
} }
Value { Value {
value: UntaggedValue::Primitive(Primitive::Nothing), value: UntaggedValue::Primitive(Primitive::Nothing),

View File

@ -76,7 +76,7 @@ fn if_command(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
} }
match condition.block.block[0].pipelines.get(0) { match condition.block.block[0].pipelines.get(0) {
Some(item) => match item.list.get(0) { Some(item) => match item.list.get(0) {
Some(ClassifiedCommand::Expr(expr)) => expr.clone(), Some(ClassifiedCommand::Expr(expr)) => expr,
_ => { _ => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Expected a condition", "Expected a condition",

View File

@ -55,7 +55,7 @@ pub fn letcmd(args: CommandArgs) -> Result<OutputStream, ShellError> {
} }
match rhs.block.block[0].pipelines.get(0) { match rhs.block.block[0].pipelines.get(0) {
Some(item) => match item.list.get(0) { Some(item) => match item.list.get(0) {
Some(ClassifiedCommand::Expr(expr)) => (expr.clone(), rhs.captured.clone()), Some(ClassifiedCommand::Expr(expr)) => (expr, &rhs.captured),
_ => { _ => {
return Err(ShellError::labeled_error( return Err(ShellError::labeled_error(
"Expected a value", "Expected a value",

View File

@ -6,7 +6,7 @@ use log::trace;
use nu_errors::{ArgumentError, ShellError}; use nu_errors::{ArgumentError, ShellError};
use nu_protocol::did_you_mean; use nu_protocol::did_you_mean;
use nu_protocol::{ use nu_protocol::{
hir::{self, CapturedBlock, Expression, ExternalRedirection, RangeOperator, SpannedExpression}, hir::{self, CapturedBlock, Expression, RangeOperator, SpannedExpression},
Dictionary, Dictionary,
}; };
use nu_protocol::{ use nu_protocol::{
@ -154,11 +154,8 @@ pub fn evaluate_baseline_expr(
} }
} }
let mut block = block.clone();
block.infer_params();
Ok( Ok(
UntaggedValue::Block(Box::new(CapturedBlock::new(block, captured))) UntaggedValue::Block(Box::new(CapturedBlock::new(block.clone(), captured)))
.into_value(&tag), .into_value(&tag),
) )
} }
@ -276,9 +273,6 @@ fn evaluate_invocation(block: &hir::Block, ctx: &EvaluationContext) -> Result<Va
None => InputStream::empty(), None => InputStream::empty(),
}; };
let mut block = block.clone();
block.set_redirect(ExternalRedirection::Stdout);
let result = run_block(&block, ctx, input)?; let result = run_block(&block, ctx, input)?;
let output = result.into_vec(); let output = result.into_vec();

View File

@ -425,9 +425,11 @@ fn parse_invocation(
}; };
scope.enter_scope(); scope.enter_scope();
let (classified_block, err) = classify_block(&lite_block, scope); let (mut classified_block, err) = classify_block(&lite_block, scope);
scope.exit_scope(); scope.exit_scope();
classified_block.set_redirect(ExternalRedirection::Stdout);
( (
SpannedExpression::new(Expression::Invocation(classified_block), lite_arg.span), SpannedExpression::new(Expression::Invocation(classified_block), lite_arg.span),
err, err,
@ -2044,6 +2046,7 @@ pub fn classify_block(
output.definitions.insert(name, definition.clone()); output.definitions.insert(name, definition.clone());
} }
} }
output.infer_params();
(output, error) (output, error)
} }

View File

@ -22,8 +22,9 @@ impl OutputStream {
} }
pub fn empty() -> OutputStream { pub fn empty() -> OutputStream {
let v: VecDeque<ReturnValue> = VecDeque::new(); OutputStream {
v.into() values: Box::new(std::iter::empty()),
}
} }
pub fn one(item: impl Into<ReturnValue>) -> OutputStream { pub fn one(item: impl Into<ReturnValue>) -> OutputStream {