App-PS1
view release on metacpan or search on metacpan
lib/App/PS1.pm view on Meta::CPAN
}
my $total = $self->parts_size;
my $spare = $self->cols - $total;
my $spare_size = $spare / ( @{$self->parts} - 1 );
while ($spare < 0 || $spare_size < 0) {
pop @{$self->parts};
if ( @{$self->parts} == 1 ) {
$total = $self->parts_size;
$spare = $self->cols - $total;
$spare_size = $spare;
last;
}
$total = $self->parts_size;
$spare = $self->cols - $total;
$spare_size = ( @{$self->parts} - 1 ) ? $spare / ( @{$self->parts} - 1 ) : 0;
}
if ( $total <= $self->cols ) {
my $line = '';
my $extra = 0;
for my $i ( 0 .. @{$self->parts} - 2 ) {
my $div_first = $i ? 2 : 1;
my $div_second = $i == @{$self->parts} - 2 ? 1 : 2;
my $spaces;
if ( $total < $self->cols / 2 ) {
$spaces = ( $self->cols / ( @{$self->parts} - 1 ) - $self->parts->[$i][0] / $div_first - $self->parts->[$i + 1][0] / $div_second );
}
else {
$spaces = $spare_size;
$spare -= $spare_size - ( $spaces - int $spaces );
}
$extra += $spaces - int $spaces;
$line .= $self->parts->[$i][1];
$line .= ' ' x $spaces;
if ( $extra > 1 ) {
$line .= ' ' x $extra;
$spare -= int $extra;
$extra = $extra - int $extra;
}
}
if ( $extra > 0.1 ) {
$line .= ' ';
}
$line .= $self->parts->[-1][1];
my $colour = $ENV{APP_PS1_BACKGROUND} || 52;
$out = $self->colour('background') . $line . "\e[0m\n";
}
return $out;
}
sub parse_options {
my ($self, $options_txt, $name) = @_;
return {} if !$options_txt;
require JSON::XS;
my $options = eval { JSON::XS::decode_json($options_txt) };
my $error = $@;
if ($error && $self->verbose) {
cluck "Error reading $name\'s options ($options_txt)! $error\n";
}
return $options || {};
}
sub parts_size {
my ($self) = @_;
return sum map { $_->[0] } @{$self->parts};
}
sub load {
my ($self, $plugin) = @_;
$self->plugins({}) if !$self->plugins;
return 1 if $self->plugins->{$plugin};
my $module = 'App::PS1::Plugin::' . ucfirst $plugin;
my $file = 'App/PS1/Plugin/' . ( ucfirst $plugin ) . '.pm';
eval { require $file };
warn $@ if $@;
return 0 if $@;
push @App::PS1::ISA, $module;
return $self->plugins->{$plugin} = 1;
}
sub surround {
my ($self, $count, $text) = @_;
return if !defined $text || !$count;
my $left = $self->safe ? 'âº' : '<';
my $right = $self->safe ? 'â»' : '>';
$count += 2;
$text = $self->colour('marker') . "$left$text" . $self->colour('marker') . $right;
return ($count, $text);
}
sub colour {
my ($self, $name) = @_;
my $colour = $theme{$self->theme}{$name} || [];
return
$self->bw || !$colour ? ''
: $t256 && !$self->low ? Term::Colour256::color($colour->[1])
: Term::ANSIColor::color($colour->[0]);
}
1;
__END__
=head1 NAME
( run in 0.624 second using v1.01-cache-2.11-cpan-39bf76dae61 )