2022-01-26 22:54:31 +01:00
|
|
|
use chrono::{DateTime, FixedOffset};
|
2022-02-03 22:18:18 +01:00
|
|
|
use nu_engine::{column::column_does_not_exist, CallExt};
|
2022-01-22 21:49:50 +01:00
|
|
|
use nu_protocol::{
|
2022-02-03 22:18:18 +01:00
|
|
|
ast::Call,
|
|
|
|
engine::{Command, EngineState, Stack},
|
|
|
|
Category, Config, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature,
|
|
|
|
Span, SyntaxShape, Value,
|
2022-01-22 21:49:50 +01:00
|
|
|
};
|
2022-01-26 23:44:37 +01:00
|
|
|
use std::cmp::Ordering;
|
2022-01-22 21:49:50 +01:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct SortBy;
|
|
|
|
|
|
|
|
impl Command for SortBy {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"sort-by"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
|
|
|
Signature::build("sort-by")
|
|
|
|
.rest("columns", SyntaxShape::Any, "the column(s) to sort by")
|
|
|
|
.switch("reverse", "Sort in reverse order", Some('r'))
|
2022-02-03 22:18:18 +01:00
|
|
|
.switch(
|
|
|
|
"insensitive",
|
|
|
|
"Sort string-based columns case-insensitively",
|
|
|
|
Some('i'),
|
|
|
|
)
|
2022-01-22 21:49:50 +01:00
|
|
|
.category(Category::Filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Sort by the given columns, in increasing order."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
example: "[2 0 1] | sort-by",
|
|
|
|
description: "sort the list by increasing value",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![Value::test_int(0), Value::test_int(1), Value::test_int(2)],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
example: "[2 0 1] | sort-by -r",
|
|
|
|
description: "sort the list by decreasing value",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![Value::test_int(2), Value::test_int(1), Value::test_int(0)],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
example: "[betty amy sarah] | sort-by",
|
|
|
|
description: "sort a list of strings",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::test_string("amy"),
|
|
|
|
Value::test_string("betty"),
|
|
|
|
Value::test_string("sarah"),
|
|
|
|
],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
example: "[betty amy sarah] | sort-by -r",
|
|
|
|
description: "sort a list of strings in reverse",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::test_string("sarah"),
|
|
|
|
Value::test_string("betty"),
|
|
|
|
Value::test_string("amy"),
|
|
|
|
],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
2022-02-03 22:18:18 +01:00
|
|
|
Example {
|
|
|
|
description: "Sort strings (case-insensitive)",
|
|
|
|
example: "echo [airplane Truck Car] | sort-by -i",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::test_string("airplane"),
|
|
|
|
Value::test_string("Car"),
|
|
|
|
Value::test_string("Truck"),
|
|
|
|
],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Sort strings (reversed case-insensitive)",
|
|
|
|
example: "echo [airplane Truck Car] | sort-by -i -r",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::test_string("Truck"),
|
|
|
|
Value::test_string("Car"),
|
|
|
|
Value::test_string("airplane"),
|
|
|
|
],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
2022-01-22 21:49:50 +01:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let columns: Vec<String> = call.rest(engine_state, stack, 0)?;
|
|
|
|
let reverse = call.has_flag("reverse");
|
2022-02-03 22:18:18 +01:00
|
|
|
let insensitive = call.has_flag("insensitive");
|
2022-01-23 00:36:27 +01:00
|
|
|
let metadata = &input.metadata();
|
2022-02-03 22:18:18 +01:00
|
|
|
let config = stack.get_config()?;
|
2022-01-22 21:49:50 +01:00
|
|
|
let mut vec: Vec<_> = input.into_iter().collect();
|
|
|
|
|
2022-02-03 22:18:18 +01:00
|
|
|
sort(&mut vec, columns, call, insensitive, &config)?;
|
2022-01-22 21:49:50 +01:00
|
|
|
|
|
|
|
if reverse {
|
|
|
|
vec.reverse()
|
|
|
|
}
|
|
|
|
|
|
|
|
let iter = vec.into_iter();
|
2022-01-23 00:36:27 +01:00
|
|
|
match &*metadata {
|
|
|
|
Some(m) => {
|
|
|
|
Ok(iter.into_pipeline_data_with_metadata(m.clone(), engine_state.ctrlc.clone()))
|
|
|
|
}
|
|
|
|
None => Ok(iter.into_pipeline_data(engine_state.ctrlc.clone())),
|
|
|
|
}
|
2022-01-22 21:49:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-03 22:18:18 +01:00
|
|
|
pub fn sort(
|
|
|
|
vec: &mut [Value],
|
|
|
|
columns: Vec<String>,
|
|
|
|
call: &Call,
|
|
|
|
insensitive: bool,
|
|
|
|
config: &Config,
|
|
|
|
) -> Result<(), ShellError> {
|
2022-01-22 21:49:50 +01:00
|
|
|
match &vec[0] {
|
|
|
|
Value::Record {
|
2022-01-24 05:52:19 +01:00
|
|
|
cols,
|
2022-01-22 21:49:50 +01:00
|
|
|
vals: _input_vals,
|
|
|
|
..
|
|
|
|
} => {
|
2022-01-24 05:52:19 +01:00
|
|
|
if columns.is_empty() {
|
|
|
|
println!("sort-by requires a column name to sort table data");
|
|
|
|
return Err(ShellError::CantFindColumn(call.head, call.head));
|
|
|
|
}
|
|
|
|
|
|
|
|
if column_does_not_exist(columns.clone(), cols.to_vec()) {
|
|
|
|
return Err(ShellError::CantFindColumn(call.head, call.head));
|
|
|
|
}
|
|
|
|
|
2022-02-13 03:48:50 +01:00
|
|
|
// check to make sure each value in each column in the record
|
|
|
|
// that we asked for is a string. So, first collect all the columns
|
|
|
|
// that we asked for into vals, then later make sure they're all
|
|
|
|
// strings.
|
|
|
|
let mut vals = vec![];
|
|
|
|
for item in vec.iter() {
|
|
|
|
for col in &columns {
|
|
|
|
let val = match item.get_data_by_key(col) {
|
|
|
|
Some(v) => v,
|
|
|
|
None => Value::nothing(Span::test_data()),
|
|
|
|
};
|
|
|
|
vals.push(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let should_sort_case_insensitively = insensitive
|
|
|
|
&& vals
|
|
|
|
.iter()
|
|
|
|
.all(|x| matches!(x.get_type(), nu_protocol::Type::String));
|
|
|
|
|
2022-01-22 21:49:50 +01:00
|
|
|
vec.sort_by(|a, b| {
|
2022-02-03 22:18:18 +01:00
|
|
|
process(
|
|
|
|
a,
|
|
|
|
b,
|
|
|
|
&columns[0],
|
|
|
|
call,
|
|
|
|
should_sort_case_insensitively,
|
|
|
|
config,
|
|
|
|
)
|
|
|
|
.expect("sort_by Value::Record bug")
|
2022-01-22 21:49:50 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
_ => {
|
2022-02-03 22:18:18 +01:00
|
|
|
vec.sort_by(|a, b| {
|
2022-02-13 03:48:50 +01:00
|
|
|
if insensitive {
|
2022-02-03 22:18:18 +01:00
|
|
|
let lowercase_left = Value::string(
|
|
|
|
a.into_string("", config).to_ascii_lowercase(),
|
|
|
|
Span::test_data(),
|
|
|
|
);
|
|
|
|
let lowercase_right = Value::string(
|
|
|
|
b.into_string("", config).to_ascii_lowercase(),
|
|
|
|
Span::test_data(),
|
|
|
|
);
|
2022-02-13 06:30:57 +01:00
|
|
|
coerce_compare(&lowercase_left, &lowercase_right, call)
|
2022-02-03 22:18:18 +01:00
|
|
|
.expect("sort_by default bug")
|
|
|
|
} else {
|
2022-02-13 18:23:54 +01:00
|
|
|
coerce_compare(a, b, call).expect("sort_by default bug")
|
2022-02-03 22:18:18 +01:00
|
|
|
}
|
|
|
|
});
|
2022-01-22 21:49:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn process(
|
|
|
|
left: &Value,
|
|
|
|
right: &Value,
|
|
|
|
column: &str,
|
|
|
|
call: &Call,
|
2022-02-03 22:18:18 +01:00
|
|
|
insensitive: bool,
|
|
|
|
config: &Config,
|
2022-02-13 18:23:54 +01:00
|
|
|
) -> Result<Ordering, ShellError> {
|
2022-01-22 21:49:50 +01:00
|
|
|
let left_value = left.get_data_by_key(column);
|
|
|
|
|
|
|
|
let left_res = match left_value {
|
|
|
|
Some(left_res) => left_res,
|
|
|
|
None => Value::Nothing { span: call.head },
|
|
|
|
};
|
|
|
|
|
|
|
|
let right_value = right.get_data_by_key(column);
|
|
|
|
|
|
|
|
let right_res = match right_value {
|
|
|
|
Some(right_res) => right_res,
|
|
|
|
None => Value::Nothing { span: call.head },
|
|
|
|
};
|
|
|
|
|
2022-02-03 22:18:18 +01:00
|
|
|
if insensitive {
|
|
|
|
let lowercase_left = Value::string(
|
|
|
|
left_res.into_string("", config).to_ascii_lowercase(),
|
|
|
|
Span::test_data(),
|
|
|
|
);
|
|
|
|
let lowercase_right = Value::string(
|
|
|
|
right_res.into_string("", config).to_ascii_lowercase(),
|
|
|
|
Span::test_data(),
|
|
|
|
);
|
2022-02-13 06:30:57 +01:00
|
|
|
coerce_compare(&lowercase_left, &lowercase_right, call)
|
2022-02-03 22:18:18 +01:00
|
|
|
} else {
|
2022-02-13 06:30:57 +01:00
|
|
|
coerce_compare(&left_res, &right_res, call)
|
2022-02-03 22:18:18 +01:00
|
|
|
}
|
2022-01-22 21:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum CompareValues {
|
|
|
|
Ints(i64, i64),
|
2022-01-26 23:44:37 +01:00
|
|
|
Floats(f64, f64),
|
2022-01-22 21:49:50 +01:00
|
|
|
String(String, String),
|
|
|
|
Booleans(bool, bool),
|
2022-01-26 22:54:31 +01:00
|
|
|
Filesize(i64, i64),
|
|
|
|
Date(DateTime<FixedOffset>, DateTime<FixedOffset>),
|
2022-01-22 21:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
impl CompareValues {
|
|
|
|
pub fn compare(&self) -> std::cmp::Ordering {
|
|
|
|
match self {
|
|
|
|
CompareValues::Ints(left, right) => left.cmp(right),
|
2022-01-26 23:44:37 +01:00
|
|
|
CompareValues::Floats(left, right) => process_floats(left, right),
|
2022-01-22 21:49:50 +01:00
|
|
|
CompareValues::String(left, right) => left.cmp(right),
|
|
|
|
CompareValues::Booleans(left, right) => left.cmp(right),
|
2022-01-26 22:54:31 +01:00
|
|
|
CompareValues::Filesize(left, right) => left.cmp(right),
|
|
|
|
CompareValues::Date(left, right) => left.cmp(right),
|
2022-01-22 21:49:50 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 23:44:37 +01:00
|
|
|
pub fn process_floats(left: &f64, right: &f64) -> std::cmp::Ordering {
|
|
|
|
let result = left.partial_cmp(right);
|
|
|
|
match result {
|
|
|
|
Some(Ordering::Greater) => Ordering::Greater,
|
|
|
|
Some(Ordering::Less) => Ordering::Less,
|
|
|
|
_ => Ordering::Equal,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-13 18:23:54 +01:00
|
|
|
/*
|
|
|
|
Arbitrary Order of Values:
|
|
|
|
Floats
|
|
|
|
Ints
|
|
|
|
Strings
|
|
|
|
Bools
|
2022-02-13 19:20:50 +01:00
|
|
|
Lists
|
2022-02-13 18:23:54 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
pub fn coerce_compare(left: &Value, right: &Value, call: &Call) -> Result<Ordering, ShellError> {
|
|
|
|
Ok(match (left, right) {
|
2022-01-26 23:44:37 +01:00
|
|
|
(Value::Float { val: left, .. }, Value::Float { val: right, .. }) => {
|
2022-02-13 18:23:54 +01:00
|
|
|
CompareValues::Floats(*left, *right).compare()
|
2022-01-26 23:44:37 +01:00
|
|
|
}
|
2022-01-26 22:54:31 +01:00
|
|
|
(Value::Filesize { val: left, .. }, Value::Filesize { val: right, .. }) => {
|
2022-02-13 18:23:54 +01:00
|
|
|
CompareValues::Filesize(*left, *right).compare()
|
2022-01-22 21:49:50 +01:00
|
|
|
}
|
2022-01-26 22:54:31 +01:00
|
|
|
(Value::Date { val: left, .. }, Value::Date { val: right, .. }) => {
|
2022-02-13 18:23:54 +01:00
|
|
|
CompareValues::Date(*left, *right).compare()
|
2022-01-26 22:54:31 +01:00
|
|
|
}
|
2022-01-22 21:49:50 +01:00
|
|
|
(Value::Int { val: left, .. }, Value::Int { val: right, .. }) => {
|
2022-02-13 18:23:54 +01:00
|
|
|
CompareValues::Ints(*left, *right).compare()
|
2022-01-22 21:49:50 +01:00
|
|
|
}
|
|
|
|
(Value::String { val: left, .. }, Value::String { val: right, .. }) => {
|
2022-02-13 18:23:54 +01:00
|
|
|
CompareValues::String(left.clone(), right.clone()).compare()
|
2022-01-22 21:49:50 +01:00
|
|
|
}
|
|
|
|
(Value::Bool { val: left, .. }, Value::Bool { val: right, .. }) => {
|
2022-02-13 18:23:54 +01:00
|
|
|
CompareValues::Booleans(*left, *right).compare()
|
2022-01-22 21:49:50 +01:00
|
|
|
}
|
2022-02-13 06:30:57 +01:00
|
|
|
|
2022-02-13 19:20:50 +01:00
|
|
|
// FIXME: Not sure how to compare and sort lists
|
|
|
|
(Value::List { .. }, Value::List { .. }) => Ordering::Equal,
|
|
|
|
|
2022-02-13 18:23:54 +01:00
|
|
|
// Floats will always come before Ints
|
|
|
|
(Value::Float { .. }, Value::Int { .. }) => Ordering::Less,
|
|
|
|
(Value::Int { .. }, Value::Float { .. }) => Ordering::Greater,
|
|
|
|
|
|
|
|
// Floats will always come before Strings
|
|
|
|
(Value::Float { .. }, Value::String { .. }) => Ordering::Less,
|
|
|
|
(Value::String { .. }, Value::Float { .. }) => Ordering::Greater,
|
|
|
|
|
|
|
|
// Floats will always come before Bools
|
|
|
|
(Value::Float { .. }, Value::Bool { .. }) => Ordering::Less,
|
|
|
|
(Value::Bool { .. }, Value::Float { .. }) => Ordering::Greater,
|
|
|
|
|
2022-02-13 19:20:50 +01:00
|
|
|
// Floats will always come before Lists
|
|
|
|
(Value::Float { .. }, Value::List { .. }) => Ordering::Less,
|
|
|
|
(Value::List { .. }, Value::Float { .. }) => Ordering::Greater,
|
|
|
|
|
2022-02-13 18:23:54 +01:00
|
|
|
// Ints will always come before strings
|
|
|
|
(Value::Int { .. }, Value::String { .. }) => Ordering::Less,
|
|
|
|
(Value::String { .. }, Value::Int { .. }) => Ordering::Greater,
|
|
|
|
|
|
|
|
// Ints will always come before Bools
|
|
|
|
(Value::Int { .. }, Value::Bool { .. }) => Ordering::Less,
|
|
|
|
(Value::Bool { .. }, Value::Int { .. }) => Ordering::Greater,
|
|
|
|
|
2022-02-13 19:20:50 +01:00
|
|
|
// Ints will always come before Lists
|
|
|
|
(Value::Int { .. }, Value::List { .. }) => Ordering::Less,
|
|
|
|
(Value::List { .. }, Value::Int { .. }) => Ordering::Greater,
|
|
|
|
|
2022-02-13 18:23:54 +01:00
|
|
|
// Strings will always come before Bools
|
|
|
|
(Value::String { .. }, Value::Bool { .. }) => Ordering::Less,
|
|
|
|
(Value::Bool { .. }, Value::String { .. }) => Ordering::Greater,
|
|
|
|
|
2022-02-13 19:20:50 +01:00
|
|
|
// Strings will always come before Lists
|
|
|
|
(Value::String { .. }, Value::List { .. }) => Ordering::Less,
|
|
|
|
(Value::List { .. }, Value::String { .. }) => Ordering::Greater,
|
|
|
|
|
|
|
|
// Bools will always come before Lists
|
|
|
|
(Value::Bool { .. }, Value::List { .. }) => Ordering::Less,
|
|
|
|
(Value::List { .. }, Value::Bool { .. }) => Ordering::Greater,
|
|
|
|
|
2022-02-13 06:30:57 +01:00
|
|
|
_ => {
|
|
|
|
let description = format!("not able to compare {:?} with {:?}\n", left, right);
|
2022-02-13 18:23:54 +01:00
|
|
|
return Err(ShellError::TypeMismatch(description, call.head));
|
2022-02-13 06:30:57 +01:00
|
|
|
}
|
2022-02-13 18:23:54 +01:00
|
|
|
})
|
2022-01-22 21:49:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(SortBy {})
|
|
|
|
}
|
|
|
|
}
|