mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 14:40:06 +02:00
Simplify expressions (#3389)
* WIP: experiment with simpler expressions * fix simple invoke * update tests * fix a few tests * Make paren parsing more robust * fix external args * Remove old invocation * Update tests * Update tests
This commit is contained in:
@ -44,7 +44,7 @@ impl WholeStreamCommand for Command {
|
||||
},
|
||||
Example {
|
||||
description: "Check that all values are even",
|
||||
example: "echo [2 4 6 8] | all? $(= $it mod 2) == 0",
|
||||
example: "echo [2 4 6 8] | all? ($it mod 2) == 0",
|
||||
result: Some(vec![Value::from(true)]),
|
||||
},
|
||||
]
|
||||
|
@ -69,7 +69,7 @@ following values:
|
||||
https://en.wikipedia.org/wiki/ANSI_escape_code
|
||||
|
||||
OSC: '\x1b]' is not required for --osc parameter
|
||||
Example: echo [$(ansi -o '0') 'some title' $(char bel)] | str collect
|
||||
Example: echo [(ansi -o '0') 'some title' (char bel)] | str collect
|
||||
Format: #
|
||||
0 Set window title and icon name
|
||||
1 Set icon name
|
||||
@ -96,7 +96,7 @@ Format: #
|
||||
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"#,
|
||||
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",
|
||||
)]),
|
||||
@ -104,7 +104,7 @@ Format: #
|
||||
Example {
|
||||
description:
|
||||
"Use ansi to color text (rb = red bold, gb = green bold, pb = purple bold)",
|
||||
example: r#"echo [$(ansi -e '3;93;41m') Hello $(ansi reset) " " $(ansi gb) Nu " " $(ansi pb) World] | str collect"#,
|
||||
example: r#"echo [(ansi -e '3;93;41m') Hello (ansi reset) " " (ansi gb) Nu " " (ansi pb) World] | str collect"#,
|
||||
result: Some(vec![Value::from(
|
||||
"\u{1b}[3;93;41mHello\u{1b}[0m \u{1b}[1;32mNu \u{1b}[1;35mWorld",
|
||||
)]),
|
||||
@ -299,7 +299,7 @@ pub fn str_to_ansi(s: &str) -> Option<String> {
|
||||
// Reference for ansi codes https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797
|
||||
// Another good reference http://ascii-table.com/ansi-escape-sequences.php
|
||||
|
||||
// For setting title like `echo [$(char title) $(pwd) $(char bel)] | str collect`
|
||||
// For setting title like `echo [(char title) (pwd) (char bel)] | str collect`
|
||||
"title" => Some("\x1b]2;".to_string()), // ESC]2; xterm sets window title using OSC syntax escapes
|
||||
|
||||
// Ansi Erase Sequences
|
||||
|
@ -31,7 +31,7 @@ impl WholeStreamCommand for SubCommand {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "strip ansi escape sequences from string",
|
||||
example: "echo [$(ansi gb) 'hello' $(ansi reset)] | str collect | ansi strip",
|
||||
example: "echo [(ansi gb) 'hello' (ansi reset)] | str collect | ansi strip",
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ impl WholeStreamCommand for Command {
|
||||
},
|
||||
Example {
|
||||
description: "Check if any of the values is odd",
|
||||
example: "echo [2 4 1 6 8] | any? $(= $it mod 2) == 1",
|
||||
example: "echo [2 4 1 6 8] | any? ($it mod 2) == 1",
|
||||
result: Some(vec![Value::from(true)]),
|
||||
},
|
||||
]
|
||||
|
@ -35,7 +35,7 @@ impl WholeStreamCommand for Char {
|
||||
},
|
||||
Example {
|
||||
description: "Output prompt character, newline and a hamburger character",
|
||||
example: r#"echo $(char prompt) $(char newline) $(char hamburger)"#,
|
||||
example: r#"echo (char prompt) (char newline) (char hamburger)"#,
|
||||
result: Some(vec![
|
||||
UntaggedValue::string("\u{25b6}").into(),
|
||||
UntaggedValue::string("\n").into(),
|
||||
|
@ -42,7 +42,7 @@ impl WholeStreamCommand for Each {
|
||||
},
|
||||
Example {
|
||||
description: "Echo the square of each integer",
|
||||
example: "echo [1 2 3] | each { echo $(= $it * $it) }",
|
||||
example: "echo [1 2 3] | each { echo ($it * $it) }",
|
||||
result: Some(vec![
|
||||
UntaggedValue::int(1).into(),
|
||||
UntaggedValue::int(4).into(),
|
||||
|
@ -63,7 +63,7 @@ impl WholeStreamCommand for Command {
|
||||
),
|
||||
},Example {
|
||||
description: "use a block if setting the empty cell contents is wanted",
|
||||
example: "echo [[2020/04/16 2020/07/10 2020/11/16]; ['' [27] [37]]] | empty? 2020/04/16 { = [33 37] }",
|
||||
example: "echo [[2020/04/16 2020/07/10 2020/11/16]; ['' [27] [37]]] | empty? 2020/04/16 { [33 37] }",
|
||||
result: Some(
|
||||
vec![
|
||||
UntaggedValue::row(indexmap! {
|
||||
|
@ -39,12 +39,12 @@ impl WholeStreamCommand for Command {
|
||||
},
|
||||
Example {
|
||||
description: "flatten a column having a nested table",
|
||||
example: "echo [[origin, people]; [Ecuador, $(echo [[name, meal]; ['Andres', 'arepa']])]] | flatten | get meal",
|
||||
example: "echo [[origin, people]; [Ecuador, (echo [[name, meal]; ['Andres', 'arepa']])]] | flatten | get meal",
|
||||
result: Some(vec![Value::from("arepa")]),
|
||||
},
|
||||
Example {
|
||||
description: "restrict the flattening by passing column names",
|
||||
example: "echo [[origin, crate, versions]; [World, $(echo [[name]; ['nu-cli']]), ['0.21', '0.22']]] | flatten versions | last | get versions",
|
||||
example: "echo [[origin, crate, versions]; [World, (echo [[name]; ['nu-cli']]), ['0.21', '0.22']]] | flatten versions | last | get versions",
|
||||
result: Some(vec![Value::from("0.22")]),
|
||||
}
|
||||
]
|
||||
|
@ -100,7 +100,7 @@ impl WholeStreamCommand for Command {
|
||||
Example {
|
||||
description:
|
||||
"use the block form to generate a grouping key when each row gets processed",
|
||||
example: "echo [1 3 1 3 2 1 1] | group-by { = ($it - 1) mod 3 }",
|
||||
example: "echo [1 3 1 3 2 1 1] | group-by { ($it - 1) mod 3 }",
|
||||
result: Some(vec![UntaggedValue::row(indexmap! {
|
||||
"0".to_string() => UntaggedValue::Table(vec![
|
||||
UntaggedValue::int(1).into(),
|
||||
|
@ -51,7 +51,7 @@ impl WholeStreamCommand for Command {
|
||||
.into()]),
|
||||
},Example {
|
||||
description: "Use in block form for more involved insertion logic",
|
||||
example: "echo [[author, lucky_number]; ['Yehuda', 4]] | insert success { = $it.lucky_number * 10 }",
|
||||
example: "echo [[author, lucky_number]; ['Yehuda', 4]] | insert success { $it.lucky_number * 10 }",
|
||||
result: Some(vec![UntaggedValue::row(indexmap! {
|
||||
"author".to_string() => Value::from("Yehuda"),
|
||||
"lucky_number".to_string() => UntaggedValue::int(4).into(),
|
||||
|
@ -67,7 +67,7 @@ On Windows, an extra 'prefix' column is added."#
|
||||
},
|
||||
Example {
|
||||
description: "Replace a complex extension",
|
||||
example: r"echo 'C:\Users\viking\spam.tar.gz' | path parse -e tar.gz | update extension { = txt }",
|
||||
example: r"echo 'C:\Users\viking\spam.tar.gz' | path parse -e tar.gz | update extension { 'txt' }",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
@ -93,7 +93,7 @@ On Windows, an extra 'prefix' column is added."#
|
||||
},
|
||||
Example {
|
||||
description: "Replace a complex extension",
|
||||
example: r"echo '/home/viking/spam.tar.gz' | path parse -e tar.gz | update extension { = txt }",
|
||||
example: r"echo '/home/viking/spam.tar.gz' | path parse -e tar.gz | update extension { 'txt' }",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
|
@ -57,22 +57,22 @@ impl WholeStreamCommand for Reduce {
|
||||
vec![
|
||||
Example {
|
||||
description: "Simple summation (equivalent to math sum)",
|
||||
example: "echo 1 2 3 4 | reduce { = $acc + $it }",
|
||||
example: "echo 1 2 3 4 | reduce { $acc + $it }",
|
||||
result: Some(vec![UntaggedValue::int(10).into()]),
|
||||
},
|
||||
Example {
|
||||
description: "Summation from starting value using fold",
|
||||
example: "echo 1 2 3 4 | reduce -f $(= -1) { = $acc + $it }",
|
||||
example: "echo 1 2 3 4 | reduce -f (-1) { $acc + $it }",
|
||||
result: Some(vec![UntaggedValue::int(9).into()]),
|
||||
},
|
||||
Example {
|
||||
description: "Folding with rows",
|
||||
example: "<table> | reduce -f 1.6 { = $acc * $(echo $it.a | str to-int) + $(echo $it.b | str to-int) }",
|
||||
example: "<table> | reduce -f 1.6 { $acc * (echo $it.a | str to-int) + (echo $it.b | str to-int) }",
|
||||
result: None,
|
||||
},
|
||||
Example {
|
||||
description: "Numbered reduce to find index of longest word",
|
||||
example: "echo one longest three bar | reduce -n { if $(echo $it.item | str length) > $(echo $acc.item | str length) {echo $it} {echo $acc}} | get index",
|
||||
example: "echo one longest three bar | reduce -n { if ($it.item | str length) > ($acc.item | str length) {echo $it} {echo $acc}} | get index",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
|
@ -69,7 +69,7 @@ impl WholeStreamCommand for Command {
|
||||
},
|
||||
Example {
|
||||
description: "Treat each row as a markdown element",
|
||||
example: "echo [[H1]; [\"Welcome to Nushell\"]] | append $(ls | first 2) | to md --per-element --pretty",
|
||||
example: "echo [[H1]; [\"Welcome to Nushell\"]] | append (ls | first 2) | to md --per-element --pretty",
|
||||
result: Some(vec![Value::from(one(r#"
|
||||
# Welcome to Nushell
|
||||
| name | type | chickens | modified |
|
||||
|
Reference in New Issue
Block a user