mirror of
https://github.com/nushell/nushell.git
synced 2025-08-14 02:28:35 +02:00
default command introduced.
This commit is contained in:
@ -310,6 +310,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
||||
per_item_command(Echo),
|
||||
whole_stream_command(Config),
|
||||
whole_stream_command(Compact),
|
||||
whole_stream_command(Default),
|
||||
whole_stream_command(SkipWhile),
|
||||
per_item_command(Enter),
|
||||
per_item_command(Help),
|
||||
|
@ -17,6 +17,7 @@ pub(crate) mod count;
|
||||
pub(crate) mod cp;
|
||||
pub(crate) mod date;
|
||||
pub(crate) mod debug;
|
||||
pub(crate) mod default;
|
||||
pub(crate) mod echo;
|
||||
pub(crate) mod enter;
|
||||
pub(crate) mod env;
|
||||
@ -105,6 +106,7 @@ pub(crate) use count::Count;
|
||||
pub(crate) use cp::Cpy;
|
||||
pub(crate) use date::Date;
|
||||
pub(crate) use debug::Debug;
|
||||
pub(crate) use default::Default;
|
||||
pub(crate) use echo::Echo;
|
||||
pub(crate) use enter::Enter;
|
||||
pub(crate) use env::Env;
|
||||
|
72
src/commands/default.rs
Normal file
72
src/commands/default.rs
Normal file
@ -0,0 +1,72 @@
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::errors::ShellError;
|
||||
use crate::parser::CommandRegistry;
|
||||
use crate::prelude::*;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct DefaultArgs {
|
||||
column: Tagged<String>,
|
||||
value: Tagged<Value>,
|
||||
}
|
||||
|
||||
pub struct Default;
|
||||
|
||||
impl WholeStreamCommand for Default {
|
||||
fn name(&self) -> &str {
|
||||
"default"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("default")
|
||||
.required("column name", SyntaxShape::String, "the name of the column")
|
||||
.required(
|
||||
"column value",
|
||||
SyntaxShape::Any,
|
||||
"the value of the column to default",
|
||||
)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Sets a default row's column if missing."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
args.process(registry, default)?.run()
|
||||
}
|
||||
}
|
||||
|
||||
fn default(
|
||||
DefaultArgs { column, value }: DefaultArgs,
|
||||
RunnableContext { input, .. }: RunnableContext,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
let stream = input
|
||||
.values
|
||||
.map(move |item| {
|
||||
let mut result = VecDeque::new();
|
||||
|
||||
let should_add = match item {
|
||||
Tagged {
|
||||
item: Value::Row(ref r),
|
||||
..
|
||||
} => r.get_data(&column.item).borrow().is_none(),
|
||||
_ => false,
|
||||
};
|
||||
|
||||
if should_add {
|
||||
match item.insert_data_at_path(&column.item, value.item.clone()) {
|
||||
Some(new_value) => result.push_back(ReturnSuccess::value(new_value)),
|
||||
None => {}
|
||||
}
|
||||
} else {
|
||||
result.push_back(ReturnSuccess::value(item));
|
||||
}
|
||||
result
|
||||
})
|
||||
.flatten();
|
||||
|
||||
Ok(stream.to_output_stream())
|
||||
}
|
Reference in New Issue
Block a user