clawless-tui 0.6.1

Presentation layer for TUI-based applications using the Clawless framework
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
//! Pull-based, queryable view of the event stream
//!
//! This module defines [`Projection`] and [`Entry`], the pull-based counterpart to the push-based
//! [`TerminalPresenter`]. A projection consumes events from an [`EventReceiver`] in the background
//! and provides read access to accumulated state at any time. TUI applications query the projection
//! on each render frame without interacting with the event system directly.
//!
//! The projection translates internal [`Event`]s into [`Entry`] values so that TUI consumers never
//! interact with the event system directly. Queries return cloned snapshots of the accumulated
//! state. The read lock is held only while cloning the snapshot, keeping contention with the
//! drain task's write lock brief.
//!
//! # Examples
//!
//! ```
//! use clawless_core::event::{Event, event_channel};
//! use clawless_tui::projection::Projection;
//!
//! # #[tokio::main]
//! # async fn main() {
//! let (sender, receiver) = event_channel();
//! let projection = Projection::new(receiver);
//!
//! sender.send(Event::Message("hello".to_string()))
//!     .await
//!     .expect("should send");
//! drop(sender);
//!
//! // Wait for the drain task, which finishes once the dropped sender closes the channel
//! while !projection.is_complete() {
//!     tokio::task::yield_now().await;
//! }
//!
//! assert_eq!(projection.entries().len(), 1);
//! # }
//! ```
//!
//! [`Event`]: clawless_core::event::Event
//! [`EventReceiver`]: clawless_core::event::EventReceiver
//! [`TerminalPresenter`]: https://docs.rs/clawless-cli/latest/clawless_cli/presenter/struct.TerminalPresenter.html

use std::sync::{Arc, RwLock};

use clawless_core::event::{Event, EventReceiver};
use tokio::task::JoinHandle;

pub use self::entry::Entry;
use self::state::ProjectionState;

/// The form of an [`Event`] that a TUI application reads
mod entry;
/// The state that the projection lock protects
mod state;

/// Pull-based, queryable view of the event stream
///
/// `Projection` consumes events from an [`EventReceiver`] in the background and provides read
/// access to accumulated state at any time. TUI applications query the projection on each render
/// frame without interacting with the event system directly.
///
/// Construction starts a background drain task via [`tokio::spawn`]. The task reads events from
/// the receiver, translates each into an [`Entry`], and appends it to internal storage. When the
/// event channel closes (all senders dropped), the task marks the projection as complete.
///
/// Query methods take `&self` and return cloned snapshots of the accumulated state. The drain
/// task and query callers synchronize through a [`RwLock`], allowing concurrent reads from
/// multiple render frames without blocking on each other.
///
/// # Examples
///
/// ```
/// use clawless_core::event::{Event, event_channel};
/// use clawless_tui::projection::Projection;
///
/// # #[tokio::main]
/// # async fn main() {
/// let (sender, receiver) = event_channel();
/// let projection = Projection::new(receiver);
///
/// sender.send(Event::Message("processing".to_string()))
///     .await
///     .expect("should send");
/// drop(sender);
///
/// // Wait for the drain task, which finishes once the dropped sender closes the channel
/// while !projection.is_complete() {
///     tokio::task::yield_now().await;
/// }
///
/// let messages = projection.messages();
/// assert_eq!(messages.len(), 1);
/// # }
/// ```
///
/// [`EventReceiver`]: clawless_core::event::EventReceiver
/// [`RwLock`]: std::sync::RwLock
// r[impl projection.new]
// r[impl projection.new.drain]
// r[impl projection.safety.send]
// r[impl projection.safety.sync]
// r[impl projection.safety.unpin]
#[derive(Debug)]
pub struct Projection {
    /// The entries so far, which the projection shares with the drain task
    state: Arc<RwLock<ProjectionState>>,
    /// Handle to the drain task, which stops when Clawless drops the projection
    _drain_handle: JoinHandle<()>,
}

impl Projection {
    /// Creates a new projection that drains events from the given receiver
    ///
    /// Spawns a background task that reads events from `receiver`, translates each into an
    /// [`Entry`], and appends it to internal storage. The task runs until the event channel
    /// closes, at which point it marks the projection as complete.
    ///
    /// The returned projection is immediately ready to query. Early queries return empty results
    /// until events arrive.
    ///
    /// # Panics
    ///
    /// Panics if called outside of a Tokio runtime.
    ///
    /// # Examples
    ///
    /// ```
    /// use clawless_core::event::event_channel;
    /// use clawless_tui::projection::Projection;
    ///
    /// # #[tokio::main]
    /// # async fn main() {
    /// let (_sender, receiver) = event_channel();
    /// let projection = Projection::new(receiver);
    ///
    /// assert!(projection.entries().is_empty());
    /// # }
    /// ```
    pub fn new(receiver: EventReceiver) -> Self {
        let state = Arc::new(RwLock::new(ProjectionState::default()));
        let drain_state = Arc::clone(&state);

        let handle = tokio::spawn(drain(receiver, drain_state));

        Self {
            state,
            _drain_handle: handle,
        }
    }

    /// Returns all accumulated entries in receive order
    ///
    /// Returns a cloned snapshot of the entry list. The snapshot is consistent: it reflects all
    /// events drained up to the moment the read lock is acquired.
    ///
    /// # Panics
    ///
    /// Panics if the internal lock is poisoned.
    // r[impl projection.query.entries]
    // Panics only on a poisoned lock, as documented above.
    #[allow(clippy::expect_used)]
    pub fn entries(&self) -> Vec<Entry> {
        self.state.read().expect("lock poisoned").entries()
    }

    /// Returns accumulated message entries only
    ///
    /// Equivalent to calling [`entries`] and filtering to [`Entry::Message`] variants.
    ///
    /// # Panics
    ///
    /// Panics if the internal lock is poisoned.
    ///
    /// [`entries`]: Projection::entries
    // r[impl projection.query.messages]
    // Panics only on a poisoned lock, as documented above.
    #[allow(clippy::expect_used)]
    pub fn messages(&self) -> Vec<Entry> {
        self.state.read().expect("lock poisoned").messages()
    }

    /// Returns accumulated detail entries only
    ///
    /// Equivalent to calling [`entries`] and filtering to [`Entry::Detail`] variants.
    ///
    /// # Panics
    ///
    /// Panics if the internal lock is poisoned.
    ///
    /// [`entries`]: Projection::entries
    // r[impl projection.query.details]
    // Panics only on a poisoned lock, as documented above.
    #[allow(clippy::expect_used)]
    pub fn details(&self) -> Vec<Entry> {
        self.state.read().expect("lock poisoned").details()
    }

    /// Returns accumulated artifact entries only
    ///
    /// Equivalent to calling [`entries`] and filtering to [`Entry::Artifact`] variants.
    ///
    /// # Panics
    ///
    /// Panics if the internal lock is poisoned.
    ///
    /// [`entries`]: Projection::entries
    // r[impl projection.query.artifacts]
    // Panics only on a poisoned lock, as documented above.
    #[allow(clippy::expect_used)]
    pub fn artifacts(&self) -> Vec<Entry> {
        self.state.read().expect("lock poisoned").artifacts()
    }

    /// Reports whether the event stream has closed and all buffered events have been drained
    ///
    /// Returns `true` once all [`EventSender`]s have been dropped and the drain task has
    /// processed every buffered event. Before that point, returns `false`.
    ///
    /// # Panics
    ///
    /// Panics if the internal lock is poisoned.
    ///
    /// [`EventSender`]: clawless_core::event::EventSender
    // r[impl projection.lifecycle.complete]
    // Panics only on a poisoned lock, as documented above.
    #[allow(clippy::expect_used)]
    pub fn is_complete(&self) -> bool {
        self.state.read().expect("lock poisoned").is_complete()
    }
}

/// Drains events from the receiver into the shared state
///
/// Runs until `receiver.recv()` returns `None` (all senders dropped, channel empty). Each event
/// is translated into an [`Entry`] and appended to the state. When the loop exits, the state is
/// marked as complete.
///
/// # Panics
///
/// Panics if the internal lock is poisoned.
// The lock is poisoned only if another thread panicked while holding it. The drain task has
// no way to publish events into a state it cannot lock, so it fails loudly instead.
#[allow(clippy::expect_used)]
async fn drain(mut receiver: EventReceiver, state: Arc<RwLock<ProjectionState>>) {
    while let Some(event) = receiver.recv().await {
        let entry = match event {
            Event::Message(text) => Entry::Message(text),
            Event::Detail(text) => Entry::Detail(text),
            Event::Artifact(artifact) => Entry::Artifact(Arc::from(artifact)),
        };
        state.write().expect("lock poisoned").push(entry);
    }
    state.write().expect("lock poisoned").set_complete();
}

#[cfg(test)]
mod tests {
    // An assertion in a test panics by design. A `# Panics` section on every test
    // would repeat that and give the reader no information.
    #![allow(clippy::missing_panics_doc)]

    use std::fmt;

    use clawless_core::event::event_channel;
    use serde::Serialize;

    use super::*;

    #[derive(Clone, Debug, Serialize)]
    struct TestArtifact {
        value: String,
    }

    impl fmt::Display for TestArtifact {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", self.value)
        }
    }

    // r[verify projection.query.artifacts]
    #[tokio::test]
    async fn artifacts_returns_artifact_entries_only() {
        let (sender, receiver) = event_channel();
        let projection = Projection::new(receiver);

        sender
            .send(Event::Message("msg".to_string()))
            .await
            .expect("should send");
        sender
            .send(Event::Artifact(Box::new(TestArtifact {
                value: "art".to_string(),
            })))
            .await
            .expect("should send");
        drop(sender);
        tokio::task::yield_now().await;

        let artifacts = projection.artifacts();

        assert_eq!(artifacts.len(), 1);
        let Entry::Artifact(a) = &artifacts[0] else {
            panic!("expected Entry::Artifact");
        };
        assert_eq!(a.to_string(), "art");
    }

    // r[verify projection.query.details]
    #[tokio::test]
    async fn details_returns_detail_entries_only() {
        let (sender, receiver) = event_channel();
        let projection = Projection::new(receiver);

        sender
            .send(Event::Detail("dtl".to_string()))
            .await
            .expect("should send");
        sender
            .send(Event::Message("msg".to_string()))
            .await
            .expect("should send");
        sender
            .send(Event::Detail("dtl2".to_string()))
            .await
            .expect("should send");
        drop(sender);
        tokio::task::yield_now().await;

        let details = projection.details();

        assert_eq!(details.len(), 2);
        let Entry::Detail(s) = &details[0] else {
            panic!("expected Entry::Detail");
        };
        assert_eq!(s, "dtl");
        let Entry::Detail(s) = &details[1] else {
            panic!("expected Entry::Detail");
        };
        assert_eq!(s, "dtl2");
    }

    #[tokio::test]
    async fn drain_processes_buffered_events_before_completing() {
        let (sender, receiver) = event_channel();

        sender
            .send(Event::Message("buffered".to_string()))
            .await
            .expect("should send");
        drop(sender);

        let projection = Projection::new(receiver);
        tokio::task::yield_now().await;

        assert!(projection.is_complete());
        let entries = projection.entries();
        assert_eq!(entries.len(), 1);
        let Entry::Message(s) = &entries[0] else {
            panic!("expected Entry::Message");
        };
        assert_eq!(s, "buffered");
    }

    // r[verify projection.entry.artifact]
    #[tokio::test]
    async fn entries_returns_artifact_events_as_entries() {
        let (sender, receiver) = event_channel();
        let projection = Projection::new(receiver);

        sender
            .send(Event::Artifact(Box::new(TestArtifact {
                value: "result".to_string(),
            })))
            .await
            .expect("should send");
        drop(sender);
        tokio::task::yield_now().await;

        let entries = projection.entries();

        assert_eq!(entries.len(), 1);
        let Entry::Artifact(a) = &entries[0] else {
            panic!("expected Entry::Artifact");
        };
        assert_eq!(a.to_string(), "result");
    }

    // r[verify projection.entry.detail]
    #[tokio::test]
    async fn entries_returns_detail_events_as_entries() {
        let (sender, receiver) = event_channel();
        let projection = Projection::new(receiver);

        sender
            .send(Event::Detail("info".to_string()))
            .await
            .expect("should send");
        drop(sender);
        tokio::task::yield_now().await;

        let entries = projection.entries();

        assert_eq!(entries.len(), 1);
        let Entry::Detail(s) = &entries[0] else {
            panic!("expected Entry::Detail");
        };
        assert_eq!(s, "info");
    }

    // r[verify projection.entry.message]
    // r[verify projection.query.entries]
    #[tokio::test]
    async fn entries_returns_message_events_as_entries() {
        let (sender, receiver) = event_channel();
        let projection = Projection::new(receiver);

        sender
            .send(Event::Message("hello".to_string()))
            .await
            .expect("should send");
        drop(sender);
        tokio::task::yield_now().await;

        let entries = projection.entries();

        assert_eq!(entries.len(), 1);
        let Entry::Message(s) = &entries[0] else {
            panic!("expected Entry::Message");
        };
        assert_eq!(s, "hello");
    }

    // r[verify projection.entry.order]
    #[tokio::test]
    async fn entries_preserves_receive_order() {
        let (sender, receiver) = event_channel();
        let projection = Projection::new(receiver);

        sender
            .send(Event::Message("first".to_string()))
            .await
            .expect("should send");
        sender
            .send(Event::Detail("second".to_string()))
            .await
            .expect("should send");
        sender
            .send(Event::Message("third".to_string()))
            .await
            .expect("should send");
        drop(sender);
        tokio::task::yield_now().await;

        let entries = projection.entries();

        assert_eq!(entries.len(), 3);
        let Entry::Message(s) = &entries[0] else {
            panic!("expected Entry::Message");
        };
        assert_eq!(s, "first");
        let Entry::Detail(s) = &entries[1] else {
            panic!("expected Entry::Detail");
        };
        assert_eq!(s, "second");
        let Entry::Message(s) = &entries[2] else {
            panic!("expected Entry::Message");
        };
        assert_eq!(s, "third");
    }

    #[tokio::test]
    async fn is_complete_returns_false_while_channel_open() {
        let (_sender, receiver) = event_channel();
        let projection = Projection::new(receiver);

        tokio::task::yield_now().await;

        assert!(!projection.is_complete());
    }

    // r[verify projection.query.messages]
    #[tokio::test]
    async fn messages_returns_message_entries_only() {
        let (sender, receiver) = event_channel();
        let projection = Projection::new(receiver);

        sender
            .send(Event::Message("msg".to_string()))
            .await
            .expect("should send");
        sender
            .send(Event::Detail("dtl".to_string()))
            .await
            .expect("should send");
        sender
            .send(Event::Message("msg2".to_string()))
            .await
            .expect("should send");
        drop(sender);
        tokio::task::yield_now().await;

        let messages = projection.messages();

        assert_eq!(messages.len(), 2);
        let Entry::Message(s) = &messages[0] else {
            panic!("expected Entry::Message");
        };
        assert_eq!(s, "msg");
        let Entry::Message(s) = &messages[1] else {
            panic!("expected Entry::Message");
        };
        assert_eq!(s, "msg2");
    }

    // r[verify projection.new]
    #[tokio::test]
    async fn new_returns_empty_projection() {
        let (_sender, receiver) = event_channel();

        let projection = Projection::new(receiver);

        assert!(projection.entries().is_empty());
        assert!(!projection.is_complete());
    }

    // r[verify projection.new.drain]
    // r[verify projection.lifecycle.complete]
    #[tokio::test]
    async fn new_starts_drain_that_completes_when_channel_closes() {
        let (sender, receiver) = event_channel();
        let projection = Projection::new(receiver);

        drop(sender);
        tokio::task::yield_now().await;

        assert!(projection.is_complete());
    }

    // r[verify projection.safety.send]
    #[test]
    fn trait_send() {
        fn assert_send<T: Send>() {}
        assert_send::<Projection>();
    }

    // r[verify projection.safety.sync]
    #[test]
    fn trait_sync() {
        fn assert_sync<T: Sync>() {}
        assert_sync::<Projection>();
    }

    // r[verify projection.safety.unpin]
    #[test]
    fn trait_unpin() {
        fn assert_unpin<T: Unpin>() {}
        assert_unpin::<Projection>();
    }
}