mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-31 23:28:45 +00:00
Add a string_output_stream_t to collect builtin output
This is used when creating a function; this breaks a dependency on the more complicated buffered_output_stream_t to ease refactoring.
This commit is contained in:
parent
fc97151aec
commit
cd9a035f02
3 changed files with 18 additions and 4 deletions
|
@ -373,6 +373,8 @@ void fd_output_stream_t::append(const wchar_t *s, size_t amt) {
|
||||||
|
|
||||||
void null_output_stream_t::append(const wchar_t *, size_t) {}
|
void null_output_stream_t::append(const wchar_t *, size_t) {}
|
||||||
|
|
||||||
|
void string_output_stream_t::append(const wchar_t *s, size_t amt) { contents_.append(s, amt); }
|
||||||
|
|
||||||
void buffered_output_stream_t::append(const wchar_t *s, size_t amt) { buffer_.append(s, s + amt); }
|
void buffered_output_stream_t::append(const wchar_t *s, size_t amt) { buffer_.append(s, s + amt); }
|
||||||
|
|
||||||
void buffered_output_stream_t::append_with_separation(const wchar_t *s, size_t len,
|
void buffered_output_stream_t::append_with_separation(const wchar_t *s, size_t len,
|
||||||
|
|
13
src/io.h
13
src/io.h
|
@ -475,6 +475,19 @@ class fd_output_stream_t final : public output_stream_t {
|
||||||
bool errored_{false};
|
bool errored_{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/// A simple output stream which buffers into a wcstring.
|
||||||
|
class string_output_stream_t final : public output_stream_t {
|
||||||
|
public:
|
||||||
|
string_output_stream_t() = default;
|
||||||
|
void append(const wchar_t *s, size_t amt) override;
|
||||||
|
|
||||||
|
/// \return the wcstring containing the output.
|
||||||
|
const wcstring &contents() const { return contents_; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
wcstring contents_;
|
||||||
|
};
|
||||||
|
|
||||||
/// An output stream for builtins which buffers into a separated buffer.
|
/// An output stream for builtins which buffers into a separated buffer.
|
||||||
class buffered_output_stream_t final : public output_stream_t {
|
class buffered_output_stream_t final : public output_stream_t {
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -390,9 +390,8 @@ end_execution_reason_t parse_execution_context_t::run_function_statement(
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
trace_if_enabled(*parser, L"function", arguments);
|
trace_if_enabled(*parser, L"function", arguments);
|
||||||
// no limit on the amount of output from builtin_function()
|
null_output_stream_t outs;
|
||||||
buffered_output_stream_t outs(0);
|
string_output_stream_t errs;
|
||||||
buffered_output_stream_t errs(0);
|
|
||||||
io_streams_t streams(outs, errs);
|
io_streams_t streams(outs, errs);
|
||||||
int err_code = 0;
|
int err_code = 0;
|
||||||
maybe_t<int> err = builtin_function(*parser, streams, arguments, pstree, statement);
|
maybe_t<int> err = builtin_function(*parser, streams, arguments, pstree, statement);
|
||||||
|
@ -402,7 +401,7 @@ end_execution_reason_t parse_execution_context_t::run_function_statement(
|
||||||
parser->set_last_statuses(statuses_t::just(err_code));
|
parser->set_last_statuses(statuses_t::just(err_code));
|
||||||
}
|
}
|
||||||
|
|
||||||
wcstring errtext = errs.contents();
|
const wcstring &errtext = errs.contents();
|
||||||
if (!errtext.empty()) {
|
if (!errtext.empty()) {
|
||||||
return this->report_error(err_code, header, L"%ls", errtext.c_str());
|
return this->report_error(err_code, header, L"%ls", errtext.c_str());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue