mirror of
https://github.com/nushell/nushell
synced 2024-12-28 14:03:09 +00:00
commands to engine p (#3417)
* commands to engine p * Clippy suggestion * Clippy suggestion
This commit is contained in:
parent
3aa00b78f9
commit
0905a2c3a2
6 changed files with 88 additions and 107 deletions
|
@ -2,16 +2,11 @@ use crate::prelude::*;
|
|||
use nu_data::base::select_fields;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape};
|
||||
use nu_protocol::{Signature, SyntaxShape};
|
||||
use nu_source::Tagged;
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Arguments {
|
||||
columns: Option<Tagged<u64>>,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"drop column"
|
||||
|
@ -29,7 +24,7 @@ impl WholeStreamCommand for SubCommand {
|
|||
"Remove the last number of columns. If you want to remove columns by name, try 'reject'."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
drop(args)
|
||||
}
|
||||
|
||||
|
@ -47,8 +42,9 @@ impl WholeStreamCommand for SubCommand {
|
|||
}
|
||||
}
|
||||
|
||||
fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
let (Arguments { columns }, input) = args.process()?;
|
||||
fn drop(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once()?;
|
||||
let columns: Option<Tagged<u64>> = args.opt(0)?;
|
||||
|
||||
let to_drop = if let Some(quantity) = columns {
|
||||
*quantity as usize
|
||||
|
@ -56,7 +52,8 @@ fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
|||
1
|
||||
};
|
||||
|
||||
Ok(input
|
||||
Ok(args
|
||||
.input
|
||||
.map(move |item| {
|
||||
let headers = item.data_descriptors();
|
||||
|
||||
|
@ -66,10 +63,9 @@ fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
|||
n => &headers[..n - to_drop],
|
||||
};
|
||||
|
||||
select_fields(&item, descs, item.tag())
|
||||
Ok(select_fields(&item, descs, item.tag()))
|
||||
})
|
||||
.map(ReturnSuccess::value)
|
||||
.to_action_stream())
|
||||
.to_input_stream())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
|
@ -6,11 +6,6 @@ use nu_source::Tagged;
|
|||
|
||||
pub struct Command;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Arguments {
|
||||
rows: Option<Tagged<u64>>,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for Command {
|
||||
fn name(&self) -> &str {
|
||||
"drop"
|
||||
|
@ -28,7 +23,7 @@ impl WholeStreamCommand for Command {
|
|||
"Remove the last number of rows or columns."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
drop(args)
|
||||
}
|
||||
|
||||
|
@ -51,9 +46,10 @@ impl WholeStreamCommand for Command {
|
|||
}
|
||||
}
|
||||
|
||||
fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
let (Arguments { rows }, input) = args.process()?;
|
||||
let v: Vec<_> = input.into_vec();
|
||||
fn drop(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once()?;
|
||||
let rows: Option<Tagged<u64>> = args.opt(0)?;
|
||||
let v: Vec<_> = args.input.into_vec();
|
||||
|
||||
let rows_to_drop = if let Some(quantity) = rows {
|
||||
*quantity as usize
|
||||
|
@ -62,7 +58,7 @@ fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
|||
};
|
||||
|
||||
Ok(if rows_to_drop == 0 {
|
||||
v.into_iter().to_action_stream()
|
||||
v.into_iter().map(Ok).to_input_stream()
|
||||
} else {
|
||||
let k = if v.len() < rows_to_drop {
|
||||
0
|
||||
|
@ -70,8 +66,8 @@ fn drop(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
|||
v.len() - rows_to_drop
|
||||
};
|
||||
|
||||
let iter = v.into_iter().take(k);
|
||||
let iter = v.into_iter().map(Ok).take(k);
|
||||
|
||||
iter.to_action_stream()
|
||||
iter.to_input_stream()
|
||||
})
|
||||
}
|
||||
|
|
|
@ -7,17 +7,9 @@ use nu_protocol::{
|
|||
Signature, SyntaxShape, UntaggedValue, Value,
|
||||
};
|
||||
use nu_source::Tagged;
|
||||
use serde::Deserialize;
|
||||
|
||||
pub struct EachGroup;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct EachGroupArgs {
|
||||
group_size: Tagged<usize>,
|
||||
block: CapturedBlock,
|
||||
//numbered: Tagged<bool>,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for EachGroup {
|
||||
fn name(&self) -> &str {
|
||||
"each group"
|
||||
|
@ -45,21 +37,24 @@ impl WholeStreamCommand for EachGroup {
|
|||
}]
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, raw_args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn run(&self, raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let context = Arc::new(EvaluationContext::from_args(&raw_args));
|
||||
let external_redirection = raw_args.call_info.args.external_redirection;
|
||||
let (each_args, input): (EachGroupArgs, _) = raw_args.process()?;
|
||||
let block = Arc::new(Box::new(each_args.block));
|
||||
let args = raw_args.evaluate_once()?;
|
||||
|
||||
let group_size: Tagged<usize> = args.req(0)?;
|
||||
let block: CapturedBlock = args.req(1)?;
|
||||
let block = Arc::new(Box::new(block));
|
||||
|
||||
let each_group_iterator = EachGroupIterator {
|
||||
block,
|
||||
context,
|
||||
group_size: each_args.group_size.item,
|
||||
input,
|
||||
group_size: group_size.item,
|
||||
input: args.input,
|
||||
external_redirection,
|
||||
};
|
||||
|
||||
Ok(each_group_iterator.flatten().to_action_stream())
|
||||
Ok(each_group_iterator.flatten().map(Ok).to_input_stream())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -5,17 +5,9 @@ use nu_engine::WholeStreamCommand;
|
|||
use nu_errors::ShellError;
|
||||
use nu_protocol::{hir::CapturedBlock, Primitive, Signature, SyntaxShape, UntaggedValue};
|
||||
use nu_source::Tagged;
|
||||
use serde::Deserialize;
|
||||
|
||||
pub struct EachWindow;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct EachWindowArgs {
|
||||
window_size: Tagged<usize>,
|
||||
block: CapturedBlock,
|
||||
stride: Option<Tagged<usize>>,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for EachWindow {
|
||||
fn name(&self) -> &str {
|
||||
"each window"
|
||||
|
@ -49,22 +41,29 @@ impl WholeStreamCommand for EachWindow {
|
|||
}]
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, raw_args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn run(&self, raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let context = Arc::new(EvaluationContext::from_args(&raw_args));
|
||||
let external_redirection = raw_args.call_info.args.external_redirection;
|
||||
let (each_args, mut input): (EachWindowArgs, _) = raw_args.process()?;
|
||||
let block = Arc::new(Box::new(each_args.block));
|
||||
|
||||
let mut window: Vec<_> = input
|
||||
let mut args = raw_args.evaluate_once()?;
|
||||
let window_size: Tagged<usize> = args.req(0)?;
|
||||
let block: CapturedBlock = args.req(1)?;
|
||||
let stride: Option<Tagged<usize>> = args.get_flag("stride")?;
|
||||
|
||||
let block = Arc::new(Box::new(block));
|
||||
|
||||
let mut window: Vec<_> = args
|
||||
.input
|
||||
.by_ref()
|
||||
.take(*each_args.window_size - 1)
|
||||
.take(*window_size - 1)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
// `window` must start with dummy values, which will be removed on the first iteration
|
||||
let stride = each_args.stride.map(|x| *x).unwrap_or(1);
|
||||
let stride = stride.map(|x| *x).unwrap_or(1);
|
||||
window.insert(0, UntaggedValue::Primitive(Primitive::Nothing).into());
|
||||
|
||||
Ok(input
|
||||
Ok(args
|
||||
.input
|
||||
.enumerate()
|
||||
.map(move |(i, input)| {
|
||||
// This would probably be more efficient if `last` was a VecDeque
|
||||
|
@ -89,7 +88,8 @@ impl WholeStreamCommand for EachWindow {
|
|||
})
|
||||
.flatten()
|
||||
.flatten()
|
||||
.to_action_stream())
|
||||
.map(Ok)
|
||||
.to_input_stream())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -2,17 +2,12 @@ use crate::prelude::*;
|
|||
use nu_engine::evaluate_baseline_expr;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
||||
use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
|
||||
use nu_source::Tagged;
|
||||
use std::borrow::Borrow;
|
||||
|
||||
pub struct Format;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct FormatArgs {
|
||||
pattern: Tagged<String>,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for Format {
|
||||
fn name(&self) -> &str {
|
||||
"format"
|
||||
|
@ -30,7 +25,7 @@ impl WholeStreamCommand for Format {
|
|||
"Format columns into a string using a simple pattern."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
format_command(args)
|
||||
}
|
||||
|
||||
|
@ -43,14 +38,16 @@ impl WholeStreamCommand for Format {
|
|||
}
|
||||
}
|
||||
|
||||
fn format_command(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn format_command(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let ctx = Arc::new(EvaluationContext::from_args(&args));
|
||||
let (FormatArgs { pattern }, input) = args.process()?;
|
||||
let args = args.evaluate_once()?;
|
||||
let pattern: Tagged<String> = args.req(0)?;
|
||||
|
||||
let format_pattern = format(&pattern);
|
||||
let commands = Arc::new(format_pattern);
|
||||
|
||||
Ok(input
|
||||
Ok(args
|
||||
.input
|
||||
.map(move |value| {
|
||||
let mut output = String::new();
|
||||
let commands = commands.clone();
|
||||
|
@ -82,9 +79,9 @@ fn format_command(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
|||
}
|
||||
}
|
||||
|
||||
ReturnSuccess::value(UntaggedValue::string(output).into_untagged_value())
|
||||
Ok(UntaggedValue::string(output).into_untagged_value())
|
||||
})
|
||||
.to_action_stream())
|
||||
.to_input_stream())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
|
@ -2,20 +2,12 @@ use crate::prelude::*;
|
|||
use nu_data::base::shape::InlineShape;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
ColumnPath, Primitive::Filesize, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
|
||||
};
|
||||
use nu_protocol::{ColumnPath, Primitive::Filesize, Signature, SyntaxShape, UntaggedValue, Value};
|
||||
use nu_source::Tagged;
|
||||
use nu_value_ext::get_data_by_column_path;
|
||||
|
||||
pub struct FileSize;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Arguments {
|
||||
field: ColumnPath,
|
||||
format: Tagged<String>,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for FileSize {
|
||||
fn name(&self) -> &str {
|
||||
"format filesize"
|
||||
|
@ -39,7 +31,7 @@ impl WholeStreamCommand for FileSize {
|
|||
"Converts a column of filesizes to some specified format"
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
filesize(args)
|
||||
}
|
||||
|
||||
|
@ -63,50 +55,55 @@ fn process_row(
|
|||
input: Value,
|
||||
format: Tagged<String>,
|
||||
field: Arc<ColumnPath>,
|
||||
) -> Result<ActionStream, ShellError> {
|
||||
Ok({
|
||||
let replace_for = get_data_by_column_path(&input, &field, move |_, _, error| error);
|
||||
match replace_for {
|
||||
Ok(s) => {
|
||||
if let Value {
|
||||
value: UntaggedValue::Primitive(Filesize(fs)),
|
||||
..
|
||||
} = s
|
||||
{
|
||||
let byte_format = InlineShape::format_bytes(&fs, Some(&format.item));
|
||||
let byte_value = Value::from(byte_format.1);
|
||||
ActionStream::one(ReturnSuccess::value(
|
||||
input.replace_data_at_column_path(&field, byte_value).expect("Given that the existence check was already done, this shouldn't trigger never"),
|
||||
))
|
||||
} else {
|
||||
return Err(ShellError::labeled_error(
|
||||
"the data in this row is not of the type filesize",
|
||||
"invalid datatype in row",
|
||||
input.tag(),
|
||||
));
|
||||
}
|
||||
) -> Result<Value, ShellError> {
|
||||
let replace_for = get_data_by_column_path(&input, &field, move |_, _, error| error);
|
||||
match replace_for {
|
||||
Ok(s) => {
|
||||
if let Value {
|
||||
value: UntaggedValue::Primitive(Filesize(fs)),
|
||||
..
|
||||
} = s
|
||||
{
|
||||
let byte_format = InlineShape::format_bytes(&fs, Some(&format.item));
|
||||
let byte_value = Value::from(byte_format.1);
|
||||
Ok(input
|
||||
.replace_data_at_column_path(&field, byte_value)
|
||||
.expect(
|
||||
"Given that the existence check was already done, this shouldn't trigger never",
|
||||
))
|
||||
} else {
|
||||
Err(ShellError::labeled_error(
|
||||
"the data in this row is not of the type filesize",
|
||||
"invalid datatype in row",
|
||||
input.tag(),
|
||||
))
|
||||
}
|
||||
Err(e) => ActionStream::one(Err(e)),
|
||||
}
|
||||
})
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
fn filesize(raw_args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
let (Arguments { field, format }, input) = raw_args.process()?;
|
||||
fn filesize(raw_args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let args = raw_args.evaluate_once()?;
|
||||
|
||||
let field: ColumnPath = args.req(0)?;
|
||||
let format: Tagged<String> = args.req(1)?;
|
||||
let field = Arc::new(field);
|
||||
|
||||
Ok(input
|
||||
Ok(args
|
||||
.input
|
||||
.map(move |input| {
|
||||
let format = format.clone();
|
||||
let field = field.clone();
|
||||
|
||||
match process_row(input, format, field) {
|
||||
Ok(s) => s,
|
||||
Err(e) => ActionStream::one(Err(e)),
|
||||
Ok(s) => Ok(s),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
})
|
||||
.flatten()
|
||||
.to_action_stream())
|
||||
.map(Ok)
|
||||
.to_input_stream())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
|
Loading…
Reference in a new issue