ratatui-ratty 0.3.0

A Ratatui widget for rendering inline 3D graphics via Ratty Graphics Protocol
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
use std::{
    fs, io,
    path::PathBuf,
    time::{SystemTime, UNIX_EPOCH},
};

use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers};
use ratatui::{
    DefaultTerminal,
    buffer::Buffer,
    layout::{Rect, Size},
    style::{Color, Style},
    text::{Line, Span},
    widgets::{Block, Paragraph, Widget},
};
use ratatui_image::{
    Image as TerminalImage, Resize,
    picker::{Picker, ProtocolType},
    protocol::Protocol,
};
use ratatui_ratty::{RattyGraphic, RattyGraphicSettings};

fn main() -> io::Result<()> {
    let mut terminal = ratatui::init();
    let result = run(&mut terminal);
    ratatui::restore();
    result
}

fn run(terminal: &mut DefaultTerminal) -> io::Result<()> {
    let mut document = TempleEditor::new()?;

    loop {
        terminal.draw(|frame| {
            let area = frame.area();
            let cursor = document.render(frame, area);
            frame.set_cursor_position(cursor);
        })?;

        if let Event::Key(key) = event::read()? {
            if matches!(key.code, KeyCode::Char('q')) && key.modifiers.is_empty() {
                document.clear()?;
                return Ok(());
            }
            document.handle_key(key);
        }
    }
}

struct TempleEditor<'a> {
    lines: Vec<Vec<DocCell>>,
    objects: Vec<Option<PlacedGraphic<'a>>>,
    asset_pool: Vec<String>,
    next_object_id: u32,
    debug_cells: bool,
    cursor_row: usize,
    cursor_col: usize,
    scroll: u16,
    viewport_height: u16,
    image: Protocol,
    show_logo: bool,
}

impl<'a> TempleEditor<'a> {
    fn new() -> io::Result<Self> {
        let image_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets/TempleOS.jpg");
        let image = image::ImageReader::open(image_path)
            .map_err(io::Error::other)?
            .decode()
            .map_err(io::Error::other)?;
        let mut picker = Picker::halfblocks();
        picker.set_protocol_type(ProtocolType::Kitty);
        let mut editor = Self {
            lines: initial_lines(),
            objects: Vec::new(),
            asset_pool: discover_obj_assets().unwrap_or_default(),
            next_object_id: 100,
            debug_cells: false,
            cursor_row: 0,
            cursor_col: 0,
            scroll: 0,
            viewport_height: 1,
            image: picker
                .new_protocol(image, Size::new(18, 12), Resize::Fit(None))
                .map_err(io::Error::other)?,
            show_logo: true,
        };
        editor.insert_startup_objects();
        Ok(editor)
    }

    fn render(&mut self, frame: &mut ratatui::Frame<'_>, area: Rect) -> (u16, u16) {
        let buf = frame.buffer_mut();
        let header = Rect::new(area.x, area.y, area.width, 3);
        let body = Rect::new(
            area.x,
            area.y.saturating_add(3),
            area.width,
            area.height.saturating_sub(3),
        );

        Paragraph::new(Line::from(vec![
            Span::styled("arrows", Style::default().fg(Color::Cyan)),
            Span::raw(": move  "),
            Span::styled("ctrl+v", Style::default().fg(Color::Cyan)),
            Span::raw(": insert obj  "),
            Span::styled("enter", Style::default().fg(Color::Cyan)),
            Span::raw(": split  "),
            Span::styled("bckspc/del", Style::default().fg(Color::Cyan)),
            Span::raw(": edit  "),
            Span::styled("d", Style::default().fg(Color::Cyan)),
            Span::raw(": debug  "),
            Span::styled("ctrl+l", Style::default().fg(Color::Cyan)),
            Span::raw(": logo  "),
            Span::styled("q", Style::default().fg(Color::Cyan)),
            Span::raw(": quit"),
        ]))
        .block(Block::bordered().title(Span::styled(
            "Ratty Editor Demo",
            Style::default().fg(Color::Yellow),
        )))
        .render(header, buf);

        let block = Block::bordered()
            .border_style(Style::default().fg(Color::White))
            .title("TempleOS-Notes.HC");
        let inner = block.inner(body);
        block.render(body, buf);

        self.viewport_height = inner.height.max(1);
        self.ensure_cursor_visible();

        for y in 0..inner.height {
            let row = self.scroll as usize + y as usize;
            if row >= self.lines.len() {
                continue;
            }
            self.render_line(buf, inner, row, y);
        }

        self.sync_objects(buf, inner);
        let image_width = inner.width.min(18);
        let image_height = inner.height.min(12);
        if self.show_logo && image_width > 0 && image_height > 0 {
            let image_area = Rect::new(
                inner.x + inner.width.saturating_sub(image_width + 1),
                inner.y,
                image_width,
                image_height,
            );
            frame.render_widget(TerminalImage::new(&self.image), image_area);
            let caption_y = image_area
                .y
                .saturating_add(image_area.height)
                .saturating_sub(3);
            if caption_y < inner.y.saturating_add(inner.height) {
                frame.render_widget(
                    Paragraph::new("TempleOS logo\nrendered by Kitty\nImage protocol")
                        .style(Style::default().fg(Color::White).italic()),
                    Rect::new(image_area.x, caption_y, image_area.width, 3),
                );
            }
        }

        (
            inner.x.saturating_add(self.cursor_col as u16),
            inner
                .y
                .saturating_add(self.cursor_row as u16)
                .saturating_sub(self.scroll),
        )
    }

    fn render_line(&self, buf: &mut Buffer, inner: Rect, row: usize, view_y: u16) {
        let y = inner.y.saturating_add(view_y);
        let line = &self.lines[row];
        for (index, cell) in line.iter().take(inner.width as usize).enumerate() {
            let x = inner.x.saturating_add(index as u16);
            match cell {
                DocCell::Char(ch) => {
                    if let Some(screen_cell) = buf.cell_mut((x, y)) {
                        screen_cell
                            .set_char(*ch)
                            .set_style(Style::default().fg(Color::White));
                    }
                }
                DocCell::Object(_) => {
                    if let Some(screen_cell) = buf.cell_mut((x, y)) {
                        screen_cell.set_char(' ');
                        if self.debug_cells {
                            screen_cell.set_style(Style::default().bg(Color::Gray));
                        }
                    }
                }
            }
        }
    }

    fn sync_objects(&mut self, buf: &mut Buffer, inner: Rect) {
        let visible_top = self.scroll as usize;
        let visible_bottom = visible_top + inner.height as usize;

        for row in 0..self.lines.len() {
            if row < visible_top || row >= visible_bottom {
                continue;
            }

            let view_y = (row - visible_top) as u16;
            for (col, cell) in self.lines[row].iter().enumerate() {
                let DocCell::Object(index) = cell else {
                    continue;
                };
                let Some(object) = self.objects.get_mut(*index).and_then(Option::as_mut) else {
                    continue;
                };
                let anchor_x = inner.x.saturating_add(col as u16);
                let anchor_y = inner.y.saturating_add(view_y);
                let place = place_at_anchor(
                    &object.graphic,
                    anchor_x,
                    anchor_y,
                    object.width,
                    object.height,
                );

                if !object.visible {
                    emit_sequence(buf, anchor_x, anchor_y, &place);
                    emit_sequence(buf, anchor_x, anchor_y, &object.graphic.register_sequence());
                    object.visible = true;
                } else {
                    emit_sequence(buf, anchor_x, anchor_y, &place);
                }
            }
        }

        let mut keep_visible = vec![false; self.objects.len()];
        for row in visible_top..visible_bottom.min(self.lines.len()) {
            for cell in &self.lines[row] {
                if let DocCell::Object(index) = cell
                    && *index < keep_visible.len()
                {
                    keep_visible[*index] = true;
                }
            }
        }

        for (index, object) in self.objects.iter_mut().enumerate() {
            let Some(object) = object.as_mut() else {
                continue;
            };
            if object.visible && !keep_visible.get(index).copied().unwrap_or(false) {
                emit_sequence(buf, inner.x, inner.y, &object.graphic.delete_sequence());
                object.visible = false;
            }
        }
    }

    fn handle_key(&mut self, key: KeyEvent) {
        match key.code {
            KeyCode::Left => self.move_left(),
            KeyCode::Right => self.move_right(),
            KeyCode::Up => self.move_up(),
            KeyCode::Down => self.move_down(),
            KeyCode::Home => self.cursor_col = 0,
            KeyCode::End => self.cursor_col = self.current_line().len(),
            KeyCode::Enter => self.insert_newline(),
            KeyCode::Backspace => self.backspace(),
            KeyCode::Delete => self.delete(),
            KeyCode::Char('d') if key.modifiers.is_empty() => {
                self.debug_cells = !self.debug_cells;
            }
            KeyCode::Char('l') if key.modifiers == KeyModifiers::CONTROL => {
                self.show_logo = !self.show_logo;
            }
            KeyCode::Char('v') if key.modifiers == KeyModifiers::CONTROL => {
                self.insert_random_object()
            }
            KeyCode::Tab => {
                for _ in 0..4 {
                    self.insert_char(' ');
                }
            }
            KeyCode::Char(ch)
                if key.modifiers.is_empty() || key.modifiers == KeyModifiers::SHIFT =>
            {
                self.insert_char(ch);
            }
            _ => {}
        }

        self.ensure_cursor_in_bounds();
        self.ensure_cursor_visible();
    }

    fn insert_object(&mut self, placement: ObjectPlacement) {
        while self.lines.len() <= placement.row {
            self.lines.push(Vec::new());
        }
        while self.lines[placement.row].len() < placement.col {
            self.lines[placement.row].push(DocCell::Char(' '));
        }

        let index = self.objects.len();
        self.objects.push(Some(PlacedGraphic {
            graphic: RattyGraphic::new(
                RattyGraphicSettings::new(placement.path)
                    .id(placement.id)
                    .scale(placement.scale)
                    .depth(3.0)
                    .color(random_color(placement.id))
                    .brightness(1.0)
                    .animate(placement.animate),
            ),
            width: placement.width,
            height: placement.height,
            visible: false,
        }));
        self.lines[placement.row].insert(placement.col, DocCell::Object(index));
    }

    fn insert_startup_objects(&mut self) {
        let presets = [
            ("sprite_12_offset_32218.obj", 6, 36, 6, 4, 0.75),
            ("black.obj", 17, 12, 12, 7, 1.00),
            ("bomber.obj", 32, 10, 18, 7, 1.00),
            ("battle.obj", 32, 30, 14, 8, 1.00),
            ("sprite_18_offset_48048.obj", 47, 16, 16, 8, 0.95),
        ];

        for (name, row, col, width, height, scale) in presets {
            if self.asset_pool.iter().any(|asset| asset == name) {
                let path = format!("widget/assets/{name}");
                let id = self.next_object_id;
                self.next_object_id += 1;
                self.insert_object(ObjectPlacement {
                    id,
                    path,
                    row,
                    col,
                    width,
                    height,
                    scale,
                    animate: true,
                });
            }
        }
    }

    fn insert_random_object(&mut self) {
        if self.asset_pool.is_empty() {
            return;
        }

        let nanos = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map(|duration| duration.as_nanos() as usize)
            .unwrap_or(0);
        let asset = self.asset_pool[nanos % self.asset_pool.len()].clone();
        let path = format!("widget/assets/{asset}");
        let row = self.cursor_row;
        let col = self.cursor_col;
        let id = self.next_object_id;
        self.next_object_id += 1;
        self.insert_object(ObjectPlacement {
            id,
            path,
            row,
            col,
            width: 16,
            height: 8,
            scale: 1.0,
            animate: true,
        });
        self.cursor_col += 1;
    }

    fn insert_char(&mut self, ch: char) {
        let cursor_row = self.cursor_row;
        let cursor_col = self.cursor_col;
        self.lines[cursor_row].insert(cursor_col, DocCell::Char(ch));
        self.cursor_col += 1;
    }

    fn insert_newline(&mut self) {
        let tail = self.lines[self.cursor_row].split_off(self.cursor_col);
        self.lines.insert(self.cursor_row + 1, tail);
        self.cursor_row += 1;
        self.cursor_col = 0;
    }

    fn backspace(&mut self) {
        if self.cursor_col > 0 {
            self.cursor_col -= 1;
            self.remove_cell(self.cursor_row, self.cursor_col);
        } else if self.cursor_row > 0 {
            let current = self.lines.remove(self.cursor_row);
            self.cursor_row -= 1;
            self.cursor_col = self.lines[self.cursor_row].len();
            self.lines[self.cursor_row].extend(current);
        }
    }

    fn delete(&mut self) {
        if self.cursor_col < self.current_line().len() {
            self.remove_cell(self.cursor_row, self.cursor_col);
        } else if self.cursor_row + 1 < self.lines.len() {
            let next = self.lines.remove(self.cursor_row + 1);
            self.lines[self.cursor_row].extend(next);
        }
    }

    fn remove_cell(&mut self, row: usize, col: usize) {
        if col >= self.lines[row].len() {
            return;
        }
        let removed = self.lines[row].remove(col);
        if let DocCell::Object(index) = removed
            && let Some(object) = self.objects.get_mut(index).and_then(Option::take)
        {
            let _ = object.graphic.clear();
        }
    }

    fn move_left(&mut self) {
        if self.cursor_col > 0 {
            self.cursor_col -= 1;
        } else if self.cursor_row > 0 {
            self.cursor_row -= 1;
            self.cursor_col = self.lines[self.cursor_row].len();
        }
    }

    fn move_right(&mut self) {
        if self.cursor_col < self.current_line().len() {
            self.cursor_col += 1;
        } else if self.cursor_row + 1 < self.lines.len() {
            self.cursor_row += 1;
            self.cursor_col = 0;
        }
    }

    fn move_up(&mut self) {
        if self.cursor_row > 0 {
            self.cursor_row -= 1;
            self.cursor_col = self.cursor_col.min(self.current_line().len());
        }
    }

    fn move_down(&mut self) {
        if self.cursor_row + 1 < self.lines.len() {
            self.cursor_row += 1;
            self.cursor_col = self.cursor_col.min(self.current_line().len());
        }
    }

    fn current_line(&self) -> &Vec<DocCell> {
        &self.lines[self.cursor_row]
    }

    fn ensure_cursor_in_bounds(&mut self) {
        self.cursor_row = self.cursor_row.min(self.lines.len().saturating_sub(1));
        self.cursor_col = self.cursor_col.min(self.current_line().len());
    }

    fn ensure_cursor_visible(&mut self) {
        let cursor_row = self.cursor_row as u16;
        if cursor_row < self.scroll {
            self.scroll = cursor_row;
        } else if cursor_row >= self.scroll.saturating_add(self.viewport_height) {
            self.scroll = cursor_row.saturating_sub(self.viewport_height.saturating_sub(1));
        }
    }

    fn clear(&self) -> io::Result<()> {
        for object in self.objects.iter().flatten() {
            object.graphic.clear()?;
        }
        Ok(())
    }
}

#[derive(Clone)]
enum DocCell {
    Char(char),
    Object(usize),
}

struct PlacedGraphic<'a> {
    graphic: RattyGraphic<'a>,
    width: u16,
    height: u16,
    visible: bool,
}

struct ObjectPlacement {
    id: u32,
    path: String,
    row: usize,
    col: usize,
    width: u16,
    height: u16,
    scale: f32,
    animate: bool,
}

fn initial_lines() -> Vec<Vec<DocCell>> {
    [
        "TempleOS was Terry A. Davis's operating system, compiler, editor,",
        "shell, graphics stack, and personal computing world built around",
        "a deliberately small and unified design.",
        "",
        "> In TempleOS, a program can contain sprite data inline, so art",
        "> assets for a game or demo can live in the same .HC file as",
        "> the code that draws or uses them.",
        "",
        "Ratty is inspired by that direction. The goal here is not only to",
        "render graphics in a terminal window, but to let them belong to the",
        "same editable surface as the text around them.",
        "",
        "This editor keeps 3D objects as first-class document cells.",
        "Move around, type, split lines, delete text, and the objects move",
        "with the surrounding document instead of floating above it.",
        "",
        "",
        "",
        "",
        "",
        "",
        "Above is one inline object placed directly in the document flow.",
        "It is not a detached overlay or preview. It lives in the same",
        "buffer as the lines describing what you are looking at.",
        "",
        "The important idea here is not only that objects can be rendered.",
        "It is that they can belong to the document itself.",
        "",
        "Here is another example. Two extracted TempleOS assets can sit",
        "side by side inside the same editable buffer, still anchored to",
        "cells, still moving when the surrounding text changes.",
        "",
        "",
        "",
        "",
        "The assets in this demo were extracted from TempleOS itself and",
        "dropped into the same editable surface as the text that describes them.",
        "",
        "That makes the command line feel less like a scrolling log and",
        "more like a document or notebook where code, notes, and graphics",
        "share one continuous space.",
        "",
        "Now try it yourself. Press Ctrl+V to insert a random TempleOS",
        "object anywhere in this document, then edit around it and watch",
        "it stay attached to the document instead of the screen.",
        "",
        "",
        "",
        "",
        "",
        "-----",
        "",
        "What's reality?",
        "I don't know. When my bird was looking at my computer monitor I",
        "thought, \"That bird has no idea what he's looking at.\" And yet",
        "what does the bird do? Does he panic? No, he can't really panic,",
        "he just does the best he can. Is he able to live in a world where",
        "he's so ignorant? Well, he doesn't really have a choice. The bird",
        "is okay even though he doesn't understand the world. You're that",
        "bird looking at the monitor, and you're thinking to yourself,",
        "\"I can figure this out.\" Maybe you have some bird ideas.",
        "",
        "Maybe that's the best you can do.",
        "",
        "— Terry Davis",
        "",
        "EOF",
    ]
    .into_iter()
    .map(|line| line.chars().map(DocCell::Char).collect())
    .collect()
}

fn emit_sequence(buf: &mut Buffer, x: u16, y: u16, sequence: &str) {
    let Some(cell) = buf.cell_mut((x, y)) else {
        return;
    };
    let existing = cell.symbol();
    let mut symbol = String::with_capacity(sequence.len() + existing.len());
    symbol.push_str(sequence);
    symbol.push_str(existing);
    cell.set_symbol(&symbol);
}

fn place_at_anchor(
    graphic: &RattyGraphic<'_>,
    anchor_x: u16,
    anchor_y: u16,
    width: u16,
    height: u16,
) -> String {
    let settings = graphic.settings();
    format!(
        "\x1b_ratty;g;p;id={};row={};col={};w={};h={};animate={};scale={};depth={};color={};brightness={};px={};py={};pz={};rx={};ry={};rz={};sx={};sy={};sz={}\x1b\\",
        settings.id,
        anchor_y,
        anchor_x,
        width.max(1),
        height.max(1),
        u8::from(settings.animate),
        settings.scale,
        settings.depth,
        settings
            .color
            .map(|[r, g, b]| format!("{r:02x}{g:02x}{b:02x}"))
            .unwrap_or_else(|| "ffffff".to_string()),
        settings.brightness,
        settings.offset[0],
        settings.offset[1],
        settings.offset[2],
        settings.rotation[0],
        settings.rotation[1],
        settings.rotation[2],
        settings.scale3[0],
        settings.scale3[1],
        settings.scale3[2],
    )
}

fn discover_obj_assets() -> io::Result<Vec<String>> {
    let assets_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("assets");
    let mut assets = fs::read_dir(assets_dir)?
        .filter_map(|entry| {
            let entry = entry.ok()?;
            let path = entry.path();
            (path.extension().and_then(|ext| ext.to_str()) == Some("obj"))
                .then(|| path.file_name()?.to_str().map(ToOwned::to_owned))
                .flatten()
        })
        .collect::<Vec<_>>();
    assets.sort();
    Ok(assets)
}

fn random_color(seed: u32) -> [u8; 3] {
    const PALETTE: [[u8; 3]; 8] = [
        [255, 111, 97],
        [255, 179, 71],
        [255, 241, 118],
        [129, 199, 132],
        [79, 195, 247],
        [126, 87, 194],
        [244, 143, 177],
        [144, 202, 249],
    ];
    let mut seed = seed;
    seed ^= seed << 13;
    seed ^= seed >> 17;
    seed ^= seed << 5;
    PALETTE[(seed as usize) % PALETTE.len()]
}