pub struct TerminalWriter<W: Write> { /* private fields */ }Expand description
Unified terminal output coordinator.
Enforces the one-writer rule and implements inline mode correctly. All terminal output should go through this struct.
Implementations§
Source§impl<W: Write> TerminalWriter<W>
impl<W: Write> TerminalWriter<W>
Sourcepub fn new(
writer: W,
screen_mode: ScreenMode,
ui_anchor: UiAnchor,
capabilities: TerminalCapabilities,
) -> Self
pub fn new( writer: W, screen_mode: ScreenMode, ui_anchor: UiAnchor, capabilities: TerminalCapabilities, ) -> Self
Create a new terminal writer.
§Arguments
writer- Output destination (takes ownership for one-writer rule)screen_mode- Inline or alternate screen modeui_anchor- Where to anchor UI in inline modecapabilities- Terminal capabilities
Sourcepub fn with_diff_config(
writer: W,
screen_mode: ScreenMode,
ui_anchor: UiAnchor,
capabilities: TerminalCapabilities,
diff_config: RuntimeDiffConfig,
) -> Self
pub fn with_diff_config( writer: W, screen_mode: ScreenMode, ui_anchor: UiAnchor, capabilities: TerminalCapabilities, diff_config: RuntimeDiffConfig, ) -> Self
Create a new terminal writer with custom diff strategy configuration.
§Arguments
writer- Output destination (takes ownership for one-writer rule)screen_mode- Inline or alternate screen modeui_anchor- Where to anchor UI in inline modecapabilities- Terminal capabilitiesdiff_config- Configuration for diff strategy selection
§Example
use ftui_runtime::{TerminalWriter, ScreenMode, UiAnchor, RuntimeDiffConfig};
use ftui_core::terminal_capabilities::TerminalCapabilities;
// Disable Bayesian selection for deterministic diffing
let config = RuntimeDiffConfig::default()
.with_bayesian_enabled(false);
let writer = TerminalWriter::with_diff_config(
std::io::stdout(),
ScreenMode::AltScreen,
UiAnchor::Bottom,
TerminalCapabilities::detect(),
config,
);Sourcepub fn diff_config(&self) -> &RuntimeDiffConfig
pub fn diff_config(&self) -> &RuntimeDiffConfig
Get the current diff configuration.
Sourcepub fn with_evidence_sink(self, sink: EvidenceSink) -> Self
pub fn with_evidence_sink(self, sink: EvidenceSink) -> Self
Attach an evidence sink for diff decision logging.
Sourcepub fn set_evidence_sink(&mut self, sink: Option<EvidenceSink>)
pub fn set_evidence_sink(&mut self, sink: Option<EvidenceSink>)
Set the evidence JSONL sink for diff decision logging.
Sourcepub fn with_render_trace(self, recorder: RenderTraceRecorder) -> Self
pub fn with_render_trace(self, recorder: RenderTraceRecorder) -> Self
Attach a render-trace recorder.
Sourcepub fn set_render_trace(&mut self, recorder: Option<RenderTraceRecorder>)
pub fn set_render_trace(&mut self, recorder: Option<RenderTraceRecorder>)
Set the render-trace recorder.
Sourcepub fn diff_strategy_mut(&mut self) -> &mut DiffStrategySelector
pub fn diff_strategy_mut(&mut self) -> &mut DiffStrategySelector
Get mutable access to the diff strategy selector.
Useful for advanced scenarios like manual posterior updates.
Sourcepub fn diff_strategy(&self) -> &DiffStrategySelector
pub fn diff_strategy(&self) -> &DiffStrategySelector
Get the diff strategy selector (read-only).
Sourcepub fn last_diff_strategy(&self) -> Option<DiffStrategy>
pub fn last_diff_strategy(&self) -> Option<DiffStrategy>
Get the last diff strategy selected during present, if any.
Sourcepub fn set_size(&mut self, width: u16, height: u16)
pub fn set_size(&mut self, width: u16, height: u16)
Set the terminal size.
Call this when the terminal is resized.
Sourcepub fn take_render_buffer(&mut self, width: u16, height: u16) -> Buffer
pub fn take_render_buffer(&mut self, width: u16, height: u16) -> Buffer
Take a reusable render buffer sized for the current frame.
Uses a spare buffer when available to avoid per-frame allocation.
Sourcepub fn screen_mode(&self) -> ScreenMode
pub fn screen_mode(&self) -> ScreenMode
Get the current screen mode.
Sourcepub fn render_height_hint(&self) -> u16
pub fn render_height_hint(&self) -> u16
Height to use for rendering a frame.
In inline auto mode, this returns the configured maximum (clamped to terminal height) so measurement can determine actual UI height.
Sourcepub fn inline_auto_bounds(&self) -> Option<(u16, u16)>
pub fn inline_auto_bounds(&self) -> Option<(u16, u16)>
Get sanitized min/max bounds for inline auto mode (clamped to terminal height).
Sourcepub fn auto_ui_height(&self) -> Option<u16>
pub fn auto_ui_height(&self) -> Option<u16>
Get the cached auto UI height (inline auto mode only).
Sourcepub fn set_auto_ui_height(&mut self, height: u16)
pub fn set_auto_ui_height(&mut self, height: u16)
Update the computed height for inline auto mode.
Sourcepub fn clear_auto_ui_height(&mut self)
pub fn clear_auto_ui_height(&mut self)
Clear the cached auto UI height (inline auto mode only).
Sourcepub fn inline_strategy(&self) -> InlineStrategy
pub fn inline_strategy(&self) -> InlineStrategy
Get the inline mode rendering strategy.
Sourcepub fn scroll_region_active(&self) -> bool
pub fn scroll_region_active(&self) -> bool
Check if a scroll region is currently active.
Sourcepub fn present_ui(
&mut self,
buffer: &Buffer,
cursor: Option<(u16, u16)>,
cursor_visible: bool,
) -> Result<()>
pub fn present_ui( &mut self, buffer: &Buffer, cursor: Option<(u16, u16)>, cursor_visible: bool, ) -> Result<()>
Present a UI frame.
In inline mode, this:
- Begins synchronized output (if supported)
- Saves cursor position
- Moves to UI region and clears it
- Renders the buffer using the presenter
- Restores cursor position
- Moves cursor to requested UI position (if any)
- Applies cursor visibility
- Ends synchronized output
In AltScreen mode, this just renders the buffer and positions cursor.
Sourcepub fn present_ui_owned(
&mut self,
buffer: Buffer,
cursor: Option<(u16, u16)>,
cursor_visible: bool,
) -> Result<()>
pub fn present_ui_owned( &mut self, buffer: Buffer, cursor: Option<(u16, u16)>, cursor_visible: bool, ) -> Result<()>
Present a UI frame, taking ownership of the buffer (O(1) — no clone).
Prefer this over [present_ui] when the caller has an owned buffer
that won’t be reused, as it avoids an O(width × height) clone.
Sourcepub fn write_log(&mut self, text: &str) -> Result<()>
pub fn write_log(&mut self, text: &str) -> Result<()>
Write log output (goes to scrollback region in inline mode).
In inline mode, this writes to the log region (above UI for bottom-anchored, below UI for top-anchored). The cursor is explicitly positioned in the log region before writing to prevent UI corruption.
In AltScreen mode, logs are typically not shown (returns Ok silently).
Sourcepub fn clear_screen(&mut self) -> Result<()>
pub fn clear_screen(&mut self) -> Result<()>
Clear the screen.
Sourcepub fn hide_cursor(&mut self) -> Result<()>
pub fn hide_cursor(&mut self) -> Result<()>
Hide the cursor.
Sourcepub fn show_cursor(&mut self) -> Result<()>
pub fn show_cursor(&mut self) -> Result<()>
Show the cursor.
Sourcepub fn pool(&self) -> &GraphemePool
pub fn pool(&self) -> &GraphemePool
Get the grapheme pool for interning complex characters.
Sourcepub fn pool_mut(&mut self) -> &mut GraphemePool
pub fn pool_mut(&mut self) -> &mut GraphemePool
Get mutable access to the grapheme pool.
Sourcepub fn links(&self) -> &LinkRegistry
pub fn links(&self) -> &LinkRegistry
Get the link registry.
Sourcepub fn links_mut(&mut self) -> &mut LinkRegistry
pub fn links_mut(&mut self) -> &mut LinkRegistry
Get mutable access to the link registry.
Sourcepub fn pool_and_links_mut(&mut self) -> (&mut GraphemePool, &mut LinkRegistry)
pub fn pool_and_links_mut(&mut self) -> (&mut GraphemePool, &mut LinkRegistry)
Borrow the grapheme pool and link registry together.
This avoids double-borrowing self at call sites that need both.
Sourcepub fn capabilities(&self) -> &TerminalCapabilities
pub fn capabilities(&self) -> &TerminalCapabilities
Get the terminal capabilities.
Sourcepub fn into_inner(self) -> Option<W>
pub fn into_inner(self) -> Option<W>
Consume the writer and return the underlying writer.
Performs cleanup operations before returning.
Returns None if the buffer could not be flushed.
Sourcepub fn gc(&mut self)
pub fn gc(&mut self)
Perform garbage collection on the grapheme pool.
Frees graphemes that are not referenced by the current front buffer (prev_buffer).
This should be called periodically (e.g. every N frames) to prevent memory leaks
in long-running applications with dynamic content (e.g. streaming logs with emoji).