boxmux 0.240.3373

YAML-driven terminal UI framework for rich, interactive CLI applications and dashboards with PTY 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
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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
#[cfg(test)]
mod tests {
    use super::super::choice_content::ChoiceContent;
    use super::super::renderable_content::*;
    use super::super::text_content::TextContent;
    use crate::model::choice::Choice;
    use crate::{Bounds, ScreenBuffer};
    use std::time::Duration;
    use std::time::SystemTime;

    #[test]
    fn test_content_event_creation() {
        // Test basic event creation
        let click_event = ContentEvent::new_click(Some((10, 5)), Some("choice_1".to_string()));
        assert_eq!(click_event.event_type, EventType::Click);
        assert_eq!(click_event.position, Some((10, 5)));
        assert_eq!(click_event.zone_id, Some("choice_1".to_string()));
        assert_eq!(click_event.data.mouse_button, Some(MouseButton::Left));

        // Test hover event
        let hover_event = ContentEvent::new_hover((15, 8), Some("text_area".to_string()));
        assert_eq!(hover_event.event_type, EventType::Hover);
        assert_eq!(hover_event.position, Some((15, 8)));
        assert_eq!(hover_event.zone_id, Some("text_area".to_string()));

        // Test key press event
        let key_event = ContentEvent::new_keypress(
            "ArrowDown".to_string(),
            vec![KeyModifier::Ctrl],
            Some("menu".to_string()),
        );
        assert_eq!(key_event.event_type, EventType::KeyPress);
        assert!(key_event.key_info().is_some());
        assert_eq!(key_event.key_info().unwrap().key, "ArrowDown");
        assert_eq!(
            key_event.key_info().unwrap().modifiers,
            vec![KeyModifier::Ctrl]
        );
    }

    #[test]
    fn test_content_event_helper_methods() {
        let click_event = ContentEvent::new_click(None, None);
        assert!(click_event.is_click());
        assert!(!click_event.is_double_click());
        assert!(!click_event.is_keyboard());

        let key_event = ContentEvent::new_keypress("Enter".to_string(), vec![], None);
        assert!(!key_event.is_click());
        assert!(key_event.is_keyboard());

        let scroll_event = ContentEvent::new_scroll(ScrollDirection::Down, 3, None);
        assert!(scroll_event.scroll_info().is_some());
        assert_eq!(
            scroll_event.scroll_info().unwrap().direction,
            ScrollDirection::Down
        );
        assert_eq!(scroll_event.scroll_info().unwrap().amount, 3);
    }

    #[test]
    fn test_choice_content_event_handling() {
        let mut choices = vec![
            Choice {
                content: Some("Option 1".to_string()),
                selected: false,
                waiting: false,
                ..Default::default()
            },
            Choice {
                content: Some("Option 2".to_string()),
                selected: true,
                waiting: false,
                ..Default::default()
            },
        ];

        let white_color = Some("white".to_string());
        let yellow_color = Some("yellow".to_string());
        let blue_color = Some("blue".to_string());

        let mut choice_content = ChoiceContent::new(
            &choices,
            &white_color,
            &None,
            &yellow_color,
            &blue_color,
            &None,
            &None,
        );

        // Test valid choice click
        let click_event = ContentEvent::new_click(Some((5, 0)), Some("choice_0".to_string()));
        let result = choice_content.handle_event(&click_event);
        assert_eq!(result, EventResult::Handled);

        // Test invalid choice click
        let invalid_click = ContentEvent::new_click(Some((5, 0)), Some("choice_5".to_string()));
        let result = choice_content.handle_event(&invalid_click);
        assert_eq!(result, EventResult::NotHandled);

        // Test hover event
        let hover_event = ContentEvent::new_hover((5, 1), Some("choice_1".to_string()));
        let result = choice_content.handle_event(&hover_event);
        assert_eq!(result, EventResult::HandledContinue);

        // Test keyboard navigation
        let key_event = ContentEvent::new_keypress(
            "ArrowDown".to_string(),
            vec![],
            Some("choice_0".to_string()),
        );
        let result = choice_content.handle_event(&key_event);
        assert_eq!(result, EventResult::Handled);

        // Test non-navigation key
        let other_key = ContentEvent::new_keypress("x".to_string(), vec![], None);
        let result = choice_content.handle_event(&other_key);
        assert_eq!(result, EventResult::NotHandled);
    }

    #[test]
    fn test_text_content_event_handling() {
        let white_text_color = Some("white".to_string());

        let mut text_content =
            TextContent::new("Sample text content\nLine 2", &white_text_color, &None);

        // Test click - text generally doesn't handle clicks
        let click_event = ContentEvent::new_click(Some((5, 0)), None);
        let result = text_content.handle_event(&click_event);
        assert_eq!(result, EventResult::NotHandled);

        // Test Ctrl+C for copy
        let copy_event = ContentEvent::new_keypress("c".to_string(), vec![KeyModifier::Ctrl], None);
        let result = text_content.handle_event(&copy_event);
        assert_eq!(result, EventResult::NotHandled); // Could be handled at higher level

        // Test scroll event
        let scroll_event = ContentEvent::new_scroll(ScrollDirection::Down, 1, None);
        let result = text_content.handle_event(&scroll_event);
        assert_eq!(result, EventResult::HandledContinue); // Let scrollbars handle
    }

    #[test]
    fn test_event_backward_compatibility() {
        let mut choices = vec![Choice {
            content: Some("Test Choice".to_string()),
            selected: false,
            waiting: false,
            ..Default::default()
        }];

        let white_dep_color = Some("white".to_string());
        let yellow_dep_color = Some("yellow".to_string());
        let blue_dep_color = Some("blue".to_string());

        let mut choice_content = ChoiceContent::new(
            &choices,
            &white_dep_color,
            &None,
            &yellow_dep_color,
            &blue_dep_color,
            &None,
            &None,
        );

        // Test event system with proper click events
        let click_event = ContentEvent::new_click(Some((5, 0)), Some("choice_0".to_string()));
        let result = choice_content.handle_event(&click_event);
        assert!(matches!(result, EventResult::Handled));

        let invalid_event = ContentEvent::new_click(Some((5, 0)), Some("choice_5".to_string()));
        let invalid_result = choice_content.handle_event(&invalid_event);
        assert!(matches!(invalid_result, EventResult::NotHandled));

        // Test with text content
        let mut text_content = TextContent::new("test", &None, &None);
        let text_event = ContentEvent::new_click(Some((0, 0)), None);
        let result = text_content.handle_event(&text_event);
        assert!(matches!(result, EventResult::NotHandled)); // Text doesn't handle clicks
    }

    #[test]
    fn test_custom_events() {
        let mut choices = vec![Choice {
            content: Some("Custom Choice".to_string()),
            ..Default::default()
        }];

        let mut choice_content =
            ChoiceContent::new(&choices, &None, &None, &None, &None, &None, &None);

        // Test custom event
        let custom_event = ContentEvent::new_custom(
            "selection_changed".to_string(),
            Some("choice_0".to_string()),
            Some("choice_0".to_string()),
        );

        let result = choice_content.handle_event(&custom_event);
        assert_eq!(result, EventResult::NotHandled); // Custom events not handled by default
    }

    #[test]
    fn test_event_data_defaults() {
        let event_data = EventData::default();
        assert!(event_data.mouse_button.is_none());
        assert!(event_data.key.is_none());
        assert!(event_data.scroll.is_none());
        assert!(event_data.size.is_none());
        assert!(event_data.custom_data.is_none());
    }

    #[test]
    fn test_multiple_event_types() {
        let mut text_content = TextContent::new("test content", &None, &None);

        // Test focus event
        let focus_event = ContentEvent::new_focus(None);
        let result = text_content.handle_event(&focus_event);
        assert_eq!(result, EventResult::NotHandled);

        // Test blur event
        let blur_event = ContentEvent::new_blur(None);
        let result = text_content.handle_event(&blur_event);
        assert_eq!(result, EventResult::NotHandled);

        // Test resize event
        let resize_event = ContentEvent::new_resize((100, 50));
        let result = text_content.handle_event(&resize_event);
        assert_eq!(result, EventResult::NotHandled);

        // Test double click
        let mut event = ContentEvent::new_click(None, None);
        event.event_type = EventType::DoubleClick;
        let result = text_content.handle_event(&event);
        assert_eq!(result, EventResult::NotHandled);
    }

    #[test]
    fn test_mouse_button_variants() {
        // Test different mouse buttons
        let left_click = ContentEvent::new_click_with_button(None, None, MouseButton::Left);
        assert_eq!(left_click.mouse_button(), Some(&MouseButton::Left));

        let right_click = ContentEvent::new_click_with_button(None, None, MouseButton::Right);
        assert_eq!(right_click.mouse_button(), Some(&MouseButton::Right));

        let middle_click = ContentEvent::new_click_with_button(None, None, MouseButton::Middle);
        assert_eq!(middle_click.mouse_button(), Some(&MouseButton::Middle));

        let wheel_up = ContentEvent::new_click_with_button(None, None, MouseButton::WheelUp);
        assert_eq!(wheel_up.mouse_button(), Some(&MouseButton::WheelUp));
    }

    #[test]
    fn test_key_modifiers() {
        let event = ContentEvent::new_keypress(
            "s".to_string(),
            vec![KeyModifier::Ctrl, KeyModifier::Shift],
            None,
        );

        let key_info = event.key_info().unwrap();
        assert_eq!(key_info.key, "s");
        assert!(key_info.modifiers.contains(&KeyModifier::Ctrl));
        assert!(key_info.modifiers.contains(&KeyModifier::Shift));
        assert!(!key_info.modifiers.contains(&KeyModifier::Alt));
    }

    #[test]
    fn test_scroll_directions() {
        let down_scroll = ContentEvent::new_scroll(ScrollDirection::Down, 5, None);
        assert_eq!(
            down_scroll.scroll_info().unwrap().direction,
            ScrollDirection::Down
        );

        let up_scroll = ContentEvent::new_scroll(ScrollDirection::Up, 3, None);
        assert_eq!(
            up_scroll.scroll_info().unwrap().direction,
            ScrollDirection::Up
        );

        let left_scroll = ContentEvent::new_scroll(ScrollDirection::Left, 2, None);
        assert_eq!(
            left_scroll.scroll_info().unwrap().direction,
            ScrollDirection::Left
        );

        let right_scroll = ContentEvent::new_scroll(ScrollDirection::Right, 1, None);
        assert_eq!(
            right_scroll.scroll_info().unwrap().direction,
            ScrollDirection::Right
        );
    }

    #[test]
    fn test_mouse_move_events() {
        // Test basic mouse move
        let move_event =
            ContentEvent::new_mouse_move(Some((5, 5)), (10, 8), Some("text_area".to_string()));
        assert!(move_event.is_mouse_move());
        assert_eq!(move_event.position, Some((10, 8)));

        let move_info = move_event.mouse_move_info().unwrap();
        assert_eq!(move_info.from_position, Some((5, 5)));
        assert_eq!(move_info.to_position, (10, 8));
        assert_eq!(move_info.delta, (5, 3));
        assert!(!move_info.is_dragging);

        // Test mouse drag
        let drag_event = ContentEvent::new_mouse_drag(
            (5, 5),
            (15, 10),
            MouseButton::Left,
            Some("choice_1".to_string()),
        );
        assert!(drag_event.is_mouse_move());
        assert!(drag_event.is_drag());

        let drag_info = drag_event.mouse_move_info().unwrap();
        assert!(drag_info.is_dragging);
        assert_eq!(drag_info.drag_button, Some(MouseButton::Left));
        assert_eq!(drag_info.delta, (10, 5));
    }

    #[test]
    fn test_hover_events() {
        // Test hover enter
        let hover_enter = ContentEvent::new_hover_enter(
            (10, 5),
            "choice_0".to_string(),
            Some("previous_zone".to_string()),
        );
        assert!(hover_enter.is_hover_enter());
        assert!(!hover_enter.is_hover_leave());

        let hover_info = hover_enter.hover_info().unwrap();
        assert_eq!(hover_info.state, HoverState::Enter);
        assert_eq!(hover_info.current_zone, Some("choice_0".to_string()));
        assert_eq!(hover_info.previous_zone, Some("previous_zone".to_string()));

        // Test hover leave
        let hover_leave = ContentEvent::new_hover_leave(
            (12, 6),
            "choice_0".to_string(),
            Some("new_zone".to_string()),
        );
        assert!(hover_leave.is_hover_leave());
        assert!(!hover_leave.is_hover_enter());

        // Test hover move with duration
        let hover_move = ContentEvent::new_hover_move(
            (11, 5),
            "choice_0".to_string(),
            Duration::from_millis(1500),
        );
        let hover_info = hover_move.hover_info().unwrap();
        assert_eq!(hover_info.state, HoverState::Move);
        assert!(hover_info.hover_duration.is_some());
        assert_eq!(
            hover_info.hover_duration.unwrap(),
            Duration::from_millis(1500)
        );
    }

    #[test]
    fn test_box_resize_events() {
        let resize_event = ContentEvent::new_box_resize(
            BoxResizeType::Interactive,
            (10, 10, 50, 30), // original bounds
            (10, 10, 60, 35), // new bounds
            ResizeAnchor::BottomRight,
            ResizeState::InProgress,
        );

        assert!(resize_event.is_box_resize());

        let resize_info = resize_event.box_resize_info().unwrap();
        assert_eq!(resize_info.resize_type, BoxResizeType::Interactive);
        assert_eq!(resize_info.original_bounds, (10, 10, 50, 30));
        assert_eq!(resize_info.new_bounds, (10, 10, 60, 35));
        assert_eq!(resize_info.anchor, ResizeAnchor::BottomRight);
        assert_eq!(resize_info.state, ResizeState::InProgress);
    }

    #[test]
    fn test_title_change_events() {
        let title_event = ContentEvent::new_title_change(
            Some("Old Title".to_string()),
            "New Title".to_string(),
            TitleChangeSource::User,
            true,
        );

        assert!(title_event.is_title_change());

        let title_info = title_event.title_change_info().unwrap();
        assert_eq!(title_info.old_title, Some("Old Title".to_string()));
        assert_eq!(title_info.new_title, "New Title");
        assert_eq!(title_info.source, TitleChangeSource::User);
        assert!(title_info.persist);
    }

    #[test]
    fn test_extended_choice_event_handling() {
        let mut choices = vec![Choice {
            content: Some("Hover Choice".to_string()),
            selected: false,
            waiting: false,
            ..Default::default()
        }];

        let white_hover_color = Some("white".to_string());
        let yellow_hover_color = Some("yellow".to_string());
        let blue_hover_color = Some("blue".to_string());

        let mut choice_content = ChoiceContent::new(
            &choices,
            &white_hover_color,
            &None,
            &yellow_hover_color,
            &blue_hover_color,
            &None,
            &None,
        );

        // Test hover enter
        let hover_enter = ContentEvent::new_hover_enter((5, 0), "choice_0".to_string(), None);
        let result = choice_content.handle_event(&hover_enter);
        assert_eq!(result, EventResult::HandledContinue);

        // Test mouse move (non-drag)
        let mouse_move =
            ContentEvent::new_mouse_move(Some((5, 0)), (6, 0), Some("choice_0".to_string()));
        let result = choice_content.handle_event(&mouse_move);
        assert_eq!(result, EventResult::HandledContinue);

        // Test mouse drag
        let mouse_drag = ContentEvent::new_mouse_drag(
            (5, 0),
            (10, 0),
            MouseButton::Left,
            Some("choice_0".to_string()),
        );
        let result = choice_content.handle_event(&mouse_drag);
        assert_eq!(result, EventResult::NotHandled); // Let higher level handle
    }

    #[test]
    fn test_extended_text_event_handling() {
        let white_interaction_color = Some("white".to_string());

        let mut text_content = TextContent::new(
            "Sample text for interaction testing",
            &white_interaction_color,
            &None,
        );

        // Test mouse move over text
        let mouse_move = ContentEvent::new_mouse_move(Some((5, 0)), (10, 0), None);
        let result = text_content.handle_event(&mouse_move);
        assert_eq!(result, EventResult::NotHandled);

        // Test text selection drag
        let drag_event = ContentEvent::new_mouse_drag((5, 0), (15, 0), MouseButton::Left, None);
        let result = text_content.handle_event(&drag_event);
        assert_eq!(result, EventResult::NotHandled); // Could be implemented for text selection

        // Test hover for tooltips
        let hover_enter = ContentEvent::new_hover_enter((10, 0), "word_zone".to_string(), None);
        let result = text_content.handle_event(&hover_enter);
        assert_eq!(result, EventResult::NotHandled); // Could show word tooltips

        // Test resize triggering reflow
        let resize_event = ContentEvent::new_box_resize(
            BoxResizeType::Interactive,
            (0, 0, 50, 20),
            (0, 0, 60, 20),
            ResizeAnchor::Right,
            ResizeState::Completed,
        );
        let result = text_content.handle_event(&resize_event);
        assert_eq!(result, EventResult::StateChanged); // Text needs reflow
    }

    #[test]
    fn test_resize_anchor_types() {
        // Test all resize anchor types
        let anchors = vec![
            ResizeAnchor::TopLeft,
            ResizeAnchor::TopRight,
            ResizeAnchor::BottomLeft,
            ResizeAnchor::BottomRight,
            ResizeAnchor::Top,
            ResizeAnchor::Bottom,
            ResizeAnchor::Left,
            ResizeAnchor::Right,
        ];

        for anchor in anchors {
            let resize_event = ContentEvent::new_box_resize(
                BoxResizeType::Interactive,
                (10, 10, 50, 30),
                (15, 15, 55, 35),
                anchor.clone(),
                ResizeState::Started,
            );

            assert!(resize_event.is_box_resize());
            assert_eq!(resize_event.box_resize_info().unwrap().anchor, anchor);
        }
    }

    #[test]
    fn test_title_change_sources() {
        let sources = vec![
            TitleChangeSource::User,
            TitleChangeSource::PTY,
            TitleChangeSource::Script,
            TitleChangeSource::API,
            TitleChangeSource::System,
        ];

        for source in sources {
            let title_event = ContentEvent::new_title_change(
                None,
                "Test Title".to_string(),
                source.clone(),
                false,
            );

            assert!(title_event.is_title_change());
            assert_eq!(title_event.title_change_info().unwrap().source, source);
        }
    }

    #[test]
    fn test_movement_delta_calculation() {
        // Test positive movement
        let move_event = ContentEvent::new_mouse_move(Some((5, 10)), (15, 20), None);
        assert_eq!(move_event.movement_delta(), Some((10, 10)));

        // Test negative movement
        let move_back = ContentEvent::new_mouse_move(Some((20, 25)), (10, 15), None);
        assert_eq!(move_back.movement_delta(), Some((-10, -10)));

        // Test no previous position
        let move_start = ContentEvent::new_mouse_move(None, (10, 10), None);
        assert_eq!(move_start.movement_delta(), Some((0, 0)));

        // Test non-move event
        let click_event = ContentEvent::new_click(None, None);
        assert_eq!(click_event.movement_delta(), None);
    }

    #[test]
    fn test_extended_event_data_defaults() {
        let event_data = EventData::default();
        assert!(event_data.mouse_button.is_none());
        assert!(event_data.key.is_none());
        assert!(event_data.scroll.is_none());
        assert!(event_data.size.is_none());
        assert!(event_data.mouse_move.is_none());
        assert!(event_data.hover.is_none());
        assert!(event_data.box_resize.is_none());
        assert!(event_data.title_change.is_none());
        assert!(event_data.custom_data.is_none());
    }
}