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 table = context.scope.get_command("table");
let pivot_mode = configuration.pivot_mode();
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() {
match input_stream.next() {
Some(y) => {
@ -183,41 +178,57 @@ pub fn autoview(context: CommandArgs) -> Result<OutputStream, ShellError> {
}
Value {
value: UntaggedValue::Row(row),
value: UntaggedValue::Row(ref row),
..
} if pivot_mode.is_always()
|| (pivot_mode.is_auto()
&& (row
.entries
.iter()
.map(|(_, v)| v.convert_to_string())
.collect::<Vec<_>>()
.iter()
.fold(0usize, |acc, len| acc + len.len())
+ row.entries.iter().count() * 2)
> term_width) =>
{
let mut entries = vec![];
for (key, value) in row.entries.iter() {
entries.push(vec![
nu_table::StyledString::new(
key.to_string(),
TextStyle::new()
.alignment(nu_table::Alignment::Left)
.fg(nu_ansi_term::Color::Green)
.bold(Some(true)),
),
nu_table::StyledString::new(
format_leaf(value).plain_string(100_000),
nu_table::TextStyle::basic_left(),
),
]);
} => {
let pivot_mode = configuration.pivot_mode();
let term_width = context.host.lock().width();
if pivot_mode.is_always()
|| (pivot_mode.is_auto()
&& (row
.entries
.iter()
.map(|(_, v)| v.convert_to_string())
.collect::<Vec<_>>()
.iter()
.fold(0usize, |acc, len| acc + len.len())
+ row.entries.iter().count() * 2)
> term_width)
{
let mut entries = vec![];
for (key, value) in row.entries.iter() {
entries.push(vec![
nu_table::StyledString::new(
key.to_string(),
TextStyle::new()
.alignment(nu_table::Alignment::Left)
.fg(nu_ansi_term::Color::Green)
.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: 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) {
Some(item) => match item.list.get(0) {
Some(ClassifiedCommand::Expr(expr)) => expr.clone(),
Some(ClassifiedCommand::Expr(expr)) => expr,
_ => {
return Err(ShellError::labeled_error(
"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) {
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(
"Expected a value",

View File

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

View File

@ -425,9 +425,11 @@ fn parse_invocation(
};
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();
classified_block.set_redirect(ExternalRedirection::Stdout);
(
SpannedExpression::new(Expression::Invocation(classified_block), lite_arg.span),
err,
@ -2044,6 +2046,7 @@ pub fn classify_block(
output.definitions.insert(name, definition.clone());
}
}
output.infer_params();
(output, error)
}

View File

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