Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull from master #1

Merged
merged 5 commits into from
Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions crates/nu-cli/src/commands/ansi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ impl WholeStreamCommand for Ansi {
example: r#"ansi reset"#,
result: Some(vec![Value::from("\u{1b}[0m")]),
},
Example {
description:
"Use ansi to color text (rb = red bold, gb = green bold, pb = purple bold)",
example: r#"echo [$(ansi rb) Hello " " $(ansi gb) Nu " " $(ansi pb) World] | str collect"#,
result: Some(vec![Value::from(
"\u{1b}[1;31mHello \u{1b}[1;32mNu \u{1b}[1;35mWorld",
)]),
},
]
}

Expand Down
21 changes: 16 additions & 5 deletions crates/nu-cli/src/commands/char_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,22 @@ impl WholeStreamCommand for Char {
}

fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Output newline",
example: r#"char newline"#,
result: Some(vec![Value::from("\n")]),
}]
vec![
Example {
description: "Output newline",
example: r#"char newline"#,
result: Some(vec![Value::from("\n")]),
},
Example {
description: "Output prompt character, newline and a hamburger character",
example: r#"echo $(char prompt) $(char newline) $(char hamburger)"#,
result: Some(vec![
UntaggedValue::string("\u{25b6}").into(),
UntaggedValue::string("\n").into(),
UntaggedValue::string("\u{2261}").into(),
]),
},
]
}

async fn run(
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cli/src/commands/math/median.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn median(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
sorted.push(item.clone());
}

crate::commands::sort_by::sort(&mut sorted, &[], name)?;
crate::commands::sort_by::sort(&mut sorted, &[], name, false)?;

match take {
Pick::Median => {
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cli/src/commands/math/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ pub fn mode(values: &[Value], name: &Tag) -> Result<Value, ShellError> {
}
}

crate::commands::sort_by::sort(&mut modes, &[], name)?;
crate::commands::sort_by::sort(&mut modes, &[], name, false)?;
Ok(UntaggedValue::Table(modes).into_value(name))
}

Expand Down
62 changes: 57 additions & 5 deletions crates/nu-cli/src/commands/sort_by.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct SortBy;
#[derive(Deserialize)]
pub struct SortByArgs {
rest: Vec<Tagged<String>>,
insensitive: bool,
}

#[async_trait]
Expand All @@ -20,7 +21,13 @@ impl WholeStreamCommand for SortBy {
}

fn signature(&self) -> Signature {
Signature::build("sort-by").rest(SyntaxShape::String, "the column(s) to sort by")
Signature::build("sort-by")
.switch(
"insensitive",
"Sort string-based columns case insensitively",
Some('i'),
)
.rest(SyntaxShape::String, "the column(s) to sort by")
}

fn usage(&self) -> &str {
Expand Down Expand Up @@ -57,6 +64,24 @@ impl WholeStreamCommand for SortBy {
example: "ls | sort-by type size",
result: None,
},
Example {
description: "Sort strings (case sensitive)",
example: "echo [airplane Truck Car] | sort-by",
result: Some(vec![
UntaggedValue::string("Car").into(),
UntaggedValue::string("Truck").into(),
UntaggedValue::string("airplane").into(),
]),
},
Example {
description: "Sort strings (case insensitive)",
example: "echo [airplane Truck Car] | sort-by -i",
result: Some(vec![
UntaggedValue::string("airplane").into(),
UntaggedValue::string("Car").into(),
UntaggedValue::string("Truck").into(),
]),
},
]
}
}
Expand All @@ -68,10 +93,10 @@ async fn sort_by(
let registry = registry.clone();
let tag = args.call_info.name_tag.clone();

let (SortByArgs { rest }, mut input) = args.process(&registry).await?;
let (SortByArgs { rest, insensitive }, mut input) = args.process(&registry).await?;
let mut vec = input.drain_vec().await;

sort(&mut vec, &rest, &tag)?;
sort(&mut vec, &rest, &tag, insensitive)?;

Ok(futures::stream::iter(vec.into_iter()).to_output_stream())
}
Expand All @@ -80,6 +105,7 @@ pub fn sort(
vec: &mut [Value],
keys: &[Tagged<String>],
tag: impl Into<Tag>,
insensitive: bool,
) -> Result<(), ShellError> {
let tag = tag.into();

Expand Down Expand Up @@ -107,12 +133,38 @@ pub fn sort(
value: UntaggedValue::Primitive(_),
..
} => {
vec.sort_by(|a, b| coerce_compare(a, b).expect("Unimplemented BUG: What about primitives that don't have an order defined?").compare());
let should_sort_case_insensitively = insensitive && vec.iter().all(|x| x.is_string());

vec.sort_by(|a, b| {
if should_sort_case_insensitively {
let lowercase_a_string = a.expect_string().to_ascii_lowercase();
let lowercase_b_string = b.expect_string().to_ascii_lowercase();

lowercase_a_string.cmp(&lowercase_b_string)
} else {
coerce_compare(a, b).expect("Unimplemented BUG: What about primitives that don't have an order defined?").compare()
}
});
}
_ => {
let calc_key = |item: &Value| {
keys.iter()
.map(|f| get_data_by_key(item, f.borrow_spanned()))
.map(|f| {
let mut value_option = get_data_by_key(item, f.borrow_spanned());

if insensitive {
if let Some(value) = &value_option {
if let Ok(string_value) = value.as_string() {
value_option = Some(
UntaggedValue::string(string_value.to_ascii_lowercase())
.into_value(value.tag.clone()),
)
}
}
}

value_option
})
.collect::<Vec<Option<Value>>>()
};
vec.sort_by_cached_key(calc_key);
Expand Down
Loading