mirror of
https://github.com/nushell/nushell.git
synced 2025-08-15 23:17:53 +02:00
Merge branch 'master' into surf
This commit is contained in:
@ -168,6 +168,8 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
|
||||
whole_stream_command(SortBy),
|
||||
whole_stream_command(Tags),
|
||||
whole_stream_command(First),
|
||||
whole_stream_command(Last),
|
||||
whole_stream_command(FromArray),
|
||||
whole_stream_command(FromArray),
|
||||
whole_stream_command(FromCSV),
|
||||
whole_stream_command(FromINI),
|
||||
|
@ -22,6 +22,7 @@ crate mod from_toml;
|
||||
crate mod from_xml;
|
||||
crate mod from_yaml;
|
||||
crate mod get;
|
||||
crate mod last;
|
||||
crate mod lines;
|
||||
crate mod ls;
|
||||
crate mod mkdir;
|
||||
@ -76,6 +77,7 @@ crate use from_toml::FromTOML;
|
||||
crate use from_xml::FromXML;
|
||||
crate use from_yaml::FromYAML;
|
||||
crate use get::Get;
|
||||
crate use last::Last;
|
||||
crate use lines::Lines;
|
||||
crate use ls::LS;
|
||||
crate use mkdir::Mkdir;
|
||||
|
59
src/commands/last.rs
Normal file
59
src/commands/last.rs
Normal file
@ -0,0 +1,59 @@
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::errors::ShellError;
|
||||
use crate::parser::CommandRegistry;
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct Last;
|
||||
|
||||
impl WholeStreamCommand for Last {
|
||||
fn run(
|
||||
&self,
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
last(args, registry)
|
||||
}
|
||||
|
||||
fn name(&self) -> &str {
|
||||
"last"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("last").required("amount", SyntaxType::Literal)
|
||||
}
|
||||
}
|
||||
|
||||
fn last(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once(registry)?;
|
||||
|
||||
let amount = args.expect_nth(0)?.as_i64();
|
||||
|
||||
let amount = match amount {
|
||||
Ok(o) => o,
|
||||
Err(_) => {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Value is not a number",
|
||||
"expected integer",
|
||||
args.expect_nth(0)?.span(),
|
||||
))
|
||||
}
|
||||
};
|
||||
|
||||
if amount <= 0 {
|
||||
return Err(ShellError::labeled_error(
|
||||
"Value is too low",
|
||||
"expected a positive integer",
|
||||
args.expect_nth(0)?.span(),
|
||||
))
|
||||
}
|
||||
|
||||
let stream = async_stream_block! {
|
||||
let v: Vec<_> = args.input.into_vec().await;
|
||||
let k = v.len() - (amount as usize);
|
||||
for x in v[k..].iter() {
|
||||
let y: Tagged<Value> = x.clone();
|
||||
yield ReturnSuccess::value(y)
|
||||
}
|
||||
};
|
||||
Ok(stream.to_output_stream())
|
||||
}
|
@ -18,7 +18,7 @@ impl WholeStreamCommand for SortBy {
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("sort-by")
|
||||
Signature::build("sort-by").switch("reverse")
|
||||
}
|
||||
}
|
||||
|
||||
@ -37,13 +37,21 @@ fn sort_by(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream
|
||||
|
||||
let output = input.values.collect::<Vec<_>>();
|
||||
|
||||
let reverse = args.has("reverse");
|
||||
let output = output.map(move |mut vec| {
|
||||
vec.sort_by_key(|item| {
|
||||
let calc_key = |item: &Tagged<Value>| {
|
||||
fields
|
||||
.iter()
|
||||
.map(|f| item.get_data_by_key(f).map(|i| i.clone()))
|
||||
.collect::<Vec<Option<Tagged<Value>>>>()
|
||||
});
|
||||
};
|
||||
if reverse {
|
||||
vec.sort_by_cached_key(|item| {
|
||||
std::cmp::Reverse(calc_key(item))
|
||||
});
|
||||
} else {
|
||||
vec.sort_by_cached_key(calc_key);
|
||||
}
|
||||
|
||||
vec.into_iter().collect::<VecDeque<_>>()
|
||||
});
|
||||
|
@ -3,7 +3,6 @@
|
||||
#![feature(generators)]
|
||||
#![feature(try_trait)]
|
||||
#![feature(bind_by_move_pattern_guards)]
|
||||
#![feature(core_intrinsics)]
|
||||
#![feature(option_flattening)]
|
||||
#![feature(specialization)]
|
||||
#![feature(proc_macro_hygiene)]
|
||||
|
@ -4,7 +4,7 @@ use std::error::Error;
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let matches = App::new("nushell")
|
||||
.version("0.1.3")
|
||||
.version(clap::crate_version!())
|
||||
.arg(
|
||||
Arg::with_name("loglevel")
|
||||
.short("l")
|
||||
|
@ -14,7 +14,7 @@ pub trait ExtractType: Sized {
|
||||
|
||||
impl<T> ExtractType for T {
|
||||
default fn extract(_value: &Tagged<Value>) -> Result<T, ShellError> {
|
||||
let name = std::intrinsics::type_name::<T>();
|
||||
let name = std::any::type_name::<T>();
|
||||
Err(ShellError::unimplemented(format!(
|
||||
"<T> ExtractType for {}",
|
||||
name
|
||||
@ -32,7 +32,7 @@ impl<T> ExtractType for T {
|
||||
|
||||
impl<T: ExtractType> ExtractType for Vec<Tagged<T>> {
|
||||
fn extract(value: &Tagged<Value>) -> Result<Self, ShellError> {
|
||||
let name = std::intrinsics::type_name::<T>();
|
||||
let name = std::any::type_name::<T>();
|
||||
trace!("<Vec> Extracting {:?} for Vec<{}>", value, name);
|
||||
|
||||
match value.item() {
|
||||
@ -69,8 +69,8 @@ impl<T: ExtractType> ExtractType for Vec<Tagged<T>> {
|
||||
|
||||
impl<T: ExtractType, U: ExtractType> ExtractType for (T, U) {
|
||||
fn extract(value: &Tagged<Value>) -> Result<(T, U), ShellError> {
|
||||
let t_name = std::intrinsics::type_name::<T>();
|
||||
let u_name = std::intrinsics::type_name::<U>();
|
||||
let t_name = std::any::type_name::<T>();
|
||||
let u_name = std::any::type_name::<U>();
|
||||
|
||||
trace!("Extracting {:?} for ({}, {})", value, t_name, u_name);
|
||||
|
||||
@ -98,7 +98,7 @@ impl<T: ExtractType, U: ExtractType> ExtractType for (T, U) {
|
||||
|
||||
impl<T: ExtractType> ExtractType for Option<T> {
|
||||
fn extract(value: &Tagged<Value>) -> Result<Option<T>, ShellError> {
|
||||
let name = std::intrinsics::type_name::<T>();
|
||||
let name = std::any::type_name::<T>();
|
||||
trace!("<Option> Extracting {:?} for Option<{}>", value, name);
|
||||
|
||||
let result = match value.item() {
|
||||
@ -123,7 +123,7 @@ impl<T: ExtractType> ExtractType for Option<T> {
|
||||
|
||||
impl<T: ExtractType> ExtractType for Tagged<T> {
|
||||
fn extract(value: &Tagged<Value>) -> Result<Tagged<T>, ShellError> {
|
||||
let name = std::intrinsics::type_name::<T>();
|
||||
let name = std::any::type_name::<T>();
|
||||
trace!("<Tagged> Extracting {:?} for Tagged<{}>", value, name);
|
||||
|
||||
Ok(T::extract(value)?.tagged(value.tag()))
|
||||
|
@ -70,7 +70,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut ConfigDeserializer<'de> {
|
||||
V: Visitor<'de>,
|
||||
{
|
||||
let value = self.pop();
|
||||
let name = std::intrinsics::type_name::<V::Value>();
|
||||
let name = std::any::type_name::<V::Value>();
|
||||
trace!("<Deserialize any> Extracting {:?}", name);
|
||||
|
||||
V::Value::extract(&value.val)
|
||||
@ -237,7 +237,7 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut ConfigDeserializer<'de> {
|
||||
|
||||
if self.saw_root {
|
||||
let value = self.pop();
|
||||
let name = std::intrinsics::type_name::<V::Value>();
|
||||
let name = std::any::type_name::<V::Value>();
|
||||
trace!("Extracting {:?} for {:?}", value.val, name);
|
||||
V::Value::extract(&value.val)
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user