Skip to main content

rusty_bubbletea/
renderer.rs

1//! Cleanroom Rust port of upstream Go source file: `renderer.go`
2//! Upstream Target Tag / Version: `v2.0.8`
3//!
4//! <public-docs>
5//! # Renderer Trait
6//!
7//! Renderer interface trait for Bubble Tea v2.0.8 (`render(View)`, `flush(bool)`, `insert_above`, `clear_screen`).
8//! </public-docs>
9
10use crate::model::Cmd;
11use crate::mouse::MouseMsg;
12use crate::view::View;
13use std::fmt;
14
15/// Renderer interface for Bubble Tea v2.0.8.
16pub trait Renderer: Send + Sync {
17    /// Starts the renderer.
18    fn start(&mut self);
19
20    /// Closes the renderer and flushes any remaining data.
21    fn close(&mut self) -> Result<(), Box<dyn std::error::Error>>;
22
23    /// Renders a declarative View frame.
24    fn render(&mut self, view: View);
25
26    /// Flushes renderer output buffer to terminal stdout.
27    fn flush(&mut self, closing: bool) -> Result<(), Box<dyn std::error::Error>>;
28
29    /// Resets renderer to initial state.
30    fn reset(&mut self);
31
32    /// Inserts unmanaged lines above the TUI renderer.
33    fn insert_above(&mut self, s: String) -> Result<(), Box<dyn std::error::Error>>;
34
35    /// Resize notification.
36    fn resize(&mut self, width: usize, height: usize);
37
38    /// Clears terminal screen.
39    fn clear_screen(&mut self);
40
41    /// Write raw string to output.
42    fn write_string(&mut self, s: &str) -> Result<usize, Box<dyn std::error::Error>>;
43
44    /// Mouse event interceptor.
45    fn on_mouse(&mut self, msg: MouseMsg) -> Cmd;
46
47    /// Sets the cursor movement optimizations (hard tabs, backspace,
48    /// newline mapping).
49    fn set_optimizations(&mut self, hard_tabs: bool, backspace: bool, map_nl: bool);
50
51    /// Sets the terminal color profile used for downsampling colors.
52    fn set_color_profile(&mut self, p: rusty_colorprofile::Profile);
53}
54
55/// PrintLineMsg represents a line printed above the TUI.
56#[derive(Debug, Clone, PartialEq, Eq)]
57pub struct PrintLineMsg {
58    /// Message body.
59    pub message_body: String,
60}
61
62/// Println prints above the Program.
63pub fn print_ln(args: fmt::Arguments<'_>) -> Cmd {
64    let body = args.to_string();
65    Some(Box::new(move || {
66        Some(Box::new(PrintLineMsg { message_body: body }))
67    }))
68}
69
70/// Printf prints formatted output above the Program.
71pub fn print_f(args: fmt::Arguments<'_>) -> Cmd {
72    let body = args.to_string();
73    Some(Box::new(move || {
74        Some(Box::new(PrintLineMsg { message_body: body }))
75    }))
76}