1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Allow pedantic lints for API ergonomics in early development.
//! # Bubbletea
//!
//! A powerful TUI (Terminal User Interface) framework based on The Elm Architecture.
//!
//! Bubbletea provides a functional approach to building terminal applications with:
//! - A simple **Model-Update-View** architecture
//! - **Command-based** side effects
//! - **Type-safe messages** with downcasting
//! - Full **keyboard and mouse** support
//! - **Frame-rate limited** rendering (60 FPS default)
//!
//! ## Role in `charmed_rust`
//!
//! Bubbletea is the core runtime and event loop for the entire ecosystem:
//! - **bubbles** builds reusable widgets on top of the Model/Msg/Cmd pattern.
//! - **huh** composes form flows using bubbletea models.
//! - **wish** serves bubbletea programs over SSH.
//! - **glow** uses bubbletea for pager-style Markdown viewing.
//! - **demo_showcase** is the flagship multi-page bubbletea app.
//!
//! ## The Elm Architecture
//!
//! Bubbletea follows the Elm Architecture pattern:
//!
//! - **Model**: Your application state
//! - **Update**: A pure function that processes messages and returns commands
//! - **View**: A pure function that renders state to a string
//! - **Cmd**: Lazy IO operations that produce messages
//!
//! ## Quick Start
//!
//! ```rust,ignore
//! use bubbletea::{Program, Model, Message, Cmd, KeyMsg, KeyType};
//!
//! struct Counter {
//! count: i32,
//! }
//!
//! struct IncrementMsg;
//! struct DecrementMsg;
//!
//! impl Model for Counter {
//! fn init(&self) -> Option<Cmd> {
//! None
//! }
//!
//! fn update(&mut self, msg: Message) -> Option<Cmd> {
//! if msg.is::<IncrementMsg>() {
//! self.count += 1;
//! } else if msg.is::<DecrementMsg>() {
//! self.count -= 1;
//! } else if let Some(key) = msg.downcast_ref::<KeyMsg>() {
//! match key.key_type {
//! KeyType::CtrlC | KeyType::Esc => return Some(bubbletea::quit()),
//! KeyType::Runes if key.runes == vec!['q'] => return Some(bubbletea::quit()),
//! _ => {}
//! }
//! }
//! None
//! }
//!
//! fn view(&self) -> String {
//! format!(
//! "Count: {}\n\nPress +/- to change, q to quit",
//! self.count
//! )
//! }
//! }
//!
//! fn main() -> Result<(), bubbletea::Error> {
//! let model = Counter { count: 0 };
//! let final_model = Program::new(model)
//! .with_alt_screen()
//! .run()?;
//! println!("Final count: {}", final_model.count);
//! Ok(())
//! }
//! ```
//!
//! ## Messages
//!
//! Messages are type-erased using [`Message`]. You can create custom message types
//! and downcast them in your update function:
//!
//! ```rust
//! use bubbletea::Message;
//!
//! struct MyCustomMsg { value: i32 }
//!
//! let msg = Message::new(MyCustomMsg { value: 42 });
//!
//! // Check type
//! if msg.is::<MyCustomMsg>() {
//! // Downcast to access
//! if let Some(custom) = msg.downcast::<MyCustomMsg>() {
//! assert_eq!(custom.value, 42);
//! }
//! }
//! ```
//!
//! ## Commands
//!
//! Commands are lazy IO operations that produce messages:
//!
//! ```rust
//! use bubbletea::{Cmd, Message, batch, sequence};
//! use std::time::Duration;
//!
//! // Simple command
//! let cmd = Cmd::new(|| Message::new("done"));
//!
//! // Batch commands (run concurrently)
//! let cmds = batch(vec![
//! Some(Cmd::new(|| Message::new(1))),
//! Some(Cmd::new(|| Message::new(2))),
//! ]);
//!
//! // Sequence commands (run in order)
//! let cmds = sequence(vec![
//! Some(Cmd::new(|| Message::new(1))),
//! Some(Cmd::new(|| Message::new(2))),
//! ]);
//! ```
//!
//! ## Keyboard Input
//!
//! Keyboard events are delivered as [`KeyMsg`]:
//!
//! ```rust
//! use bubbletea::{KeyMsg, KeyType, Message};
//!
//! fn handle_key(msg: Message) {
//! if let Some(key) = msg.downcast_ref::<KeyMsg>() {
//! match key.key_type {
//! KeyType::Enter => println!("Enter pressed"),
//! KeyType::CtrlC => println!("Ctrl+C pressed"),
//! KeyType::Runes => println!("Typed: {:?}", key.runes),
//! _ => {}
//! }
//! }
//! }
//! ```
//!
//! ## Mouse Input
//!
//! Enable mouse tracking with `with_mouse_cell_motion()` or `with_mouse_all_motion()`:
//!
//! ```rust,ignore
//! use bubbletea::{Program, MouseMsg, MouseButton, MouseAction};
//!
//! let program = Program::new(model)
//! .with_mouse_cell_motion() // Track clicks and drags
//! .run()?;
//!
//! // In update:
//! if let Some(mouse) = msg.downcast_ref::<MouseMsg>() {
//! if mouse.button == MouseButton::Left && mouse.action == MouseAction::Press {
//! println!("Click at ({}, {})", mouse.x, mouse.y);
//! }
//! }
//! ```
//!
//! ## Screen Control
//!
//! Control terminal features with screen commands:
//!
//! ```rust
//! use bubbletea::screen;
//!
//! // In update, return a command:
//! let cmd = screen::enter_alt_screen();
//! let cmd = screen::hide_cursor();
//! let cmd = screen::enable_mouse_cell_motion();
//! ```
// Re-exports
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
pub use ;
// Re-export derive macro when macros feature is enabled.
// Derive macros and traits live in different namespaces, so both can be named `Model`.
// Users can write `#[derive(bubbletea::Model)]` for the macro and `impl bubbletea::Model` for the trait.
pub use *;
/// Prelude module for convenient imports.