blib/lib/Reply/Plugin/Prompt.pm

filter: Statementstmt 54.6% 76 / 139 Branchbran 15.9% 7 / 44 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 73.6% 14 / 19 Podpod 22.2% 2 / 9 totaltotal 46.3% 100 / 216 CC 25 SCAR 38.8
CC
25
Cov
45%
prompt
156.0
section_wakatime
20.0
section_os
12.5
CRAP 48.4
↑ Summary
1 package Reply::Plugin::Prompt;
2 1 $Reply::Plugin::Prompt::VERSION = '0.0.21.0';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Cwd qw(abs_path getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Env qw(HOME);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use POSIX qw(strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Term::ANSIColor;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use base 'Reply::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Exporter qw(import);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 our @EXPORT =
13 qw(section_wakatime section_result section_path section_os section_version section_time);
14  
15 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=10.5
16 1 ▶ if ( $^O eq 'MSWin32' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 1 1
17 0 require Win32::Console::ANSI;
18 0 Win32::Console::ANSI->import;
19 }
20 }
21  
22 1 my @sections =
23 ( 'wakatime', 'result', 'os', 'version', 'path', 'time' ); # section order
24 1 my %section_colors = (
25 'wakatime' => '',
26 'result' => 'yellow on_red',
27 'os' => 'black on_yellow',
28 'version' => 'blue on_black',
29 'path' => 'white on_blue',
30 'time' => 'black on_white',
31 );
32 1 my $sep = ''; # section separator
33 1 my $insert_text = ' %s '; # whitespaces which padded around section text
34 1 my $insert_result = '✘ %s';
35 1 my $insert_version = ' %s';
36 1 my $insert_os = '%s';
37 1 my $insert_time = ' %s';
38 1 my $time_format = '%H:%M:%S'; # time format
39 1 my $prompt_char = '❯ '; # prompt character
40 1 my $wakatime_cmd =
41 'wakatime-cli --write --plugin=repl-reply-wakatime --entity-type=app --entity=perl --alternate-language=perl --project=%s &';
42  
43 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my $class = shift;
45 0 my $self = $class->SUPER::new(@_);
46 0 $self->{counter} = 0;
47 0 $self->{prompted} = 0;
48 0 return $self;
49 }
50  
51 0 ▶sub section_wakatime {
Subroutine section_wakatime: not called CC=4 SCAR=30.0
Pod undocumented
52 0 my $cmd = $wakatime_cmd;
53 0 ▶ if ( $cmd =~ /%s/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /%s/) 0 0 0
54 0 ▶ my $null = $^O eq 'MSWin32' ? 'nul' : '/dev/null';
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
55 0 my $old = $?;
56 0 my $project = `git rev-parse --show-toplevel 2> $null`;
57 0 $? = $old;
58 0 chomp $project;
59 0 ▶ $project = abs_path getcwd if $project eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $project eq "" 0 0 0
60 0 $project = basename $project;
61 0 $cmd = sprintf( $cmd, $project );
62 }
63 0 open my $fh, '-|', $cmd;
64 }
65  
66 2 ▶sub section_result {
Subroutine section_result: called CC=2 SCAR=7.0
Pod undocumented
67 2 my $rst = '';
68 2 ▶ if ( $? != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? != 0) 2 0 2
69 2 $rst = color('yellow on_red') . " ✘ $? " . color('red on_yellow') . '';
70 }
71 2 return $rst;
72 }
73  
74 1 ▶sub section_path {
Subroutine section_path: called CC=2 SCAR=7.0
Pod undocumented
75 1 my $path = abs_path getcwd;
76 1 $path =~ s/^\Q$HOME\E/~/;
77 1 my $icon;
78 1 ▶ if ( $path eq '~' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path eq '~') { } 0 1 1
79 0 $icon = '';
80 }
81 else {
82 1 $icon = '';
83 }
84 1 $path = $icon . ' ' . $path;
85 1 $path =~ s|([^/]+)$|color('bold') . $1|e;
86 1 return $path;
87 }
88  
89 1 ▶sub section_os {
Subroutine section_os: called CC=7 SCAR=25.2
Pod undocumented
90 1 my $os_icon = '';
91 1 my $lsb_id;
92 1 my %lsb_ids = (
93 'Arch' => '',
94 'Gentoo' => '',
95 'Ubuntu' => '',
96 'Cent' => '',
97 'Debian' => '',
98 );
99 1 ▶ if ( $^O eq 'linux' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($^O eq 'linux') { } 1 0 1
elsif ($^O eq 'MSWin32') { } 0 0 0
elsif ($^O eq 'MacOS') { } 0 0 0
100 1 $os_icon = '';
101 1 $lsb_id = `lsb_release -i`;
102 1 chomp $lsb_id;
103 1 $lsb_id =~ s/.+:\s+//;
104 1 ▶ if ( exists $lsb_ids{$lsb_id} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $lsb_ids{$lsb_id}) { } 0 1 1
elsif (exists $ENV{'PREFIX'} and $ENV{'PREFIX'} eq '/data/data/com.termux/files/usr') { } 0 2 2
105 0 $os_icon = $lsb_ids{$lsb_id};
106 }
107 ▶ elsif ( exists $ENV{PREFIX}
Condition33%1/3
exists $ENV{"PREFIX"} and $ENV{"PREFIX"} eq "/data/data/com.termux/files/usr"
!ll&&!rl&&r
100
MC/DC0%0/2
exists $ENV{"PREFIX"} and $ENV{"PREFIX"} eq "/data/data/com.termux/files/usr"
exists $ENV{"PREFIX"} $ENV{"PREFIX"} eq "/data/data/com.termux/files/usr"
Truth table33%1/3
A and B
A= exists $ENV{"PREFIX"}B= $ENV{"PREFIX"} eq "/data/data/com.termux/files/usr"
ABresult
0X0
100
111
108 and $ENV{PREFIX} eq '/data/data/com.termux/files/usr' )
109 {
110 0 $os_icon = '';
111 }
112 }
113 elsif ( $^O eq 'MSWin32' ) {
114 0 $os_icon = '';
115 }
116 elsif ( $^O eq 'MacOS' ) {
117 0 $os_icon = '';
118 }
119 1 return $os_icon;
120 }
121  
122 2 ▶sub section_version {
Subroutine section_version: called CC=1 SCAR=0.0
Pod undocumented
123 2 my $version = $];
124 2 $version =~ s/0+$//;
125 2 return $version;
126 }
127  
128 0 ▶sub section_time {
Subroutine section_time: not called CC=1 SCAR=6.9
Pod undocumented
129 0 return strftime( shift, localtime );
130 }
131  
132 # https://stackoverflow.com/questions/10342875/how-to-properly-use-the-try-catch-in-perl-that-error-pm-provides/10343025
133 1 ▶eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require File::XDG; my $xdg = "File::XDG"->new("name", "reply", "api", 1); my $config = $xdg->config_home->child("prompt.pl")->slurp_utf8; eval $config; 1 } } or '???' 0 1 1
134 1 require File::XDG;
135 1 my $xdg = File::XDG->new( name => 'reply', api => 1 );
136 1 my $config = $xdg->config_home->child('prompt.pl')->slurp_utf8;
137 0 eval $config;
138 0 1;
139 } or 1;
140  
141 # these information will not change so source them once.
142 1 my $os = section_os;
143 1 my $version = section_version;
144 1 $insert_version = sprintf( $insert_version, $version );
145 1 $insert_os = sprintf( $insert_os, $os );
146  
147 0 ▶sub prompt {
Subroutine prompt: not called CC=12 SCAR=50.5
Pod documented
148 0 my $self = shift;
149 0 $self->{prompted} = 1;
150 0 my $result = $?;
151 0 my @new_sections = ();
152  
153 0 ▶ if ( $? == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($? == 0) { } 0 0 0
154 0 foreach my $section (@sections) {
155 0 ▶ if ( $section eq 'result' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section eq "result") 0 0 0
156 0 next;
157 }
158 0 push @new_sections, $section;
159 }
160 }
161 else {
162 0 @new_sections = @sections;
163 }
164  
165 0 my $ps1 = '';
166 0 my $last_bg = '';
167 0 foreach my $section (@new_sections) {
168 0 my $text = '';
169 0 ▶ if ( $section eq 'wakatime' ) {
Branch: 6 branches
BranchTrueFalseTotal
if ($section eq 'wakatime') { } 0 0 0
elsif ($section eq 'result') { } 0 0 0
elsif ($section eq 'path') { } 0 0 0
elsif ($section eq 'time') { } 0 0 0
elsif ($section eq 'os') { } 0 0 0
elsif ($section eq 'version') { } 0 0 0
170 0 section_wakatime;
171 0 next;
172 }
173 elsif ( $section eq 'result' ) {
174 0 $text = sprintf( $insert_result, $result );
175 }
176 elsif ( $section eq 'path' ) {
177 0 $text = section_path;
178 }
179 elsif ( $section eq 'time' ) {
180 0 my $time = section_time $time_format;
181 0 $text = sprintf( $insert_time, $time );
182 }
183 elsif ( $section eq 'os' ) {
184 0 $text = $insert_os;
185 }
186 elsif ( $section eq 'version' ) {
187 0 $text = $insert_version;
188 }
189 else {
190 0 die "$section is not supported!";
191 }
192 0 my $color = $section_colors{$section};
193 0 ▶ if ( $last_bg eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($last_bg eq '') { } 0 0 0
194 0 $ps1 .= color($color) . sprintf( $insert_text, $text );
195 }
196 else {
197 0 my ($bg) = $color =~ /(?<=on_)(\S+)/g;
198 0 my ($fg) = $color =~ /(?<!on_)(\S+)/g;
199 0 $ps1 .=
200 color("$last_bg on_$bg")
201 . $sep
202 . color($fg)
203 . sprintf( $insert_text, $text );
204 }
205 0 ($last_bg) = $color =~ /(?<=on_)(\S+)/g;
206 }
207 return
208 color('reset')
209 . $ps1
210 . color("reset $last_bg")
211 . $sep
212 . color('reset') . "\n"
213 . $self->{counter}
214 0 . $prompt_char;
215 }
216  
217 0 ▶sub loop {
Subroutine loop: not called CC=2 SCAR=17.9
Pod documented
218 0 my $self = shift;
219 0 my ($continue) = @_;
220 0 ▶ $self->{counter}++ if $self->{prompted};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"prompted"} 0 0 0
221 0 $self->{prompted} = 0;
222 0 $continue;
223 }
224  
225 1 1;
226  
227 __END__
↑ Summary