refactor all write_alls to ensure flushing (#5567)

This commit is contained in:
Darren Schroeder
2022-05-17 13:28:18 -05:00
committed by GitHub
parent f26d3bf8d7
commit f0cb2f38df
14 changed files with 85 additions and 85 deletions

View File

@ -1,23 +1,20 @@
use super::{Command, EnvVars, Stack};
use crate::Value;
use crate::{
ast::Block, AliasId, BlockId, Config, DeclId, Example, Module, ModuleId, OverlayId, ShellError,
Signature, Span, Type, VarId, Variable,
};
use core::panic;
use std::borrow::Borrow;
use std::collections::HashSet;
use std::path::Path;
#[cfg(feature = "plugin")]
use std::path::PathBuf;
use std::{
collections::HashMap,
sync::{atomic::AtomicBool, Arc},
};
use crate::Value;
use std::borrow::Borrow;
use std::collections::HashSet;
use std::path::Path;
#[cfg(feature = "plugin")]
use std::path::PathBuf;
static PWD_ENV: &str = "PWD";
pub static DEFAULT_OVERLAY_NAME: &str = "zero";
@ -651,6 +648,17 @@ impl EngineState {
.write_all(line.as_bytes())
.map_err(|err| ShellError::PluginFailedToLoad(err.to_string()))
})
.and_then(|_| {
plugin_file.flush().map_err(|err| {
ShellError::GenericError(
"Error flushing plugin file".to_string(),
format! {"{}", err},
None,
None,
Vec::new(),
)
})
})
})
})
}

View File

@ -1,13 +1,10 @@
use std::{
io::Write,
sync::{atomic::AtomicBool, Arc},
};
use crate::{
ast::{Call, PathMember},
engine::{EngineState, Stack, StateWorkingSet},
format_error, Config, ListStream, RawStream, ShellError, Span, Value,
};
use nu_utils::{stdout_write_all_and_flush, stdout_write_all_as_binary_and_flush};
use std::sync::{atomic::AtomicBool, Arc};
/// The foundational abstraction for input and output to commands
///
@ -426,7 +423,7 @@ impl PipelineData {
// to create the table value that will be printed in the terminal
let config = engine_state.get_config();
let stdout = std::io::stdout();
// let stdout = std::io::stdout();
if let PipelineData::ExternalStream {
stdout: stream,
@ -436,8 +433,9 @@ impl PipelineData {
{
if let Some(stream) = stream {
for s in stream {
let _ = stdout.lock().write_all(s?.as_binary()?);
let _ = stdout.lock().flush()?;
let s_live = s?;
let bin_output = s_live.as_binary()?;
stdout_write_all_as_binary_and_flush(bin_output)?
}
}
@ -459,8 +457,6 @@ impl PipelineData {
)?;
for item in table {
let stdout = std::io::stdout();
let mut out = if let Value::Error { error } = item {
let working_set = StateWorkingSet::new(engine_state);
@ -475,17 +471,11 @@ impl PipelineData {
out.push('\n');
}
match stdout.lock().write_all(out.as_bytes()) {
Ok(_) => {
let _ = stdout.lock().flush()?;
}
Err(err) => eprintln!("{}", err),
};
stdout_write_all_and_flush(out)?
}
}
None => {
for item in self {
let stdout = std::io::stdout();
let mut out = if let Value::Error { error } = item {
let working_set = StateWorkingSet::new(engine_state);
@ -500,12 +490,7 @@ impl PipelineData {
out.push('\n');
}
match stdout.lock().write_all(out.as_bytes()) {
Ok(_) => {
let _ = stdout.lock().flush()?;
}
Err(err) => eprintln!("{}", err),
};
stdout_write_all_and_flush(out)?
}
}
};