| 1 | package Term::Fabulous::Terminal::Termbox::Cells; | ||
| 2 | |||
| 3 | use v5.32; | ||
| 4 | use warnings; | ||
| 5 | use feature 'signatures'; | ||
| 6 | no warnings 'experimental::signatures'; | ||
| 7 | |||
| 8 | our $VERSION = '0.05'; | ||
| 9 | |||
| 10 | use Object::Pad 0.825; | ||
| 11 | |||
| 12 | use Term::Fabulous::Render::Target::Mask; | ||
| 13 | use Term::Fabulous::Render::Target::Sixel; | ||
| 14 | |||
| 15 | class Term::Fabulous::Terminal::Termbox::Cells | ||
| 16 | :does(Term::Fabulous::Render::Target::Mask) | ||
| 17 | :does(Term::Fabulous::Render::Target::Sixel) | ||
| 18 | :strict(params) | ||
| 19 | { | ||
| 20 | use List::Util qw(any min); | ||
| 21 | use Term::Fabulous::Termbox qw( | ||
| 22 | tb_clear tb_present tb_set_cell tb_extend_cell tb_get_cell tb_print tb_width tb_height tb_send tf_flush | ||
| 23 | tf_cells_differ tf_invalidate_cells TB_DEFAULT TB_OK | ||
| 24 | ); | ||
| 25 | use Term::Fabulous::Render::Geometry qw(row_spans_outside); | ||
| 26 | |||
| 27 | # In inline mode, the terminal row of row 0 and the rows of the region. | ||
| 28 | field $region_top :reader = undef; | ||
| 29 | field $region_rows :reader = undef; | ||
| 30 | |||
| 31 | # The pixels of a cell while the terminal shows sixel, else empty; the | ||
| 32 | # pictures of the frame being painted, and those on the screen. | ||
| 33 | field @_cell_size; | ||
| 34 | field @_sixels_due; | ||
| 35 | field @_sixels_shown; | ||
| 36 | |||
| 37 | method place_region ( $top, $rows ) { | ||
| 38 | die "Term::Fabulous::Terminal::Termbox::Cells: place_region needs both a top row and a number of rows, or neither" | ||
| 39 | if defined $top != defined $rows; | ||
| 40 | ( $region_top, $region_rows ) = ( $top, $rows ); | ||
| 41 | return; | ||
| 42 | } | ||
| 43 | |||
| 44 | # termbox2 keeps its back buffer between frames, so the cells of kept | ||
| 45 | # rects still hold what the previous frame painted there. | ||
| 46 | method clear_cells (@kept_rects) { | ||
| 47 | if ( !@kept_rects ) { | ||
| 48 | tb_clear(); | ||
| 49 | return; | ||
| 50 | } | ||
| 51 | my $width = tb_width(); | ||
| 52 | my $rows = $region_rows // tb_height(); | ||
| 53 | foreach my $y ( 0 .. $rows - 1 ) { | ||
| 54 | foreach my $span ( row_spans_outside( $y, 0, $width, @kept_rects ) ) { | ||
| 55 | my ( $from, $to ) = @$span; | ||
| 56 | tb_print( $from, $self->_terminal_row($y), TB_DEFAULT, TB_DEFAULT, ' ' x ( $to - $from ) ); | ||
| 57 | } | ||
| 58 | } | ||
| 59 | return; | ||
| 60 | } | ||
| 61 | |||
| 62 | method set_sixel_cell_size (@size) { | ||
| 63 | die "Term::Fabulous::Terminal::Termbox::Cells: set_sixel_cell_size needs a width and a height of at least 1 pixel, or nothing" | ||
| 64 | unless !@size || ( @size == 2 && !grep { !defined || !/\A[1-9][0-9]*\z/ } @size ); | ||
| 65 | @_cell_size = @size; | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | method sixel_cell_size () { | ||
| 70 | return @_cell_size; | ||
| 71 | } | ||
| 72 | |||
| 73 | # A picture that reaches the terminal's last row makes the terminal | ||
| 74 | # scroll the screen up, as the cursor moves below the picture. | ||
| 75 | method sixel_area ( $width, $height ) { | ||
| 76 | my $last_row = tb_height() - 1 - ( $region_top // 0 ); | ||
| 77 | return [ 0, 0, $width, min( $height, $last_row ) ]; | ||
| 78 | } | ||
| 79 | |||
| 80 | method show_sixels (@placements) { | ||
| 81 | @_sixels_due = @placements; | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | |||
| 85 | # The terminal forgets the pictures when termbox2 starts again. | ||
| 86 | method forget_sixels () { | ||
| 87 | ( @_sixels_due, @_sixels_shown ) = (); | ||
| 88 | return; | ||
| 89 | } | ||
| 90 | |||
| 91 | # termbox2 sends only the cells that changed, so a picture that is | ||
| 92 | # gone gets the cells below it drawn again, which erases it. A picture | ||
| 93 | # is sent after the cells, again whenever a cell below it changed. | ||
| 94 | # In inline mode the hidden cursor waits at the start of the region's | ||
| 95 | # first row: a terminal that rewraps its lines on a resize moves it | ||
| 96 | # along, so asking for it finds the region again. | ||
| 97 | method present_cells () { | ||
| 98 | my @gone = grep { | ||
| 99 | my $shown = $_; | ||
| 100 | !any { _same_sixel( $shown, $_ ) } @_sixels_due | ||
| 101 | } @_sixels_shown; | ||
| 102 | $self->_invalidate_sixel($_) foreach @gone; | ||
| 103 | my @new = grep { | ||
| 104 | my $due = $_; | ||
| 105 | !( any { _same_sixel( $due, $_ ) } @_sixels_shown ) || $self->_sixel_cells_differ($due) | ||
| 106 | } @_sixels_due; | ||
| 107 | @_sixels_shown = @_sixels_due; | ||
| 108 | @_sixels_due = (); | ||
| 109 | |||
| 110 | tb_present(); | ||
| 111 | tb_send( sprintf( "\e[%d;%dH", $self->_terminal_row( $_->{y} ) + 1, $_->{x} + 1 ) . $_->{data} ) foreach @new; | ||
| 112 | tb_send( "\e[" . ( $region_top + 1 ) . ";1H" ) if defined $region_top; | ||
| 113 | tf_flush() if @new || defined $region_top; | ||
| 114 | return; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub _same_sixel ( $one, $other ) { | ||
| 118 | return $one->{x} == $other->{x} && $one->{y} == $other->{y} && $one->{columns} == $other->{columns} && $one->{rows} == $other->{rows} && $one->{data} eq $other->{data}; | ||
| 119 | } | ||
| 120 | |||
| 121 | method _invalidate_sixel ($placement) { | ||
| 122 | tf_invalidate_cells( $placement->{x}, $self->_terminal_row( $placement->{y} ), $placement->{columns}, $placement->{rows} ); | ||
| 123 | return; | ||
| 124 | } | ||
| 125 | |||
| 126 | method _sixel_cells_differ ($placement) { | ||
| 127 | my $rc = tf_cells_differ( $placement->{x}, $self->_terminal_row( $placement->{y} ), $placement->{columns}, $placement->{rows}, \my $differ ); | ||
| 128 | die "Term::Fabulous::Terminal::Termbox::Cells: tf_cells_differ failed (status $rc)" unless $rc == TB_OK; | ||
| 129 | return $differ; | ||
| 130 | } | ||
| 131 | |||
| 132 | method put_cell ( $x, $y, $glyph, $fg, $bg ) { | ||
| 133 | tb_set_cell( $x, $self->_terminal_row($y), $glyph, $fg, $bg ); | ||
| 134 | return; | ||
| 135 | } | ||
| 136 | |||
| 137 | method put_extension ( $x, $y, $character ) { | ||
| 138 | tb_extend_cell( $x, $self->_terminal_row($y), $character ); | ||
| 139 | return; | ||
| 140 | } | ||
| 141 | |||
| 142 | method put_row ( $x, $y, $columns, $bg ) { | ||
| 143 | tb_print( $x, $self->_terminal_row($y), TB_DEFAULT, $bg, ' ' x $columns ); | ||
| 144 | return; | ||
| 145 | } | ||
| 146 | |||
| 147 | # A cleared cell holds a space, so the back buffer always has a glyph. | ||
| 148 | method painted_cell ( $x, $y ) { | ||
| 149 | my ( $status, $glyph, $fg, $bg ) = tb_get_cell( $x, $self->_terminal_row($y), 1 ); | ||
| 150 | return () unless $status == TB_OK; | ||
| 151 | return ( $glyph, $fg, $bg ); | ||
| 152 | } | ||
| 153 | |||
| 154 | method _terminal_row ($y) { | ||
| 155 | return $y + ( $region_top // 0 ); | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | 1; | ||
| 160 | |||
| 161 | __END__ |