diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-07-21 04:26:10 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-07-21 04:26:10 +0800 |
| commit | 2683d7c6c3c88355fd3188ac2b2c68338ae2d205 (patch) | |
| tree | cc89cfe0d377e8550aa3577add86f4366b804c07 /mingling_core/src | |
| parent | a3b75b7eeae5e7a0c53b6167bafb75a85fa6d9ce (diff) | |
feat(core, macros): add r_append! macro and RenderResult::append_other
Implement `RenderResult::append_other()` to merge buffered content and
immediate output behavior from one result into another. Add the
`r_append!` macro with both explicit buffer (`dst, src`) and implicit
buffer (`src`) usage forms, re-exported from `mingling::prelude`.
Diffstat (limited to 'mingling_core/src')
| -rw-r--r-- | mingling_core/src/renderer/render_result.rs | 44 |
1 files changed, 44 insertions, 0 deletions
diff --git a/mingling_core/src/renderer/render_result.rs b/mingling_core/src/renderer/render_result.rs index af63cdd..a495857 100644 --- a/mingling_core/src/renderer/render_result.rs +++ b/mingling_core/src/renderer/render_result.rs @@ -209,6 +209,50 @@ impl RenderResult { self.append_to_buffer("\n", mode); } + /// Appends the contents of another `RenderResult` to this one. + /// + /// If this `RenderResult` has `immediate_output` enabled but the other does not, + /// the other's content will be immediately flushed to the appropriate output stream + /// (stdout/stderr) while also being appended to the render buffer. + /// + /// The `exit_code` of the other result is **not** transferred — only the buffered + /// content and the `immediate_output` flag of the other result are merged. + /// + /// # Arguments + /// + /// * `other` - The `RenderResult` whose contents should be appended. + /// + /// # Examples + /// + /// ``` + /// use mingling_core::{RenderResult, RenderResultMode}; + /// + /// let mut dest = RenderResult::default(); + /// let mut src = RenderResult::default(); + /// + /// src.append_to_buffer("Hello", RenderResultMode::Stdout); + /// src.append_to_buffer(" Error", RenderResultMode::Stderr); + /// + /// dest.append_otehr(src); + /// assert_eq!(dest.to_string(), "Hello Error"); + /// ``` + pub fn append_other(&mut self, other: impl Into<RenderResult>) { + let other = other.into(); + + // If self has immediate output enabled, but the input does not, the input needs immediate output. + let immediate_output = !other.immediate_output && self.immediate_output; + + for i in other.render_buffer { + if immediate_output { + match &i.1 { + Stdout => print!("{}", i.0), + Stderr => eprint!("{}", i.0), + } + } + self.render_buffer.push(i); + } + } + /// Appends the given text to the rendered content. /// /// # Examples |
