blib/lib/Zoidberg/Fish/Log.pm

filter: Statementstmt 6.2% 11 / 176 Branchbran 0.0% 0 / 134 Conditioncond 0.0% 0 / 53 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 30.0% 3 / 10 Podpod 71.4% 5 / 7 totaltotal 4.6% 19 / 410 CC 92 SCAR 63.0
CC
92
Cov
3%
history
1056.0
read_log_file
552.0
fc
380.0
CRAP 545.5
↑ Summary blib/lib/Zoidberg/Fish/Intel.pm »
1 package Zoidberg::Fish::Log;
2  
3 1 our $VERSION = '0.981';
4  
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 #use AutoLoader 'AUTOLOAD';
7 1 ▶use Zoidberg::Utils qw/:default path getopt output_is_captured/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use base 'Zoidberg::Fish';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # TODO purge history with some intervals
11  
12 0 ▶sub init {
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
13 0 my $self = shift;
14 0 @$self{qw/pid init_time/} = ($$, time);
15 0 ▶ close $$self{logfh} if $$self{logfh};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"logfh"} 0 0 0
16 0 my $file = path( $$self{config}{logfile} );
17 0 my $fh; # undefined scalar => new anonymous filehandle on open()
18 0 ▶ if (open $fh, ">>$file") {
Branch: 1 branch
BranchTrueFalseTotal
if (open $fh, ">>$file") { } 0 0 0
19 0 my $oldfh = select $fh;
20 0 $| = 1;
21 0 select $oldfh;
22 0 $$self{logfh} = $fh;
23 0 $self->add_events('prompt');
24 # TODO also set event for change of hist file => re-init filehandle
25 }
26 else {
27 0 delete $$self{logfh};
28 0 complain "Log file not writeable, logging disabled";
29 }
30 }
31 # TODO in %Env::PS1::map
32 # \! The history number of the next command.
33 # \# The command number of the next command
34 # (like history number, but minus the lines read from the history file)
35  
36 # TODO "history_reset" event for when we are forced to read a new hist file
37  
38 # TODO: HISTTIMEFORMAT should give an insight in the timestamps
39 0 ▶sub history {
Subroutine history: not called CC=32 SCAR=69.6
Pod documented
40 0 my $self = shift;
41 0 my ($opts, $args) = getopt('nonu,-n reverse,-r read type$ +* -* @', @_);
42 0 ▶ my $tag = $$opts{type} || 'cmd';
Condition0%0/2
$opts->{'type'} || 'cmd'
l!l
00
MC/DC0%0/1
$opts->{'type'} || 'cmd'
$opts->{'type'}
Truth table0%0/2
A
A= $opts->{'type'}
Aresult
11
00
43 0 ▶ unshift @$args, grep /^[+-]\d+$/, @{$$opts{_opts}} if exists $$opts{_opts};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"_opts"} 0 0 0
44 0 ▶ error 'to many arguments' if @$args > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$args > 2 0 0 0
45  
46 # find the rigth history
47 0 my $re;
48 0 ▶ if ($$opts{read} or ! $$self{read_log}) { $re = $self->read_log_file($tag) }
Branch: 3 branches
BranchTrueFalseTotal
if ($opts->{'read'} or not $self->{'read_log'}) { } 0 0 0
elsif (exists $self->{'logs'}{$tag}) { } 0 0 0
elsif ($tag eq 'cmd') { } 0 0 0
Condition0%0/3
$opts->{"read"} or not $self->{"read_log"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{"read"} or not $self->{"read_log"}
$opts->{"read"} not $self->{"read_log"}
Truth table0%0/3
A or not(B)
A= $opts->{"read"}B= not $self->{"read_log"}
ABresult
1X1
011
000
49 0 elsif (exists $$self{logs}{$tag}) { $re = $$self{logs}{$tag} }
50 0 elsif ($tag eq 'cmd') { $re = $$self{shell}->builtin('GetHistory') }
51 0 debug 'found '.scalar(@$re).' records for '.$tag;
52  
53 # set history numbers
54 0 ▶ unless (output_is_captured) {
Branch: 1 branch
BranchTrueFalseTotal
if (output_is_captured()) { } 0 0 0
55 0 ▶ my $i = ($tag eq 'cmd') ? ($$self{command_number} - @$re + 1) : 1;
Branch: 1 branch
BranchTrueFalseTotal
$tag eq 'cmd' ? : 0 0 0
56 # TODO make this depend on init time indexing ... $$self{shell}{command_number}
57 # avoid modifying the original reference
58 # ouput format found in posix spec for fc
59 0 $re = [ map {$a = $_; $a =~ s/^/\t/mg; $a} @$re ];
60 0 ▶ @$re = map {$i++.$_} @$re unless $$opts{nonu};
Branch: 1 branch
BranchTrueFalseTotal
unless $opts->{"nonu"} 0 0 0
61 }
62 0 else { $re = [ @$re ] } # force copy
63  
64 # get range if any
65 0 ▶ if (@$args) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$args) { } 0 0 0
elsif ($tag eq 'cmd' and defined $self->{'config'}{'maxlines'}) { } 0 0 0
66 0 for (@$args) { # string match
67 0 ▶ next if /^[+-]?\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if /^[+-]?\d+$/ 0 0 0
68 0 ▶ my $regex = ref($_) ? $_ : qr/^\d*\t?\Q$_\E/;
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 0 0
69 0 my ($i, $done) = (0, 0);
70 0 for (reverse @$re) {
71 0 ▶ $i--; next unless $_ =~ $regex;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /$regex/ 0 0 0
72 0 ▶ ++$done and last;
Branch: 1 branch
BranchTrueFalseTotal
++$done and last 0 0 0
73 }
74 0 ▶ error "no record matching '$_'" unless $done;
Branch: 1 branch
BranchTrueFalseTotal
unless $done 0 0 0
75 0 ▶ if (@$args == 0 or $$args[0] == $$args[1]) { # default last for string
Branch: 1 branch
BranchTrueFalseTotal
if (@$args == 0 or $args->[0] == $args->[1]) { } 0 0 0
Condition0%0/3
@$args == 0 or $args->[0] == $args->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
@$args == 0 or $args->[0] == $args->[1]
@$args == 0 $args->[0] == $args->[1]
Truth table0%0/3
A or B
A= @$args == 0B= $args->[0] == $args->[1]
ABresult
1X1
011
000
76 0 @$args = ($i, $i);
77 0 last;
78 }
79 0 else { $_ = $i }
80 }
81 0 ▶ $$args[1] = scalar @$re unless defined $$args[1]; # default default last
Branch: 1 branch
BranchTrueFalseTotal
unless defined $args->[1] 0 0 0
82 0 my $total = scalar @$re;
83 0 for (@$args) { # convert negative 2 positive
84 0 ▶ error 'index out of range: '.$_
Branch: 1 branch
BranchTrueFalseTotal
if $_ == 0 or $_ < -$total or $_ > $total 0 0 0
Condition0%0/6
$_ == 0 or $_ < -$total or $_ > $total
l!l&&r!l&&!r
000
$_ == 0 or $_ < -$total
l!l&&r!l&&!r
000
MC/DC0%0/3
$_ == 0 or $_ < -$total or $_ > $total
$_ == 0 $_ < -$total $_ > $total
Truth table0%0/4
A or B or C
A= $_ == 0B= $_ < -$totalC= $_ > $total
ABCresult
1XX1
01X1
0011
0000
85 if $_ == 0 or $_ < -$total or $_ > $total;
86 0 ▶ $_ += $total+1 if $_ < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 0 0 0 0
87 }
88 0 ▶ if ($$args[0] > $$args[1]) { # check order of args
Branch: 1 branch
BranchTrueFalseTotal
if ($args->[0] > $args->[1]) 0 0 0
89 0 ▶ $$opts{reverse} = $$opts{reverse} ? 0 : 1 ;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'reverse'} ? : 0 0 0
90 0 @$args = reverse @$args;
91 }
92 0 debug "history range: $$args[0] .. $$args[1]";
93 0 @$re = @$re[$$args[0]-1 .. $$args[1]-1];
94 }
95 ▶ elsif ($tag eq 'cmd' and defined $$self{config}{maxlines}) {
Condition0%0/3
$tag eq "cmd" and defined $self->{"config"}{"maxlines"}
!ll&&!rl&&r
000
MC/DC0%0/2
$tag eq "cmd" and defined $self->{"config"}{"maxlines"}
$tag eq "cmd" defined $self->{"config"}{"maxlines"}
Truth table0%0/3
A and B
A= $tag eq "cmd"B= defined $self->{"config"}{"maxlines"}
ABresult
0X0
100
111
96 # FIXME temp hack till ReadLine gets maxlines
97 0 my @range = ($#$re - $$self{config}{maxlines}, $#$re);
98 0 @$re = @$re[$range[0] .. $range[1]];
99 }
100  
101 0 ▶ output $$opts{reverse} ? [reverse @$re] : $re;
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'reverse'} ? : 0 0 0
102 }
103  
104 0 ▶sub read_log_file {
Subroutine read_log_file: not called CC=23 SCAR=63.1
Pod undocumented
105 0 my ($self, $tag) = @_;
106 0 ▶ my %tags = $tag ? ( $tag => [] ) : ();
Branch: 1 branch
BranchTrueFalseTotal
$tag ? : 0 0 0
107 0 ▶ if ($$self{config}{keep}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"keep"}) 0 0 0
108 0 $tags{$_} = [] for keys %{$$self{config}{keep}};
109 }
110 0 ▶ return unless %tags;
Branch: 1 branch
BranchTrueFalseTotal
unless %tags 0 0 0
111 0 my $file = path( $$self{config}{logfile} );
112 0 ▶ unless ($file) {
Branch: 3 branches
BranchTrueFalseTotal
if ($file) { } 0 0 0
if (do { -e $file and not -r _ }) { } 0 0 0
elsif (-s _) { } 0 0 0
113 0 complain 'No log file defined, can\'t read history';
114 0 return;
115 }
116 ▶ elsif (-e $file and ! -r _) {
Condition0%0/3
-e $file and not -r _
!ll&&!rl&&r
000
MC/DC0%0/2
-e $file and not -r _
-e $file not -r _
Truth table0%0/3
A and not(B)
A= -e $fileB= not -r _
ABresult
0X0
100
111
117 0 complain 'Log file not readable, can\'t read history';
118 0 return;
119 }
120 elsif (-s _) {
121 # TODO ignore lines from other shell instances ... use pid + init timestamp
122 0 debug "Going to read $file";
123 0 ▶ open IN, $file || error 'Could not open log file !?';
Condition0%0/3
$file || error('Could not open log file !?')
l!l&&r!l&&!r
000
MC/DC0%0/2
$file || error('Could not open log file !?')
$file error('Could not open log file !?')
Truth table0%0/3
A || B
A= $fileB= error('Could not open log file !?')
ABresult
1X1
011
000
124 0 ▶ while (<IN>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IN)) 0 0 0
125 # pid time type string
126 0 ▶ m/-\s*\[\s*(\d+),\s*(\d+)\s*,\s*(\w+)\s*,\s*"(.*?)"\s*\]\s*$/ or next;
Branch: 1 branch
BranchTrueFalseTotal
/-\s*\[\s*(\d+),\s*(\d+)\s*,\s*(\w+)\s*,\s*"(.*?)"\s*\]\s*$/ or next 0 0 0
127 0 push @{$tags{$3}}, $4
128 0 ▶ if exists $tags{$3} and ($2 < $$self{init_time} or $1 == $$self{pid});
Branch: 1 branch
BranchTrueFalseTotal
if exists $tags{$3} and $2 < $self->{"init_time"} || $1 == $self->{"pid"} 0 0 0
Condition0%0/6
exists $tags{$3} and $2 < $self->{"init_time"} || $1 == $self->{"pid"}
!ll&&!rl&&r
000
$2 < $self->{"init_time"} or $1 == $self->{"pid"}
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $tags{$3} and $2 < $self->{"init_time"} || $1 == $self->{"pid"}
exists $tags{$3} $2 < $self->{"init_time"} $1 == $self->{"pid"}
Truth table0%0/4
A and B or C
A= exists $tags{$3}B= $2 < $self->{"init_time"}C= $1 == $self->{"pid"}
ABCresult
0XX0
1000
11X1
1011
129 # if record newer then init_time and not matching our pid it's not ours
130 }
131 0 close IN;
132 }
133  
134 0 my $re;
135 0 $$self{logs} = {}; # reset
136 0 debug 'found the following tags in log: '.join(' ', keys %tags);
137 0 for (keys %tags) {
138 0 ▶ my @t = map {s/(\\\\)|(\\n)|\\(.)/$1?'\\':$2?"\n":$3/eg; $_}
Branch: 2 branches
BranchTrueFalseTotal
$1 ? : 0 0 0
$2 ? : 0 0 0
139 0 @{ delete $tags{$_} };
140 0 ▶ if ($$self{config}{keep}{$_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"config"}{"keep"}{$_}) 0 0 0
141 @t = reverse( ( reverse @t )[0 .. $$self{config}{keep}{$_}] )
142 0 ▶ if @t > $$self{config}{keep}{$_};
Branch: 1 branch
BranchTrueFalseTotal
if @t > $self->{"config"}{"keep"}{$_} 0 0 0
143 0 $$self{logs}{$_} = \@t;
144 }
145 0 ▶ $re = \@t if $_ eq $tag;
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $tag 0 0 0
146 0 ▶ $$self{command_number} = scalar @t if $_ eq 'cmd';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "cmd" 0 0 0
147 }
148  
149 0 $$self{read_log}++;
150 0 ▶ return wantarray ? @$re : $re;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
151 }
152  
153 # sub cmd {
154 0 ▶sub prompt {
Subroutine prompt: not called CC=5 SCAR=34.0
Pod undocumented
155 # my ($self, undef, $cmd) = @_;
156 0 my $self = shift;
157 0 my $cmd = $$self{shell}{previous_cmd};
158 0 ▶ return unless $$self{settings}{interactive} and $$self{logfh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"settings"}{"interactive"} and $self->{"logfh"} 0 0 0
Condition0%0/3
$self->{"settings"}{"interactive"} and $self->{"logfh"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"settings"}{"interactive"} and $self->{"logfh"}
$self->{"settings"}{"interactive"} $self->{"logfh"}
Truth table0%0/3
A and B
A= $self->{"settings"}{"interactive"}B= $self->{"logfh"}
ABresult
0X0
100
111
159 0 $cmd =~ s/(["\\])/\\$1/g;
160 0 $cmd =~ s/\n/\\n/g;
161 0 print {$$self{logfh}} "- [ $$self{pid}, ".time().", cmd, \"$cmd\" ]\n"
162 0 ▶ unless $$self{config}{no_duplicates} and $cmd eq $$self{prev_cmd};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"no_duplicates"} and $cmd eq $self->{"prev_cmd"} 0 0 0
Condition0%0/3
$self->{"config"}{"no_duplicates"} and $cmd eq $self->{"prev_cmd"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"config"}{"no_duplicates"} and $cmd eq $self->{"prev_cmd"}
$self->{"config"}{"no_duplicates"} $cmd eq $self->{"prev_cmd"}
Truth table0%0/3
A and B
A= $self->{"config"}{"no_duplicates"}B= $cmd eq $self->{"prev_cmd"}
ABresult
0X0
100
111
163 0 $$self{prev_cmd} = $cmd;
164 0 $$self{command_number}++;
165 }
166  
167 0 ▶sub log {
Subroutine log: not called CC=9 SCAR=45.0
Pod documented
168 0 my ($self, $string, $type) = @_;
169 0 ▶ $type ||= 'log';
Condition0%0/2
$type ||= "log"
l!l
00
MC/DC0%0/1
$type ||= "log"
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
170 0 ▶ return prompt($self, undef, $string) if $type eq 'cmd';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "cmd" 0 0 0
171 0 ▶ if (exists $$self{config}{keep}{$type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"config"}{"keep"}{$type}) 0 0 0
172 0 ▶ $$self{logs}{$type} ||= [];
Condition0%0/2
$self->{"logs"}{$type} ||= []
l!l
00
MC/DC0%0/1
$self->{"logs"}{$type} ||= []
$self->{"logs"}{$type}
Truth table0%0/2
A
A= $self->{"logs"}{$type}
Aresult
11
00
173 0 ▶ unless ($$self{config}{no_duplicates} and $string eq $$self{logs}{$type}[-1]) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"config"}{"no_duplicates"} and $string eq $self->{"logs"}{$type}[-1]) 0 0 0
Condition0%0/3
$self->{"config"}{"no_duplicates"} and $string eq $self->{"logs"}{$type}[-1]
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"config"}{"no_duplicates"} and $string eq $self->{"logs"}{$type}[-1]
$self->{"config"}{"no_duplicates"} $string eq $self->{"logs"}{$type}[-1]
Truth table0%0/3
A and B
A= $self->{"config"}{"no_duplicates"}B= $string eq $self->{"logs"}{$type}[-1]
ABresult
0X0
100
111
174 0 push @{$$self{logs}{$type}}, $string;
175 0 shift @{$$self{logs}{$type}}
176 0 ▶ if @{$$self{logs}{$type}} > $$self{config}{keep}{$type};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"logs"}{$type};} > $self->{"config"}{"keep"}{$type} 0 0 0
177 }
178 }
179 0 ▶ return unless $$self{logfh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"logfh"} 0 0 0
180 0 $string =~ s/(["\\])/\\$1/g;
181 0 $string =~ s/\n/\\n/g;
182 0 print {$$self{logfh}} "- [ $$self{pid}, ".time().', '.$type.", \"$string\" ]\n";
183 }
184  
185 0 ▶sub round_up {
Subroutine round_up: not called CC=7 SCAR=40.3
Pod documented
186 0 my $self = shift;
187  
188 0 ▶ return unless $$self{logfh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"logfh"} 0 0 0
189 0 close $$self{logfh};
190  
191 my $max = defined( $$self{config}{maxlines} )
192 0 ▶ ? $$self{config}{maxlines} : $ENV{HISTSIZE} ;
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'config'}{'maxlines'} ? : 0 0 0
193 0 ▶ return unless defined $max;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $max 0 0 0
194 0 my $file = path( $$self{config}{logfile} );
195  
196 0 ▶ open IN, $file or error "Could not open hist file";
Branch: 1 branch
BranchTrueFalseTotal
open IN, $file or error("Could not open hist file") 0 0 0
197 0 my @lines = (reverse (<IN>))[0 .. $max-1];
198 0 ▶ close IN or error "Could not read hist file";
Branch: 1 branch
BranchTrueFalseTotal
close IN or error("Could not read hist file") 0 0 0
199  
200 0 ▶ open OUT, ">$file" or error "Could not open hist file";
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">$file" or error("Could not open hist file") 0 0 0
201 0 print OUT reverse @lines;
202 0 close OUT;
203 }
204  
205 #1;
206  
207 #__END__
208  
209 =head1 NAME
210  
211 Zoidberg::Fish::Log - History and log plugin for Zoidberg
212  
213 =head1 SYNOPSIS
214  
215 This module is a Zoidberg plugin, see Zoidberg::Fish for details.
216  
217 =head1 DESCRIPTION
218  
219 This plugin listens to the 'prompt' event and records all
220 input in the history log.
221  
222 If multiple instances of zoid are using the same history file
223 their histories will be merged.
224  
225 TODO option for more bash like behaviour
226  
227 In order to use the editor feature of the L<fc> command the module
228 L<File::Temp> should be installed.
229  
230 =head1 EXPORT
231  
232 None by default.
233  
234 =head1 CONFIG
235  
236 =over 4
237  
238 =item loghist
239  
240 Unless this config is set no commands are recorded.
241  
242 =item logfile
243  
244 File to store the history. Defaults to "~/.%s.log.yaml" where '%s' is
245 replaced with the program name. Hence the default for B<zoid> is
246 F<~/.zoid.log.yaml>.
247  
248 =item maxlines
249  
250 Maximum number of lines in the history. If not set the environment variable
251 'HISTSIZE' is used. In fact the number of lines can be a bit more then this
252 value on run time because the file is not purged after every write.
253  
254 =item no_duplicates
255  
256 If set a command will not be saved if it is the same as the previous command.
257  
258 =item keep
259  
260 Hash with log types mapped to a number representing the maximal number of lines
261 to keep in memory for this type. In contrast to the commandline history,
262 history arrays for these types are completely managed by this module.
263  
264 =back
265  
266 =head1 COMMANDS
267  
268 =over 4
269  
270 =item fc [-r][-e editor] [I<first> [I<last>]]
271  
272 =item fc -l [-nr] [I<first> [I<last>]]
273  
274 =item fc -s [I<old>=I<new>] [I<first> [I<last>]]
275  
276 "Fix command", this builtin allows you to edit and re-execute commands
277 from the history. I<first> and I<last> are either command numbers or strings
278 matching the beginning of a command; a negative number is used to designate
279 commands by counting back from the current one. Use the '-l' option to list
280 the commands in the history, and the '-n' switch to surpress the command
281 numbers in the listing.The '-r' switch reverses the order of the commands.
282 The '-s' switch re-executes the commands without editing.
283  
284 I<first> and I<last> default to '-16' and '-1' when the '-l' option is given.
285 Otherwise I<first> defaults to '-1' and I<last> defaults to I<first>.
286  
287 Note that the selection of the editor is not POSIX compliant
288 but follows bash, if no editor is given using the '-e' option
289 the environment variables 'FCEDIT' and 'EDITOR' are both checked,
290 if neither is set, B<vi> is used.
291 ( According to POSIX we should use 'ed' by default and probably
292 ignore the 'EDITOR' varaiable, but I don't think that is "What You Want" )
293  
294 Following B<zsh> setting the editor to '-' is identical with using
295 the I<-s> switch.
296  
297 Also note that B<fc> removes itself from the history and adds the resulting
298 command instead.
299  
300 Typically B<r> is aliased to 'fc -s' so B<r> will re-execute the last
301 command, optionally followed by a substitution and/or a string to match
302 the begin of the command.
303  
304 TODO: regex/glob substitution for '-s' switch; now only does string substitution.
305  
306 =cut
307  
308 0 ▶sub fc {
Subroutine fc: not called CC=19 SCAR=59.4
Pod documented
309 0 my $self = shift;
310 0 my ($opt, $args) = getopt 'reverse,-r editor,-e$ list,-l nonu,-n -s -* +* @', @_;
311 0 ▶ unshift @$args, grep /^[+-]\d+$/, @{$$opt{_opts}} if exists $$opt{_opts};
Branch: 1 branch
BranchTrueFalseTotal
if exists $opt->{"_opts"} 0 0 0
312 0 ▶ my @replace = split('=', shift(@$args), 2) if $$args[0] =~ /=/;
Branch: 1 branch
BranchTrueFalseTotal
if $args->[0] =~ /=/ 0 0 0
313 0 ▶ error 'to many arguments' if @$args > 2;
Branch: 1 branch
BranchTrueFalseTotal
if @$args > 2 0 0 0
314 0 my ($first, $last) = @$args;
315  
316 # get selection
317 0 ▶ if (!$first) { ($first,$last) = $$opt{list} ? (-16, -1) : (-1, -1) }
Branch: 3 branches
BranchTrueFalseTotal
if (not $first) { } 0 0 0
elsif (not $last) { } 0 0 0
$opt->{'list'} ? : 0 0 0
318 0 ▶ elsif (!$last) { $last = $$opt{list} ? '-1' : $first }
Branch: 1 branch
BranchTrueFalseTotal
$opt->{'list'} ? : 0 0 0
319  
320 # list history ?
321 0 my @hist_opts = map "--$_", grep $$opt{$_}, qw/nonu reverse/;
322 0 ▶ return $$self{shell}->builtin('history', @hist_opts, $first, $last) if $$opt{list};
Branch: 1 branch
BranchTrueFalseTotal
if $opt->{"list"} 0 0 0
323  
324 # get/edit commands
325 my $cmd = join "\n",
326 0 @{ $$self{shell}->builtin('history', @hist_opts, $first, $last) };
327 0 ▶ $cmd =~ s{\Q$replace[0]\E}{$replace[1]}g if @replace;
Branch: 1 branch
BranchTrueFalseTotal
if @replace 0 0 0
328 0 ▶ my $editor = $$opt{editor} || $ENV{FCEDIT} || $ENV{EDITOR} || 'vi';
Condition0%0/8
$opt->{'editor'} || $ENV{'FCEDIT'} || $ENV{'EDITOR'} || 'vi'
l!l
00
$opt->{"editor"} or $ENV{"FCEDIT"} or $ENV{"EDITOR"}
l!l&&r!l&&!r
000
$opt->{"editor"} or $ENV{"FCEDIT"}
l!l&&r!l&&!r
000
MC/DC0%0/3
$opt->{'editor'} || $ENV{'FCEDIT'} || $ENV{'EDITOR'} || 'vi'
$opt->{"editor"} $ENV{"FCEDIT"} $ENV{"EDITOR"}
Truth table0%0/4
A or B or C
A= $opt->{"editor"}B= $ENV{"FCEDIT"}C= $ENV{"EDITOR"}
ABCresult
1XX1
01X1
0011
0000
329 0 ▶ unless ($$opt{'-s'} or $editor eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->{'-s'} or $editor eq '-') { } 0 0 0
Condition0%0/3
$opt->{"-s"} or $editor eq "-"
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt->{"-s"} or $editor eq "-"
$opt->{"-s"} $editor eq "-"
Truth table0%0/3
A or B
A= $opt->{"-s"}B= $editor eq "-"
ABresult
1X1
011
000
330 # edit history - editor behaviour consistent with T:RL:Z
331 0 debug "going to edit: << '...'\n$cmd\n...\nwith: $editor";
332 0 ▶ eval 'require File::Temp' || error 'need File::Temp from CPAN';
Branch: 1 branch
BranchTrueFalseTotal
eval "require File::Temp" or error("need File::Temp from CPAN") 0 0 0
333 0 my ($fh, $file) = File::Temp::tempfile(
334 'Zoid_fc_XXXXX', DIR => File::Spec->tmpdir );
335 0 print {$fh} $cmd;
336 0 close $fh;
337 0 $$self{shell}->shell($editor.' '.$file);
338 0 ▶ error if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
339 0 ▶ open TMP, $file or error "Could not read $file";
Branch: 1 branch
BranchTrueFalseTotal
open TMP, $file or error("Could not read $file") 0 0 0
340 0 my $cmd = join '', <TMP>;
341 0 close TMP;
342 0 unlink $file;
343 }
344 0 else { debug "going to execute without editing: << '...'\n$cmd\n..." }
345  
346 # execute commands
347 0 ▶ $$self{shell}->shell($cmd) if length $cmd;
Branch: 1 branch
BranchTrueFalseTotal
if length $cmd 0 0 0
348 0 $$self{shell}{previous_cmd} = $cmd; # reset string to be logged
349  
350 # TODO inherit environment and redirection from self
351 }
352  
353 =item history [--type I<type>] [--read] [-n|--nonu] [-r|--reverse] [I<first> [I<last>]]
354  
355 Returns (a part of) the history. By default it tries to find the commandline
356 history (depending on GetHistory), but the '--read' option forces reading the
357 history file. To get other log types, like 'pwd', use the '--type' option.
358 The '--nonu' option surpressees line numbering for the terminal output.
359  
360 The arguments I<first> and I<last> can either be a positive or negative integer,
361 representing the command number or reverse offset, or a string matching the begin
362 of the command. If only one integer is given I<last> defaults to '-1'; if only one
363 string is given I<last> defaults to I<first>. As a bonus you can supply a regex
364 reference instead of a string when using the perl interface.
365  
366 Note that unlike B<fc> the B<history> command is not specified by posix and
367 the implementation varies widely for different shells. In zoid, B<fc> is build on
368 top of B<history>, so options for B<history> are chosen consistently with B<fc>.
369  
370 =item log I<string> I<type>
371  
372 Adds I<string> to the history file with the current timestamp
373 and the supplied I<type> tag. The type defaults to "log".
374 If the type is set to "hist" the entry will become part of the
375 command history after the history file is read again.
376  
377 =back
378  
379 =head1 AUTHOR
380  
381 Jaap Karssenberg (Pardus) E<lt>pardus@cpan.orgE<gt>
382  
383 Copyright (c) 2011 Jaap G Karssenberg and Joel Berger. All rights reserved.
384 This program is free software; you can redistribute it and/or
385 modify it under the same terms as Perl itself.
386  
387 =head1 SEE ALSO
388  
389 L<Zoidberg>
390  
391 =cut
392  
393 1 1;
394  
↑ Summary blib/lib/Zoidberg/Fish/Intel.pm »