pub trait DeltaRenderer {
// Required methods
fn render_first_delta(
accumulated: &str,
prefix: &str,
colors: Colors,
terminal_mode: TerminalMode,
) -> String;
fn render_subsequent_delta(
accumulated: &str,
prefix: &str,
colors: Colors,
terminal_mode: TerminalMode,
) -> String;
// Provided method
fn render_completion(terminal_mode: TerminalMode) -> String { ... }
}Expand description
Renderer for streaming delta content.
This trait defines the contract for rendering streaming deltas consistently across all parsers. Implementations must ensure:
- First chunk: Shows prefix with accumulated content, ending with newline + cursor up
- Subsequent chunks: Clear line, rewrite with prefix and accumulated content, newline + cursor up
- Completion: Move cursor down + newline when streaming completes
§Rendering Rules
-
render_first_delta(): Called for the first delta of a content block- Must include prefix
- Must end with newline + cursor up (
\n\x1b[1A) for in-place updates (in Full mode) - Shows the accumulated content so far
-
render_subsequent_delta(): Called for subsequent deltas- Must include prefix (rewrite entire line)
- Must use
\x1b[2K\rto clear entire line and return to start (in Full mode) - Shows the full accumulated content (not just the new delta)
- Must end with newline + cursor up (
\n\x1b[1A) (in Full mode)
-
render_completion(): Called when streaming completes- Returns cursor down + newline (
\x1b[1B\n) in Full mode - Returns simple newline in Basic/None mode
- Returns cursor down + newline (
§Terminal Mode Awareness
The renderer automatically adapts output based on terminal capability:
- Full mode: Uses cursor positioning for in-place updates
- Basic mode: Uses colors but simple line output (no cursor positioning)
- None mode: Plain text output (no ANSI sequences)
§Example
use crate::json_parser::delta_display::DeltaRenderer;
use crate::logger::Colors;
use crate::json_parser::TerminalMode;
let colors = Colors { enabled: true };
let terminal_mode = TerminalMode::detect();
// First chunk
let output = DeltaRenderer::render_first_delta(
"Hello",
"ccs-glm",
colors,
terminal_mode
);
// Second chunk
let output = DeltaRenderer::render_subsequent_delta(
"Hello World",
"ccs-glm",
colors,
terminal_mode
);
// Complete
let output = DeltaRenderer::render_completion(terminal_mode);Required Methods§
Sourcefn render_first_delta(
accumulated: &str,
prefix: &str,
colors: Colors,
terminal_mode: TerminalMode,
) -> String
fn render_first_delta( accumulated: &str, prefix: &str, colors: Colors, terminal_mode: TerminalMode, ) -> String
Render the first delta of a content block.
This is called when streaming begins for a new content block. The output should include the prefix and the accumulated content.
§Arguments
accumulated- The full accumulated content so farprefix- The agent prefix (e.g., “ccs-glm”)colors- Terminal colorsterminal_mode- The detected terminal capability mode
§Returns
A formatted string with prefix and content. In Full mode, ends with \n\x1b[1A.
Sourcefn render_subsequent_delta(
accumulated: &str,
prefix: &str,
colors: Colors,
terminal_mode: TerminalMode,
) -> String
fn render_subsequent_delta( accumulated: &str, prefix: &str, colors: Colors, terminal_mode: TerminalMode, ) -> String
Render a subsequent delta (in-place update).
This is called for all deltas after the first. The output should clear the entire line and rewrite with the prefix and accumulated content in Full mode, or append content in Basic/None mode.
§Arguments
accumulated- The full accumulated content so farprefix- The agent prefix (e.g., “ccs-glm”)colors- Terminal colorsterminal_mode- The detected terminal capability mode
§Returns
A formatted string with prefix and content.
Provided Methods§
Sourcefn render_completion(terminal_mode: TerminalMode) -> String
fn render_completion(terminal_mode: TerminalMode) -> String
Render the completion of streaming.
This is called when streaming completes to move cursor down and add newline. This method ONLY handles cursor state cleanup - it does NOT render content.
The streamed content is already visible on the terminal from previous deltas. This method simply positions the cursor correctly for subsequent output.
§Arguments
terminal_mode- The detected terminal capability mode
§Returns
A string with appropriate cursor sequence for the terminal mode.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.