Make external command substitution works friendly(like fish shell, trailing ending newlines) (#7156)

# Description

As title, when execute external sub command, auto-trimming end
new-lines, like how fish shell does.

And if the command is executed directly like: `cat tmp`, the result
won't change.

Fixes: #6816
Fixes: #3980


Note that although nushell works correctly by directly replace output of
external command to variable(or other places like string interpolation),
it's not friendly to user, and users almost want to use `str trim` to
trim trailing newline, I think that's why fish shell do this
automatically.

If the pr is ok, as a result, no more `str trim -r` is required when
user is writing scripts which using external commands.

# User-Facing Changes
Before:
<img width="523" alt="img"
src="https://user-images.githubusercontent.com/22256154/202468810-86b04dbb-c147-459a-96a5-e0095eeaab3d.png">

After:
<img width="505" alt="img"
src="https://user-images.githubusercontent.com/22256154/202468599-7b537488-3d6b-458e-9d75-d85780826db0.png">


# Tests + Formatting

Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace --features=extra -- -D warnings -D
clippy::unwrap_used -A clippy::needless_collect` to check that you're
using the standard code style
- `cargo test --workspace --features=extra` to check that all tests pass

# After Submitting

If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
WindSoilder
2022-11-23 11:51:57 +08:00
committed by GitHub
parent 8cda641350
commit b662c2eb96
18 changed files with 162 additions and 25 deletions

View File

@ -19,7 +19,7 @@ pub enum Expr {
Var(VarId),
VarDecl(VarId),
Call(Box<Call>),
ExternalCall(Box<Expression>, Vec<Expression>),
ExternalCall(Box<Expression>, Vec<Expression>, bool), // head, args, is_subexpression
Operator(Operator),
RowCondition(BlockId),
UnaryNot(Box<Expression>),

View File

@ -177,7 +177,7 @@ impl Expression {
}
Expr::CellPath(_) => false,
Expr::DateTime(_) => false,
Expr::ExternalCall(head, args) => {
Expr::ExternalCall(head, args, _) => {
if head.has_in_variable(working_set) {
return true;
}
@ -374,7 +374,7 @@ impl Expression {
}
Expr::CellPath(_) => {}
Expr::DateTime(_) => {}
Expr::ExternalCall(head, args) => {
Expr::ExternalCall(head, args, _) => {
head.replace_in_variable(working_set, new_var_id);
for arg in args {
arg.replace_in_variable(working_set, new_var_id)
@ -534,7 +534,7 @@ impl Expression {
}
Expr::CellPath(_) => {}
Expr::DateTime(_) => {}
Expr::ExternalCall(head, args) => {
Expr::ExternalCall(head, args, _) => {
head.replace_span(working_set, replaced, new_span);
for arg in args {
arg.replace_span(working_set, replaced, new_span)

View File

@ -6,6 +6,12 @@ use crate::{
use nu_utils::{stderr_write_all_and_flush, stdout_write_all_and_flush};
use std::sync::{atomic::AtomicBool, Arc};
const LINE_ENDING: &str = if cfg!(target_os = "windows") {
"\r\n"
} else {
"\n"
};
/// The foundational abstraction for input and output to commands
///
/// This represents either a single Value or a stream of values coming into the command or leaving a command.
@ -45,6 +51,7 @@ pub enum PipelineData {
exit_code: Option<ListStream>,
span: Span,
metadata: Option<PipelineMetadata>,
trim_end_newline: bool,
},
}
@ -121,6 +128,7 @@ impl PipelineData {
PipelineData::ExternalStream {
stdout: Some(mut s),
exit_code,
trim_end_newline,
..
} => {
let mut items = vec![];
@ -141,6 +149,8 @@ impl PipelineData {
let _: Vec<_> = exit_code.into_iter().collect();
}
// NOTE: currently trim-end-newline only handles for string output.
// For binary, user might need origin data.
if s.is_binary {
let mut output = vec![];
for item in items {
@ -168,6 +178,9 @@ impl PipelineData {
}
}
}
if trim_end_newline {
output.truncate(output.trim_end_matches(LINE_ENDING).len())
}
Value::String {
val: output,
span, // FIXME?
@ -193,7 +206,9 @@ impl PipelineData {
PipelineData::ListStream(s, ..) => Ok(s.into_string(separator, config)),
PipelineData::ExternalStream { stdout: None, .. } => Ok(String::new()),
PipelineData::ExternalStream {
stdout: Some(s), ..
stdout: Some(s),
trim_end_newline,
..
} => {
let mut output = String::new();
@ -206,6 +221,9 @@ impl PipelineData {
Err(e) => return Err(e),
}
}
if trim_end_newline {
output.truncate(output.trim_end_matches(LINE_ENDING).len());
}
Ok(output)
}
}
@ -294,11 +312,15 @@ impl PipelineData {
}
PipelineData::ExternalStream {
stdout: Some(stream),
trim_end_newline,
..
} => {
let collected = stream.into_bytes()?;
if let Ok(st) = String::from_utf8(collected.clone().item) {
if let Ok(mut st) = String::from_utf8(collected.clone().item) {
if trim_end_newline {
st.truncate(st.trim_end_matches(LINE_ENDING).len());
}
Ok(f(Value::String {
val: st,
span: collected.span,
@ -348,11 +370,15 @@ impl PipelineData {
}
PipelineData::ExternalStream {
stdout: Some(stream),
trim_end_newline,
..
} => {
let collected = stream.into_bytes()?;
if let Ok(st) = String::from_utf8(collected.clone().item) {
if let Ok(mut st) = String::from_utf8(collected.clone().item) {
if trim_end_newline {
st.truncate(st.trim_end_matches(LINE_ENDING).len())
}
Ok(f(Value::String {
val: st,
span: collected.span,
@ -397,11 +423,15 @@ impl PipelineData {
}
PipelineData::ExternalStream {
stdout: Some(stream),
trim_end_newline,
..
} => {
let collected = stream.into_bytes()?;
if let Ok(st) = String::from_utf8(collected.clone().item) {
if let Ok(mut st) = String::from_utf8(collected.clone().item) {
if trim_end_newline {
st.truncate(st.trim_end_matches(LINE_ENDING).len())
}
let v = Value::String {
val: st,
span: collected.span,