opentui_rust 0.2.1

High-performance terminal UI rendering engine with alpha blending and diffed buffers
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
//! Test data generators for OpenTUI tests.
//!
//! Provides sample data, random generators, and common test scenarios.

#![allow(clippy::float_cmp)] // Exact float comparison is intentional in tests
#![allow(clippy::uninlined_format_args)] // Clarity over style in test code
#![allow(dead_code)] // Shared test data; not every integration test uses every helper/constant

use opentui::buffer::OptimizedBuffer;
use opentui::cell::Cell;
use opentui::color::Rgba;
use opentui::style::{Style, TextAttributes};
use opentui_rust as opentui;

/// Sample ANSI escape sequences for testing.
pub mod ansi_sequences {
    /// Clear entire screen.
    pub const CLEAR_SCREEN: &[u8] = b"\x1b[2J";

    /// Move cursor to home position (1,1).
    pub const CURSOR_HOME: &[u8] = b"\x1b[H";

    /// Hide cursor.
    pub const CURSOR_HIDE: &[u8] = b"\x1b[?25l";

    /// Show cursor.
    pub const CURSOR_SHOW: &[u8] = b"\x1b[?25h";

    /// Enter alternate screen buffer.
    pub const ALT_SCREEN_ENTER: &[u8] = b"\x1b[?1049h";

    /// Leave alternate screen buffer.
    pub const ALT_SCREEN_LEAVE: &[u8] = b"\x1b[?1049l";

    /// Enable mouse button tracking (SGR mode).
    pub const MOUSE_ENABLE: &[u8] = b"\x1b[?1000h\x1b[?1006h";

    /// Disable mouse tracking.
    pub const MOUSE_DISABLE: &[u8] = b"\x1b[?1000l\x1b[?1006l";

    /// Enable mouse motion tracking.
    pub const MOUSE_MOTION_ENABLE: &[u8] = b"\x1b[?1003h";

    /// Reset all attributes.
    pub const SGR_RESET: &[u8] = b"\x1b[0m";

    /// Bold on.
    pub const SGR_BOLD: &[u8] = b"\x1b[1m";

    /// Italic on.
    pub const SGR_ITALIC: &[u8] = b"\x1b[3m";

    /// Underline on.
    pub const SGR_UNDERLINE: &[u8] = b"\x1b[4m";

    /// Red foreground.
    pub const SGR_FG_RED: &[u8] = b"\x1b[31m";

    /// Green foreground.
    pub const SGR_FG_GREEN: &[u8] = b"\x1b[32m";

    /// Blue foreground.
    pub const SGR_FG_BLUE: &[u8] = b"\x1b[34m";

    /// Red background.
    pub const SGR_BG_RED: &[u8] = b"\x1b[41m";

    /// Synchronized update begin.
    pub const SYNC_BEGIN: &[u8] = b"\x1b[?2026h";

    /// Synchronized update end.
    pub const SYNC_END: &[u8] = b"\x1b[?2026l";

    /// Build a cursor position sequence.
    pub fn cursor_position(row: u16, col: u16) -> Vec<u8> {
        format!("\x1b[{};{}H", row, col).into_bytes()
    }

    /// Build a 24-bit foreground color sequence.
    pub fn fg_rgb(r: u8, g: u8, b: u8) -> Vec<u8> {
        format!("\x1b[38;2;{};{};{}m", r, g, b).into_bytes()
    }

    /// Build a 24-bit background color sequence.
    pub fn bg_rgb(r: u8, g: u8, b: u8) -> Vec<u8> {
        format!("\x1b[48;2;{};{};{}m", r, g, b).into_bytes()
    }

    /// Build a 256-color foreground sequence.
    pub fn fg_256(color: u8) -> Vec<u8> {
        format!("\x1b[38;5;{}m", color).into_bytes()
    }

    /// Build a 256-color background sequence.
    pub fn bg_256(color: u8) -> Vec<u8> {
        format!("\x1b[48;5;{}m", color).into_bytes()
    }

    /// Build an SGR mouse event sequence.
    pub fn mouse_sgr(button: u8, x: u16, y: u16, press: bool) -> Vec<u8> {
        let suffix = if press { 'M' } else { 'm' };
        format!("\x1b[<{};{};{}{}", button, x + 1, y + 1, suffix).into_bytes()
    }
}

/// Sample text content for testing.
pub mod sample_text {
    /// Short ASCII text.
    pub const SHORT_ASCII: &str = "Hello, World!";

    /// Longer ASCII text for wrapping tests.
    pub const LONG_ASCII: &str =
        "The quick brown fox jumps over the lazy dog. Pack my box with five dozen liquor jugs.";

    /// Multi-line text.
    pub const MULTILINE: &str = "Line 1\nLine 2\nLine 3\nLine 4\nLine 5";

    /// Text with tabs.
    pub const WITH_TABS: &str = "Col1\tCol2\tCol3\nA\tB\tC";

    /// Unicode text with various scripts.
    pub const UNICODE_MIXED: &str = "Hello 世界 مرحبا Привет 🌍";

    /// CJK text (full-width characters).
    pub const CJK: &str = "日本語テキスト";

    /// Emoji text.
    pub const EMOJI_BASIC: &str = "😀 🎉 🚀 ❤️ 🔥";

    /// ZWJ emoji sequences.
    pub const EMOJI_ZWJ: &str = "👨‍👩‍👧‍👦 👩‍💻 🏳️‍🌈";

    /// Text with combining characters.
    pub const COMBINING: &str = "café naïve résumé";

    /// RTL text (Arabic).
    pub const RTL_ARABIC: &str = "مرحبا بالعالم";

    /// Code snippet for syntax highlighting tests.
    pub const RUST_CODE: &str = r#"fn main() {
    let x = 42;
    println!("Hello, {}!", x);
}"#;

    /// JSON sample.
    pub const JSON_SAMPLE: &str = r#"{"name": "test", "value": 123, "active": true}"#;

    /// Markdown sample.
    pub const MARKDOWN_SAMPLE: &str =
        "# Heading\n\n**Bold** and *italic* text.\n\n- List item 1\n- List item 2";
}

/// Common color palettes for testing.
pub mod colors {
    use super::*;

    /// Basic ANSI colors.
    pub const ANSI_BLACK: Rgba = Rgba::rgb(0.0, 0.0, 0.0);
    pub const ANSI_RED: Rgba = Rgba::rgb(0.8, 0.0, 0.0);
    pub const ANSI_GREEN: Rgba = Rgba::rgb(0.0, 0.8, 0.0);
    pub const ANSI_YELLOW: Rgba = Rgba::rgb(0.8, 0.8, 0.0);
    pub const ANSI_BLUE: Rgba = Rgba::rgb(0.0, 0.0, 0.8);
    pub const ANSI_MAGENTA: Rgba = Rgba::rgb(0.8, 0.0, 0.8);
    pub const ANSI_CYAN: Rgba = Rgba::rgb(0.0, 0.8, 0.8);
    pub const ANSI_WHITE: Rgba = Rgba::rgb(0.8, 0.8, 0.8);

    /// Semi-transparent colors for blending tests.
    pub const SEMI_RED: Rgba = Rgba::new(1.0, 0.0, 0.0, 0.5);
    pub const SEMI_GREEN: Rgba = Rgba::new(0.0, 1.0, 0.0, 0.5);
    pub const SEMI_BLUE: Rgba = Rgba::new(0.0, 0.0, 1.0, 0.5);

    /// Generate a grayscale color.
    pub fn grayscale(level: f32) -> Rgba {
        Rgba::rgb(level, level, level)
    }

    /// Generate a color from HSV.
    pub fn from_hsv(h: f32, s: f32, v: f32) -> Rgba {
        Rgba::from_hsv(h, s, v)
    }

    /// Generate a random-ish color from a seed (deterministic).
    pub fn from_seed(seed: u32) -> Rgba {
        let r = (seed.wrapping_mul(1_103_515_245).wrapping_add(12345) % 256) as f32 / 255.0;
        let g = (seed.wrapping_mul(1_103_515_245).wrapping_add(12346) % 256) as f32 / 255.0;
        let b = (seed.wrapping_mul(1_103_515_245).wrapping_add(12347) % 256) as f32 / 255.0;
        Rgba::rgb(r, g, b)
    }
}

/// Buffer generators for common test scenarios.
pub mod buffers {
    use super::*;

    /// Create an empty buffer with default cells.
    pub fn empty(width: u32, height: u32) -> OptimizedBuffer {
        OptimizedBuffer::new(width, height)
    }

    /// Create a buffer cleared to a specific background color.
    pub fn cleared(width: u32, height: u32, bg: Rgba) -> OptimizedBuffer {
        let mut buffer = OptimizedBuffer::new(width, height);
        buffer.clear(bg);
        buffer
    }

    /// Create a buffer with a checkerboard pattern.
    pub fn checkerboard(width: u32, height: u32, c1: char, c2: char) -> OptimizedBuffer {
        let mut buffer = OptimizedBuffer::new(width, height);
        for y in 0..height {
            for x in 0..width {
                let c = if (x + y) % 2 == 0 { c1 } else { c2 };
                buffer.set(x, y, Cell::new(c, Style::default()));
            }
        }
        buffer
    }

    /// Create a buffer with a gradient (for color testing).
    pub fn color_gradient(width: u32, height: u32) -> OptimizedBuffer {
        let mut buffer = OptimizedBuffer::new(width, height);
        for y in 0..height {
            for x in 0..width {
                let r = x as f32 / width as f32;
                let g = y as f32 / height as f32;
                let b = 0.5;
                let color = Rgba::rgb(r, g, b);
                buffer.set(x, y, Cell::new(' ', Style::bg(color)));
            }
        }
        buffer
    }

    /// Create a buffer with text at specified positions.
    pub fn with_text(width: u32, height: u32, texts: &[((u32, u32), &str)]) -> OptimizedBuffer {
        let mut buffer = OptimizedBuffer::new(width, height);
        for ((x, y), text) in texts {
            for (i, c) in text.chars().enumerate() {
                let cell_x = x + i as u32;
                if cell_x < width {
                    buffer.set(cell_x, *y, Cell::new(c, Style::default()));
                }
            }
        }
        buffer
    }

    /// Create a buffer with a box drawn in it.
    pub fn with_box(
        width: u32,
        height: u32,
        box_x: u32,
        box_y: u32,
        box_w: u32,
        box_h: u32,
    ) -> OptimizedBuffer {
        let mut buffer = OptimizedBuffer::new(width, height);

        // Top edge
        for x in box_x..box_x + box_w {
            buffer.set(x, box_y, Cell::new('', Style::default()));
        }
        // Bottom edge
        for x in box_x..box_x + box_w {
            buffer.set(x, box_y + box_h - 1, Cell::new('', Style::default()));
        }
        // Left edge
        for y in box_y..box_y + box_h {
            buffer.set(box_x, y, Cell::new('', Style::default()));
        }
        // Right edge
        for y in box_y..box_y + box_h {
            buffer.set(box_x + box_w - 1, y, Cell::new('', Style::default()));
        }
        // Corners
        buffer.set(box_x, box_y, Cell::new('', Style::default()));
        buffer.set(box_x + box_w - 1, box_y, Cell::new('', Style::default()));
        buffer.set(box_x, box_y + box_h - 1, Cell::new('', Style::default()));
        buffer.set(
            box_x + box_w - 1,
            box_y + box_h - 1,
            Cell::new('', Style::default()),
        );

        buffer
    }

    /// Create a buffer filled with styled cells.
    pub fn styled_fill(width: u32, height: u32, c: char, style: Style) -> OptimizedBuffer {
        let mut buffer = OptimizedBuffer::new(width, height);
        for y in 0..height {
            for x in 0..width {
                buffer.set(x, y, Cell::new(c, style));
            }
        }
        buffer
    }

    /// Standard terminal sizes for testing.
    pub fn standard_80x24() -> OptimizedBuffer {
        OptimizedBuffer::new(80, 24)
    }

    pub fn standard_132x43() -> OptimizedBuffer {
        OptimizedBuffer::new(132, 43)
    }

    pub fn minimal_40x12() -> OptimizedBuffer {
        OptimizedBuffer::new(40, 12)
    }

    pub fn large_200x60() -> OptimizedBuffer {
        OptimizedBuffer::new(200, 60)
    }
}

/// Style generators for testing.
pub mod styles {
    use super::*;

    /// Default style.
    pub fn default() -> Style {
        Style::default()
    }

    /// Bold style.
    pub fn bold() -> Style {
        Style::default().with_attributes(TextAttributes::BOLD)
    }

    /// Italic style.
    pub fn italic() -> Style {
        Style::default().with_attributes(TextAttributes::ITALIC)
    }

    /// Underline style.
    pub fn underline() -> Style {
        Style::default().with_attributes(TextAttributes::UNDERLINE)
    }

    /// Combined bold + italic.
    pub fn bold_italic() -> Style {
        Style::default().with_attributes(TextAttributes::BOLD | TextAttributes::ITALIC)
    }

    /// Style with foreground color.
    pub fn fg(color: Rgba) -> Style {
        Style::fg(color)
    }

    /// Style with background color.
    pub fn bg(color: Rgba) -> Style {
        Style::bg(color)
    }

    /// Style with both foreground and background.
    pub fn fg_bg(fg: Rgba, bg: Rgba) -> Style {
        Style::builder().fg(fg).bg(bg).build()
    }

    /// Error style (red foreground).
    pub fn error() -> Style {
        Style::fg(Rgba::RED).with_attributes(TextAttributes::BOLD)
    }

    /// Success style (green foreground).
    pub fn success() -> Style {
        Style::fg(Rgba::GREEN)
    }

    /// Warning style (yellow foreground).
    pub fn warning() -> Style {
        Style::fg(Rgba::new(1.0, 0.8, 0.0, 1.0))
    }
}

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

    #[test]
    fn test_ansi_cursor_position() {
        let seq = ansi_sequences::cursor_position(5, 10);
        assert_eq!(seq, b"\x1b[5;10H");
    }

    #[test]
    fn test_ansi_fg_rgb() {
        let seq = ansi_sequences::fg_rgb(255, 128, 64);
        assert_eq!(seq, b"\x1b[38;2;255;128;64m");
    }

    #[test]
    fn test_ansi_mouse_sgr() {
        let seq = ansi_sequences::mouse_sgr(0, 10, 20, true);
        assert_eq!(seq, b"\x1b[<0;11;21M");
    }

    #[test]
    fn test_buffer_checkerboard() {
        let buffer = buffers::checkerboard(4, 4, 'X', 'O');
        assert_eq!(buffer.width(), 4);
        assert_eq!(buffer.height(), 4);
    }

    #[test]
    fn test_buffer_with_text() {
        let buffer = buffers::with_text(20, 5, &[((0, 0), "Hello"), ((0, 1), "World")]);
        assert_eq!(buffer.width(), 20);
    }

    #[test]
    fn test_color_from_seed_deterministic() {
        let c1 = colors::from_seed(42);
        let c2 = colors::from_seed(42);
        assert_eq!(c1.r, c2.r);
        assert_eq!(c1.g, c2.g);
        assert_eq!(c1.b, c2.b);
    }

    #[test]
    fn test_style_combinations() {
        let s = styles::bold_italic();
        assert!(s.attributes.contains(TextAttributes::BOLD));
        assert!(s.attributes.contains(TextAttributes::ITALIC));
    }
}