tui-lipan 0.1.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
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
use ratatui::buffer::Buffer;
use ratatui::symbols::merge::MergeStrategy;
use ratatui::text::{Line, Span};
use unicode_width::UnicodeWidthStr;

use crate::backend::ratatui_backend::common::{
    ClipBounds, border_horizontal_char, border_tabs_title_line, clear_fg_preserve_bg_clipped,
    fill_rect_clipped_style, render_line_clipped, richtext_to_spans, style_paints_bg,
    style_uses_backdrop_bg, to_ratatui_border_set, to_ratatui_style,
    to_ratatui_style_with_terminal_bg, truncate_spans,
};
use crate::backend::ratatui_backend::renderers::frame::utils::build_header_line;
use crate::style::{Color, Edge, Paint, Rect, Style};
use crate::widgets::internal::{FrameGeometry, FrameProps};
use crate::widgets::{BorderMergeMode, DecorationGlyph, DecorationPlacement, EdgeDecoration};

pub(crate) struct FrameRenderCtx {
    pub active: bool,
    pub is_hovered: bool,
    pub clip_rect: Option<Rect>,
    pub terminal_bg: Option<Color>,
}

struct BorderCellDraw<'a> {
    style: ratatui::style::Style,
    clip: &'a ClipBounds,
    buf_bounds: &'a ClipBounds,
    border_merge_mode: BorderMergeMode,
}

struct CapDraw<'a> {
    style: ratatui::style::Style,
    clip: &'a ClipBounds,
    buf_bounds: &'a ClipBounds,
    is_start: bool,
    border_merge_mode: BorderMergeMode,
}

pub(crate) fn render_frame(
    f: &mut ratatui::Frame<'_>,
    props: &FrameProps,
    geometry: &FrameGeometry,
    ctx: FrameRenderCtx,
) {
    let body_rect = geometry.body_rect;

    let transparent_decoration_bg_snapshot =
        snapshot_transparent_decoration_backgrounds(f, props, geometry, &ctx);

    if props.border {
        render_border_frame(f, props, geometry, &ctx);
    } else {
        render_plain_frame(f, props, body_rect, &ctx);
        render_plain_frame_status(f, props, geometry, &ctx);
    }

    render_frame_decorations(f, props, geometry, &ctx);

    restore_decoration_backgrounds(f, &transparent_decoration_bg_snapshot, ctx.clip_rect);
}

#[derive(Clone, Debug)]
struct DecorationBackgroundSnapshot {
    rect: Rect,
    colors: Vec<ratatui::style::Color>,
}

fn snapshot_transparent_decoration_backgrounds(
    f: &mut ratatui::Frame<'_>,
    props: &FrameProps,
    geometry: &FrameGeometry,
    ctx: &FrameRenderCtx,
) -> Vec<DecorationBackgroundSnapshot> {
    if props.decorations.is_empty() {
        return Vec::new();
    }

    let content_rect = geometry.content_rect;
    let mut outside_offsets = crate::style::Padding::default();
    let mut snapshots = Vec::new();
    let buf = f.buffer_mut();
    let bounds = ClipBounds::from_rrect(buf.area);

    for decoration in &props.decorations {
        let style = resolve_edge_decoration_style(decoration, ctx.active, ctx.is_hovered);
        if !matches!(style.bg, Some(bg) if bg.is_transparent_sentinel()) {
            continue;
        }

        let mut band_rect = match decoration.placement {
            DecorationPlacement::Outside => {
                decoration_band_outside(geometry.outer_rect, decoration, &mut outside_offsets)
            }
            DecorationPlacement::Border => {
                decoration_band_border(geometry.outer_rect, geometry.body_rect, decoration)
            }
            DecorationPlacement::Inside => decoration_band_on_rect(content_rect, decoration),
        };

        if let Some(clip) = ctx.clip_rect {
            band_rect = band_rect.intersection(&clip);
        }
        if band_rect.is_empty() {
            continue;
        }

        let mut colors = Vec::with_capacity(band_rect.w as usize * band_rect.h as usize);
        for y in band_rect.y..band_rect.y.saturating_add(band_rect.h as i16) {
            for x in band_rect.x..band_rect.x.saturating_add(band_rect.w as i16) {
                if !bounds.contains(x as i32, y as i32) {
                    colors.push(ratatui::style::Color::Reset);
                    continue;
                }
                let color = buf
                    .cell((x as u16, y as u16))
                    .map(|cell| cell.bg)
                    .unwrap_or(ratatui::style::Color::Reset);
                colors.push(color);
            }
        }
        snapshots.push(DecorationBackgroundSnapshot {
            rect: band_rect,
            colors,
        });
    }

    snapshots
}

fn restore_decoration_backgrounds(
    f: &mut ratatui::Frame<'_>,
    snapshots: &[DecorationBackgroundSnapshot],
    clip_rect: Option<Rect>,
) {
    if snapshots.is_empty() {
        return;
    }

    let buf = f.buffer_mut();
    let bounds = ClipBounds::from_rrect(buf.area);
    for snapshot in snapshots {
        let mut rect = snapshot.rect;
        if let Some(clip) = clip_rect {
            rect = rect.intersection(&clip);
        }
        if rect.is_empty() {
            continue;
        }

        let mut i = 0usize;
        for y in snapshot.rect.y..snapshot.rect.y.saturating_add(snapshot.rect.h as i16) {
            for x in snapshot.rect.x..snapshot.rect.x.saturating_add(snapshot.rect.w as i16) {
                let saved_bg = snapshot.colors[i];
                i += 1;
                if !rect.contains(x, y) || !bounds.contains(x as i32, y as i32) {
                    continue;
                }
                if let Some(cell) = buf.cell_mut((x as u16, y as u16)) {
                    cell.bg = saved_bg;
                }
            }
        }
    }
}

fn render_frame_decorations(
    f: &mut ratatui::Frame<'_>,
    props: &FrameProps,
    geometry: &FrameGeometry,
    ctx: &FrameRenderCtx,
) {
    if props.decorations.is_empty() {
        return;
    }

    let content_rect = geometry.content_rect;

    let buf = f.buffer_mut();
    let clip = ctx
        .clip_rect
        .map(ClipBounds::from_rect)
        .unwrap_or_else(ClipBounds::unbounded);
    let buf_bounds = ClipBounds::from_rrect(buf.area);

    let mut outside_offsets = crate::style::Padding::default();

    for decoration in &props.decorations {
        let band_rect = match decoration.placement {
            DecorationPlacement::Outside => {
                decoration_band_outside(geometry.outer_rect, decoration, &mut outside_offsets)
            }
            DecorationPlacement::Border => {
                decoration_band_border(geometry.outer_rect, geometry.body_rect, decoration)
            }
            DecorationPlacement::Inside => decoration_band_on_rect(content_rect, decoration),
        };

        if band_rect.w == 0 || band_rect.h == 0 {
            continue;
        }

        let decoration_merge_mode =
            if props.border && matches!(decoration.placement, DecorationPlacement::Border) {
                props.border_merge_mode
            } else {
                BorderMergeMode::Replace
            };

        let style = resolve_edge_decoration_style(decoration, ctx.active, ctx.is_hovered);
        let rstyle = to_ratatui_style_with_terminal_bg(style, ctx.terminal_bg);
        let symbol = decoration.glyph.resolve(decoration.edge).to_string();
        draw_symbol_rect(
            buf,
            band_rect,
            &symbol,
            rstyle,
            &clip,
            &buf_bounds,
            decoration_merge_mode,
        );

        if let Some(cap) = decoration.cap_start {
            draw_cap(
                buf,
                band_rect,
                decoration.edge,
                cap,
                &CapDraw {
                    style: rstyle,
                    clip: &clip,
                    buf_bounds: &buf_bounds,
                    is_start: true,
                    border_merge_mode: decoration_merge_mode,
                },
            );
        }
        if let Some(cap) = decoration.cap_end {
            draw_cap(
                buf,
                band_rect,
                decoration.edge,
                cap,
                &CapDraw {
                    style: rstyle,
                    clip: &clip,
                    buf_bounds: &buf_bounds,
                    is_start: false,
                    border_merge_mode: decoration_merge_mode,
                },
            );
        }
    }
}

fn draw_cap(
    buf: &mut Buffer,
    band: Rect,
    edge: crate::style::Edge,
    glyph: DecorationGlyph,
    draw: &CapDraw<'_>,
) {
    if band.w == 0 || band.h == 0 {
        return;
    }
    let symbol = glyph.resolve(edge).to_string();
    let cap_rect = match edge {
        Edge::Left | Edge::Right => {
            let y = if draw.is_start {
                band.y
            } else {
                band.y.saturating_add(band.h as i16).saturating_sub(1)
            };
            Rect {
                x: band.x,
                y,
                w: band.w,
                h: 1,
            }
        }
        Edge::Top | Edge::Bottom => {
            let x = if draw.is_start {
                band.x
            } else {
                band.x.saturating_add(band.w as i16).saturating_sub(1)
            };
            Rect {
                x,
                y: band.y,
                w: 1,
                h: band.h,
            }
        }
    };
    draw_symbol_rect(
        buf,
        cap_rect,
        &symbol,
        draw.style,
        draw.clip,
        draw.buf_bounds,
        draw.border_merge_mode,
    );
}

pub(crate) fn resolve_edge_decoration_style(
    decoration: &EdgeDecoration,
    active: bool,
    is_hovered: bool,
) -> Style {
    let mut style = decoration.style;
    if is_hovered && let Some(hover_style) = decoration.hover_style {
        style = style.patch(hover_style);
    }
    if active && let Some(focus_style) = decoration.focus_style {
        style = style.patch(focus_style);
    }
    if style.fg.is_none() {
        style.fg = Some(Paint::Solid(Color::Reset));
    }
    if style.bg.is_none() {
        style.bg = Some(Paint::Solid(Color::Reset));
    }
    style
}

fn decoration_band_outside(
    rect: Rect,
    decoration: &EdgeDecoration,
    offsets: &mut crate::style::Padding,
) -> Rect {
    let thickness = decoration.thickness.max(1);
    match decoration.edge {
        Edge::Top => {
            let y = rect.y.saturating_add(offsets.top as i16);
            offsets.top = offsets.top.saturating_add(thickness);
            Rect {
                x: rect.x,
                y,
                w: rect.w,
                h: thickness.min(rect.h),
            }
        }
        Edge::Bottom => {
            let h = thickness.min(rect.h);
            let y = rect
                .y
                .saturating_add(rect.h as i16)
                .saturating_sub(h as i16)
                .saturating_sub(offsets.bottom as i16);
            offsets.bottom = offsets.bottom.saturating_add(thickness);
            Rect {
                x: rect.x,
                y,
                w: rect.w,
                h,
            }
        }
        Edge::Left => {
            let x = rect.x.saturating_add(offsets.left as i16);
            offsets.left = offsets.left.saturating_add(thickness);
            Rect {
                x,
                y: rect.y,
                w: thickness.min(rect.w),
                h: rect.h,
            }
        }
        Edge::Right => {
            let w = thickness.min(rect.w);
            let x = rect
                .x
                .saturating_add(rect.w as i16)
                .saturating_sub(w as i16)
                .saturating_sub(offsets.right as i16);
            offsets.right = offsets.right.saturating_add(thickness);
            Rect {
                x,
                y: rect.y,
                w,
                h: rect.h,
            }
        }
    }
}

fn decoration_band_on_rect(rect: Rect, decoration: &EdgeDecoration) -> Rect {
    let thickness = decoration.thickness.max(1);
    match decoration.edge {
        Edge::Top => Rect {
            x: rect.x,
            y: rect.y,
            w: rect.w,
            h: thickness.min(rect.h),
        },
        Edge::Bottom => Rect {
            x: rect.x,
            y: rect
                .y
                .saturating_add(rect.h as i16)
                .saturating_sub(thickness.min(rect.h) as i16),
            w: rect.w,
            h: thickness.min(rect.h),
        },
        Edge::Left => Rect {
            x: rect.x,
            y: rect.y,
            w: thickness.min(rect.w),
            h: rect.h,
        },
        Edge::Right => Rect {
            x: rect
                .x
                .saturating_add(rect.w as i16)
                .saturating_sub(thickness.min(rect.w) as i16),
            y: rect.y,
            w: thickness.min(rect.w),
            h: rect.h,
        },
    }
}

fn decoration_band_border(frame_rect: Rect, _body_rect: Rect, decoration: &EdgeDecoration) -> Rect {
    let thickness = decoration.thickness.max(1);
    match decoration.edge {
        Edge::Top => Rect {
            x: frame_rect.x,
            y: frame_rect.y,
            w: frame_rect.w,
            h: thickness.min(frame_rect.h),
        },
        Edge::Bottom => Rect {
            x: frame_rect.x,
            y: frame_rect
                .y
                .saturating_add(frame_rect.h as i16)
                .saturating_sub(thickness.min(frame_rect.h) as i16),
            w: frame_rect.w,
            h: thickness.min(frame_rect.h),
        },
        Edge::Left => Rect {
            x: frame_rect.x,
            y: frame_rect.y,
            w: thickness.min(frame_rect.w),
            h: frame_rect.h,
        },
        Edge::Right => Rect {
            x: frame_rect
                .x
                .saturating_add(frame_rect.w as i16)
                .saturating_sub(thickness.min(frame_rect.w) as i16),
            y: frame_rect.y,
            w: thickness.min(frame_rect.w),
            h: frame_rect.h,
        },
    }
}

fn is_box_drawing_symbol(symbol: &str) -> bool {
    let mut chars = symbol.chars();
    let Some(ch) = chars.next() else {
        return false;
    };
    chars.next().is_none() && (0x2500..=0x257F).contains(&(ch as u32))
}

fn to_merge_strategy(strategy: BorderMergeMode) -> MergeStrategy {
    match strategy {
        BorderMergeMode::Replace => MergeStrategy::Replace,
        BorderMergeMode::Exact => MergeStrategy::Exact,
        BorderMergeMode::Fuzzy => MergeStrategy::Fuzzy,
    }
}

fn draw_border_cell(buf: &mut Buffer, x: i32, y: i32, symbol: &str, draw: &BorderCellDraw<'_>) {
    if !draw.clip.contains(x, y) || !draw.buf_bounds.contains(x, y) {
        return;
    }
    let Some(cell) = buf.cell_mut((x as u16, y as u16)) else {
        return;
    };

    let should_merge = draw.border_merge_mode != BorderMergeMode::Replace
        && is_box_drawing_symbol(cell.symbol())
        && is_box_drawing_symbol(symbol);

    if should_merge {
        cell.merge_symbol(symbol, to_merge_strategy(draw.border_merge_mode));
    } else {
        cell.set_symbol(symbol);
    }
    cell.set_style(draw.style);
}

fn draw_symbol_rect(
    buf: &mut Buffer,
    rect: Rect,
    symbol: &str,
    style: ratatui::style::Style,
    clip: &ClipBounds,
    buf_bounds: &ClipBounds,
    border_merge_mode: BorderMergeMode,
) {
    if rect.w == 0 || rect.h == 0 {
        return;
    }
    let x = rect.x as i32;
    let y = rect.y as i32;
    let w = rect.w as i32;
    let h = rect.h as i32;
    let start_x = x.max(clip.min_x);
    let end_x = (x + w - 1).min(clip.max_x);
    let start_y = y.max(clip.min_y);
    let end_y = (y + h - 1).min(clip.max_y);
    let border_draw = BorderCellDraw {
        style,
        clip,
        buf_bounds,
        border_merge_mode,
    };

    for cy in start_y..=end_y {
        for cx in start_x..=end_x {
            draw_border_cell(buf, cx, cy, symbol, &border_draw);
        }
    }
}

pub(crate) fn resolve_block_style(
    props: &FrameProps,
    active: bool,
    is_hovered: bool,
) -> (Style, crate::style::BorderStyle) {
    let mut block_style = props.style;
    let mut border_style = props.border_style;

    if active && let Some(fbs) = props.focus_border_style() {
        border_style = fbs;
    }

    if is_hovered && let Some(hs) = props.hover_style() {
        block_style = block_style.patch(hs);
    }

    if active && let Some(fs) = props.focus_style() {
        block_style = block_style.patch(fs);
    }

    (block_style, border_style)
}

fn render_border_frame(
    f: &mut ratatui::Frame<'_>,
    props: &FrameProps,
    geometry: &FrameGeometry,
    ctx: &FrameRenderCtx,
) {
    let rect = geometry.frame_rect;
    let join_overlap = geometry.join_overlap;

    if props.compact || (props.collapsible && rect.h < 3) {
        render_compact_frame(f, props, rect, ctx);
        return;
    }

    let (block_style, border_style) = resolve_block_style(props, ctx.active, ctx.is_hovered);
    let border_rstyle = to_ratatui_style(block_style);

    if style_uses_backdrop_bg(block_style) {
        clear_fg_preserve_bg_clipped(f, rect, ctx.clip_rect);
    } else if style_paints_bg(block_style) {
        fill_rect_clipped_style(f, rect, block_style, ctx.clip_rect, ctx.terminal_bg);
    }

    let buf = f.buffer_mut();
    let x = rect.x as i32;
    let y = rect.y as i32;
    let w = rect.w as i32;
    let h = rect.h as i32;
    let mut left = x;
    let mut top = y;
    if props.join_frame {
        if join_overlap.left {
            left = left.saturating_sub(1);
        }
        if join_overlap.top {
            top = top.saturating_sub(1);
        }
    }
    let right = x + w - 1;
    let bottom = y + h - 1;

    let set = to_ratatui_border_set(border_style).unwrap_or(ratatui::symbols::border::PLAIN);

    let clip = ctx
        .clip_rect
        .map(ClipBounds::from_rect)
        .unwrap_or_else(ClipBounds::unbounded);
    let buf_bounds = ClipBounds::from_rrect(buf.area);
    let border_merge_mode = props.border_merge_mode;
    let border_draw = BorderCellDraw {
        style: border_rstyle,
        clip: &clip,
        buf_bounds: &buf_bounds,
        border_merge_mode,
    };

    draw_border_cell(buf, left, top, set.top_left, &border_draw);
    draw_border_cell(buf, right, top, set.top_right, &border_draw);
    draw_border_cell(buf, left, bottom, set.bottom_left, &border_draw);
    draw_border_cell(buf, right, bottom, set.bottom_right, &border_draw);

    let h_char = set.horizontal_top;
    let b_char = set.horizontal_bottom;
    let start_x = (left + 1).max(clip.min_x);
    let end_x = right.min(clip.max_x);
    for cx in start_x..end_x {
        draw_border_cell(buf, cx, top, h_char, &border_draw);
        draw_border_cell(buf, cx, bottom, b_char, &border_draw);
    }

    let start_y = (top + 1).max(clip.min_y);
    let end_y = bottom.min(clip.max_y);
    for cy in start_y..end_y {
        if props.border_edges.has_left() {
            draw_border_cell(buf, left, cy, set.vertical_left, &border_draw);
        }
        if props.border_edges.has_right() {
            draw_border_cell(buf, right, cy, set.vertical_right, &border_draw);
        }
    }

    if let Some(line) = build_header_line(props, block_style, ctx.active, rect.w, h_char, None) {
        let line_width = right.saturating_sub(left).saturating_sub(1);
        render_line_clipped(buf, left + 1, top, line_width, &line, ctx.clip_rect);
    }

    let max_title_w = rect.w.saturating_sub(2);
    let status_style = {
        let mut s = block_style.patch(props.status_style);
        if ctx.active
            && let Some(fss) = props.focus_status_style()
        {
            s = block_style.patch(fss);
        }
        s
    };

    let footer_line = if let Some(status) = &props.status {
        let spans = richtext_to_spans(status, status_style);
        let spans = truncate_spans(spans, max_title_w);
        let line = Line::from(spans).left_aligned();
        Some(apply_footer_padding(
            line,
            props.footer_padding,
            b_char,
            block_style,
        ))
    } else if let Some(status) = &props.status_center {
        let spans = richtext_to_spans(status, status_style);
        let spans = truncate_spans(spans, max_title_w);
        let line = Line::from(spans).centered();
        Some(apply_footer_padding(
            line,
            props.footer_padding,
            b_char,
            block_style,
        ))
    } else if let Some(status) = &props.status_right {
        let spans = richtext_to_spans(status, status_style);
        let spans = truncate_spans(spans, max_title_w);
        let line = Line::from(spans).right_aligned();
        Some(apply_footer_padding(
            line,
            props.footer_padding,
            b_char,
            block_style,
        ))
    } else {
        None
    };

    if let Some(line) = footer_line {
        let line_width = right.saturating_sub(left).saturating_sub(1);
        render_line_clipped(buf, left + 1, bottom, line_width, &line, ctx.clip_rect);
    }

    if let Some(inner_style) = props.inner_style() {
        fill_rect_clipped_style(
            f,
            geometry.body_rect,
            inner_style,
            ctx.clip_rect,
            ctx.terminal_bg,
        );
    }
}

fn apply_footer_padding<'a>(
    mut line: Line<'a>,
    p: crate::style::Padding,
    fill_char: &str,
    style: Style,
) -> Line<'a> {
    if p.left == 0 && p.right == 0 {
        return line;
    }
    let rstyle = to_ratatui_style(style);
    let left_span = Span::styled(fill_char.repeat(p.left as usize), rstyle);
    let right_span = Span::styled(fill_char.repeat(p.right as usize), rstyle);
    if p.left > 0 {
        line.spans.insert(0, left_span);
    }
    if p.right > 0 {
        line.spans.push(right_span);
    }
    line
}

fn render_plain_frame(
    f: &mut ratatui::Frame<'_>,
    props: &FrameProps,
    rect: Rect,
    ctx: &FrameRenderCtx,
) {
    if rect.w == 0 || rect.h == 0 {
        return;
    }

    let (block_style, _) = resolve_block_style(props, ctx.active, ctx.is_hovered);
    fill_rect_clipped_style(f, rect, block_style, ctx.clip_rect, ctx.terminal_bg);

    if let Some(inner_style) = props.inner_style() {
        fill_rect_clipped_style(f, rect, inner_style, ctx.clip_rect, ctx.terminal_bg);
    }
}

fn render_plain_frame_status(
    f: &mut ratatui::Frame<'_>,
    props: &FrameProps,
    geometry: &FrameGeometry,
    ctx: &FrameRenderCtx,
) {
    let Some(status_rect) = geometry.status_rect else {
        return;
    };

    let (block_style, _) = resolve_block_style(props, ctx.active, ctx.is_hovered);
    let mut status_style = block_style.patch(props.status_style);
    if ctx.active
        && let Some(fss) = props.focus_status_style()
    {
        status_style = block_style.patch(fss);
    }

    let line = if let Some(status) = &props.status {
        Some(
            Line::from(truncate_spans(
                richtext_to_spans(status, status_style),
                status_rect.w,
            ))
            .left_aligned(),
        )
    } else if let Some(status) = &props.status_center {
        Some(
            Line::from(truncate_spans(
                richtext_to_spans(status, status_style),
                status_rect.w,
            ))
            .centered(),
        )
    } else {
        props.status_right.as_ref().map(|status| {
            Line::from(truncate_spans(
                richtext_to_spans(status, status_style),
                status_rect.w,
            ))
            .right_aligned()
        })
    };

    if let Some(line) = line {
        render_line_clipped(
            f.buffer_mut(),
            status_rect.x as i32,
            status_rect.y as i32,
            status_rect.w as i32,
            &line,
            ctx.clip_rect,
        );
    }
}

fn render_compact_frame(
    f: &mut ratatui::Frame<'_>,
    props: &FrameProps,
    rect: Rect,
    ctx: &FrameRenderCtx,
) {
    if rect.w == 0 || rect.h == 0 {
        return;
    }

    let compact_rect = Rect { h: 1, ..rect };

    let (block_style, border_style) = resolve_block_style(props, ctx.active, ctx.is_hovered);

    let mut title_style = block_style.patch(props.title_style);
    if ctx.active
        && let Some(fts) = props.focus_title_style()
    {
        title_style = block_style.patch(fts);
    }

    let status_style = block_style.patch(props.status_style);

    let width = rect.w as usize;

    let dash = border_horizontal_char(border_style);
    let block_rstyle = to_ratatui_style(block_style);

    let mut title_spans: Vec<Span<'_>> = if props.has_header {
        Vec::new()
    } else if props.has_border() && !props.tab_titles.is_empty() {
        let mut active_tab_style = block_style.patch(props.active_tab_style);
        if ctx.active
            && let Some(fts) = props.focus_active_tab_style()
        {
            active_tab_style = block_style.patch(fts);
        }
        let mut inactive_tab_style = block_style.patch(props.inactive_tab_style);
        if ctx.active
            && let Some(ifts) = props.focus_inactive_tab_style()
        {
            inactive_tab_style = block_style.patch(ifts);
        }
        border_tabs_title_line(
            &props.tab_titles,
            props.active_tab,
            active_tab_style,
            inactive_tab_style,
            props.tab_variant,
            block_style,
            title_style,
        )
        .spans
    } else if let Some(t) = &props.title {
        richtext_to_spans(t, title_style)
    } else {
        Vec::new()
    };

    if let Some(prefix) = &props.title_prefix {
        let prefix_spans = richtext_to_spans(prefix, title_style);
        if title_spans.is_empty() {
            title_spans = prefix_spans;
        } else {
            let sep_span = Span::styled(dash.to_string(), block_rstyle);
            let mut out = Vec::with_capacity(title_spans.len() + prefix_spans.len() + 1);
            out.extend(prefix_spans);
            out.push(sep_span);
            out.extend(title_spans);
            title_spans = out;
        }
    }

    if let Some(suffix) = &props.title_suffix {
        let suffix_spans = richtext_to_spans(suffix, title_style);
        if title_spans.is_empty() {
            title_spans = suffix_spans;
        } else {
            let sep_span = Span::styled(dash.to_string(), block_rstyle);
            title_spans.push(sep_span);
            title_spans.extend(suffix_spans);
        }
    }

    let status_spans: Vec<Span<'_>> = props
        .status_right
        .as_ref()
        .or(props.status_center.as_ref())
        .or(props.status.as_ref())
        .map(|s| richtext_to_spans(s, status_style))
        .unwrap_or_default();

    let title_w: usize = title_spans
        .iter()
        .map(|span| UnicodeWidthStr::width(span.content.as_ref()))
        .sum();
    let status_w: usize = status_spans
        .iter()
        .map(|span| UnicodeWidthStr::width(span.content.as_ref()))
        .sum();

    let has_title = title_w > 0;
    let has_status = status_w > 0;

    let cap_left = 1usize;
    let cap_right = if width > 1 { 1usize } else { 0usize };

    let title_left_count = if has_title {
        props.header_padding.left as usize
    } else {
        0
    };
    let title_right_count = if has_title {
        props.header_padding.right as usize
    } else {
        0
    };
    let status_left_count = if has_status {
        props.footer_padding.left as usize
    } else {
        0
    };
    let status_right_count = if has_status {
        props.footer_padding.right as usize
    } else {
        0
    };

    let separator_min = if has_title && has_status { 2 } else { 0 };

    let fixed_w = cap_left
        + cap_right
        + title_left_count
        + title_right_count
        + status_left_count
        + status_right_count
        + separator_min;
    let content_budget = width.saturating_sub(fixed_w);

    let (final_title_spans, final_status_spans) = if title_w + status_w <= content_budget {
        (title_spans, status_spans)
    } else if title_w <= content_budget.saturating_sub(status_w.min(content_budget / 3)) {
        let remaining = content_budget.saturating_sub(title_w);
        (title_spans, truncate_spans(status_spans, remaining as u16))
    } else {
        let title_budget = content_budget.saturating_mul(2) / 3;
        let status_budget = content_budget.saturating_sub(title_budget);
        (
            truncate_spans(title_spans, title_budget as u16),
            truncate_spans(status_spans, status_budget as u16),
        )
    };

    let separator_dashes = width.saturating_sub(
        fixed_w
            + final_title_spans
                .iter()
                .map(|s| UnicodeWidthStr::width(s.content.as_ref()))
                .sum::<usize>()
            + final_status_spans
                .iter()
                .map(|s| UnicodeWidthStr::width(s.content.as_ref()))
                .sum::<usize>(),
    ) + separator_min;

    let mut spans = Vec::with_capacity(9);

    spans.push(Span::styled(dash.repeat(cap_left), block_rstyle));

    if title_left_count > 0 {
        spans.push(Span::styled(dash.repeat(title_left_count), block_rstyle));
    }

    spans.extend(final_title_spans);

    if title_right_count > 0 {
        spans.push(Span::styled(dash.repeat(title_right_count), block_rstyle));
    }

    if separator_dashes > 0 {
        spans.push(Span::styled(dash.repeat(separator_dashes), block_rstyle));
    }

    if status_left_count > 0 {
        spans.push(Span::styled(dash.repeat(status_left_count), block_rstyle));
    }

    if !final_status_spans.is_empty() {
        spans.extend(final_status_spans);
    }

    if status_right_count > 0 {
        spans.push(Span::styled(dash.repeat(status_right_count), block_rstyle));
    }

    if cap_right > 0 {
        spans.push(Span::styled(dash.repeat(cap_right), block_rstyle));
    }

    let line = Line::from(spans);

    if style_uses_backdrop_bg(block_style) {
        clear_fg_preserve_bg_clipped(f, compact_rect, ctx.clip_rect);
    } else if style_paints_bg(block_style) {
        fill_rect_clipped_style(f, compact_rect, block_style, ctx.clip_rect, ctx.terminal_bg);
    }

    let buf = f.buffer_mut();
    render_line_clipped(
        buf,
        rect.x as i32,
        rect.y as i32,
        rect.w as i32,
        &line,
        ctx.clip_rect,
    );
}