envision 0.16.0

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
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
//! Event types for terminal input.

use super::key::{Key, KeyEvent, Modifiers};
use super::mouse::{MouseButton, MouseEvent, MouseEventKind};

/// A terminal input event.
///
/// This provides a unified interface for handling input events. The same
/// type is used whether events come from a real terminal or are injected
/// programmatically.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Event {
    /// A keyboard event
    Key(KeyEvent),

    /// A mouse event
    Mouse(MouseEvent),

    /// A resize event (width, height)
    Resize(u16, u16),

    /// Focus gained
    FocusGained,

    /// Focus lost
    FocusLost,

    /// A paste event (bracketed paste content)
    Paste(String),
}

impl Event {
    /// Creates a key press event for a character.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::input::Event;
    ///
    /// let event = Event::char('a');
    /// assert!(event.is_key());
    /// ```
    pub fn char(c: char) -> Self {
        Self::Key(KeyEvent::char(c))
    }

    /// Creates a key press event for a character with modifiers.
    pub fn char_with(c: char, modifiers: Modifiers) -> Self {
        let mut ke = KeyEvent::char(c);
        ke.modifiers |= modifiers;
        Self::Key(ke)
    }

    /// Creates a key press event for a special key.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::input::{Event, Key};
    ///
    /// let event = Event::key(Key::Enter);
    /// assert!(event.is_key());
    /// ```
    pub fn key(key: Key) -> Self {
        Self::Key(KeyEvent::new(key))
    }

    /// Creates a key press event with modifiers.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::input::{Event, Key, Modifiers};
    ///
    /// let event = Event::key_with(Key::Char('s'), Modifiers::CONTROL);
    /// assert!(event.is_key());
    /// ```
    pub fn key_with(key: Key, modifiers: Modifiers) -> Self {
        let mut ev = KeyEvent::new(key);
        ev.modifiers |= modifiers;
        Self::Key(ev)
    }

    /// Creates a Ctrl+key event.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::input::Event;
    ///
    /// let event = Event::ctrl('c');
    /// assert!(event.is_key());
    /// ```
    pub fn ctrl(c: char) -> Self {
        Self::Key(KeyEvent::ctrl(c))
    }

    /// Creates an Alt+key event.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::input::Event;
    ///
    /// let event = Event::alt('x');
    /// assert!(event.is_key());
    /// ```
    pub fn alt(c: char) -> Self {
        Self::Key(KeyEvent {
            code: Key::Char(c.to_ascii_lowercase()),
            modifiers: Modifiers::ALT,
            kind: super::key::KeyEventKind::Press,
            raw_char: Some(c),
        })
    }

    /// Creates a mouse click event at the specified position.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::input::Event;
    ///
    /// let event = Event::click(10, 5);
    /// assert!(event.is_mouse());
    /// ```
    pub fn click(x: u16, y: u16) -> Self {
        Self::Mouse(MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: x,
            row: y,
            modifiers: Modifiers::NONE,
        })
    }

    /// Creates a mouse click event with a specific button.
    pub fn click_button(x: u16, y: u16, button: MouseButton) -> Self {
        Self::Mouse(MouseEvent {
            kind: MouseEventKind::Down(button),
            column: x,
            row: y,
            modifiers: Modifiers::NONE,
        })
    }

    /// Creates a mouse release event.
    pub fn mouse_up(x: u16, y: u16) -> Self {
        Self::Mouse(MouseEvent {
            kind: MouseEventKind::Up(MouseButton::Left),
            column: x,
            row: y,
            modifiers: Modifiers::NONE,
        })
    }

    /// Creates a mouse move event.
    pub fn mouse_move(x: u16, y: u16) -> Self {
        Self::Mouse(MouseEvent {
            kind: MouseEventKind::Moved,
            column: x,
            row: y,
            modifiers: Modifiers::NONE,
        })
    }

    /// Creates a mouse drag event.
    pub fn mouse_drag(x: u16, y: u16, button: MouseButton) -> Self {
        Self::Mouse(MouseEvent {
            kind: MouseEventKind::Drag(button),
            column: x,
            row: y,
            modifiers: Modifiers::NONE,
        })
    }

    /// Creates a scroll up event.
    pub fn scroll_up(x: u16, y: u16) -> Self {
        Self::Mouse(MouseEvent {
            kind: MouseEventKind::ScrollUp,
            column: x,
            row: y,
            modifiers: Modifiers::NONE,
        })
    }

    /// Creates a scroll down event.
    pub fn scroll_down(x: u16, y: u16) -> Self {
        Self::Mouse(MouseEvent {
            kind: MouseEventKind::ScrollDown,
            column: x,
            row: y,
            modifiers: Modifiers::NONE,
        })
    }

    /// Returns true if this is a key event.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::input::Event;
    ///
    /// assert!(Event::char('a').is_key());
    /// assert!(!Event::click(0, 0).is_key());
    /// ```
    pub fn is_key(&self) -> bool {
        matches!(self, Event::Key(_))
    }

    /// Returns true if this is a mouse event.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::input::Event;
    ///
    /// assert!(Event::click(0, 0).is_mouse());
    /// assert!(!Event::char('a').is_mouse());
    /// ```
    pub fn is_mouse(&self) -> bool {
        matches!(self, Event::Mouse(_))
    }

    /// Returns the key event if this is one.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::input::{Event, Key};
    ///
    /// let event = Event::key(Key::Enter);
    /// assert!(event.as_key().is_some());
    /// assert!(Event::click(0, 0).as_key().is_none());
    /// ```
    pub fn as_key(&self) -> Option<&KeyEvent> {
        match self {
            Event::Key(e) => Some(e),
            _ => None,
        }
    }

    /// Returns the mouse event if this is one.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::input::Event;
    ///
    /// let event = Event::click(5, 10);
    /// assert!(event.as_mouse().is_some());
    /// assert!(Event::char('a').as_mouse().is_none());
    /// ```
    pub fn as_mouse(&self) -> Option<&MouseEvent> {
        match self {
            Event::Mouse(e) => Some(e),
            _ => None,
        }
    }

    /// Returns a short string identifying the event variant.
    ///
    /// This is useful for logging and tracing. It returns the variant
    /// name (e.g., `"Key"`, `"Mouse"`, `"Resize"`) without the inner data.
    ///
    /// # Example
    ///
    /// ```rust
    /// use envision::input::{Event, Key};
    ///
    /// assert_eq!(Event::char('a').kind_name(), "Key");
    /// assert_eq!(Event::click(0, 0).kind_name(), "Mouse");
    /// assert_eq!(Event::Resize(80, 24).kind_name(), "Resize");
    /// assert_eq!(Event::FocusGained.kind_name(), "FocusGained");
    /// assert_eq!(Event::FocusLost.kind_name(), "FocusLost");
    /// ```
    pub fn kind_name(&self) -> &'static str {
        match self {
            Event::Key(_) => "Key",
            Event::Mouse(_) => "Mouse",
            Event::Resize(_, _) => "Resize",
            Event::FocusGained => "FocusGained",
            Event::FocusLost => "FocusLost",
            Event::Paste(_) => "Paste",
        }
    }
}

impl From<KeyEvent> for Event {
    fn from(event: KeyEvent) -> Self {
        Event::Key(event)
    }
}

impl From<MouseEvent> for Event {
    fn from(event: MouseEvent) -> Self {
        Event::Mouse(event)
    }
}

/// Builder for creating key events with specific properties.
#[derive(Clone, Debug)]
pub struct KeyEventBuilder {
    key: Option<Key>,
    modifiers: Modifiers,
    kind: super::key::KeyEventKind,
}

impl Default for KeyEventBuilder {
    fn default() -> Self {
        Self {
            key: None,
            modifiers: Modifiers::NONE,
            kind: super::key::KeyEventKind::Press,
        }
    }
}

impl KeyEventBuilder {
    /// Creates a new builder.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the key.
    pub fn code(mut self, key: Key) -> Self {
        self.key = Some(key);
        self
    }

    /// Sets the key to a character.
    pub fn char(mut self, c: char) -> Self {
        self.key = Some(Key::Char(c));
        self
    }

    /// Adds the Control modifier.
    pub fn ctrl(mut self) -> Self {
        self.modifiers |= Modifiers::CONTROL;
        self
    }

    /// Adds the Alt modifier.
    pub fn alt(mut self) -> Self {
        self.modifiers |= Modifiers::ALT;
        self
    }

    /// Adds the Shift modifier.
    pub fn shift(mut self) -> Self {
        self.modifiers |= Modifiers::SHIFT;
        self
    }

    /// Sets the modifiers directly.
    pub fn modifiers(mut self, modifiers: Modifiers) -> Self {
        self.modifiers = modifiers;
        self
    }

    /// Sets the event kind (Press, Release, Repeat).
    pub fn kind(mut self, kind: super::key::KeyEventKind) -> Self {
        self.kind = kind;
        self
    }

    /// Builds the key event.
    pub fn build(self) -> KeyEvent {
        let key = self.key.unwrap_or(Key::Esc);
        KeyEvent {
            code: key,
            modifiers: self.modifiers,
            kind: self.kind,
            raw_char: match key {
                Key::Char(c) => Some(c),
                _ => None,
            },
        }
    }

    /// Builds and wraps in a Event.
    pub fn into_event(self) -> Event {
        Event::Key(self.build())
    }
}

/// Builder for creating mouse events with specific properties.
#[derive(Clone, Debug)]
pub struct MouseEventBuilder {
    kind: MouseEventKind,
    column: u16,
    row: u16,
    modifiers: Modifiers,
}

impl MouseEventBuilder {
    /// Creates a new builder at position (0, 0).
    pub fn new() -> Self {
        Self {
            kind: MouseEventKind::Moved,
            column: 0,
            row: 0,
            modifiers: Modifiers::NONE,
        }
    }

    /// Sets the position.
    pub fn at(mut self, x: u16, y: u16) -> Self {
        self.column = x;
        self.row = y;
        self
    }

    /// Sets the event to a click.
    pub fn click(mut self) -> Self {
        self.kind = MouseEventKind::Down(MouseButton::Left);
        self
    }

    /// Sets the event to a right-click.
    pub fn right_click(mut self) -> Self {
        self.kind = MouseEventKind::Down(MouseButton::Right);
        self
    }

    /// Sets the event to a middle-click.
    pub fn middle_click(mut self) -> Self {
        self.kind = MouseEventKind::Down(MouseButton::Middle);
        self
    }

    /// Sets the event to a mouse up.
    pub fn up(mut self) -> Self {
        self.kind = MouseEventKind::Up(MouseButton::Left);
        self
    }

    /// Sets the event to a drag.
    pub fn drag(mut self) -> Self {
        self.kind = MouseEventKind::Drag(MouseButton::Left);
        self
    }

    /// Sets the event to a scroll up.
    pub fn scroll_up(mut self) -> Self {
        self.kind = MouseEventKind::ScrollUp;
        self
    }

    /// Sets the event to a scroll down.
    pub fn scroll_down(mut self) -> Self {
        self.kind = MouseEventKind::ScrollDown;
        self
    }

    /// Adds the Control modifier.
    pub fn ctrl(mut self) -> Self {
        self.modifiers |= Modifiers::CONTROL;
        self
    }

    /// Adds the Alt modifier.
    pub fn alt(mut self) -> Self {
        self.modifiers |= Modifiers::ALT;
        self
    }

    /// Adds the Shift modifier.
    pub fn shift(mut self) -> Self {
        self.modifiers |= Modifiers::SHIFT;
        self
    }

    /// Builds the mouse event.
    pub fn build(self) -> MouseEvent {
        MouseEvent {
            kind: self.kind,
            column: self.column,
            row: self.row,
            modifiers: self.modifiers,
        }
    }

    /// Builds and wraps in a Event.
    pub fn into_event(self) -> Event {
        Event::Mouse(self.build())
    }
}

impl Default for MouseEventBuilder {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests;