ratatui-unity 0.1.0

A Rust native plugin that brings Ratatui's TUI ecosystem to Unity 3D game engine — for all platforms.
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
//! Layout splitting and frame-replay logic.
//!
//! The FFI layer at the crate root queues [`WidgetCommand`]s into
//! [`TerminalState::commands`] and registers areas via
//! [`do_split`]. At end-of-frame [`render_all_commands`] replays the queue
//! into a single `terminal.draw()` call, materializing each command as a
//! concrete ratatui widget.

use ratatui::{
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    symbols,
    text::{Line, Span},
    widgets::{
        canvas::{self, Canvas, Circle, Map, MapResolution, Rectangle},
        Axis, Bar, BarChart, BarGroup, Block, Borders, Chart, Dataset, Gauge, LineGauge,
        List, ListItem, ListState, Paragraph, Row, Cell, Scrollbar, ScrollbarOrientation,
        ScrollbarState, Sparkline, Table, TableState, Tabs, Wrap,
    },
};

use crate::terminal::{AxisInfo, CanvasShape, DatasetInfo, TerminalState, WidgetCommand};

// ─── Layout ──────────────────────────────────────────────────────────────────

/// Constraint type discriminant for [`Constraint::Length`].
const CONSTRAINT_LENGTH: u8 = 0;
/// Constraint type discriminant for [`Constraint::Min`].
const CONSTRAINT_MIN: u8 = 1;
/// Constraint type discriminant for [`Constraint::Max`].
const CONSTRAINT_MAX: u8 = 2;
/// Constraint type discriminant for [`Constraint::Percentage`].
const CONSTRAINT_PERCENTAGE: u8 = 3;
/// Constraint type discriminant for [`Constraint::Fill`].
///
/// The decoder treats any value other than the named discriminants as Fill,
/// so this constant is unused at compile time but kept for documentation.
#[allow(dead_code)]
const CONSTRAINT_FILL: u8 = 4;

/// Splits an existing area into child areas and registers each child in the
/// state's area map.
///
/// # Parameters
/// - `area_id`: parent area id.
/// - `direction`: `0` horizontal, anything else vertical.
/// - `constraint_types` / `constraint_values`: parallel slices describing
///   each child's constraint kind and value. See [`constraint_from_bytes`].
/// - `out_ids`: caller-allocated buffer that receives the new area ids in
///   layout order.
///
/// Returns the number of child ids actually written, which is
/// `min(constraint_types.len(), constraint_values.len(), out_ids.len(),
/// computed_chunks.len())`. Returns `0` when the parent id is unknown.
pub fn do_split(
    state: &mut TerminalState,
    area_id: u32,
    direction: u8,
    constraint_types: &[u8],
    constraint_values: &[u16],
    out_ids: &mut [u32],
) -> u32 {
    let parent_rect = match state.area_map.get(&area_id).copied() {
        Some(r) => r,
        None => return 0,
    };

    let dir = if direction == 0 {
        Direction::Horizontal
    } else {
        Direction::Vertical
    };

    let count = constraint_types.len().min(constraint_values.len());
    let constraints: Vec<Constraint> = constraint_types[..count]
        .iter()
        .zip(&constraint_values[..count])
        .map(|(&t, &v)| constraint_from_bytes(t, v))
        .collect();

    let chunks = Layout::default()
        .direction(dir)
        .constraints(constraints)
        .split(parent_rect);

    let produced = chunks.len().min(out_ids.len());
    for (i, &rect) in chunks.iter().enumerate().take(produced) {
        let id = state.register_area(rect);
        out_ids[i] = id;
    }
    produced as u32
}

// ─── Frame rendering ─────────────────────────────────────────────────────────

/// Replays all queued [`WidgetCommand`]s inside a single
/// [`Terminal::draw`](ratatui::Terminal::draw) call.
///
/// The command queue and the area map are taken out of `state` to avoid
/// double-borrowing the terminal during the draw closure, then the area map
/// is restored. The queue is left empty afterwards; [`crate::ratatui_begin_frame`]
/// would clear it anyway.
///
/// Commands whose `area_id` is not present in the area map are silently
/// skipped.
///
/// # Panics
///
/// Panics if the underlying ratatui terminal draw call returns an error,
/// which on a [`TestBackend`](ratatui::backend::TestBackend) is unreachable
/// in practice.
pub fn render_all_commands(state: &mut TerminalState) {
    let commands = std::mem::take(&mut state.commands);
    let area_map = std::mem::take(&mut state.area_map);

    state
        .terminal
        .draw(|frame| {
            for cmd in &commands {
                match cmd {
                    WidgetCommand::Block { area_id, title, borders, style } => {
                        if let Some(&area) = area_map.get(area_id) {
                            frame.render_widget(
                                Block::default()
                                    .title(title.as_str())
                                    .borders(borders_from_u8(*borders))
                                    .style(*style),
                                area,
                            );
                        }
                    }

                    WidgetCommand::Paragraph { area_id, text, alignment, wrap, style } => {
                        if let Some(&area) = area_map.get(area_id) {
                            let mut para = Paragraph::new(text.as_str())
                                .alignment(alignment_from_u8(*alignment))
                                .style(*style);
                            if *wrap {
                                para = para.wrap(Wrap { trim: false });
                            }
                            frame.render_widget(para, area);
                        }
                    }

                    WidgetCommand::StyledParagraph { area_id, alignment, wrap, lines } => {
                        if let Some(&area) = area_map.get(area_id) {
                            let ratatui_lines: Vec<Line> = lines
                                .iter()
                                .map(|spans| {
                                    Line::from(
                                        spans
                                            .iter()
                                            .map(|s| Span::styled(s.text.clone(), s.style))
                                            .collect::<Vec<_>>(),
                                    )
                                })
                                .collect();
                            let mut para = Paragraph::new(ratatui_lines)
                                .alignment(alignment_from_u8(*alignment));
                            if *wrap {
                                para = para.wrap(Wrap { trim: false });
                            }
                            frame.render_widget(para, area);
                        }
                    }

                    WidgetCommand::List { area_id, items, selected, style } => {
                        if let Some(&area) = area_map.get(area_id) {
                            let list_items: Vec<ListItem> = items
                                .lines()
                                .map(ListItem::new)
                                .collect();
                            let list = List::new(list_items)
                                .style(*style)
                                .highlight_symbol("> ")
                                .highlight_style(Style::default().add_modifier(Modifier::BOLD));

                            if *selected >= 0 {
                                let mut list_state = ListState::default()
                                    .with_selected(Some(*selected as usize));
                                frame.render_stateful_widget(list, area, &mut list_state);
                            } else {
                                frame.render_widget(list, area);
                            }
                        }
                    }

                    WidgetCommand::Gauge { area_id, ratio, label, style } => {
                        if let Some(&area) = area_map.get(area_id) {
                            frame.render_widget(
                                Gauge::default()
                                    .ratio(ratio.clamp(0.0, 1.0))
                                    .label(label.as_str())
                                    .style(*style),
                                area,
                            );
                        }
                    }

                    WidgetCommand::LineGauge { area_id, ratio, label, style } => {
                        if let Some(&area) = area_map.get(area_id) {
                            frame.render_widget(
                                LineGauge::default()
                                    .ratio(ratio.clamp(0.0, 1.0))
                                    .label(label.as_str())
                                    .style(*style),
                                area,
                            );
                        }
                    }

                    WidgetCommand::Tabs { area_id, titles, selected, style } => {
                        if let Some(&area) = area_map.get(area_id) {
                            let tab_lines: Vec<Line> = titles
                                .lines()
                                .map(|t| Line::from(t.to_owned()))
                                .collect();
                            let highlight = Style::default()
                                .fg(Color::Black)
                                .bg(style.fg.unwrap_or(Color::Cyan))
                                .add_modifier(Modifier::BOLD);
                            frame.render_widget(
                                Tabs::new(tab_lines)
                                    .select(*selected as usize)
                                    .style(*style)
                                    .highlight_style(highlight),
                                area,
                            );
                        }
                    }

                    WidgetCommand::Sparkline { area_id, data, style } => {
                        if let Some(&area) = area_map.get(area_id) {
                            frame.render_widget(
                                Sparkline::default().data(data).style(*style),
                                area,
                            );
                        }
                    }

                    WidgetCommand::BarChart { area_id, bars, bar_width, bar_gap, style } => {
                        if let Some(&area) = area_map.get(area_id) {
                            let bar_items: Vec<Bar> = bars
                                .iter()
                                .map(|(label, value)| {
                                    Bar::default().label(label.as_str()).value(*value)
                                })
                                .collect();
                            let group = BarGroup::default().bars(&bar_items);
                            frame.render_widget(
                                BarChart::default()
                                    .data(group)
                                    .bar_width(*bar_width)
                                    .bar_gap(*bar_gap)
                                    .style(*style),
                                area,
                            );
                        }
                    }

                    WidgetCommand::Table { area_id, data, style } => {
                        if let Some(&area) = area_map.get(area_id) {
                            render_table(frame, area, data, *style);
                        }
                    }

                    WidgetCommand::TableEx {
                        area_id,
                        data,
                        col_constraints,
                        selected_row,
                        style,
                    } => {
                        if let Some(&area) = area_map.get(area_id) {
                            render_table_ex(frame, area, data, col_constraints, *selected_row, *style);
                        }
                    }

                    WidgetCommand::Scrollbar {
                        area_id,
                        content_length,
                        position,
                        viewport_length,
                        orientation,
                    } => {
                        if let Some(&area) = area_map.get(area_id) {
                            let orient = scrollbar_orientation_from_u8(*orientation);
                            let mut scroll_state = ScrollbarState::default()
                                .content_length(*content_length as usize)
                                .position(*position as usize)
                                .viewport_content_length(*viewport_length as usize);
                            frame.render_stateful_widget(
                                Scrollbar::new(orient),
                                area,
                                &mut scroll_state,
                            );
                        }
                    }

                    WidgetCommand::Calendar { area_id, year, month, day } => {
                        if let Some(&area) = area_map.get(area_id) {
                            render_calendar(frame, area, *year, *month, *day);
                        }
                    }

                    WidgetCommand::Chart { area_id, x_axis, y_axis, datasets } => {
                        if let Some(&area) = area_map.get(area_id) {
                            render_chart(frame, area, x_axis, y_axis, datasets);
                        }
                    }

                    WidgetCommand::Canvas {
                        area_id,
                        x_min,
                        x_max,
                        y_min,
                        y_max,
                        marker,
                        shapes,
                    } => {
                        if let Some(&area) = area_map.get(area_id) {
                            render_canvas(frame, area, *x_min, *x_max, *y_min, *y_max, *marker, shapes);
                        }
                    }
                }
            }
        })
        .expect("terminal draw failed");

    state.area_map = area_map;
    // commands are intentionally dropped here — begin_frame() would clear them anyway
}

// ─── Table helpers ────────────────────────────────────────────────────────────

/// Renders a simple equal-width table from `data`.
///
/// `data` format: first line tab-separated headers, subsequent lines
/// tab-separated rows. All columns receive the same percentage width.
///
/// Columns are capped at the area width: more columns than cells cannot be
/// displayed, and an unbounded column count both overflows the u16 width
/// math and makes the layout solver pathologically slow.
fn render_table(frame: &mut ratatui::Frame, area: Rect, data: &str, style: Style) {
    let max_cols = (area.width as usize).max(1);
    let mut lines = data.lines();
    let headers: Vec<&str> = lines
        .next()
        .unwrap_or("")
        .split('\t')
        .take(max_cols)
        .collect();
    let col_count = headers.len().max(1);
    let header_row = Row::new(headers.iter().map(|h| Cell::from(*h)));
    let rows: Vec<Row> = lines
        .map(|line| {
            Row::new(line.split('\t').take(max_cols).map(Cell::from).collect::<Vec<_>>())
        })
        .collect();
    // Divide in usize: casting col_count to u16 first can truncate to 0
    // (e.g. 65536 columns) and panic with a division by zero.
    let equal_width = (100 / col_count) as u16;
    let widths: Vec<Constraint> =
        (0..col_count).map(|_| Constraint::Percentage(equal_width)).collect();
    frame.render_widget(
        Table::new(rows, widths).header(header_row).style(style),
        area,
    );
}

/// Renders an extended table with typed column constraints and optional row
/// highlighting.
///
/// If `col_constraints` is empty all columns get equal percentage widths.
/// A non-negative `selected_row` enables a stateful render with a bold
/// highlight on the selected row.
///
/// Columns (and explicit column constraints) are capped at the area width;
/// see `render_table` for the rationale.
fn render_table_ex(
    frame: &mut ratatui::Frame,
    area: Rect,
    data: &str,
    col_constraints: &[(u8, u16)],
    selected_row: i32,
    style: Style,
) {
    let max_cols = (area.width as usize).max(1);
    let mut lines = data.lines();
    let headers: Vec<&str> = lines
        .next()
        .unwrap_or("")
        .split('\t')
        .take(max_cols)
        .collect();
    let col_count = headers.len().max(1);
    let rows: Vec<Row> = lines
        .map(|line| {
            Row::new(line.split('\t').take(max_cols).map(Cell::from).collect::<Vec<_>>())
        })
        .collect();
    let widths: Vec<Constraint> = if col_constraints.is_empty() {
        // Divide in usize: see render_table for the truncation hazard.
        let eq = (100 / col_count) as u16;
        (0..col_count).map(|_| Constraint::Percentage(eq)).collect()
    } else {
        col_constraints
            .iter()
            .take(max_cols)
            .map(|&(t, v)| constraint_from_bytes(t, v))
            .collect()
    };
    let header_row = Row::new(headers.iter().map(|h| Cell::from(*h)));
    let table = Table::new(rows, widths)
        .header(header_row)
        .style(style)
        .row_highlight_style(Style::default().add_modifier(Modifier::BOLD));

    if selected_row >= 0 {
        let mut ts = TableState::default().with_selected(Some(selected_row as usize));
        frame.render_stateful_widget(table, area, &mut ts);
    } else {
        frame.render_widget(table, area);
    }
}

// ─── Calendar ────────────────────────────────────────────────────────────────

/// Renders a monthly calendar for the given date.
///
/// Invalid dates fall back to January 1 of `year`; if even that fails, the
/// renderer uses 2024-01-01 as a last-resort hardcoded date.
fn render_calendar(frame: &mut ratatui::Frame, area: Rect, year: i32, month: u8, day: u8) {
    use ratatui::widgets::calendar::{CalendarEventStore, Monthly};

    let m = month_to_time(month);
    let d = day.clamp(1, 28);
    let date = time::Date::from_calendar_date(year, m, d).unwrap_or_else(|_| {
        time::Date::from_calendar_date(year, time::Month::January, 1)
            .unwrap_or_else(|_| {
                time::Date::from_calendar_date(2024, time::Month::January, 1)
                    .expect("hardcoded valid date")
            })
    });

    let calendar = Monthly::new(date, CalendarEventStore::default())
        .show_month_header(Style::default())
        .show_weekdays_header(Style::default());
    frame.render_widget(calendar, area);
}

/// Maps a `1..=12` month index to [`time::Month`]; out-of-range values fall
/// back to January.
fn month_to_time(month: u8) -> time::Month {
    match month {
        1 => time::Month::January,
        2 => time::Month::February,
        3 => time::Month::March,
        4 => time::Month::April,
        5 => time::Month::May,
        6 => time::Month::June,
        7 => time::Month::July,
        8 => time::Month::August,
        9 => time::Month::September,
        10 => time::Month::October,
        11 => time::Month::November,
        12 => time::Month::December,
        _ => time::Month::January,
    }
}

// ─── Chart ───────────────────────────────────────────────────────────────────

/// Builds a [`Chart`] from optional axes and datasets and renders it.
///
/// Axis titles use a gray foreground; dataset colors come from each
/// [`DatasetInfo`]. Markers map via [`marker_from_u8`].
fn render_chart(
    frame: &mut ratatui::Frame,
    area: Rect,
    x_axis: &Option<AxisInfo>,
    y_axis: &Option<AxisInfo>,
    datasets: &[DatasetInfo],
) {
    let ratatui_datasets: Vec<Dataset> = datasets
        .iter()
        .map(|d| {
            Dataset::default()
                .name(d.name.as_str())
                .marker(marker_from_u8(d.marker))
                .style(Style::default().fg(Color::Rgb(d.r, d.g, d.b)))
                .data(d.points.as_slice())
        })
        .collect();

    let mut chart = Chart::new(ratatui_datasets);

    if let Some(ax) = x_axis {
        chart = chart.x_axis(
            Axis::default()
                .title(ax.title.as_str())
                .style(Style::default().fg(Color::Gray))
                .bounds([ax.min, ax.max]),
        );
    }
    if let Some(ay) = y_axis {
        chart = chart.y_axis(
            Axis::default()
                .title(ay.title.as_str())
                .style(Style::default().fg(Color::Gray))
                .bounds([ay.min, ay.max]),
        );
    }

    frame.render_widget(chart, area);
}

// ─── Canvas ───────────────────────────────────────────────────────────────────

/// Renders a canvas widget by replaying every queued [`CanvasShape`].
///
/// `CanvasShape::Layer` triggers a `ctx.layer()` flush so subsequent shapes
/// draw on top. Text shapes clone their string because `ctx.print` requires
/// `Into<Line<'static>>`.
fn render_canvas(
    frame: &mut ratatui::Frame,
    area: Rect,
    x_min: f64,
    x_max: f64,
    y_min: f64,
    y_max: f64,
    marker: u8,
    shapes: &[CanvasShape],
) {
    let canvas_widget = Canvas::default()
        .x_bounds([x_min, x_max])
        .y_bounds([y_min, y_max])
        .marker(marker_from_u8(marker))
        .paint(|ctx| {
            for shape in shapes {
                match shape {
                    CanvasShape::Map { resolution } => {
                        ctx.draw(&Map {
                            color: Color::White,
                            resolution: if *resolution == 0 {
                                MapResolution::Low
                            } else {
                                MapResolution::High
                            },
                        });
                    }
                    CanvasShape::Layer => {
                        ctx.layer();
                    }
                    CanvasShape::Line { x1, y1, x2, y2, r, g, b } => {
                        ctx.draw(&canvas::Line {
                            x1: *x1,
                            y1: *y1,
                            x2: *x2,
                            y2: *y2,
                            color: Color::Rgb(*r, *g, *b),
                        });
                    }
                    CanvasShape::Circle { x, y, radius, r, g, b } => {
                        ctx.draw(&Circle {
                            x: *x,
                            y: *y,
                            radius: *radius,
                            color: Color::Rgb(*r, *g, *b),
                        });
                    }
                    CanvasShape::Rectangle { x, y, w, h, r, g, b } => {
                        ctx.draw(&Rectangle {
                            x: *x,
                            y: *y,
                            width: *w,
                            height: *h,
                            color: Color::Rgb(*r, *g, *b),
                        });
                    }
                    CanvasShape::Text { x, y, text, r, g, b } => {
                        // ctx.print requires Into<Line<'static>>, so clone the String.
                        ctx.print(
                            *x,
                            *y,
                            Span::styled(
                                text.clone(),
                                Style::default().fg(Color::Rgb(*r, *g, *b)),
                            ),
                        );
                    }
                    CanvasShape::Points { coords, r, g, b } => {
                        ctx.draw(&canvas::Points {
                            coords: coords.as_slice(),
                            color: Color::Rgb(*r, *g, *b),
                        });
                    }
                }
            }
        });

    frame.render_widget(canvas_widget, area);
}

// ─── Helpers ─────────────────────────────────────────────────────────────────

/// Decodes a `(constraint_type, value)` pair into a ratatui [`Constraint`].
///
/// Mapping:
/// - `0` → [`Constraint::Length`]
/// - `1` → [`Constraint::Min`]
/// - `2` → [`Constraint::Max`]
/// - `3` → [`Constraint::Percentage`]
/// - any other → [`Constraint::Fill`]
fn constraint_from_bytes(t: u8, v: u16) -> Constraint {
    match t {
        CONSTRAINT_LENGTH => Constraint::Length(v),
        CONSTRAINT_MIN => Constraint::Min(v),
        CONSTRAINT_MAX => Constraint::Max(v),
        CONSTRAINT_PERCENTAGE => Constraint::Percentage(v),
        _ => Constraint::Fill(v),
    }
}

/// Decodes a packed border bit field into [`Borders`].
///
/// Bits: `0x01` Top, `0x02` Bottom, `0x04` Left, `0x08` Right. The value
/// `0x0F` is treated as `Borders::ALL`.
fn borders_from_u8(b: u8) -> Borders {
    if b == 0x0F {
        return Borders::ALL;
    }
    let mut borders = Borders::NONE;
    if b & 0x01 != 0 { borders |= Borders::TOP; }
    if b & 0x02 != 0 { borders |= Borders::BOTTOM; }
    if b & 0x04 != 0 { borders |= Borders::LEFT; }
    if b & 0x08 != 0 { borders |= Borders::RIGHT; }
    borders
}

/// Maps the FFI alignment code (`0` Left, `1` Center, `2` Right) to
/// [`Alignment`]. Unknown values default to `Left`.
fn alignment_from_u8(a: u8) -> Alignment {
    match a {
        1 => Alignment::Center,
        2 => Alignment::Right,
        _ => Alignment::Left,
    }
}

/// Maps the FFI marker code to [`symbols::Marker`].
///
/// `0` Dot, `1` Braille, `2` HalfBlock, `3` Block. Unknown values default to
/// `Dot`.
fn marker_from_u8(m: u8) -> symbols::Marker {
    match m {
        1 => symbols::Marker::Braille,
        2 => symbols::Marker::HalfBlock,
        3 => symbols::Marker::Block,
        _ => symbols::Marker::Dot,
    }
}

/// Maps the FFI scrollbar orientation code to [`ScrollbarOrientation`].
///
/// `0` VerticalRight, `1` VerticalLeft, `2` HorizontalBottom, `3` HorizontalTop.
/// Unknown values default to `VerticalRight`.
fn scrollbar_orientation_from_u8(o: u8) -> ScrollbarOrientation {
    match o {
        1 => ScrollbarOrientation::VerticalLeft,
        2 => ScrollbarOrientation::HorizontalBottom,
        3 => ScrollbarOrientation::HorizontalTop,
        _ => ScrollbarOrientation::VerticalRight,
    }
}