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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
//! Core App trait defining the TEA application structure.
//!
//! # Two Construction Patterns
//!
//! Applications can be started in two ways, depending on whether
//! [`App::init()`] needs injected dependencies:
//!
//! ## Standard pattern — `init()` creates the state
//!
//! Apps with `type Args = ();` can call `.build()` directly. The builder
//! invokes [`App::init()`] internally to create the initial state and any
//! startup commands.
//!
//! ```rust
//! # use envision::prelude::*;
//! # struct MyApp;
//! # #[derive(Default, Clone)]
//! # struct MyState;
//! # #[derive(Clone)]
//! # enum MyMsg {}
//! # impl App for MyApp {
//! # type State = MyState;
//! # type Message = MyMsg;
//! # type Args = ();
//! # fn init(_: ()) -> (MyState, Command<MyMsg>) { (MyState, Command::none()) }
//! # fn update(state: &mut MyState, msg: MyMsg) -> Command<MyMsg> { Command::none() }
//! # fn view(state: &MyState, frame: &mut Frame) {}
//! # }
//! let mut vt = Runtime::<MyApp, _>::virtual_builder(80, 24).build()?;
//! # Ok::<(), envision::EnvisionError>(())
//! ```
//!
//! ## Args pattern — passing dependencies into `init`
//!
//! Apps that need injected config declare a non-`()` `Args` type and pass
//! values via [`RuntimeBuilder::with_args`]. Common uses include CLI-parsed
//! paths, env-derived URLs, opened DB handles, or preloaded fixture data.
//!
//! ```rust
//! # use envision::prelude::*;
//! # use std::path::PathBuf;
//! # struct MyApp;
//! # struct MyArgs { dir: PathBuf }
//! # #[derive(Default, Clone)]
//! # struct MyState { dir: PathBuf }
//! # #[derive(Clone)]
//! # enum MyMsg {}
//! # impl App for MyApp {
//! # type State = MyState;
//! # type Message = MyMsg;
//! # type Args = MyArgs;
//! # fn init(args: MyArgs) -> (MyState, Command<MyMsg>) {
//! # (MyState { dir: args.dir }, Command::none())
//! # }
//! # fn update(state: &mut MyState, msg: MyMsg) -> Command<MyMsg> { Command::none() }
//! # fn view(state: &MyState, frame: &mut Frame) {}
//! # }
//! let args = MyArgs { dir: PathBuf::from("/tmp/example") };
//! let mut vt = Runtime::<MyApp, _>::virtual_builder(80, 24)
//! .with_args(args)
//! .build()?;
//! # Ok::<(), envision::EnvisionError>(())
//! ```
//!
//! [`Runtime::terminal_builder()`]: crate::app::Runtime::terminal_builder
//! [`Runtime::virtual_builder()`]: crate::app::Runtime::virtual_builder
//! [`RuntimeBuilder::with_args`]: crate::app::RuntimeBuilder::with_args
use Frame;
use Command;
use crateEvent;
pub
pub use OptionalArgs;
/// The core trait for TEA-style applications.
///
/// This trait defines the structure of an application following
/// The Elm Architecture pattern:
///
/// - `State`: The complete application state
/// - `Message`: Events that can modify state
/// - `Args`: Configuration / dependencies handed in at construction time
/// - `init`: Initialize state from args, plus any startup commands
/// - `update`: Handle messages and produce new state
/// - `view`: Render the current state
///
/// # Type Parameters
///
/// - `State`: Your application's state type. Derive `Clone` if you need snapshots.
/// - `Message`: The type representing all possible events/actions.
/// - `Args`: Construction-time dependencies. Use `()` if none.
///
/// # Construction
///
/// All apps must implement [`init`](App::init). For apps that need
/// construction-time dependencies (CLI args, opened DB handles, fixture
/// data, etc.), declare a custom [`Args`](App::Args) type and pass it via
/// [`RuntimeBuilder::with_args`](crate::app::RuntimeBuilder::with_args).
/// For apps with no dependencies, declare `type Args = ();` and call
/// `.build()` directly — the unit shortcut is permitted only because `()`
/// implements the sealed [`OptionalArgs`] marker.
///
/// # Examples
///
/// ## Standard pattern — `Args = ()`
///
/// ```rust
/// use envision::app::{App, Command};
/// use ratatui::Frame;
///
/// struct MyApp;
///
/// #[derive(Clone, Default)]
/// struct MyState {
/// value: String,
/// }
///
/// #[derive(Clone)]
/// enum MyMessage {
/// SetValue(String),
/// Clear,
/// }
///
/// impl App for MyApp {
/// type State = MyState;
/// type Message = MyMessage;
/// type Args = ();
///
/// fn init(_args: ()) -> (Self::State, Command<Self::Message>) {
/// (MyState::default(), Command::none())
/// }
///
/// fn update(state: &mut Self::State, msg: Self::Message) -> Command<Self::Message> {
/// match msg {
/// MyMessage::SetValue(v) => state.value = v,
/// MyMessage::Clear => state.value.clear(),
/// }
/// Command::none()
/// }
///
/// fn view(state: &Self::State, frame: &mut Frame) {
/// // Render UI
/// }
/// }
/// ```
///
/// ## Args pattern — injecting dependencies
///
/// ```rust
/// use envision::app::{App, Command};
/// use ratatui::Frame;
///
/// struct ExternalApp;
///
/// struct ExternalState {
/// config_value: String,
/// }
///
/// struct ExternalArgs {
/// initial_value: String,
/// }
///
/// #[derive(Clone)]
/// enum ExternalMsg {
/// Update(String),
/// }
///
/// impl App for ExternalApp {
/// type State = ExternalState;
/// type Message = ExternalMsg;
/// type Args = ExternalArgs;
///
/// fn init(args: ExternalArgs) -> (Self::State, Command<Self::Message>) {
/// (ExternalState { config_value: args.initial_value }, Command::none())
/// }
///
/// fn update(state: &mut Self::State, msg: Self::Message) -> Command<Self::Message> {
/// match msg {
/// ExternalMsg::Update(v) => state.config_value = v,
/// }
/// Command::none()
/// }
///
/// fn view(state: &Self::State, frame: &mut Frame) {
/// // Render UI
/// }
/// }
/// ```