mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Experiment: Allow both $true/true and $false/false (#4696)
* Change true/false to keywords * oops, clippy * Both kinds of bools * Add in some boolean variables * disable py virtualenv test for now
This commit is contained in:
@ -70,9 +70,7 @@ impl NuCompleter {
|
||||
) -> Vec<(reedline::Span, String)> {
|
||||
let mut output = vec![];
|
||||
|
||||
let builtins = [
|
||||
"$nu", "$scope", "$in", "$config", "$env", "$true", "$false", "$nothing",
|
||||
];
|
||||
let builtins = ["$nu", "$scope", "$in", "$config", "$env", "$nothing"];
|
||||
|
||||
for builtin in builtins {
|
||||
if builtin.as_bytes().starts_with(prefix) {
|
||||
|
@ -61,7 +61,7 @@ impl Command for SubCommand {
|
||||
},
|
||||
Example {
|
||||
description: "convert a boolean to a nushell binary primitive",
|
||||
example: "$true | into binary",
|
||||
example: "true | into binary",
|
||||
result: Some(Value::Binary {
|
||||
val: i64::from(1).to_le_bytes().to_vec(),
|
||||
span: Span::test_data(),
|
||||
|
@ -42,7 +42,7 @@ impl Command for SubCommand {
|
||||
vec![
|
||||
Example {
|
||||
description: "Convert value to boolean in table",
|
||||
example: "echo [[value]; ['false'] ['1'] [0] [1.0] [$true]] | into bool value",
|
||||
example: "echo [[value]; ['false'] ['1'] [0] [1.0] [true]] | into bool value",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::Record {
|
||||
@ -76,7 +76,7 @@ impl Command for SubCommand {
|
||||
},
|
||||
Example {
|
||||
description: "Convert bool to boolean",
|
||||
example: "$true | into bool",
|
||||
example: "true | into bool",
|
||||
result: Some(Value::boolean(true, span)),
|
||||
},
|
||||
Example {
|
||||
|
@ -75,7 +75,7 @@ impl Command for SubCommand {
|
||||
},
|
||||
Example {
|
||||
description: "Convert bool to integer",
|
||||
example: "[$false, $true] | into int",
|
||||
example: "[false, true] | into int",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::test_int(0), Value::test_int(1)],
|
||||
span: Span::test_data(),
|
||||
|
@ -103,7 +103,7 @@ impl Command for SubCommand {
|
||||
},
|
||||
Example {
|
||||
description: "convert boolean to string",
|
||||
example: "$true | into string",
|
||||
example: "true | into string",
|
||||
result: Some(Value::String {
|
||||
val: "true".to_string(),
|
||||
span: Span::test_data(),
|
||||
|
@ -60,8 +60,7 @@ impl Command for Debug {
|
||||
},
|
||||
Example {
|
||||
description: "Print the value of a table",
|
||||
example:
|
||||
"echo [[version patch]; [0.1.0 $false] [0.1.1 $true] [0.2.0 $false]] | debug",
|
||||
example: "echo [[version patch]; [0.1.0 false] [0.1.1 true] [0.2.0 false]] | debug",
|
||||
result: Some(Value::List {
|
||||
vals: vec![
|
||||
Value::test_string("{version: 0.1.0, patch: false}"),
|
||||
|
@ -70,7 +70,7 @@ impl Command for Let {
|
||||
},
|
||||
Example {
|
||||
description: "Set a variable based on the condition",
|
||||
example: "let x = if $false { -1 } else { 1 }",
|
||||
example: "let x = if false { -1 } else { 1 }",
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
|
@ -351,7 +351,7 @@ ls | each {|x| $x.name}
|
||||
```
|
||||
The above will create a list of the filenames in the directory.
|
||||
```
|
||||
if $true { echo "it's true" } { echo "it's not true" }
|
||||
if true { echo "it's true" } { echo "it's not true" }
|
||||
```
|
||||
This `if` call will run the first block if the expression is true, or the
|
||||
second block if the expression is false.
|
||||
|
@ -28,7 +28,7 @@ impl Command for FilterWith {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Filter dataframe using a bool mask",
|
||||
example: r#"let mask = ([$true $false] | dfr to-df);
|
||||
example: r#"let mask = ([true false] | dfr to-df);
|
||||
[[a b]; [1 2] [3 4]] | dfr to-df | dfr filter-with $mask"#,
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![
|
||||
|
@ -86,7 +86,7 @@ impl Command for ToDataFrame {
|
||||
},
|
||||
Example {
|
||||
description: "Takes a list of booleans and creates a dataframe",
|
||||
example: "[$true $true $false] | dfr to-df",
|
||||
example: "[true true false] | dfr to-df",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
"0".to_string(),
|
||||
|
@ -26,7 +26,7 @@ impl Command for AllFalse {
|
||||
vec![
|
||||
Example {
|
||||
description: "Returns true if all values are false",
|
||||
example: "[$false $false $false] | dfr to-df | dfr all-false",
|
||||
example: "[false false false] | dfr to-df | dfr all-false",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
"all_false".to_string(),
|
||||
|
@ -26,7 +26,7 @@ impl Command for AllTrue {
|
||||
vec![
|
||||
Example {
|
||||
description: "Returns true if all values are true",
|
||||
example: "[$true $true $true] | dfr to-df | dfr all-true",
|
||||
example: "[true true true] | dfr to-df | dfr all-true",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
"all_true".to_string(),
|
||||
|
@ -26,7 +26,7 @@ impl Command for ArgTrue {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Returns indexes where values are true",
|
||||
example: "[$false $true $false] | dfr to-df | dfr arg-true",
|
||||
example: "[false true false] | dfr to-df | dfr arg-true",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
"arg_true".to_string(),
|
||||
|
@ -28,7 +28,7 @@ impl Command for NotSeries {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Inverts boolean mask",
|
||||
example: "[$true $false $true] | dfr to-df | dfr not",
|
||||
example: "[true false true] | dfr to-df | dfr not",
|
||||
result: Some(
|
||||
NuDataFrame::try_from_columns(vec![Column::new(
|
||||
"0".to_string(),
|
||||
|
@ -91,7 +91,7 @@ impl Command for Find {
|
||||
},
|
||||
Example {
|
||||
description: "Find if a service is not running",
|
||||
example: "[[version patch]; [0.1.0 $false] [0.1.1 $true] [0.2.0 $false]] | find -p { |it| $it.patch }",
|
||||
example: "[[version patch]; [0.1.0 false] [0.1.1 true] [0.2.0 false]] | find -p { |it| $it.patch }",
|
||||
result: Some(Value::List {
|
||||
vals: vec![Value::test_record(
|
||||
vec!["version", "patch"],
|
||||
|
@ -38,7 +38,7 @@ impl Command for Shuffle {
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: "Shuffle rows randomly (execute it several times and see the difference)",
|
||||
example: r#"echo [[version patch]; [1.0.0 $false] [3.0.1 $true] [2.0.0 $false]] | shuffle"#,
|
||||
example: r#"echo [[version patch]; [1.0.0 false] [3.0.1 true] [2.0.0 false]] | shuffle"#,
|
||||
result: None,
|
||||
}]
|
||||
}
|
||||
|
@ -65,9 +65,9 @@ fn value_to_string(v: &Value, span: Span) -> Result<String, ShellError> {
|
||||
)),
|
||||
Value::Bool { val, .. } => {
|
||||
if *val {
|
||||
Ok("$true".to_string())
|
||||
Ok("true".to_string())
|
||||
} else {
|
||||
Ok("$false".to_string())
|
||||
Ok("false".to_string())
|
||||
}
|
||||
}
|
||||
Value::CellPath { .. } => Err(ShellError::UnsupportedInput(
|
||||
|
@ -79,7 +79,7 @@ impl Command for Shells {
|
||||
},
|
||||
Example {
|
||||
description: "Show currently active shell",
|
||||
example: r#"shells | where active == $true"#,
|
||||
example: r#"shells | where active == true"#,
|
||||
result: None,
|
||||
},
|
||||
]
|
||||
|
@ -169,7 +169,7 @@ prints out the list properly."#
|
||||
},
|
||||
Example {
|
||||
description: "Render a table with 'name' column in it to a grid",
|
||||
example: "[[name patch]; [0.1.0 $false] [0.1.1 $true] [0.2.0 $false]] | grid",
|
||||
example: "[[name patch]; [0.1.0 false] [0.1.1 true] [0.2.0 false]] | grid",
|
||||
result: Some(Value::String {
|
||||
val: "0.1.0 │ 0.1.1 │ 0.2.0".to_string(),
|
||||
span: Span::test_data(),
|
||||
|
@ -45,7 +45,7 @@ fn from_boolean() {
|
||||
let actual = nu!(
|
||||
cwd: ".", pipeline(
|
||||
r#"
|
||||
echo $true | into string
|
||||
echo true | into string
|
||||
"#
|
||||
)
|
||||
);
|
||||
|
@ -65,7 +65,7 @@ fn to_nuon_bool() {
|
||||
let actual = nu!(
|
||||
cwd: "tests/fixtures/formats", pipeline(
|
||||
r#"
|
||||
$false
|
||||
false
|
||||
| to nuon
|
||||
| from nuon
|
||||
"#
|
||||
|
@ -49,6 +49,10 @@ pub fn is_math_expression_like(bytes: &[u8]) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
if bytes == b"true" || bytes == b"false" {
|
||||
return true;
|
||||
}
|
||||
|
||||
let b = bytes[0];
|
||||
|
||||
b == b'0'
|
||||
@ -3258,6 +3262,41 @@ pub fn parse_value(
|
||||
return parse_variable_expr(working_set, span);
|
||||
}
|
||||
|
||||
if bytes == b"true" {
|
||||
if matches!(shape, SyntaxShape::Boolean) || matches!(shape, SyntaxShape::Any) {
|
||||
return (
|
||||
Expression {
|
||||
expr: Expr::Bool(true),
|
||||
span,
|
||||
ty: Type::Bool,
|
||||
custom_completion: None,
|
||||
},
|
||||
None,
|
||||
);
|
||||
} else {
|
||||
return (
|
||||
Expression::garbage(span),
|
||||
Some(ParseError::Expected("non-boolean value".into(), span)),
|
||||
);
|
||||
}
|
||||
} else if bytes == b"false" {
|
||||
if matches!(shape, SyntaxShape::Boolean) || matches!(shape, SyntaxShape::Any) {
|
||||
return (
|
||||
Expression {
|
||||
expr: Expr::Bool(false),
|
||||
span,
|
||||
ty: Type::Bool,
|
||||
custom_completion: None,
|
||||
},
|
||||
None,
|
||||
);
|
||||
} else {
|
||||
return (
|
||||
Expression::garbage(span),
|
||||
Some(ParseError::Expected("non-boolean value".into(), span)),
|
||||
);
|
||||
}
|
||||
}
|
||||
match bytes[0] {
|
||||
b'$' => return parse_dollar_expr(working_set, span),
|
||||
b'(' => {
|
||||
@ -3381,7 +3420,7 @@ pub fn parse_value(
|
||||
}
|
||||
SyntaxShape::Boolean => {
|
||||
// Redundant, though we catch bad boolean parses here
|
||||
if bytes == b"$true" || bytes == b"$false" {
|
||||
if bytes == b"true" || bytes == b"false" {
|
||||
(
|
||||
Expression {
|
||||
expr: Expr::Bool(true),
|
||||
@ -3603,6 +3642,7 @@ pub fn parse_expression(
|
||||
while pos < spans.len() {
|
||||
// Check if there is any environment shorthand
|
||||
let name = working_set.get_span_contents(spans[pos]);
|
||||
|
||||
let split = name.split(|x| *x == b'=');
|
||||
let split: Vec<_> = split.collect();
|
||||
if split.len() == 2 && !split[0].is_empty() {
|
||||
|
Reference in New Issue
Block a user