Automatically trim ends of stdin/stdout strings (#874)

This commit is contained in:
JT 2022-01-28 16:59:00 -05:00 committed by GitHub
parent c37f844644
commit 4c029d2545
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 28 additions and 5 deletions

View File

@ -124,6 +124,7 @@ impl Command for Open {
RawStream::new(
Box::new(BufferedReader { input: buf_reader }),
ctrlc,
false,
call_span,
),
call_span,

View File

@ -362,6 +362,7 @@ fn response_to_buffer(
input: buffered_input,
}),
engine_state.ctrlc.clone(),
false,
span,
),
span,

View File

@ -243,7 +243,7 @@ impl ExternalCommand {
let receiver = ChannelReceiver::new(rx);
Ok(PipelineData::RawStream(
RawStream::new(Box::new(receiver), output_ctrlc, head),
RawStream::new(Box::new(receiver), output_ctrlc, true, head),
head,
None,
))

View File

@ -72,6 +72,7 @@ impl Command for Table {
.into_iter(),
),
ctrlc,
false,
head,
),
head,
@ -188,6 +189,7 @@ impl Command for Table {
stream,
}),
ctrlc,
false,
head,
),
head,

View File

@ -9,5 +9,7 @@ pub use call_ext::CallExt;
pub use column::get_columns;
pub use documentation::{generate_docs, get_brief_help, get_documentation, get_full_help};
pub use env::*;
pub use eval::{eval_block, eval_expression, eval_expression_with_input, eval_operator};
pub use eval::{
eval_block, eval_expression, eval_expression_with_input, eval_operator, eval_subexpression,
};
pub use glob_from::glob_from;

View File

@ -150,6 +150,8 @@ impl PipelineData {
PipelineData::RawStream(s, ..) => {
let mut items = vec![];
let trim_end = s.trim_end;
for val in s {
match val {
Ok(val) => {
@ -170,6 +172,11 @@ impl PipelineData {
}
}
}
if trim_end {
output = output.trim_end().to_string();
}
Ok(output)
}
}

View File

@ -12,6 +12,7 @@ pub struct RawStream {
pub leftover: Vec<u8>,
pub ctrlc: Option<Arc<AtomicBool>>,
pub is_binary: bool,
pub trim_end: bool,
pub span: Span,
}
@ -19,6 +20,7 @@ impl RawStream {
pub fn new(
stream: Box<dyn Iterator<Item = Result<Vec<u8>, ShellError>> + Send + 'static>,
ctrlc: Option<Arc<AtomicBool>>,
trim_end: bool,
span: Span,
) -> Self {
Self {
@ -26,6 +28,7 @@ impl RawStream {
leftover: vec![],
ctrlc,
is_binary: false,
trim_end,
span,
}
}
@ -43,10 +46,16 @@ impl RawStream {
pub fn into_string(self) -> Result<String, ShellError> {
let mut output = String::new();
let trim_end = self.trim_end;
for item in self {
output.push_str(&item?.as_string()?);
}
if trim_end {
output = output.trim_end().to_string();
}
Ok(output)
}
}

View File

@ -123,6 +123,7 @@ fn main() -> Result<()> {
RawStream::new(
Box::new(BufferedReader::new(buf_reader)),
Some(ctrlc),
true,
redirect_stdin.span,
),
redirect_stdin.span,

View File

@ -1,5 +1,5 @@
use nu_cli::NushellPrompt;
use nu_engine::eval_block;
use nu_engine::eval_subexpression;
use nu_parser::parse;
use nu_protocol::{
engine::{EngineState, Stack, StateWorkingSet},
@ -59,7 +59,7 @@ fn get_prompt_string(
.and_then(|v| match v {
Value::Block { val: block_id, .. } => {
let block = engine_state.get_block(block_id);
eval_block(
eval_subexpression(
engine_state,
stack,
block,
@ -70,7 +70,7 @@ fn get_prompt_string(
Value::String { val: source, .. } => {
let mut working_set = StateWorkingSet::new(engine_state);
let (block, _) = parse(&mut working_set, None, source.as_bytes(), true);
eval_block(
eval_subexpression(
engine_state,
stack,
&block,