Skip to content

Commit

Permalink
Automatically trim ends of stdin/stdout strings (nushell#874)
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiajt committed Jan 28, 2022
1 parent c37f844 commit 4c029d2
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 5 deletions.
1 change: 1 addition & 0 deletions crates/nu-command/src/filesystem/open.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ impl Command for Open {
RawStream::new(
Box::new(BufferedReader { input: buf_reader }),
ctrlc,
false,
call_span,
),
call_span,
Expand Down
1 change: 1 addition & 0 deletions crates/nu-command/src/network/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ fn response_to_buffer(
input: buffered_input,
}),
engine_state.ctrlc.clone(),
false,
span,
),
span,
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-command/src/system/run_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
))
Expand Down
2 changes: 2 additions & 0 deletions crates/nu-command/src/viewers/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ impl Command for Table {
.into_iter(),
),
ctrlc,
false,
head,
),
head,
Expand Down Expand Up @@ -188,6 +189,7 @@ impl Command for Table {
stream,
}),
ctrlc,
false,
head,
),
head,
Expand Down
4 changes: 3 additions & 1 deletion crates/nu-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
7 changes: 7 additions & 0 deletions crates/nu-protocol/src/pipeline_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -170,6 +172,11 @@ impl PipelineData {
}
}
}

if trim_end {
output = output.trim_end().to_string();
}

Ok(output)
}
}
Expand Down
9 changes: 9 additions & 0 deletions crates/nu-protocol/src/value/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,23 @@ pub struct RawStream {
pub leftover: Vec<u8>,
pub ctrlc: Option<Arc<AtomicBool>>,
pub is_binary: bool,
pub trim_end: bool,
pub span: Span,
}

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 {
stream,
leftover: vec![],
ctrlc,
is_binary: false,
trim_end,
span,
}
}
Expand All @@ -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)
}
}
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ fn main() -> Result<()> {
RawStream::new(
Box::new(BufferedReader::new(buf_reader)),
Some(ctrlc),
true,
redirect_stdin.span,
),
redirect_stdin.span,
Expand Down
6 changes: 3 additions & 3 deletions src/prompt_update.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 4c029d2

Please sign in to comment.