envision 0.15.1

A ratatui framework for collaborative TUI development with headless testing support
Documentation
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//! Background task abstraction for TEA applications.
//!
//! The `Worker` module bridges async tasks to the TEA message loop with
//! typed progress reporting and cancellation support. It provides a high-level
//! API for spawning background work that integrates naturally with the
//! [`Command`] and [`Subscription`](crate::app::subscription::Subscription)
//! system.
//!
//! # Progress Reporting
//!
//! Workers report progress via a [`ProgressSender<P>`] where `P` is your
//! own progress type — an enum, struct, or any `Send + 'static` type.
//! Use [`send`](ProgressSender::send) for important lifecycle events and
//! [`try_send`](ProgressSender::try_send) for high-frequency updates where
//! dropping one is acceptable.
//!
//! # Example
//!
//! ```rust
//! use envision::app::worker::WorkerBuilder;
//! use envision::app::Command;
//!
//! #[derive(Clone)]
//! enum Progress {
//!     ChapterCount(usize),
//!     Encoding { percent: f32 },
//!     FileSize(u64),
//! }
//!
//! #[derive(Clone)]
//! enum Msg {
//!     Update(Progress),
//!     Done(String),
//!     Failed(String),
//! }
//!
//! let (cmd, sub, handle) = WorkerBuilder::new("transcode")
//!     .with_channel_capacity(128)
//!     .spawn(
//!         |sender, _cancel| async move {
//!             sender.send(Progress::ChapterCount(12)).await.ok();
//!             // High-frequency updates: try_send drops if channel full
//!             sender.try_send(Progress::Encoding { percent: 0.5 }).ok();
//!             Ok::<_, String>("output.m4b".to_string())
//!         },
//!         Msg::Update,
//!         |result: Result<String, String>| match result {
//!             Ok(path) => Msg::Done(path),
//!             Err(e) => Msg::Failed(e),
//!         },
//!     );
//!
//! // Cancel if needed
//! handle.cancel();
//! ```

use std::future::Future;

use tokio::sync::mpsc;
use tokio_util::sync::CancellationToken;

use super::command::Command;
use super::subscription::{BoxedSubscription, ChannelSubscription, MappedSubscription};

/// A convenience progress type providing percentage and status string.
///
/// This is one possible type for `ProgressSender<P>`. Use it when your
/// worker only needs to report a completion percentage and an optional
/// status message. For richer progress reporting, define your own type.
///
/// # Example
///
/// ```rust
/// use envision::app::worker::WorkerProgress;
///
/// let progress = WorkerProgress::new(0.5, Some("Downloading...".to_string()));
/// assert_eq!(progress.percentage(), 0.5);
/// assert_eq!(progress.status(), Some("Downloading..."));
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct WorkerProgress {
    /// Progress percentage (0.0 to 1.0, clamped).
    percentage: f32,
    /// Optional status message describing current work.
    status: Option<String>,
}

impl WorkerProgress {
    /// Creates a new progress update.
    ///
    /// The percentage is clamped to the range 0.0..=1.0.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::app::worker::WorkerProgress;
    ///
    /// let progress = WorkerProgress::new(0.75, Some("Processing...".to_string()));
    /// assert_eq!(progress.percentage(), 0.75);
    /// assert_eq!(progress.status(), Some("Processing..."));
    ///
    /// // Clamping
    /// let clamped = WorkerProgress::new(1.5, None);
    /// assert_eq!(clamped.percentage(), 1.0);
    /// ```
    pub fn new(percentage: f32, status: Option<String>) -> Self {
        Self {
            percentage: percentage.clamp(0.0, 1.0),
            status,
        }
    }

    /// Returns the progress percentage (0.0 to 1.0).
    pub fn percentage(&self) -> f32 {
        self.percentage
    }

    /// Returns the status message, if any.
    pub fn status(&self) -> Option<&str> {
        self.status.as_deref()
    }
}

/// A sender for reporting typed progress from within a worker task.
///
/// Generic over `P` — the progress message type. Use any `Send + 'static`
/// type: an enum with domain-specific variants, a struct, or the built-in
/// [`WorkerProgress`] for simple percentage+string reporting.
///
/// # Backpressure vs fire-and-forget
///
/// - [`send`](Self::send): async, applies backpressure. Use for important
///   lifecycle events (started, completed, failed) that must not be dropped.
/// - [`try_send`](Self::try_send): non-blocking, drops the message if the
///   channel is full. Use for high-frequency informational updates (per-frame
///   progress ticks, per-segment completions) where dropping one is better
///   than blocking the worker pipeline.
///
/// # Example
///
/// ```rust
/// use envision::app::worker::{WorkerBuilder, ProgressSender};
///
/// #[derive(Clone)]
/// enum Update {
///     Started,
///     Tick(f32),
///     Finished,
/// }
///
/// #[derive(Clone)]
/// enum Msg { Update(Update), Done(()) }
///
/// let (cmd, sub, handle) = WorkerBuilder::new("work")
///     .spawn(
///         |sender: ProgressSender<Update>, _cancel| async move {
///             sender.send(Update::Started).await.ok();
///             for i in 0..100 {
///                 // Non-blocking: ok to drop if channel is full
///                 sender.try_send(Update::Tick(i as f32 / 100.0)).ok();
///             }
///             sender.send(Update::Finished).await.ok();
///             Ok::<_, ()>(())
///         },
///         Msg::Update,
///         |_| Msg::Done(()),
///     );
/// ```
#[derive(Clone)]
pub struct ProgressSender<P> {
    tx: mpsc::Sender<P>,
}

impl<P: Send + 'static> ProgressSender<P> {
    /// Creates a new `ProgressSender` wrapping a tokio mpsc sender.
    ///
    /// Use this to construct a `ProgressSender` outside of
    /// [`WorkerBuilder`] — for example, in tests or when bridging
    /// to an existing channel.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::app::worker::ProgressSender;
    /// use tokio::sync::mpsc;
    ///
    /// let (tx, mut rx) = mpsc::channel::<String>(32);
    /// let sender = ProgressSender::new(tx);
    /// ```
    pub fn new(tx: mpsc::Sender<P>) -> Self {
        Self { tx }
    }

    /// Sends a progress update, waiting if the channel is full.
    ///
    /// Use this for important messages that must not be dropped (lifecycle
    /// transitions, final results, error reports).
    ///
    /// # Errors
    ///
    /// Returns `Err` if the progress channel is closed, which occurs when the
    /// worker has been cancelled or the runtime has shut down.
    pub async fn send(&self, progress: P) -> Result<(), mpsc::error::SendError<P>> {
        self.tx.send(progress).await
    }

    /// Attempts to send a progress update without blocking.
    ///
    /// Use this for high-frequency informational updates where dropping one
    /// is acceptable (progress ticks, per-segment completions, metrics).
    /// If the channel is full, the message is returned in the error.
    ///
    /// # Errors
    ///
    /// Returns `Err` if the channel is full or closed.
    pub fn try_send(&self, progress: P) -> Result<(), mpsc::error::TrySendError<P>> {
        self.tx.try_send(progress)
    }
}

/// A handle to a running worker that supports cancellation.
///
/// When dropped, the worker is automatically cancelled.
///
/// # Example
///
/// ```rust
/// use envision::app::worker::WorkerHandle;
///
/// // Handles are returned from WorkerBuilder::spawn and spawn_simple
/// // Cancellation is automatic on drop, or explicit via cancel()
/// ```
pub struct WorkerHandle {
    cancel: CancellationToken,
    id: String,
}

impl WorkerHandle {
    /// Cancels the worker.
    pub fn cancel(&self) {
        self.cancel.cancel();
    }

    /// Returns true if the worker has been cancelled.
    pub fn is_cancelled(&self) -> bool {
        self.cancel.is_cancelled()
    }

    /// Returns the worker's identifier.
    pub fn id(&self) -> &str {
        &self.id
    }
}

impl Drop for WorkerHandle {
    fn drop(&mut self) {
        self.cancel.cancel();
    }
}

/// Default channel capacity for progress updates.
const DEFAULT_CHANNEL_CAPACITY: usize = 32;

/// Builder for creating and spawning workers.
///
/// Created via [`WorkerBuilder::new`].
///
/// # Example
///
/// ```rust
/// use envision::app::worker::WorkerBuilder;
/// use envision::app::Command;
///
/// #[derive(Clone)]
/// enum Msg {
///     Done(Vec<u8>),
///     Failed(String),
/// }
///
/// let (cmd, handle) = WorkerBuilder::new("fetch")
///     .spawn_simple(
///         |_cancel| async move {
///             Ok::<_, String>(vec![1, 2, 3])
///         },
///         |result: Result<Vec<u8>, String>| match result {
///             Ok(data) => Msg::Done(data),
///             Err(e) => Msg::Failed(e),
///         },
///     );
/// ```
pub struct WorkerBuilder {
    id: String,
    channel_capacity: usize,
}

impl WorkerBuilder {
    /// Creates a new worker builder with the given identifier.
    ///
    /// The identifier is used for tracking and debugging purposes.
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            channel_capacity: DEFAULT_CHANNEL_CAPACITY,
        }
    }

    /// Sets the channel capacity for progress updates.
    ///
    /// Default is 32. Higher values prevent the worker from blocking
    /// when the application is slow to process progress messages.
    /// For high-frequency producers using [`try_send`](ProgressSender::try_send),
    /// 128 is a good starting point.
    pub fn with_channel_capacity(mut self, capacity: usize) -> Self {
        self.channel_capacity = capacity;
        self
    }

    /// Spawns a worker with typed progress reporting.
    ///
    /// The worker receives a [`ProgressSender<P>`] for sending progress
    /// updates of any user-defined type back to the application's message
    /// loop.
    ///
    /// Returns:
    /// - A [`Command`] that executes the async task
    /// - A [`BoxedSubscription`] that delivers progress updates as mapped messages
    /// - A [`WorkerHandle`] for cancellation
    ///
    /// # Type Parameters
    ///
    /// - `P`: The progress type — any `Send + 'static` type (your enum, struct, etc.)
    /// - `T`: The success type of the task
    /// - `E`: The error type of the task
    /// - `Fut`: The future returned by `task_fn`
    /// - `F`: The task function, receiving a `ProgressSender<P>` and `CancellationToken`
    /// - `Pm`: Progress message mapper (`P -> M`)
    /// - `C`: Completion message mapper (`Result<T, E> -> M`)
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::app::worker::{WorkerBuilder, ProgressSender};
    /// use envision::app::Command;
    ///
    /// #[derive(Clone)]
    /// enum Progress { Started, Percent(f32), Done }
    ///
    /// #[derive(Clone)]
    /// enum Msg { Progress(Progress), Complete(String) }
    ///
    /// let (cmd, sub, handle) = WorkerBuilder::new("process")
    ///     .spawn(
    ///         |sender: ProgressSender<Progress>, _cancel| async move {
    ///             sender.send(Progress::Started).await.ok();
    ///             sender.try_send(Progress::Percent(0.5)).ok();
    ///             sender.send(Progress::Done).await.ok();
    ///             Ok::<_, String>("result".to_string())
    ///         },
    ///         Msg::Progress,
    ///         |result: Result<String, String>| match result {
    ///             Ok(s) => Msg::Complete(s),
    ///             Err(_) => Msg::Complete("failed".into()),
    ///         },
    ///     );
    /// ```
    pub fn spawn<M, P, T, E, Fut, F, Pm, C>(
        self,
        task_fn: F,
        on_progress: Pm,
        on_complete: C,
    ) -> (Command<M>, BoxedSubscription<M>, WorkerHandle)
    where
        M: Send + 'static,
        P: Send + 'static,
        T: Send + 'static,
        E: Send + 'static,
        Fut: Future<Output = Result<T, E>> + Send + 'static,
        F: FnOnce(ProgressSender<P>, CancellationToken) -> Fut + Send + 'static,
        Pm: Fn(P) -> M + Send + 'static,
        C: FnOnce(Result<T, E>) -> M + Send + 'static,
    {
        let cancel = CancellationToken::new();
        let cancel_task = cancel.clone();

        let (progress_tx, progress_rx) = mpsc::channel(self.channel_capacity);
        let sender = ProgressSender { tx: progress_tx };

        let cmd = Command::perform_async(async move {
            let result = tokio::select! {
                result = task_fn(sender, cancel_task.clone()) => result,
                _ = cancel_task.cancelled() => return None,
            };
            Some(on_complete(result))
        });

        let subscription: BoxedSubscription<M> = Box::new(MappedSubscription::new(
            ChannelSubscription::new(progress_rx),
            on_progress,
        ));

        let handle = WorkerHandle {
            cancel,
            id: self.id,
        };

        (cmd, subscription, handle)
    }

    /// Spawns a simple worker without progress reporting.
    ///
    /// Returns:
    /// - A [`Command`] that executes the async task
    /// - A [`WorkerHandle`] for cancellation
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::app::worker::WorkerBuilder;
    /// use envision::app::Command;
    ///
    /// #[derive(Clone)]
    /// enum Msg { Done(String), Failed(String) }
    ///
    /// let (cmd, handle) = WorkerBuilder::new("fetch")
    ///     .spawn_simple(
    ///         |_cancel| async move {
    ///             Ok::<_, String>("data".to_string())
    ///         },
    ///         |result: Result<String, String>| match result {
    ///             Ok(data) => Msg::Done(data),
    ///             Err(e) => Msg::Failed(e),
    ///         },
    ///     );
    /// ```
    pub fn spawn_simple<M, T, E, Fut, F, C>(
        self,
        task_fn: F,
        on_complete: C,
    ) -> (Command<M>, WorkerHandle)
    where
        M: Send + 'static,
        T: Send + 'static,
        E: Send + 'static,
        Fut: Future<Output = Result<T, E>> + Send + 'static,
        F: FnOnce(CancellationToken) -> Fut + Send + 'static,
        C: FnOnce(Result<T, E>) -> M + Send + 'static,
    {
        let cancel = CancellationToken::new();
        let cancel_task = cancel.clone();

        let cmd = Command::perform_async(async move {
            let result = tokio::select! {
                result = task_fn(cancel_task.clone()) => result,
                _ = cancel_task.cancelled() => return None,
            };
            Some(on_complete(result))
        });

        let handle = WorkerHandle {
            cancel,
            id: self.id,
        };

        (cmd, handle)
    }
}

#[cfg(test)]
mod tests;