Acme-Curses-Marquee-Extensions

 view release on metacpan or  search on metacpan

lib/Acme/Curses/Marquee/Extensions.pm  view on Meta::CPAN

package Acme::Curses::Marquee::Extensions;
use strict;
use warnings;
require 5.006;

use base 'Acme::Curses::Marquee';
our $VERSION = 0.04;

use Text::FIGlet 2.00;
use Curses qw(A_BLINK);          #Don't clobber scroll!    XXX
use Term::ANSIColor;             #Many curses don't have color
use Every;

sub import{
  use Acme::Curses::Marquee::EVIL; #XXX Pass along vertical
}

sub new{
  my $class = shift;
  my %args = (
	      active => 0,
	      winh   => 25,
	      winw   => 80,
	      winx   => 0,
	      winy   => 0,
	      @_ );

  $args{srctxt} = delete($args{text});

  #Instantiate the font(s)
  my $font;
  if( ref($args{font}) eq 'ARRAY' ){
    local($_);

    $args{_fonts} = $args{font};
    $args{_fontSec} = pop @{$args{_fonts}} if $args{_fonts}->[-1] =~ /^\d+/;

    $font = $args{_font}->{ $args{font}=$_ } = font({}, $_, '-0') for
      reverse @{$args{_fonts}};
  }
  else{
    $font = $args{_font}->{$args{font}} = font({}, $args{font}, '-0');
  }

  #For text's rendering
  $args{height} = $font->{_header}->[1];
  $args{width}  = $args{winw};

  #Set up the display
  $args{winy} ||= int($args{winh}/2 - $args{height}/2);
  $args{win} = new Curses($args{height},$args{winw},$args{winy},$args{winx});

  my $self = bless \%args, $class;
  $self->text($self->{srctxt}) if (defined $self->{srctxt});

  return $self;
}

sub font {
  $_[0]->{_font}->{$_[1]} = Text::FIGlet->new(-f=>$_[1]);
  delete($_[0]->{_sweep});
  return $_[-1] eq '-0' ? $_[0]->{_font}->{$_[1]} : shift->SUPER::font(@_);
}

sub sweep{
  my($self, $state) = @_;

  if( $state == -1 && $self->{offset} == int($self->{txtlen}/2) ){
    $self->{txtlen} = $self->{_sweep};
  }

  if( $state ){
    return if exists($self->{_sweep});
    $self->{offset} = $self->{_sweep} = $self->{txtlen};
    $self->{txtlen}+= $self->{width};
  }
  else{
#    $self->{offset} = 0;
    $self->{txtlen} = delete($self->{_sweep});
  }
}

{
  my $i = 0;
  sub scroll{
    my $self = shift;
    #XXX offset would work if we could account for one wrap-around...
#   if( defined($self->{_fonts}) && ($self->{offset} == $self->{txtlen}) ){
    if( defined($self->{_fonts}) && every seconds=>$self->{_fontSec}||45 ){
      #XXX reposition vertically iff auto-centered
      $self->font( $self->{_fonts}->[ ++$i %scalar(@{ $self->{_fonts} }) ]);
    }
    $self->SUPER::scroll;
  }
}

{
  my $i = 0;
  my $rainbow = [qw/red yellow/,'bold yellow',qw/green cyan blue magenta/];
  sub colors{
    my($self, %p) = @_;
    my @colors = @{$p{colors}||$rainbow};
    if( every seconds=>$p{delay}||5 ){
      print color $colors[$i++%scalar @colors];
    }
  }
}

#XXX
sub blink{
  print color 'blink';
}



( run in 2.182 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )