| 1 | package Term::Caca; | ||
| 2 | our $AUTHORITY = 'cpan:YANICK'; | ||
| 3 | #ABSTRACT: perl interface for libcaca (Colour AsCii Art library) | ||
| 4 | $Term::Caca::VERSION = '3.1.0'; | ||
| 5 | 8 | ▶ | use 5.20.0; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | 8 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | |||
| 9 | 8 | ▶ | use Carp; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 8 | ▶ | use List::MoreUtils qw/ uniq /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 8 | ▶ | use List::Util qw/ pairmap /; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | |||
| 13 | 8 | ▶ | use FFI::Platypus::Memory; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 14 | 0 | ▶ | use Term::Caca::FFI ':all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 15 | use Term::Caca::Constants qw/ :colors :events /; | ||
| 16 | |||
| 17 | use Term::Caca::Event::Key::Press; | ||
| 18 | use Term::Caca::Event::Key::Release; | ||
| 19 | use Term::Caca::Event::Mouse::Motion; | ||
| 20 | use Term::Caca::Event::Mouse::Button::Press; | ||
| 21 | use Term::Caca::Event::Mouse::Button::Release; | ||
| 22 | use Term::Caca::Event::Resize; | ||
| 23 | use Term::Caca::Event::Quit; | ||
| 24 | |||
| 25 | |||
| 26 | use MooseX::MungeHas { | ||
| 27 | has_ro => [ 'is_ro' ], | ||
| 28 | has_rw => [ 'is_rw' ], | ||
| 29 | }; | ||
| 30 | |||
| 31 | use experimental qw/ | ||
| 32 | signatures | ||
| 33 | postderef | ||
| 34 | /; | ||
| 35 | |||
| 36 | |||
| 37 | sub driver_list { | ||
| 38 | +{ caca_get_display_driver_list()->@* } | ||
| 39 | } | ||
| 40 | |||
| 41 | sub drivers { | ||
| 42 | keys driver_list()->%*; | ||
| 43 | } | ||
| 44 | |||
| 45 | has_ro driver => | ||
| 46 | predicate => 1; | ||
| 47 | |||
| 48 | has_ro display => | ||
| 49 | predicate => 1, | ||
| 50 | lazy => 1, | ||
| 51 | default => sub($self) { | ||
| 52 | ( $self->has_driver | ||
| 53 | ? caca_create_display_with_driver(undef,$self->driver) | ||
| 54 | : caca_create_display(undef) ) or croak "couldn't create display"; | ||
| 55 | }; | ||
| 56 | |||
| 57 | has_ro canvas => sub($self) { caca_get_canvas($self->display) }; | ||
| 58 | |||
| 59 | has_rw title => ( | ||
| 60 | trigger => sub($self,$title) { | ||
| 61 | caca_set_display_title($self->display, $title); | ||
| 62 | } | ||
| 63 | ); | ||
| 64 | |||
| 65 | has_rw refresh_delay => ( | ||
| 66 | trigger => sub($self,$seconds) { | ||
| 67 | caca_set_display_time($self->display,int( $seconds * 1_000_000 )); | ||
| 68 | } | ||
| 69 | ); | ||
| 70 | |||
| 71 | around [qw( title refresh_delay )] => sub ($orig, $self, @rest) { | ||
| 72 | $self->$orig(@rest); | ||
| 73 | return $self; | ||
| 74 | }; | ||
| 75 | |||
| 76 | sub refresh ($self) { | ||
| 77 | caca_refresh_display($self->display); | ||
| 78 | return $self | ||
| 79 | } | ||
| 80 | |||
| 81 | sub rendering_time($self) { | ||
| 82 | return caca_get_display_time($self->display)/1_000_000; | ||
| 83 | } | ||
| 84 | |||
| 85 | # TODO fix the colors when they are a constant | ||
| 86 | sub set_color( $self, $foreground, $background ) { | ||
| 87 | |||
| 88 | if( ref $foreground or 4 == length $foreground ) { | ||
| 89 | for( $foreground, $background ) { | ||
| 90 | $_ = $self->_arg_to_color($_); | ||
| 91 | } | ||
| 92 | caca_set_color_argb($self->canvas, $foreground, $background ); | ||
| 93 | } | ||
| 94 | else { | ||
| 95 | caca_set_color_ansi($self->canvas, $foreground, $background ); | ||
| 96 | } | ||
| 97 | |||
| 98 | return $self; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub _arg_to_color($self,$arg) { | ||
| 102 | |||
| 103 | return hex $arg unless ref $arg; | ||
| 104 | |||
| 105 | return hex sprintf "%x%x%x%x", @$arg; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub char ( $self, $coord, $char = undef ) { | ||
| 109 | if( defined $char ) { | ||
| 110 | caca_put_char( $self->canvas, @$coord, ord $char ); | ||
| 111 | } | ||
| 112 | return $self; | ||
| 113 | } | ||
| 114 | |||
| 115 | sub mouse_position($self) { | ||
| 116 | [ caca_get_mouse_x( $self->display ), caca_get_mouse_y( $self->display ) ]; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub triangle ( $self, $pa, $pb, $pc, $char = undef, $fill = undef ){ | ||
| 120 | $char //= $fill; | ||
| 121 | |||
| 122 | my @args = ( $self->canvas, @$pa, @$pb, @$pc ); | ||
| 123 | |||
| 124 | if ( defined $fill ) { | ||
| 125 | caca_fill_triangle(@args, ord $char); | ||
| 126 | } | ||
| 127 | elsif( defined $char ) { | ||
| 128 | caca_draw_triangle(@args, ord $char); | ||
| 129 | } | ||
| 130 | else { | ||
| 131 | caca_draw_thin_triangle(@args); | ||
| 132 | } | ||
| 133 | |||
| 134 | return $self; | ||
| 135 | } | ||
| 136 | |||
| 137 | sub clear ($self) { | ||
| 138 | caca_clear_canvas($self->canvas); | ||
| 139 | return $self; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub _expand_drawing_options( $self, @rest ) { | ||
| 143 | @rest = ( thin => 1 ) unless @rest; | ||
| 144 | unshift @rest, 'char' if @rest == 1; | ||
| 145 | |||
| 146 | return @rest; | ||
| 147 | } | ||
| 148 | |||
| 149 | sub _primitive ( $self, $funcs, $args, @rest ) { | ||
| 150 | my %opt = $self->_expand_drawing_options(@rest); | ||
| 151 | my( $draw, $thin, $fill ) = @$funcs; | ||
| 152 | |||
| 153 | my @args = ( $self->canvas, @$args ); | ||
| 154 | |||
| 155 | $fill->(@args, ord $opt{fill}) if $opt{fill}; | ||
| 156 | |||
| 157 | $draw->(@args, ord $opt{char}) if $opt{char}; | ||
| 158 | |||
| 159 | $thin->(@args) if $opt{thin}; | ||
| 160 | |||
| 161 | return $self; | ||
| 162 | } | ||
| 163 | |||
| 164 | sub box ( $self, $c1, $c2, @rest ){ | ||
| 165 | |||
| 166 | $self->_primitive( | ||
| 167 | [ \&caca_draw_box, \&caca_draw_thin_box, \&caca_fill_box ], | ||
| 168 | [ @$c1, @$c2 ], | ||
| 169 | @rest, | ||
| 170 | ); | ||
| 171 | |||
| 172 | } | ||
| 173 | |||
| 174 | sub ellipse ( $self, $center, $rx, $ry, @rest ) { | ||
| 175 | $self->_primitive( | ||
| 176 | [ \&caca_draw_ellipse, \&caca_draw_thin_ellipse, \&caca_fill_ellipse ], | ||
| 177 | [ @$center, $rx, $ry ], | ||
| 178 | @rest, | ||
| 179 | ); | ||
| 180 | } | ||
| 181 | |||
| 182 | sub circle ( $self, $center, $radius, @rest ) { | ||
| 183 | $self->_primitive( | ||
| 184 | [ \&caca_draw_ellipse, \&caca_draw_thin_ellipse, \&caca_fill_ellipse ], | ||
| 185 | [ @$center, $radius, $radius ], | ||
| 186 | @rest, | ||
| 187 | ); | ||
| 188 | } | ||
| 189 | |||
| 190 | sub text ( $self, $coord, $text ) { | ||
| 191 | length $text > 1 | ||
| 192 | ? caca_put_str( $self->canvas, @$coord, $text ) | ||
| 193 | : caca_put_char( $self->canvas, @$coord, ord $text ); | ||
| 194 | |||
| 195 | return $self; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub polyline( $self, $points, @rest ) { | ||
| 199 | my %opts = $self->_expand_drawing_options(@rest); | ||
| 200 | |||
| 201 | my @x = map { $_->[0] } @$points; | ||
| 202 | my @y = map { $_->[1] } @$points; | ||
| 203 | my $n = @x - !$opts{close}; | ||
| 204 | |||
| 205 | $opts{char} ? caca_draw_polyline( $self->canvas, \@x, \@y, $n, ord $opts{char} ) | ||
| 206 | : caca_draw_thin_polyline( $self->canvas, \@x, \@y, $n ); | ||
| 207 | |||
| 208 | return $self; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub line ( $self, $pa, $pb, $char = undef ) { | ||
| 212 | |||
| 213 | defined ( $char ) | ||
| 214 | ? caca_draw_line($self->canvas, @$pa, @$pb, ord $char) | ||
| 215 | : caca_draw_thin_line($self->canvas, @$pa, @$pb ); | ||
| 216 | |||
| 217 | return $self; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub canvas_width($self) { | ||
| 221 | return caca_get_canvas_width($self->canvas); | ||
| 222 | } | ||
| 223 | |||
| 224 | sub canvas_height($self) { | ||
| 225 | return caca_get_canvas_height($self->canvas); | ||
| 226 | } | ||
| 227 | |||
| 228 | |||
| 229 | # TODO: troff seems to trigger a segfault | ||
| 230 | my @export_formats = qw/ caca ansi text html html3 irc ps svg tga /; | ||
| 231 | |||
| 232 | sub export( $self, $format = 'caca' ) { | ||
| 233 | |||
| 234 | croak "format '$format' not supported" | ||
| 235 | unless grep { $format eq $_ } @export_formats; | ||
| 236 | |||
| 237 | my $size = malloc UINT_SIZE(); | ||
| 238 | |||
| 239 | my $export = caca_export_canvas_to_memory( $self->canvas, $format eq 'text' ? 'ansi' : $format, $size ); | ||
| 240 | |||
| 241 | no warnings 'uninitialized'; | ||
| 242 | $export =~ s/\e\[?.*?[\@-~]//g if $format eq 'text'; | ||
| 243 | |||
| 244 | return $export; | ||
| 245 | } | ||
| 246 | |||
| 247 | sub canvas_size($self) { | ||
| 248 | [ $self->canvas_width, $self->canvas_height ]; | ||
| 249 | } | ||
| 250 | |||
| 251 | sub set_ansi_color( $self, $foreground, $background ) { | ||
| 252 | caca_set_color_ansi( $self->canvas, $foreground, $background ); | ||
| 253 | |||
| 254 | return $self; | ||
| 255 | } | ||
| 256 | |||
| 257 | my %event_map = pairmap { $a => 'Term::Caca::Event::' . $b } ( | ||
| 258 | KEY_PRESS ,=> 'Key::Press', | ||
| 259 | KEY_RELEASE ,=> 'Key::Release', | ||
| 260 | MOUSE_MOTION ,=> 'Mouse::Motion', | ||
| 261 | MOUSE_PRESS ,=> 'Mouse::Button::Press', | ||
| 262 | MOUSE_RELEASE ,=> 'Mouse::Button::Release', | ||
| 263 | RESIZE ,=> 'Resize', | ||
| 264 | QUIT ,=> 'Quit', | ||
| 265 | ); | ||
| 266 | |||
| 267 | sub wait_for_event ( $self, $mask = ANY_EVENT, $timeout = 0 ) { | ||
| 268 | |||
| 269 | $mask //= ANY_EVENT; | ||
| 270 | $timeout *= 1_000_000 unless $timeout == -1; | ||
| 271 | |||
| 272 | |||
| 273 | # TODO change the 36 to the local size of the caca_event_t | ||
| 274 | my $event = malloc 36; | ||
| 275 | |||
| 276 | caca_get_event( $self->display, $mask, $event, $timeout, 1 ); | ||
| 277 | |||
| 278 | my $class = $event_map{ caca_get_event_type( $event ) } or return; | ||
| 279 | |||
| 280 | return $class->new( event => $event ); | ||
| 281 | |||
| 282 | } | ||
| 283 | |||
| 284 | sub DEMOLISH ($self,@) { | ||
| 285 | free $self->display if $self->has_display; | ||
| 286 | } | ||
| 287 | |||
| 288 | 'Term::Caca'; | ||
| 289 | |||
| 290 | __END__ | ||