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
//! Terminal rendering logic
use std::collections::HashMap;

use crate::{
    decoder::Decoder, error::Error, Face, FaceAttrs, Glyph, Image, Position, Surface, SurfaceMut,
    SurfaceMutIter, SurfaceMutView, SurfaceOwned, Terminal, TerminalCommand, TerminalSize, RGBA,
};

/// Terminal cell kind
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum CellKind {
    /// Contains usefull content
    Content,
    /// Must be skiped during rendering
    Ignore,
    /// Must be re-rendered
    Damaged,
}

/// Terminal cell
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Cell {
    face: Face,
    character: Option<char>,
    image: Option<Image>,
    glyph: Option<Glyph>,
    kind: CellKind,
}

impl Cell {
    /// Create new cell from face and char
    pub fn new(face: Face, character: Option<char>) -> Self {
        Self {
            face,
            character,
            image: None,
            glyph: None,
            kind: CellKind::Content,
        }
    }

    /// Create new cell from image
    pub fn new_image(image: Image) -> Self {
        Self {
            face: Default::default(),
            character: None,
            image: Some(image),
            glyph: None,
            kind: CellKind::Content,
        }
    }

    /// Create new cell from glyph
    pub fn new_glyph(face: Face, glyph: Glyph) -> Self {
        Self {
            face,
            character: None,
            image: None,
            glyph: Some(glyph),
            kind: CellKind::Content,
        }
    }

    /// Create damaged cell
    fn new_damaged() -> Self {
        Self {
            face: Default::default(),
            character: None,
            image: None,
            glyph: None,
            kind: CellKind::Damaged,
        }
    }
}

impl Default for Cell {
    fn default() -> Self {
        Self {
            face: Default::default(),
            character: None,
            image: None,
            glyph: None,
            kind: CellKind::Content,
        }
    }
}

pub type TerminalSurface<'a> = SurfaceMutView<'a, Cell>;

/// Terminal renderer
///
/// This object keeps two surfaces (front and back) and on each call to frame
/// generates necessary terminal commands to reconcile them.
pub struct TerminalRenderer {
    /// Current face
    face: Face,
    /// Current cursor position
    cursor: Position,
    /// Front surface (modified)
    front: SurfaceOwned<Cell>,
    /// Back surface (keeping previous state)
    back: SurfaceOwned<Cell>,
    /// Current terminal size
    size: TerminalSize,
    /// Cache of rendered glyphs
    glyph_cache: HashMap<Glyph, Image>,
}

impl TerminalRenderer {
    /// Create new terminal renderer
    pub fn new<T: Terminal + ?Sized>(term: &mut T, clear: bool) -> Result<Self, Error> {
        let size = term.size()?;
        term.execute(TerminalCommand::Face(Default::default()))?;
        term.execute(TerminalCommand::CursorTo(Position::new(0, 0)))?;
        let mut back = SurfaceOwned::new(size.cells.height, size.cells.width);
        if clear {
            back.fill(Cell::new_damaged());
        }
        Ok(Self {
            face: Default::default(),
            cursor: Position::new(0, 0),
            front: SurfaceOwned::new(size.cells.height, size.cells.width),
            back,
            size,
            glyph_cache: HashMap::new(),
        })
    }

    /// Clear terminal
    pub fn clear<T: Terminal + ?Sized>(&mut self, term: &mut T) -> Result<(), Error> {
        // erase all images
        for cell in self.back.iter() {
            if let Some(img) = &cell.image {
                term.execute(TerminalCommand::ImageErase(img.clone(), None))?;
            }
        }

        self.face = Face::default().with_fg(Some(RGBA::new(254, 0, 253, 252)));
        self.cursor = Position::new(100_000, 100_000);
        self.front.fill(Cell::new_damaged());
        self.back.fill(Cell::new_damaged());

        Ok(())
    }

    /// View associated with the current frame
    pub fn view(&mut self) -> TerminalSurface<'_> {
        self.front.view_mut(.., ..)
    }

    /// Render the current frame
    pub fn frame<T: Terminal + ?Sized>(&mut self, term: &mut T) -> Result<(), Error> {
        // Rasterize all glyphs
        self.glyphs_reasterize(term.size()?);

        // Images can overlap and newly rendered image might be erased by erase command
        // addressed to images of the previous frame. That is why we are erasing all images
        // of the previous frame before rendering new images.
        for row in 0..self.back.height() {
            for col in 0..self.back.width() {
                let (front, back) = match (self.front.get(row, col), self.back.get(row, col)) {
                    (Some(front), Some(back)) => (front, back),
                    _ => break,
                };
                if front.image != back.image {
                    if let Some(img) = &back.image {
                        term.execute(TerminalCommand::ImageErase(
                            img.clone(),
                            Some(Position::new(row, col)),
                        ))?;
                    }
                }
                // mark all cells effected by the image as dameged
                if let Some(size) = front.image.as_ref().map(|img| img.size_cells(self.size)) {
                    let mut view = self
                        .front
                        .view_mut(row..row + size.height, col..col + size.width);
                    for cell in view.iter_mut() {
                        cell.kind = CellKind::Damaged;
                    }
                }
            }
        }

        for row in 0..self.back.height() {
            let mut col = 0;
            while col < self.back.width() {
                let (front, back) = match (self.front.get(row, col), self.back.get(row, col)) {
                    (Some(front), Some(back)) => (front, back),
                    _ => break,
                };
                if front.kind == CellKind::Ignore || front == back {
                    col += 1;
                    continue;
                }
                // update face
                if front.face != self.face {
                    term.execute(TerminalCommand::Face(front.face))?;
                    self.face = front.face;
                }
                // update position
                if self.cursor.row != row || self.cursor.col != col {
                    self.cursor.row = row;
                    self.cursor.col = col;
                    term.execute(TerminalCommand::CursorTo(self.cursor))?;
                }
                // handle image
                if let Some(image) = front.image.clone() {
                    let image_changed = front.image != back.image;
                    // make sure surface under image is not changed
                    let size = image.size_cells(self.size);
                    let mut view = self
                        .front
                        .view_mut(row..row + size.height, col..col + size.width);
                    for cell in view.iter_mut() {
                        cell.kind = CellKind::Ignore;
                    }
                    // render image if changed
                    if image_changed {
                        // issure render command
                        term.execute(TerminalCommand::Image(image, Position::new(row, col)))?;
                        // set position large enough so it would tirgger position update
                        self.cursor = Position::new(100000, 1000000);
                    }
                    col += 1;
                    continue;
                }
                // identify glyph
                let glyph = front.character.unwrap_or(' ');
                // find if it is possible to erase instead of using ' '
                if glyph == ' ' {
                    let repeats = self.find_repeats(row, col);
                    col += repeats;
                    if repeats > 4 {
                        // NOTE:
                        //   - only use erase command when it is more efficient
                        //     this value choosen arbirtraraly
                        //   - erase is not moving cursor
                        term.execute(TerminalCommand::EraseChars(repeats))?;
                    } else {
                        self.cursor.col += repeats;
                        for _ in 0..repeats {
                            term.execute(TerminalCommand::Char(glyph))?;
                        }
                    }
                } else {
                    term.execute(TerminalCommand::Char(glyph))?;
                    self.cursor.col += 1;
                    col += 1;
                }
            }
        }
        // swap buffers
        std::mem::swap(&mut self.front, &mut self.back);
        self.front.clear();
        Ok(())
    }

    /// Rasterize all glyphs in the front surface
    ///
    /// All glyphs are replaced with rasterized image
    fn glyphs_reasterize(&mut self, term_size: TerminalSize) {
        for cell in self.front.iter_mut() {
            if let Some(glyph) = &cell.glyph {
                let image = match self.glyph_cache.get(glyph) {
                    Some(image) => image.clone(),
                    None => {
                        let image = glyph.rasterize(cell.face, term_size);
                        self.glyph_cache.insert(glyph.clone(), image.clone());
                        image
                    }
                };
                cell.image = Some(image);
            }
        }
    }

    /// Find how many identical cells is located starting from provided coordinate
    fn find_repeats(&self, row: usize, col: usize) -> usize {
        let first = self.front.get(row, col);
        if first.is_none() {
            return 0;
        }
        let mut repeats = 1;
        loop {
            let src = self.front.get(row, col + repeats);
            let dst = self.back.get(row, col + repeats);
            if first == src && src != dst {
                repeats += 1;
            } else {
                break;
            }
        }
        repeats
    }
}

/// Terminal surface extention trait
pub trait TerminalSurfaceExt: SurfaceMut<Item = Cell> {
    /// Draw box
    fn draw_box(&mut self, face: Option<Face>);
    /// Draw image encoded as ascii blocks
    fn draw_image_ascii(&mut self, img: impl Surface<Item = RGBA>);
    /// Draw image
    fn draw_image(&mut self, img: Image);
    /// Erase surface with provided color
    fn erase(&mut self, color: Option<RGBA>);
    /// Write object that can be used to add text to the surface
    fn writer(&mut self) -> TerminalWriter<'_>;
}

impl<S> TerminalSurfaceExt for S
where
    S: SurfaceMut<Item = Cell>,
{
    fn draw_box(&mut self, face: Option<Face>) {
        if self.width() < 2 || self.height() < 2 {
            return;
        }
        let face = face.unwrap_or_default();

        let h = Cell::new(face, Some('─'));
        let v = Cell::new(face, Some('│'));
        self.view_mut(0, 1..-1).fill(h.clone());
        self.view_mut(-1, 1..-1).fill(h);
        self.view_mut(1..-1, 0).fill(v.clone());
        self.view_mut(1..-1, -1).fill(v);

        self.view_mut(0, 0).fill(Cell::new(face, Some('┌')));
        self.view_mut(0, -1).fill(Cell::new(face, Some('┐')));
        self.view_mut(-1, -1).fill(Cell::new(face, Some('┘')));
        self.view_mut(-1, 0).fill(Cell::new(face, Some('└')));
    }

    // draw image using unicode uppper half block symbol \u{2580}
    fn draw_image_ascii(&mut self, img: impl Surface<Item = RGBA>) {
        let height = img.height() / 2 + img.height() % 2;
        let width = img.width();
        self.view_mut(..height, ..width).fill_with(|row, col, _| {
            let fg = img.get(row * 2, col).copied();
            let bg = img.get(row * 2 + 1, col).copied();
            let face = Face::new(fg, bg, FaceAttrs::EMPTY);
            Cell::new(face, Some('\u{2580}'))
        });
    }

    fn draw_image(&mut self, img: Image) {
        if let Some(cell) = self.get_mut(0, 0) {
            *cell = Cell::new_image(img);
        }
    }

    fn erase(&mut self, color: Option<RGBA>) {
        let face = Face::default().with_bg(color);
        self.fill_with(|_, _, _| Cell::new(face, None));
    }

    fn writer(&mut self) -> TerminalWriter<'_> {
        TerminalWriter::new(self)
    }
}

pub trait TerminalWritable {
    fn fmt(&self, writer: &mut TerminalWriter<'_>) -> std::io::Result<()>;

    /// Estimate height occupied given with of the available surface
    fn height_hint(&self, _width: usize) -> Option<usize> {
        None
    }
}

impl<'a, T> TerminalWritable for &'a T
where
    T: TerminalWritable + ?Sized,
{
    fn fmt(&self, writer: &mut TerminalWriter<'_>) -> std::io::Result<()> {
        (*self).fmt(writer)
    }

    fn height_hint(&self, width: usize) -> Option<usize> {
        (*self).height_hint(width)
    }
}

pub struct TerminalWriter<'a> {
    face: Face,
    iter: SurfaceMutIter<'a, Cell>,
    decoder: crate::decoder::Utf8Decoder,
}

impl<'a> TerminalWriter<'a> {
    pub fn new<S>(surf: &'a mut S) -> Self
    where
        S: SurfaceMut<Item = Cell> + ?Sized,
    {
        Self {
            face: Default::default(),
            iter: surf.iter_mut(),
            decoder: crate::decoder::Utf8Decoder::new(),
        }
    }

    /// Create new surface with updated face
    pub fn face(self, face: Face) -> Self {
        Self { face, ..self }
    }

    /// Set current face
    pub fn face_set(&mut self, face: Face) {
        self.face = face;
    }

    /// Skip offset ammount of cells (row major order)
    pub fn skip(mut self, offset: usize) -> Self {
        if offset > 0 {
            self.iter.nth(offset - 1);
        }
        self
    }

    /// Get current position
    pub fn position(&self) -> (usize, usize) {
        self.iter.position()
    }

    pub fn display(&mut self, value: impl TerminalWritable) -> std::io::Result<()> {
        value.fmt(self)
    }

    /// Put cell
    pub fn put(&mut self, cell: Cell) -> bool {
        match self.iter.next() {
            Some(cell_ref) => {
                *cell_ref = cell;
                true
            }
            None => false,
        }
    }

    /// Put char
    pub fn put_char(&mut self, c: char, face: Face) -> bool {
        match c {
            '\r' => true,
            '\n' => {
                let index = self.iter.index();
                let shape = self.iter.shape();
                let (row, col) = self.position();
                if col != 0 {
                    let offset = shape.index(row, shape.width - 1) - index;
                    self.iter.nth(offset);
                }
                true
            }
            glyph => match self.iter.next() {
                Some(cell) => {
                    let face = cell.face.overlay(&face);
                    *cell = Cell::new(face, Some(glyph));
                    true
                }
                None => false,
            },
        }
    }
}

impl<'a> std::io::Write for TerminalWriter<'a> {
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        let mut cur = std::io::Cursor::new(buf);
        while let Some(glyph) = self.decoder.decode(&mut cur)? {
            if !self.put_char(glyph, self.face) {
                return Ok(buf.len());
            }
        }
        Ok(cur.position() as usize)
    }

    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        encoder::{Encoder, TTYEncoder},
        terminal::{Size, TerminalEvent, TerminalSize, TerminalWaker},
        TerminalCaps,
    };
    use std::io::Write;

    fn debug(view: impl Surface<Item = Cell>) -> Result<String, Error> {
        let mut encoder = TTYEncoder::default();
        let mut out = Vec::new();
        write!(&mut out, "\n┌")?;
        for _ in 0..view.width() {
            write!(&mut out, "─")?;
        }
        writeln!(&mut out, "┐")?;
        for row in 0..view.height() {
            write!(&mut out, "│")?;
            for col in 0..view.width() {
                match view.get(row, col) {
                    None => break,
                    Some(cell) => {
                        encoder.encode(&mut out, TerminalCommand::Face(cell.face))?;
                        write!(&mut out, "{}", cell.character.unwrap_or(' '))?;
                    }
                }
            }
            writeln!(&mut out, "\x1b[m│")?;
        }
        write!(&mut out, "└")?;
        for _ in 0..view.width() {
            write!(&mut out, "─")?;
        }
        write!(&mut out, "┘")?;
        Ok(String::from_utf8_lossy(&out).to_string())
    }

    struct DummyTerminal {
        size: TerminalSize,
        cmds: Vec<TerminalCommand>,
        buffer: Vec<u8>,
        capabiliets: TerminalCaps,
    }

    impl DummyTerminal {
        fn new(height: usize, width: usize) -> Self {
            Self {
                size: TerminalSize {
                    cells: Size { height, width },
                    pixels: Size {
                        height: 0,
                        width: 0,
                    },
                },
                cmds: Default::default(),
                buffer: Default::default(),
                capabiliets: TerminalCaps::default(),
            }
        }

        fn clear(&mut self) {
            self.cmds.clear();
            self.buffer.clear();
        }
    }

    impl Write for DummyTerminal {
        fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
            self.buffer.write(buf)
        }

        fn flush(&mut self) -> std::io::Result<()> {
            Ok(())
        }
    }

    impl Terminal for DummyTerminal {
        fn execute(&mut self, cmd: TerminalCommand) -> Result<(), Error> {
            self.cmds.push(cmd);
            Ok(())
        }

        fn poll(
            &mut self,
            _timeout: Option<std::time::Duration>,
        ) -> Result<Option<TerminalEvent>, Error> {
            Ok(None)
        }

        fn size(&self) -> Result<TerminalSize, Error> {
            Ok(self.size)
        }

        fn waker(&self) -> TerminalWaker {
            TerminalWaker::new(|| Ok(()))
        }

        fn frames_pending(&self) -> usize {
            0
        }

        fn frames_drop(&mut self) {}

        fn dyn_ref(&mut self) -> &mut dyn Terminal {
            self
        }

        fn capabilities(&self) -> &TerminalCaps {
            &self.capabiliets
        }
    }

    #[test]
    fn test_render() -> Result<(), Error> {
        use TerminalCommand::*;

        let purple = "bg=#d3869b".parse()?;
        let red = "bg=#fb4934".parse()?;

        let mut term = DummyTerminal::new(3, 7);
        let mut render = TerminalRenderer::new(&mut term, false)?;

        let mut view = render.view().view_owned(.., 1..);
        let mut writer = view.writer().skip(4).face(purple);
        write!(writer, "TEST")?;
        println!("writer with offset: {}", debug(render.view())?);
        render.frame(&mut term)?;
        assert_eq!(
            term.cmds,
            vec![
                Face(Default::default()),
                CursorTo(Position::new(0, 0)),
                Face(purple),
                CursorTo(Position::new(0, 5)),
                Char('T'),
                Char('E'),
                CursorTo(Position::new(1, 1)),
                Char('S'),
                Char('T'),
            ]
        );
        term.clear();

        render
            .view()
            .view_owned(1..2, 1..-1)
            .fill(Cell::new(red, None));
        println!("erase:{}", debug(render.view())?);
        render.frame(&mut term)?;
        assert_eq!(
            term.cmds,
            vec![
                Face(Default::default()),
                // erase is not used as we only need to remove two spaces
                CursorTo(Position::new(0, 5)),
                Char(' '),
                Char(' '),
                // erase is used
                Face(red),
                CursorTo(Position::new(1, 1)),
                EraseChars(5)
            ]
        );
        term.clear();

        let mut img = SurfaceOwned::new(3, 3);
        let purple_color = "#d3869b".parse()?;
        let green_color = "#b8bb26".parse()?;
        img.fill_with(|r, c, _| {
            if (r + c) % 2 == 0 {
                purple_color
            } else {
                green_color
            }
        });
        render.view().view_mut(1.., 2..).draw_image_ascii(&img);
        println!("ascii image: {}", debug(render.view())?);
        render.frame(&mut term)?;
        assert_eq!(
            term.cmds,
            vec![
                Face(Default::default()),
                Char(' '),
                Face("fg=#d3869b, bg=#b8bb26".parse()?),
                Char('▀'),
                Face("fg=#b8bb26, bg=#d3869b".parse()?),
                Char('▀'),
                Face("fg=#d3869b, bg=#b8bb26".parse()?),
                Char('▀'),
                Face(Default::default()),
                Char(' '),
                Face("fg=#d3869b".parse()?),
                CursorTo(Position { row: 2, col: 2 }),
                Char('▀'),
                Face("fg=#b8bb26".parse()?),
                Char('▀'),
                Face("fg=#d3869b".parse()?),
                Char('▀')
            ]
        );
        term.clear();

        let mut render = TerminalRenderer::new(&mut term, false)?;
        let mut view = render.view().view_owned(.., 1..-1);
        let mut writer = view.writer().face(purple);
        write!(&mut writer, "one\ntwo")?;
        println!("writer new line:{}", debug(render.view())?);
        render.frame(&mut term)?;
        assert_eq!(
            term.cmds,
            vec![
                Face(Default::default()),
                CursorTo(Position::new(0, 0)),
                Face(purple),
                CursorTo(Position::new(0, 1)),
                Char('o'),
                Char('n'),
                Char('e'),
                CursorTo(Position::new(1, 1)),
                Char('t'),
                Char('w'),
                Char('o')
            ]
        );
        term.clear();

        let mut render = TerminalRenderer::new(&mut term, false)?;
        let mut view = render.view().view_owned(.., 1..-1);
        let mut writer = view.writer().face(purple);
        write!(&mut writer, "  one\nx")?;
        println!("writer new line:{}", debug(render.view())?);
        render.frame(&mut term)?;
        assert_eq!(
            term.cmds,
            vec![
                Face(Default::default()),
                CursorTo(Position::new(0, 0)),
                Face(purple),
                CursorTo(Position::new(0, 1)),
                Char(' '),
                Char(' '),
                Char('o'),
                Char('n'),
                Char('e'),
                CursorTo(Position::new(1, 1)),
                Char('x'),
            ]
        );
        term.clear();

        Ok(())
    }
}