tui-lipan 0.2.0

Opinionated, component-based TUI framework for Rust - declarative components, reconciliation, layout engine, focus, overlays, and rich widgets on top of ratatui.
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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
use std::cell::RefCell;

use ratatui::text::Line;
use ratatui::widgets::{Block, Borders, Clear, Paragraph};
use unicode_segmentation::UnicodeSegmentation;
use unicode_width::UnicodeWidthStr;

use crate::app::copy_feedback::CopyFeedbackRange;
use crate::backend::ratatui_backend::common::{
    CursorPlacement, DEFAULT_SCROLLBAR_THUMB, IntegratedScrollbarAppearance, ScrollbarAppearance,
    ScrollbarScrollState, calculate_visible_borders, integrated_vscrollbar_track_char,
    is_cursor_visible, render_integrated_scrollbar, render_vscrollbar,
    resolve_scrollbar_thumb_style, style_paints_bg, to_ratatui_border_set, to_ratatui_border_type,
    to_ratatui_rect, to_ratatui_span, to_ratatui_style,
};
use crate::backend::ratatui_backend::render::{
    FrameIntegratedVTrack, RenderState, ancestor_frame_integrated_vtrack,
    apply_copy_feedback_to_selection_style,
};
use crate::core::node::NodeId;
use crate::style::resolve::{
    resolve_base_style, resolve_focus_style_defaults, resolve_scrollbar_theme,
};
use crate::style::{Rect, ScrollbarVariant, Span, Style, Theme, ThemeRole, resolve_slot};
use crate::utils::GridSelection;
use crate::utils::scrollbar::ScrollbarMetricsCache;
#[cfg(feature = "terminal-images")]
use crate::widgets::TerminalImageCrop;
use crate::widgets::internal::terminal_content_layout;

fn clip_spans_no_ellipsis<'a>(
    spans: Vec<ratatui::text::Span<'a>>,
    max_width: u16,
) -> Vec<ratatui::text::Span<'a>> {
    if max_width == 0 {
        return Vec::new();
    }

    let mut out = Vec::new();
    let mut used = 0u16;

    for span in spans {
        if used >= max_width {
            break;
        }

        let mut chunk = String::new();
        for grapheme in span.content.graphemes(true) {
            let w = if grapheme.chars().all(char::is_control) {
                0
            } else {
                UnicodeWidthStr::width(grapheme).min(u16::MAX as usize) as u16
            };
            if w == 0 {
                chunk.push_str(grapheme);
                continue;
            }
            if used.saturating_add(w) > max_width {
                break;
            }
            chunk.push_str(grapheme);
            used = used.saturating_add(w);
        }

        if !chunk.is_empty() {
            out.push(ratatui::text::Span::styled(chunk, span.style));
        }
    }

    out
}

/// Paint the child program's images over the text that was just drawn.
///
/// A placement carries the rect it would occupy in full, even when most of that rect is off the
/// pane, so the pixels are cropped to what is actually visible rather than squashed into it. That
/// is what makes an image scroll: each row that leaves the content rect takes a strip of source
/// pixels with it, and the rest keeps its scale.
#[cfg(feature = "terminal-images")]
fn render_terminal_images(
    f: &mut ratatui::Frame<'_>,
    node: &crate::widgets::internal::TerminalNode,
    content_rect: Rect,
    effective: ratatui::layout::Rect,
) {
    use std::sync::Arc;

    use crate::backend::ratatui_backend::renderers::image::draw_encoded_image;

    let clip_left = i32::from(effective.x);
    let clip_top = i32::from(effective.y);
    let clip_right = clip_left + i32::from(effective.width);
    let clip_bottom = clip_top + i32::from(effective.height);

    for placement in node.images.iter() {
        let left = i32::from(content_rect.x) + placement.col;
        let top = i32::from(content_rect.y) + placement.row;
        let cols = i32::from(placement.cols);
        let rows = i32::from(placement.rows);
        if cols <= 0 || rows <= 0 {
            continue;
        }

        let vis_left = left.max(clip_left);
        let vis_top = top.max(clip_top);
        let vis_right = (left + cols).min(clip_right);
        let vis_bottom = (top + rows).min(clip_bottom);
        if vis_right <= vis_left || vis_bottom <= vis_top {
            continue;
        }

        let pixels = placement.image.pixels();
        let source = placement.source_crop.unwrap_or(TerminalImageCrop {
            x: 0,
            y: 0,
            width: pixels.width(),
            height: pixels.height(),
        });
        let Some(crop) = crop_for_visible_cells(
            source,
            (cols as u32, rows as u32),
            (
                (vis_left - left) as u32,
                (vis_top - top) as u32,
                (vis_right - vis_left) as u32,
                (vis_bottom - vis_top) as u32,
            ),
        ) else {
            continue;
        };

        let whole = crop.x == 0
            && crop.y == 0
            && crop.width == pixels.width()
            && crop.height == pixels.height();
        let area = ratatui::layout::Rect {
            x: vis_left as u16,
            y: vis_top as u16,
            width: (vis_right - vis_left) as u16,
            height: (vis_bottom - vis_top) as u16,
        };

        draw_encoded_image(
            f,
            area,
            placement_source_hash(placement.image_id, placement.image.source_hash(), crop),
            || {
                if whole {
                    Arc::clone(pixels)
                } else {
                    Arc::new(pixels.crop_imm(crop.x, crop.y, crop.width, crop.height))
                }
            },
        );
    }
}

/// Map the visible cells of a placement back onto the source pixels they show.
#[cfg(feature = "terminal-images")]
fn crop_for_visible_cells(
    source: TerminalImageCrop,
    placement_cells: (u32, u32),
    visible_cells: (u32, u32, u32, u32),
) -> Option<TerminalImageCrop> {
    let (cols, rows) = placement_cells;
    let (skip_x, skip_y, keep_w, keep_h) = visible_cells;
    if source.width == 0 || source.height == 0 || cols == 0 || rows == 0 {
        return None;
    }

    let x = source.x + skip_x * source.width / cols;
    let y = source.y + skip_y * source.height / rows;
    // Round up, so a partly covered cell still gets pixels rather than a seam.
    let width = (keep_w * source.width)
        .div_ceil(cols)
        .min(source.x + source.width - x);
    let height = (keep_h * source.height)
        .div_ceil(rows)
        .min(source.y + source.height - y);

    (width > 0 && height > 0).then_some(TerminalImageCrop {
        x,
        y,
        width,
        height,
    })
}

/// The identity an encoded placement caches under: which image, which part of it, whose placement.
///
/// The crop is in there so two crops of one image cache separately. The *image id* is in there for
/// a subtler reason: a host drawing through Kitty identifies a placement by the id of its encoding,
/// so two placements sharing one encoding are one placement to it. Keying on the pixels alone would
/// hand two copies of the same picture a single id, and the host would draw one of them and drop
/// the other - which looks exactly like images vanishing as new ones arrive.
#[cfg(feature = "terminal-images")]
fn placement_source_hash(image_id: u32, source_hash: u64, crop: TerminalImageCrop) -> u64 {
    use std::hash::{Hash as _, Hasher as _};

    let mut hasher = std::collections::hash_map::DefaultHasher::new();
    image_id.hash(&mut hasher);
    source_hash.hash(&mut hasher);
    (crop.x, crop.y, crop.width, crop.height).hash(&mut hasher);
    hasher.finish()
}

pub(crate) struct TerminalRenderCtx<'a> {
    pub is_focused: bool,
    pub is_hovered: bool,
    pub blink_visible: bool,
    pub clip_rect: Option<Rect>,
    pub caret: CursorPlacement<'a>,
    pub parent_integrated_v: Option<FrameIntegratedVTrack>,
    pub metrics_cache: Option<&'a RefCell<ScrollbarMetricsCache>>,
    pub node_theme: &'a Theme,
    pub selection_style: Style,
    /// Range painted with `selection_style` for a copy flash that carries its own
    /// range, so the flash stays visible after the node's selection is cleared.
    pub flash_range: Option<CopyFeedbackRange>,
}

pub(crate) fn render_terminal(
    f: &mut ratatui::Frame<'_>,
    node: &crate::widgets::internal::TerminalNode,
    rect: Rect,
    rrect: ratatui::layout::Rect,
    ctx: TerminalRenderCtx<'_>,
) {
    let TerminalRenderCtx {
        is_focused,
        is_hovered,
        blink_visible,
        clip_rect,
        caret,
        parent_integrated_v,
        metrics_cache,
        node_theme,
        selection_style,
        flash_range,
    } = ctx;
    let theme = node_theme;
    let style = resolve_base_style(theme, node.style);
    let hover_style = resolve_slot(theme, ThemeRole::Hover, &node.hover_style);
    let focus_style = resolve_slot(theme, ThemeRole::Focus, &node.focus_style);
    let focus_content_style =
        resolve_focus_style_defaults(theme, node.focus_content_style, theme.terminal.focus);

    let mut content_style = style;
    let mut chrome_style = style;
    if is_hovered {
        content_style = content_style.patch(hover_style);
        chrome_style = chrome_style.patch(hover_style);
    }
    if is_focused {
        chrome_style = chrome_style.patch(focus_style);
        content_style = content_style.patch(focus_content_style);
    }

    let mut inner = rect;
    if node.border {
        let borders = calculate_visible_borders(rect, clip_rect);
        let mut block = Block::default()
            .borders(borders)
            .border_type(to_ratatui_border_type(node.border_style))
            .style(to_ratatui_style(chrome_style));
        if let Some(set) = to_ratatui_border_set(node.border_style) {
            block = block.border_set(set);
        }

        if style_paints_bg(chrome_style) {
            f.render_widget(Clear, rrect);
        }
        f.render_widget(block, rrect);

        if borders.contains(Borders::LEFT) {
            inner.x = inner.x.saturating_add(1);
            inner.w = inner.w.saturating_sub(1);
        }
        if borders.contains(Borders::RIGHT) {
            inner.w = inner.w.saturating_sub(1);
        }
        if borders.contains(Borders::TOP) {
            inner.y = inner.y.saturating_add(1);
            inner.h = inner.h.saturating_sub(1);
        }
        if borders.contains(Borders::BOTTOM) {
            inner.h = inner.h.saturating_sub(1);
        }
    } else if style_paints_bg(chrome_style) {
        f.render_widget(Clear, rrect);
        f.render_widget(
            Block::default().style(to_ratatui_style(chrome_style)),
            rrect,
        );
    }

    inner = inner.inset(node.padding);
    if inner.w == 0 || inner.h == 0 {
        return;
    }

    let layout = terminal_content_layout(
        inner,
        node.border,
        node.scrollbar,
        node.scrollbar_variant,
        node.scrollbar_gap,
        node.total_scrollback_rows,
        parent_integrated_v.is_some(),
    );
    let content_rect = layout.content_rect;
    let scrollbar = layout.scrollbar_visible;
    let use_integrated = layout.scrollbar_integrated;
    let content_w = content_rect.w;

    let content_rrect = to_ratatui_rect(content_rect);
    let effective = content_rrect.intersection(rrect);

    let projected = node.selection.as_ref().and_then(|sel| {
        crate::widgets::to_viewport(
            sel,
            node.scrollback_offset,
            node.total_scrollback_rows,
            content_rect.h as usize,
        )
    });

    let mut rendered_content = false;
    if content_rect.w > 0 && content_rect.h > 0 && effective.width > 0 && effective.height > 0 {
        let dx = (effective.x as i32)
            .saturating_sub(content_rect.x as i32)
            .max(0) as u16;
        let dy = (effective.y as i32)
            .saturating_sub(content_rect.y as i32)
            .max(0) as u16;

        // An explicit flash range wins: the caller copied that range and has already
        // dropped its selection, so there is nothing else left to draw the flash on.
        let flash_selection = flash_range.as_ref().map(|range| range.selection.clone());
        let paint_selection = match &flash_selection {
            Some(_) => &flash_selection,
            None => &projected,
        };
        let lines: Vec<Line<'_>> = (0..content_rect.h as usize)
            .map(|row| {
                let source = match flash_range.as_ref() {
                    Some(range) if range.selection.columns_for_row(row, 0).is_some() => {
                        range.line(row)
                    }
                    _ => node.lines.get(row).map(Vec::as_slice),
                };
                let mut spans: Vec<ratatui::text::Span<'_>> = source
                    .map(|line| {
                        apply_selection_to_row(
                            line,
                            row,
                            paint_selection,
                            selection_style,
                            content_style,
                        )
                    })
                    .unwrap_or_default();

                if spans.is_empty() {
                    spans.push(ratatui::text::Span::styled(
                        "",
                        to_ratatui_style(content_style),
                    ));
                }

                Line::from(clip_spans_no_ellipsis(spans, content_w))
            })
            .collect();

        f.render_widget(Paragraph::new(lines).scroll((dy, dx)), effective);
        rendered_content = true;

        #[cfg(feature = "terminal-images")]
        render_terminal_images(f, node, content_rect, effective);
    }

    // Render vertical scrollbar when scrollback is available.
    if scrollbar {
        let total = node.viewport_rows + node.total_scrollback_rows;
        let visible = node.viewport_rows;

        // Map vt100 scrollback (0 = bottom) to standard offset (0 = top).
        let std_offset = node
            .total_scrollback_rows
            .saturating_sub(node.scrollback_offset);

        let thumb = node.scrollbar_thumb.unwrap_or(DEFAULT_SCROLLBAR_THUMB);
        let (thumb_style, thumb_focus_style, track_style) = resolve_scrollbar_theme(
            theme,
            node.scrollbar_thumb_style,
            node.scrollbar_thumb_focus_style,
            node.scrollbar_track_style,
        );
        let mut thumb_style =
            resolve_scrollbar_thumb_style(is_focused, thumb_style, thumb_focus_style);
        if !is_focused && node.scrollbar_thumb_style.is_none() {
            thumb_style = thumb_style.dim();
        }

        if use_integrated {
            let sb_x = parent_integrated_v
                .map(|p| p.track_x)
                .unwrap_or_else(|| rect.x.saturating_add(rect.w.saturating_sub(1) as i16));
            let sb_rect = Rect {
                x: sb_x,
                y: inner.y,
                w: 1,
                h: inner.h,
            };

            let b_style = parent_integrated_v
                .map(|p| p.border_style_fallback)
                .unwrap_or(node.border_style);
            let track_glyph = parent_integrated_v.and_then(|p| p.track_glyph);
            let mut track_scratch = [0u8; 4];
            let border_char =
                integrated_vscrollbar_track_char(track_glyph, b_style, &mut track_scratch);
            let integrated_base_style = parent_integrated_v
                .map(|p| p.track_style)
                .unwrap_or(selection_style);

            render_integrated_scrollbar(
                f,
                sb_rect,
                ScrollbarScrollState {
                    offset: std_offset,
                    visible,
                    total,
                },
                IntegratedScrollbarAppearance {
                    thumb_char: thumb,
                    border_char,
                    base_style: integrated_base_style,
                    thumb_style,
                    track_style,
                    clip_rect: None,
                    metrics_cache,
                },
            );
        } else {
            let sb_rect = Rect {
                x: inner
                    .x
                    .saturating_add(inner.w.saturating_sub(1) as i16)
                    .max(0),
                y: inner.y,
                w: 1,
                h: inner.h,
            };

            let clip_rrect = clip_rect.map(to_ratatui_rect);
            render_vscrollbar(
                f,
                sb_rect,
                ScrollbarScrollState {
                    offset: std_offset,
                    visible,
                    total,
                },
                ScrollbarAppearance {
                    thumb_char: thumb,
                    thumb_style,
                    track_style,
                    clip_rect: clip_rrect,
                    metrics_cache,
                },
            );
        }
    }

    // Use the real hardware cursor (like Input/TextArea) instead of a
    // software REVERSED-cell cursor. TerminalManager applies the child's
    // DECSCUSR shape as a steady hardware cursor; the framework blink timer
    // drives blinking here, but only when the child asked for a blinking
    // cursor. A steady cursor stays lit so the child's request is honored.
    //
    // Only show cursor when at live view (scrollback_offset == 0).
    // Hide cursor when there's an active selection.
    let has_selection = projected.as_ref().is_some_and(|sel| !sel.is_empty());
    let cursor_lit = !node.cursor_blinking || blink_visible;
    if rendered_content
        && is_focused
        && node.cursor_visible
        && cursor_lit
        && node.scrollback_offset == 0
        && !has_selection
    {
        let cursor_x = content_rect.x.saturating_add(node.cursor_col as i16);
        let cursor_y = content_rect.y.saturating_add(node.cursor_row as i16);
        if is_cursor_visible(cursor_x, cursor_y, content_rect, clip_rect) {
            caret.place(
                f,
                ratatui::layout::Position::new(cursor_x as u16, cursor_y as u16),
            );
        }
    }
}

fn apply_selection_to_row<'a>(
    spans: &'a [Span],
    row: usize,
    selection: &'a Option<GridSelection>,
    selection_style: Style,
    base_style: Style,
) -> Vec<ratatui::text::Span<'a>> {
    let Some(sel) = selection else {
        return spans
            .iter()
            .map(|span| to_ratatui_span(span, base_style))
            .collect();
    };

    if sel.is_empty() {
        return spans
            .iter()
            .map(|span| to_ratatui_span(span, base_style))
            .collect();
    }

    let line_width = crate::utils::spans::line_width(spans);

    let Some((col_start, col_end)) = sel.columns_for_row(row, line_width) else {
        return spans
            .iter()
            .map(|span| to_ratatui_span(span, base_style))
            .collect();
    };

    apply_column_selection_to_spans(spans, col_start, col_end, selection_style, base_style)
}

fn apply_column_selection_to_spans(
    spans: &[Span],
    col_start: usize,
    col_end: usize,
    selection_style: Style,
    base_style: Style,
) -> Vec<ratatui::text::Span<'_>> {
    if col_start >= col_end {
        return spans
            .iter()
            .map(|span| to_ratatui_span(span, base_style))
            .collect();
    }

    let mut out = Vec::new();
    let mut col = 0usize;
    let mut run_text = String::new();
    let mut run_style: Option<ratatui::style::Style> = None;

    for span in spans {
        let span_style = base_style.patch(span.style);
        for grapheme in span.content.graphemes(true) {
            let w = if grapheme.chars().all(char::is_control) {
                0
            } else {
                UnicodeWidthStr::width(grapheme)
            };
            let selected = if w == 0 {
                col >= col_start && col < col_end
            } else {
                let cell_end = col.saturating_add(w);
                col < col_end && cell_end > col_start
            };
            let style = if selected {
                to_ratatui_style(span_style.patch(selection_style))
            } else {
                to_ratatui_style(span_style)
            };

            if run_style == Some(style) {
                run_text.push_str(grapheme);
            } else {
                if let Some(run_style) = run_style.take()
                    && !run_text.is_empty()
                {
                    out.push(ratatui::text::Span::styled(
                        std::mem::take(&mut run_text),
                        run_style,
                    ));
                }
                run_style = Some(style);
                run_text.push_str(grapheme);
            }

            if w > 0 {
                col = col.saturating_add(w);
            }
        }
    }

    if let Some(run_style) = run_style
        && !run_text.is_empty()
    {
        out.push(ratatui::text::Span::styled(run_text, run_style));
    }

    out
}

#[cfg(feature = "terminal")]
pub(crate) fn render_terminal_node(
    state: &mut RenderState<'_, '_, '_>,
    node_id: NodeId,
    node: &crate::core::node::Node,
    term: &crate::widgets::internal::TerminalNode,
    rect: Rect,
    rrect: ratatui::layout::Rect,
    clip_bounds: Option<Rect>,
) {
    let is_focused = Some(node_id) == state.ctx.focused;
    let is_hovered = Some(node_id) == state.ctx.hovered;

    let parent_integrated_v = if !term.border
        && term.scrollbar
        && matches!(term.scrollbar_variant, ScrollbarVariant::Integrated)
    {
        ancestor_frame_integrated_vtrack(state, node.parent)
    } else {
        None
    };

    let scrollbar_cache = &state.ctx.scrollbar_metrics_cache;
    let theme = node.active_theme();
    let selection_style = apply_copy_feedback_to_selection_style(
        state.ctx,
        node_id,
        resolve_slot(theme, ThemeRole::TextSelection, &term.selection_style),
    );
    let flash_range = state
        .ctx
        .copy_feedback
        .and_then(|feedback| feedback.active_range(node_id));
    let f = &mut *state.f;
    render_terminal(
        f,
        term,
        rect,
        rrect,
        TerminalRenderCtx {
            is_focused,
            is_hovered,
            blink_visible: state.ctx.blink_visible,
            clip_rect: clip_bounds,
            caret: CursorPlacement::tracked(state.ctx.cursor_position, state.ctx.tree, node_id),
            parent_integrated_v,
            metrics_cache: Some(scrollbar_cache),
            node_theme: theme,
            selection_style,
            flash_range,
        },
    );
}

pub(crate) fn terminal_cursor_position(
    node: &crate::widgets::internal::TerminalNode,
    rect: Rect,
    clip_rect: Option<Rect>,
    parent_integrated_v_edge: bool,
) -> Option<ratatui::layout::Position> {
    let inner = rect.inner(node.border, node.padding);
    if inner.w == 0 || inner.h == 0 {
        return None;
    }

    let layout = terminal_content_layout(
        inner,
        node.border,
        node.scrollbar,
        node.scrollbar_variant,
        node.scrollbar_gap,
        node.total_scrollback_rows,
        parent_integrated_v_edge,
    );
    let content_rect = layout.content_rect;
    let cursor_x = content_rect.x.saturating_add(node.cursor_col as i16);
    let cursor_y = content_rect.y.saturating_add(node.cursor_row as i16);
    if !is_cursor_visible(cursor_x, cursor_y, content_rect, clip_rect) {
        return None;
    }

    Some(ratatui::layout::Position::new(
        cursor_x as u16,
        cursor_y as u16,
    ))
}

#[cfg(test)]
mod selection_tests {
    use super::*;
    use crate::utils::{GridPos, GridSelection};
    use crate::widgets::terminal_selection_text;

    #[test]
    fn renderer_and_extractor_agree_for_wide_joined_emoji() {
        let lines = vec![vec![Span::new("a👩‍💻b")]];
        let selection = GridSelection {
            anchor: GridPos { row: 0, col: 1 },
            cursor: GridPos { row: 0, col: 3 },
        };
        let selected_style = Style::new().bg(crate::style::Color::Red);
        let rendered =
            apply_column_selection_to_spans(&lines[0], 1, 3, selected_style, Style::default());
        let selected_render_text: String = rendered
            .iter()
            .filter(|span| span.style == to_ratatui_style(selected_style))
            .map(|span| span.content.as_ref())
            .collect();

        assert_eq!(selected_render_text, "👩‍💻");
        assert_eq!(
            terminal_selection_text(&lines, &selection),
            selected_render_text
        );
    }
}