r3bl_tui 0.7.2

TUI library to build modern apps inspired by React, Elm, with Flexbox, CSS, editor component, emoji support, and more
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
/*
 *   Copyright (c) 2025 R3BL LLC
 *   All rights reserved.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */

use crate::{col, row, ColIndex, ColWidth, EditorBuffer, RowHeight, RowIndex};

#[derive(Clone, Eq, PartialEq, Debug)]
pub enum CaretColLocationInLine {
    /// Also covers state where there is no col, or only 1 col.
    AtStart,
    AtEnd,
    InMiddle,
}

#[derive(Clone, Eq, PartialEq, Debug)]
pub enum CaretRowLocationInBuffer {
    /// Also covers state where there is no row, or only 1 row.
    AtTop,
    AtBottom,
    InMiddle,
}

/// Locate the col.
#[must_use]
pub fn locate_col(editor_buffer: &EditorBuffer) -> CaretColLocationInLine {
    if col_is_at_start_of_line(editor_buffer) {
        CaretColLocationInLine::AtStart
    } else if col_is_at_end_of_line(editor_buffer) {
        CaretColLocationInLine::AtEnd
    } else {
        CaretColLocationInLine::InMiddle
    }
}

fn col_is_at_start_of_line(buffer: &EditorBuffer) -> bool {
    if buffer.line_at_caret_scr_adj().is_some() {
        buffer.get_caret_scr_adj().col_index == col(0)
    } else {
        false
    }
}

fn col_is_at_end_of_line(buffer: &EditorBuffer) -> bool {
    if buffer.line_at_caret_scr_adj().is_some() {
        let line_display_width = buffer.get_line_display_width_at_caret_scr_adj();
        buffer.get_caret_scr_adj().col_index
            == caret_scroll_index::col_index_for_width(line_display_width)
    } else {
        false
    }
}

/// Locate the row.
#[must_use]
pub fn locate_row(buffer: &EditorBuffer) -> CaretRowLocationInBuffer {
    if row_is_at_top_of_buffer(buffer) {
        CaretRowLocationInBuffer::AtTop
    } else if row_is_at_bottom_of_buffer(buffer) {
        CaretRowLocationInBuffer::AtBottom
    } else {
        CaretRowLocationInBuffer::InMiddle
    }
}

/// ```text
/// R ┌──────────┐
/// 0 ❱          │
///   └⮬─────────┘
///   C0123456789
/// ```
fn row_is_at_top_of_buffer(buffer: &EditorBuffer) -> bool {
    buffer.get_caret_scr_adj().row_index == row(0)
}

/// ```text
/// R ┌──────────┐
/// 0 │a         │
/// 1 ❱a         │
///   └⮬─────────┘
///   C0123456789
/// ```
fn row_is_at_bottom_of_buffer(buffer: &EditorBuffer) -> bool {
    if buffer.is_empty() || buffer.get_lines().len() == 1 {
        // If there is only one line, then the caret is not at the bottom, its at the top.
        false
    } else {
        /* lines.len() - 1 is the last row index */
        let max_row_index = buffer.get_max_row_index();
        buffer.get_caret_scr_adj().row_index == max_row_index
    }
}

pub mod caret_scroll_index {
    use super::{col, row, ColIndex, ColWidth, RowHeight, RowIndex};

    /// This is the same number as the given width, just in different "unit". The caret
    /// max index which is the scroll index goes 1 past the end of the given width's
    /// index.
    ///
    /// Equivalent to:
    /// ```text
    /// col_amt_index = col_amt - 1;
    /// scroll_past_col_amt_index = col_amt_index + 1;
    /// ```
    ///
    /// Here's an example:
    /// ```text
    /// R ┌──────────┐
    /// 0 ▸hello░    │
    ///   └─────▴────┘
    ///   C0123456789
    /// ```
    #[must_use]
    pub fn col_index_for_width(col_amt: ColWidth) -> ColIndex {
        col_amt.convert_to_col_index() /* -1 */ + col(1) /* +1 */
    }

    /// This is the same number as the given height, just in different "unit". The caret
    /// max index which is the scroll index goes 1 past the end of the given height's
    /// index.
    #[must_use]
    pub fn row_index_for_height(row_amt: RowHeight) -> RowIndex {
        row_amt.convert_to_row_index() /* -1 */ + row(1) /* +1 */
    }

    #[test]
    fn test_scroll_col_index_for_width() {
        use crate::width;

        let width = width(5);
        let scroll_col_index = col_index_for_width(width);
        assert_eq!(*scroll_col_index, *width);
    }

    #[test]
    fn test_scroll_row_index_for_height() {
        use crate::height;

        let height = height(5);
        let scroll_row_index = row_index_for_height(height);
        assert_eq!(*scroll_row_index, *height);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{assert_eq2, EditorEngine, EditorEngineConfig};

    #[test]
    fn test_locate_col_at_start() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Hello World"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Set caret at start of line
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        
        let location = locate_col(&buffer);
        assert_eq2!(location, CaretColLocationInLine::AtStart);
    }
    
    #[test]
    fn test_locate_col_at_end() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Hello World"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Set caret at end of line (display width 11, so caret index is also 11)
        let line_width = buffer.get_lines()[0].display_width;
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = caret_scroll_index::col_index_for_width(line_width);
        }
        
        let location = locate_col(&buffer);
        assert_eq2!(location, CaretColLocationInLine::AtEnd);
    }
    
    #[test]
    fn test_locate_col_in_middle() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Hello World"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Set caret in middle of line
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(5);
        }
        
        let location = locate_col(&buffer);
        assert_eq2!(location, CaretColLocationInLine::InMiddle);
    }
    
    #[test]
    fn test_locate_col_empty_line() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec![""]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // On empty line, caret is both at start and end
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        
        let location = locate_col(&buffer);
        // Empty line: col 0 is both start and end, implementation treats this as AtStart
        assert_eq2!(location, CaretColLocationInLine::AtStart);
    }
    
    #[test]
    fn test_locate_col_with_unicode() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Hello 😄 World"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Test at emoji position
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(6); // Right before emoji
        }
        let location = locate_col(&buffer);
        assert_eq2!(location, CaretColLocationInLine::InMiddle);
        
        // Test at end with Unicode
        let line_width = buffer.get_lines()[0].display_width;
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = caret_scroll_index::col_index_for_width(line_width);
        }
        let location = locate_col(&buffer);
        assert_eq2!(location, CaretColLocationInLine::AtEnd);
    }
    
    #[test]
    fn test_locate_row_at_top() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Line 1", "Line 2", "Line 3"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Set caret at first row
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        
        let location = locate_row(&buffer);
        assert_eq2!(location, CaretRowLocationInBuffer::AtTop);
    }
    
    #[test]
    fn test_locate_row_at_bottom() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Line 1", "Line 2", "Line 3"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Set caret at last row
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(2);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        
        let location = locate_row(&buffer);
        assert_eq2!(location, CaretRowLocationInBuffer::AtBottom);
    }
    
    #[test]
    fn test_locate_row_in_middle() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Line 1", "Line 2", "Line 3"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Set caret at middle row
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(1);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        
        let location = locate_row(&buffer);
        assert_eq2!(location, CaretRowLocationInBuffer::InMiddle);
    }
    
    #[test]
    fn test_locate_row_single_line() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Only line"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // With only one line, caret is at top (not bottom)
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        
        let location = locate_row(&buffer);
        assert_eq2!(location, CaretRowLocationInBuffer::AtTop);
    }
    
    #[test]
    fn test_locate_row_empty_buffer() {
        let buffer = EditorBuffer::new_empty(None, None);
        // Empty buffer
        
        let location = locate_row(&buffer);
        assert_eq2!(location, CaretRowLocationInBuffer::AtTop);
    }
    
    #[test]
    fn test_col_is_at_start_of_line() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Test line"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Test at start
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        let location = locate_col(&buffer);
        assert_eq2!(location, CaretColLocationInLine::AtStart);
        
        // Test not at start
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(5);
        }
        let location = locate_col(&buffer);
        assert_eq2!(location, CaretColLocationInLine::InMiddle);
    }
    
    #[test]
    fn test_col_is_at_end_of_line() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Test"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Test at end (display width 4, caret index is also 4)
        let line_width = buffer.get_lines()[0].display_width;
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = caret_scroll_index::col_index_for_width(line_width);
        }
        let location = locate_col(&buffer);
        assert_eq2!(location, CaretColLocationInLine::AtEnd);
        
        // Test not at end
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(2);
        }
        let location = locate_col(&buffer);
        assert_eq2!(location, CaretColLocationInLine::InMiddle);
    }
    
    #[test]
    fn test_row_is_at_top_of_buffer() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Line 1", "Line 2", "Line 3"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Test at top
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        let location = locate_row(&buffer);
        assert_eq2!(location, CaretRowLocationInBuffer::AtTop);
        
        // Test not at top (with 2 lines, row 1 is the bottom)
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(1);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        let location = locate_row(&buffer);
        assert_eq2!(location, CaretRowLocationInBuffer::InMiddle);
    }
    
    #[test]
    fn test_row_is_at_bottom_of_buffer() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Line 1", "Line 2", "Line 3"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Test at bottom
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(2);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        let location = locate_row(&buffer);
        assert_eq2!(location, CaretRowLocationInBuffer::AtBottom);
        
        // Test not at bottom
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(1);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        let location = locate_row(&buffer);
        assert_eq2!(location, CaretRowLocationInBuffer::InMiddle);
        
        // Test single line (should return false)
        let mut single_line_buffer = EditorBuffer::new_empty(None, None);
        single_line_buffer.init_with(vec!["Only line"]);
        {
            let buffer_mut = single_line_buffer.get_mut(engine.viewport());
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        let location = locate_row(&single_line_buffer);
        assert_eq2!(location, CaretRowLocationInBuffer::AtTop); // Single line is at top, not bottom
    }
    
    #[test]
    fn test_locate_functions_with_scroll_offset() {
        let mut buffer = EditorBuffer::new_empty(None, None);
        buffer.init_with(vec!["Very long line with many characters"]);
        let engine = EditorEngine::new(EditorEngineConfig::default());
        
        // Set scroll offset and caret
        {
            let buffer_mut = buffer.get_mut(engine.viewport());
            buffer_mut.inner.scr_ofs.row_index = row(0);
            buffer_mut.inner.scr_ofs.col_index = col(5);
            buffer_mut.inner.caret_raw.row_index = row(0);
            buffer_mut.inner.caret_raw.col_index = col(0);
        }
        
        // The caret is at the start of the visible area, but not the start of the line
        let location = locate_col(&buffer);
        // Scroll adjusted position is col 5, which is in the middle of the line
        assert_eq2!(location, CaretColLocationInLine::InMiddle);
    }
}