envision 0.10.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
//! Widget wrapper for adding annotations.

use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::widgets::Widget;

use super::types::Annotation;

/// A wrapper that adds annotation metadata to a widget.
///
/// When rendered, the annotation is registered with the current
/// annotation context (if one is active).
///
/// # Example
///
/// ```rust
/// use envision::annotation::{Annotate, Annotation};
/// use ratatui::widgets::Paragraph;
///
/// // Wrap a widget with annotation
/// let annotated = Annotate::new(
///     Paragraph::new("Click me"),
///     Annotation::button("my-button").with_label("My Button"),
/// );
/// ```
pub struct Annotate<W> {
    widget: W,
    annotation: Annotation,
}

impl<W> Annotate<W> {
    /// Creates a new annotated widget.
    pub fn new(widget: W, annotation: Annotation) -> Self {
        Self { widget, annotation }
    }

    /// Returns a reference to the annotation.
    pub fn annotation(&self) -> &Annotation {
        &self.annotation
    }

    /// Returns a mutable reference to the annotation.
    pub fn annotation_mut(&mut self) -> &mut Annotation {
        &mut self.annotation
    }

    /// Returns a reference to the inner widget.
    pub fn inner(&self) -> &W {
        &self.widget
    }

    /// Returns a mutable reference to the inner widget.
    pub fn inner_mut(&mut self) -> &mut W {
        &mut self.widget
    }

    /// Unwraps and returns the inner widget.
    pub fn into_inner(self) -> W {
        self.widget
    }

    /// Sets the focused state.
    pub fn focused(mut self, focused: bool) -> Self {
        self.annotation.focused = focused;
        self
    }

    /// Sets the disabled state.
    pub fn disabled(mut self, disabled: bool) -> Self {
        self.annotation.disabled = disabled;
        self
    }

    /// Sets the selected state.
    pub fn selected(mut self, selected: bool) -> Self {
        self.annotation.selected = selected;
        self
    }

    /// Sets the current value.
    pub fn value(mut self, value: impl Into<String>) -> Self {
        self.annotation.value = Some(value.into());
        self
    }
}

impl<W: Widget> Widget for Annotate<W> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        // Register annotation with context if available
        ANNOTATION_CONTEXT.with(|ctx| {
            if let Some(registry) = ctx.borrow_mut().as_mut() {
                registry.register(area, self.annotation.clone());
            }
        });

        // Render the inner widget
        self.widget.render(area, buf);
    }
}

/// A container annotation that can hold child widgets.
///
/// This opens an annotation scope for nested widgets.
pub struct AnnotateContainer<W> {
    widget: W,
    annotation: Annotation,
}

impl<W> AnnotateContainer<W> {
    /// Creates a new annotated container.
    pub fn new(widget: W, annotation: Annotation) -> Self {
        Self { widget, annotation }
    }
}

impl<W: Widget> Widget for AnnotateContainer<W> {
    fn render(self, area: Rect, buf: &mut Buffer) {
        // Open annotation scope
        ANNOTATION_CONTEXT.with(|ctx| {
            if let Some(registry) = ctx.borrow_mut().as_mut() {
                registry.open(area, self.annotation.clone());
            }
        });

        // Render the inner widget
        self.widget.render(area, buf);

        // Close annotation scope
        ANNOTATION_CONTEXT.with(|ctx| {
            if let Some(registry) = ctx.borrow_mut().as_mut() {
                registry.close();
            }
        });
    }
}

// Thread-local annotation context for collecting annotations during rendering
use super::registry::AnnotationRegistry;
use std::cell::RefCell;

thread_local! {
    static ANNOTATION_CONTEXT: RefCell<Option<AnnotationRegistry>> = const { RefCell::new(None) };
}

/// Sets up an annotation context for collecting annotations during rendering.
///
/// Returns the collected annotations when done.
///
/// # Example
///
/// ```rust
/// use envision::annotation::{with_annotations, Annotate, Annotation};
/// use ratatui::widgets::Paragraph;
/// use ratatui::Terminal;
/// use envision::backend::CaptureBackend;
///
/// let backend = CaptureBackend::new(80, 24);
/// let mut terminal = Terminal::new(backend).unwrap();
///
/// let registry = with_annotations(|| {
///     terminal.draw(|frame| {
///         let widget = Annotate::new(
///             Paragraph::new("Hello"),
///             Annotation::label("greeting"),
///         );
///         frame.render_widget(widget, frame.area());
///     }).unwrap();
/// });
///
/// assert_eq!(registry.len(), 1);
/// ```
pub fn with_annotations<F, R>(f: F) -> AnnotationRegistry
where
    F: FnOnce() -> R,
{
    // Set up the context
    ANNOTATION_CONTEXT.with(|ctx| {
        *ctx.borrow_mut() = Some(AnnotationRegistry::new());
    });

    // Run the function
    f();

    // Extract and return the registry
    ANNOTATION_CONTEXT.with(|ctx| ctx.borrow_mut().take().unwrap_or_default())
}

/// Runs a function with access to the current annotation registry.
///
/// This allows registering annotations manually or querying
/// during rendering.
pub fn with_registry<F, R>(f: F) -> Option<R>
where
    F: FnOnce(&mut AnnotationRegistry) -> R,
{
    ANNOTATION_CONTEXT.with(|ctx| ctx.borrow_mut().as_mut().map(f))
}

#[cfg(test)]
mod tests {
    use super::*;
    use ratatui::widgets::Paragraph;

    #[test]
    fn test_annotate_widget() {
        let widget = Annotate::new(Paragraph::new("Test"), Annotation::button("test-btn"));

        assert!(widget.annotation().has_id("test-btn"));
    }

    #[test]
    fn test_annotate_builder_methods() {
        let widget = Annotate::new(Paragraph::new("Input"), Annotation::input("name"))
            .focused(true)
            .value("John");

        assert!(widget.annotation().focused);
        assert_eq!(widget.annotation().value, Some("John".to_string()));
    }

    #[test]
    fn test_with_annotations() {
        let registry = with_annotations(|| {
            // Simulate rendering by directly using the context
            with_registry(|reg| {
                reg.register(Rect::new(0, 0, 10, 1), Annotation::button("a"));
                reg.register(Rect::new(0, 2, 10, 1), Annotation::button("b"));
            });
        });

        assert_eq!(registry.len(), 2);
    }

    #[test]
    fn test_nested_annotations() {
        let registry = with_annotations(|| {
            with_registry(|reg| {
                reg.open(Rect::new(0, 0, 80, 24), Annotation::container("main"));
                reg.register(Rect::new(5, 5, 10, 1), Annotation::button("inner"));
                reg.close();
            });
        });

        assert_eq!(registry.len(), 2);

        let inner = registry.get_by_id("inner").unwrap();
        assert_eq!(inner.parent, Some(0));
    }

    #[test]
    fn test_annotate_annotation_mut() {
        let mut widget = Annotate::new(Paragraph::new("Test"), Annotation::button("btn"));

        widget.annotation_mut().focused = true;
        assert!(widget.annotation().focused);
    }

    #[test]
    fn test_annotate_inner() {
        let widget = Annotate::new(Paragraph::new("Inner Widget"), Annotation::button("btn"));

        let _inner = widget.inner();
        // Can access inner widget
    }

    #[test]
    fn test_annotate_inner_mut() {
        let mut widget = Annotate::new(Paragraph::new("Mutable"), Annotation::button("btn"));

        let _inner = widget.inner_mut();
        // Can access inner widget mutably
    }

    #[test]
    fn test_annotate_into_inner() {
        let widget = Annotate::new(Paragraph::new("Unwrap Me"), Annotation::button("btn"));

        let _inner = widget.into_inner();
        // Successfully unwrapped
    }

    #[test]
    fn test_annotate_disabled() {
        let widget =
            Annotate::new(Paragraph::new("Disabled"), Annotation::button("btn")).disabled(true);

        assert!(widget.annotation().disabled);
    }

    #[test]
    fn test_annotate_selected() {
        let widget =
            Annotate::new(Paragraph::new("Selected"), Annotation::button("btn")).selected(true);

        assert!(widget.annotation().selected);
    }

    #[test]
    fn test_annotate_container_new() {
        let container =
            AnnotateContainer::new(Paragraph::new("Container"), Annotation::container("main"));

        // Verify it was created (can only be verified by rendering)
        let _ = container;
    }

    #[test]
    fn test_with_registry_no_context() {
        // Call with_registry outside of with_annotations context
        let result = with_registry(|_| 42);
        assert!(result.is_none());
    }

    #[test]
    fn test_with_registry_returns_value() {
        let value = with_annotations(|| with_registry(|_| 42));
        // The return value of f() is passed through
        // But with_annotations returns the registry, not the value
        assert!(value.is_empty());
    }

    #[test]
    fn test_annotate_render_with_terminal() {
        use crate::backend::CaptureBackend;
        use ratatui::Terminal;

        let backend = CaptureBackend::new(40, 10);
        let mut terminal = Terminal::new(backend).unwrap();

        let registry = with_annotations(|| {
            terminal
                .draw(|frame| {
                    let widget =
                        Annotate::new(Paragraph::new("Rendered"), Annotation::button("btn"));
                    frame.render_widget(widget, frame.area());
                })
                .unwrap();
        });

        // Widget was rendered and annotation registered
        assert_eq!(registry.len(), 1);
        assert!(registry.get_by_id("btn").is_some());

        // Text was rendered
        assert!(terminal.backend().contains_text("Rendered"));
    }

    #[test]
    fn test_annotate_container_render() {
        use crate::backend::CaptureBackend;
        use ratatui::Terminal;

        let backend = CaptureBackend::new(40, 10);
        let mut terminal = Terminal::new(backend).unwrap();

        let registry = with_annotations(|| {
            terminal
                .draw(|frame| {
                    let container = AnnotateContainer::new(
                        Paragraph::new("Container Content"),
                        Annotation::container("main"),
                    );
                    frame.render_widget(container, frame.area());
                })
                .unwrap();
        });

        // Container annotation was registered
        assert_eq!(registry.len(), 1);
        assert!(registry.get_by_id("main").is_some());
    }

    #[test]
    fn test_annotate_combined_states() {
        let widget = Annotate::new(Paragraph::new("All States"), Annotation::input("input"))
            .focused(true)
            .disabled(true)
            .selected(true)
            .value("test value");

        let annotation = widget.annotation();
        assert!(annotation.focused);
        assert!(annotation.disabled);
        assert!(annotation.selected);
        assert_eq!(annotation.value, Some("test value".to_string()));
    }

    #[test]
    fn test_nested_rendering() {
        use crate::backend::CaptureBackend;
        use ratatui::Terminal;

        let backend = CaptureBackend::new(80, 24);
        let mut terminal = Terminal::new(backend).unwrap();

        let registry = with_annotations(|| {
            terminal
                .draw(|frame| {
                    // Render a container with nested widgets
                    let container = AnnotateContainer::new(
                        Paragraph::new("Parent"),
                        Annotation::container("parent"),
                    );
                    let inner = Annotate::new(Paragraph::new("Child"), Annotation::button("child"));

                    // In real code these would be nested via layout
                    // Here we just render them separately to test
                    let area1 = Rect::new(0, 0, 40, 5);
                    let area2 = Rect::new(0, 5, 40, 5);

                    frame.render_widget(container, area1);
                    frame.render_widget(inner, area2);
                })
                .unwrap();
        });

        assert_eq!(registry.len(), 2);
    }
}