File Coverage

blib/lib/OptArgs2/StatusLine.pm
Criterion Covered Total %
statement 52 60 86.6
branch 20 24 83.3
condition 4 4 100.0
subroutine 10 11 90.9
pod 1 2 50.0
total 87 101 86.1


line stmt bran cond sub pod time code
1 1     1   43661 use strict;
  1         2  
  1         45  
2 1     1   7 use warnings;
  1         2  
  1         955  
3              
4             package OptArgs2::StatusLine;
5              
6             our $VERSION = 'v2.0.17';
7              
8 37     37 1 26705 sub RS { chr(30) }
9             my $RS = RS;
10              
11             our $WARN_FMT = "\e[38;5;220m%s\e[0m\n";
12 5     5 0 15652 sub WARN { chr(5) }
13             my $WARN = WARN;
14              
15             sub TIESCALAR {
16 1     1   2 my $class = shift;
17 1         4 bless( ( \my $str ), $class );
18             }
19              
20 41     41   32961 sub FETCH { ${ $_[0] } }
  41         218  
21              
22             sub STORE {
23 33     33   56833 my $self = shift;
24 33   100     136 my $arg = shift // return $$self = undef;
25 31         70 my %arg = ();
26              
27 31 100       119 if ( 'SCALAR' eq ref $arg ) {
    100          
28 2         8 $arg{prefix} = $$arg;
29             }
30             elsif ( '' eq $arg ) {
31 3         8 $arg{msg} = '';
32             }
33             else {
34 26         499 $arg =~ m/
35             (?:(?.+?)?(?:$RS))?
36             (?$WARN)?
37             (?.+?)?
38             (?\n)?
39             \z
40             /x;
41              
42 26         414 %arg = %+;
43             }
44              
45 31   100     386 ( $$self // '' ) =~ m/
46             (?:(?.*)(?:$RS))?
47             (?.*?)?
48             \z
49             /x;
50              
51 31         394 my %next = ( %+, NL => "\r", %arg );
52              
53 31 100       156 if ( not defined $next{prefix} ) {
54 2         23 require File::Basename;
55 2         110 $next{prefix} = File::Basename::basename($0) . ': ';
56             }
57              
58 31         114 my $fh = select;
59 31 100       228 if ( $next{WARN} ) {
    50          
60 4 50       29 if ( -t STDERR ) {
61 0         0 warn sprintf $WARN_FMT, $next{prefix} . $next{msg} . "\e[K";
62             }
63             else {
64 4         196 warn $next{prefix}, $next{msg}, "\n";
65             }
66 4 50       55 $fh->print( $next{prefix}, $next{msg}, "\n" ) if not -t $fh;
67             }
68             elsif ( -t $fh ) {
69             $fh->printflush( "\e[?25l", $next{prefix}, $next{msg}, "\e[K",
70 0         0 $next{NL} );
71             }
72             else {
73 27         166 $fh->print( $next{prefix}, $next{msg}, "\n" );
74             }
75              
76 31 100       1819 $next{msg} = '' if $next{NL} eq "\n";
77 31         96 $$self = $next{prefix} . RS . $next{msg};
78             }
79              
80             END {
81 1     1   3669 my $fh = select;
82 1 50       16 $fh->printflush("\e[?25h") if -t $fh;
83             }
84              
85             sub import {
86 2     2   425198 my $class = shift;
87 2         7 my $caller = scalar caller;
88              
89 1     1   10 no strict 'refs';
  1         2  
  1         453  
90 2         7 foreach my $arg (@_) {
91 4 100       24 if ( $arg =~ m/^\$(.*)/ ) {
    100          
    100          
92 1         4 my $name = $1;
93 1         8 tie my $x, 'OptArgs2::StatusLine';
94 1         2 *{ $caller . '::' . $name } = \$x;
  1         6  
95             }
96             elsif ( $arg eq 'RS' ) {
97 1         2 *{ $caller . '::RS' } = \&RS;
  1         8  
98             }
99             elsif ( $arg eq 'WARN' ) {
100 1         2 *{ $caller . '::WARN' } = \&WARN;
  1         34  
101             }
102             else {
103 1         9 require Carp;
104 1         193 Carp::croak('expected "RS", "WARN" or "$scalar"');
105             }
106              
107             }
108             }
109              
110             sub _explode {
111 0     0     require Carp;
112 0           my $s = shift;
113 0           $s =~ s/\n/\\n/g;
114 0           $s = join( ' . RS . ', map { qq{"$_"} } split( /$RS/, $s ) );
  0            
115 0           Carp::carp($s);
116             }
117              
118             1;
119              
120             __END__