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,41 +178,57 @@ pub fn autoview(context: CommandArgs) -> Result<OutputStream, ShellError> {
} }
Value { Value {
value: UntaggedValue::Row(row), value: UntaggedValue::Row(ref row),
.. ..
} if pivot_mode.is_always() } => {
|| (pivot_mode.is_auto() let pivot_mode = configuration.pivot_mode();
&& (row
.entries let term_width = context.host.lock().width();
.iter() if pivot_mode.is_always()
.map(|(_, v)| v.convert_to_string()) || (pivot_mode.is_auto()
.collect::<Vec<_>>() && (row
.iter() .entries
.fold(0usize, |acc, len| acc + len.len()) .iter()
+ row.entries.iter().count() * 2) .map(|(_, v)| v.convert_to_string())
> term_width) => .collect::<Vec<_>>()
{ .iter()
let mut entries = vec![]; .fold(0usize, |acc, len| acc + len.len())
for (key, value) in row.entries.iter() { + row.entries.iter().count() * 2)
entries.push(vec![ > term_width)
nu_table::StyledString::new( {
key.to_string(), let mut entries = vec![];
TextStyle::new() for (key, value) in row.entries.iter() {
.alignment(nu_table::Alignment::Left) entries.push(vec![
.fg(nu_ansi_term::Color::Green) nu_table::StyledString::new(
.bold(Some(true)), key.to_string(),
), TextStyle::new()
nu_table::StyledString::new( .alignment(nu_table::Alignment::Left)
format_leaf(value).plain_string(100_000), .fg(nu_ansi_term::Color::Green)
nu_table::TextStyle::basic_left(), .bold(Some(true)),
), ),
]); nu_table::StyledString::new(
format_leaf(value).plain_string(100_000),
nu_table::TextStyle::basic_left(),
),
]);
}
let color_hm = get_color_config();
let table =
nu_table::Table::new(vec![], entries, nu_table::Theme::compact());
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);
} }
let table =
nu_table::Table::new(vec![], entries, nu_table::Theme::compact());
println!("{}", nu_table::draw_table(&table, term_width, &color_hm));
} }
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 {