Simplify PipelineData::print (#6119)

* Simplify PipelineData::print

Signed-off-by: nibon7 <nibon7@163.com>

* make write_all_and_flush to be associated function

Signed-off-by: nibon7 <nibon7@163.com>
This commit is contained in:
nibon7 2022-07-25 19:38:21 +08:00 committed by GitHub
parent 8b35239bce
commit 5a5c65ee4b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -467,55 +467,47 @@ impl PipelineData {
self,
)?;
for item in table {
let mut out = if let Value::Error { error } = item {
let working_set = StateWorkingSet::new(engine_state);
format_error(&working_set, &error)
} else if no_newline {
item.into_string("", config)
} else {
item.into_string("\n", config)
};
if !no_newline {
out.push('\n');
}
if !to_stderr {
stdout_write_all_and_flush(out)?
} else {
stderr_write_all_and_flush(out)?
}
}
table.write_all_and_flush(engine_state, config, no_newline, to_stderr)?;
}
None => {
for item in self {
let mut out = if let Value::Error { error } = item {
let working_set = StateWorkingSet::new(engine_state);
format_error(&working_set, &error)
} else if no_newline {
item.into_string("", config)
} else {
item.into_string("\n", config)
};
if !no_newline {
out.push('\n');
}
if !to_stderr {
stdout_write_all_and_flush(out)?
} else {
stderr_write_all_and_flush(out)?
}
}
self.write_all_and_flush(engine_state, config, no_newline, to_stderr)?;
}
};
Ok(())
}
fn write_all_and_flush(
self,
engine_state: &EngineState,
config: &Config,
no_newline: bool,
to_stderr: bool,
) -> Result<(), ShellError> {
for item in self {
let mut out = if let Value::Error { error } = item {
let working_set = StateWorkingSet::new(engine_state);
format_error(&working_set, &error)
} else if no_newline {
item.into_string("", config)
} else {
item.into_string("\n", config)
};
if !no_newline {
out.push('\n');
}
if !to_stderr {
stdout_write_all_and_flush(out)?
} else {
stderr_write_all_and_flush(out)?
}
}
Ok(())
}
}
pub struct PipelineIterator(PipelineData);