forked from extern/nushell
date commands ported (#3410)
This commit is contained in:
parent
758c128147
commit
bc699a2cc1
@ -1,7 +1,7 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
|
||||
use nu_protocol::{Signature, UntaggedValue};
|
||||
|
||||
pub struct Command;
|
||||
|
||||
@ -18,10 +18,10 @@ impl WholeStreamCommand for Command {
|
||||
"Apply date function."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
Ok(ActionStream::one(ReturnSuccess::value(
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
Ok(OutputStream::one(
|
||||
UntaggedValue::string(get_full_help(&Command, args.scope())).into_value(Tag::unknown()),
|
||||
)))
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,20 +1,12 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
Dictionary, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
|
||||
};
|
||||
use nu_protocol::{Dictionary, Primitive, Signature, SyntaxShape, UntaggedValue, Value};
|
||||
use nu_source::Tagged;
|
||||
use std::fmt::{self, write};
|
||||
|
||||
pub struct Date;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct FormatArgs {
|
||||
format: Tagged<String>,
|
||||
table: bool,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for Date {
|
||||
fn name(&self) -> &str {
|
||||
"date format"
|
||||
@ -30,7 +22,7 @@ impl WholeStreamCommand for Date {
|
||||
"Format a given date using the given format string."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
format(args)
|
||||
}
|
||||
|
||||
@ -50,11 +42,15 @@ impl WholeStreamCommand for Date {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
pub fn format(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let tag = args.call_info.name_tag.clone();
|
||||
let (FormatArgs { format, table }, input) = args.process()?;
|
||||
let args = args.evaluate_once()?;
|
||||
|
||||
Ok(input
|
||||
let format: Tagged<String> = args.req(0)?;
|
||||
let table: Option<bool> = args.get_flag("table")?;
|
||||
|
||||
Ok(args
|
||||
.input
|
||||
.map(move |value| match value {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Date(dt)),
|
||||
@ -70,7 +66,7 @@ pub fn format(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
&format.tag,
|
||||
))
|
||||
} else {
|
||||
let value = if table {
|
||||
let value = if table.is_some() {
|
||||
let mut indexmap = IndexMap::new();
|
||||
indexmap.insert(
|
||||
"formatted".to_string(),
|
||||
@ -82,7 +78,7 @@ pub fn format(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
UntaggedValue::string(&output).into_value(&tag)
|
||||
};
|
||||
|
||||
ReturnSuccess::value(value)
|
||||
Ok(value)
|
||||
}
|
||||
}
|
||||
_ => Err(ShellError::labeled_error(
|
||||
@ -91,7 +87,7 @@ pub fn format(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
&tag,
|
||||
)),
|
||||
})
|
||||
.to_action_stream())
|
||||
.to_input_stream())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -3,7 +3,7 @@ use chrono_tz::TZ_VARIANTS;
|
||||
use indexmap::IndexMap;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Dictionary, ReturnSuccess, Signature, UntaggedValue};
|
||||
use nu_protocol::{Dictionary, Signature, UntaggedValue};
|
||||
|
||||
pub struct Date;
|
||||
|
||||
@ -20,7 +20,7 @@ impl WholeStreamCommand for Date {
|
||||
"List supported time zones."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
list_timezone(args)
|
||||
}
|
||||
|
||||
@ -40,7 +40,7 @@ impl WholeStreamCommand for Date {
|
||||
}
|
||||
}
|
||||
|
||||
fn list_timezone(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn list_timezone(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once()?;
|
||||
let tag = args.call_info.name_tag.clone();
|
||||
|
||||
@ -52,12 +52,10 @@ fn list_timezone(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
UntaggedValue::string(tz.name()).into_value(&tag),
|
||||
);
|
||||
|
||||
Ok(ReturnSuccess::Value(
|
||||
UntaggedValue::Row(Dictionary { entries }).into_value(&tag),
|
||||
))
|
||||
Ok(UntaggedValue::Row(Dictionary { entries }).into_value(&tag))
|
||||
});
|
||||
|
||||
Ok(list.into_iter().to_action_stream())
|
||||
Ok(list.into_iter().to_input_stream())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -19,12 +19,12 @@ impl WholeStreamCommand for Date {
|
||||
"Get the current date."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
now(args)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn now(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
pub fn now(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once()?;
|
||||
let tag = args.call_info.name_tag.clone();
|
||||
|
||||
@ -32,7 +32,7 @@ pub fn now(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
|
||||
let value = UntaggedValue::date(now.with_timezone(now.offset())).into_value(&tag);
|
||||
|
||||
Ok(ActionStream::one(value))
|
||||
Ok(OutputStream::one(value))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -3,7 +3,7 @@ use chrono::{Datelike, Timelike};
|
||||
use indexmap::IndexMap;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Dictionary, Primitive, ReturnSuccess, Signature, UntaggedValue, Value};
|
||||
use nu_protocol::{Dictionary, Primitive, Signature, UntaggedValue, Value};
|
||||
|
||||
pub struct Date;
|
||||
|
||||
@ -20,7 +20,7 @@ impl WholeStreamCommand for Date {
|
||||
"Print the date in a structured table."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
to_table(args)
|
||||
}
|
||||
|
||||
@ -33,7 +33,7 @@ impl WholeStreamCommand for Date {
|
||||
}
|
||||
}
|
||||
|
||||
fn to_table(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn to_table(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once()?;
|
||||
let tag = args.call_info.name_tag.clone();
|
||||
let input = args.input;
|
||||
@ -79,7 +79,7 @@ fn to_table(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
|
||||
let value = UntaggedValue::Row(Dictionary::from(indexmap)).into_value(&tag);
|
||||
|
||||
ReturnSuccess::value(value)
|
||||
Ok(value)
|
||||
}
|
||||
_ => Err(ShellError::labeled_error(
|
||||
"Expected a date from pipeline",
|
||||
@ -87,7 +87,7 @@ fn to_table(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
&tag,
|
||||
)),
|
||||
})
|
||||
.to_action_stream())
|
||||
.to_input_stream())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
@ -2,16 +2,11 @@ use crate::commands::date::parser::{datetime_in_timezone, ParseErrorKind};
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
||||
use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value};
|
||||
use nu_source::Tagged;
|
||||
|
||||
pub struct Date;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct DateToTimeZoneArgs {
|
||||
timezone: Tagged<String>,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for Date {
|
||||
fn name(&self) -> &str {
|
||||
"date to-timezone"
|
||||
@ -33,7 +28,7 @@ impl WholeStreamCommand for Date {
|
||||
"Use 'date list-timezone' to list all supported time zones."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
to_timezone(args)
|
||||
}
|
||||
|
||||
@ -58,11 +53,14 @@ impl WholeStreamCommand for Date {
|
||||
}
|
||||
}
|
||||
|
||||
fn to_timezone(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
fn to_timezone(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let tag = args.call_info.name_tag.clone();
|
||||
let (DateToTimeZoneArgs { timezone }, input) = args.process()?;
|
||||
let args = args.evaluate_once()?;
|
||||
|
||||
Ok(input
|
||||
let timezone: Tagged<String> = args.req(0)?;
|
||||
|
||||
Ok(args
|
||||
.input
|
||||
.map(move |value| match value {
|
||||
Value {
|
||||
value: UntaggedValue::Primitive(Primitive::Date(dt)),
|
||||
@ -71,7 +69,7 @@ fn to_timezone(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
Ok(dt) => {
|
||||
let value = UntaggedValue::date(dt).into_value(&tag);
|
||||
|
||||
ReturnSuccess::value(value)
|
||||
Ok(value)
|
||||
}
|
||||
Err(e) => Err(ShellError::labeled_error(
|
||||
error_message(e),
|
||||
@ -85,7 +83,7 @@ fn to_timezone(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
&tag,
|
||||
)),
|
||||
})
|
||||
.to_action_stream())
|
||||
.to_input_stream())
|
||||
}
|
||||
|
||||
fn error_message(err: ParseErrorKind) -> &'static str {
|
||||
|
@ -21,7 +21,7 @@ impl WholeStreamCommand for Date {
|
||||
"return the current date in utc."
|
||||
}
|
||||
|
||||
fn run_with_actions(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
utc(args)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user