File Coverage

blib/lib/Bash/History/Read.pm
Criterion Covered Total %
statement 37 42 88.1
branch 5 10 50.0
condition 1 6 16.6
subroutine 8 9 88.8
pod 2 2 100.0
total 53 69 76.8


line stmt bran cond sub pod time code
1             package Bash::History::Read;
2              
3             our $DATE = '2015-11-05'; # DATE
4             our $VERSION = '0.03'; # VERSION
5              
6 1     1   2755 use 5.010001;
  1         3  
7 1     1   5 use strict;
  1         2  
  1         20  
8 1     1   4 use warnings;
  1         1  
  1         38  
9              
10 1     1   5 use Exporter 'import';
  1         1  
  1         466  
11             our @EXPORT = qw(each_hist);
12             our @EXPORT_OK = qw(parse_bash_history_file);
13              
14             sub _doit {
15 1     1   3 my ($which, $code, $fh0) = @_;
16              
17             my $call_code = sub {
18 5     5   9 my ($ts, $content) = @_;
19 5         8 package main {
20 5         8 local $_ = $content;
21 5         9 local $main::TS = $ts;
22 5         6 local $main::PRINT = 1;
23 5         10 $code->();
24 5 50       16 if ($main::PRINT) {
25 0 0       0 print "#$ts\n" if defined $ts;
26 0         0 print;
27             }
28             }
29 1         4 };
30              
31 1         3 my $fh;
32 1 50       4 if ($which eq 'each_hist') {
33 0         0 $fh = \*ARGV;
34             } else {
35 1         2 $fh = $fh0;
36             }
37              
38 1         2 my $ts;
39 1         25 while (defined(my $line = <$fh>)) {
40 7 100       23 if ($line =~ /\A#(\d+)$/) {
41 2         10 $ts = $1;
42             } else {
43 5         10 $call_code->($ts, $line);
44 5         25 undef $ts;
45             }
46             }
47             }
48              
49             sub each_hist(&) {
50 0     0 1 0 my $code = shift;
51 0         0 _doit('each_hist', $code);
52             }
53              
54             sub parse_bash_history_file {
55 1     1 1 9 my ($path) = @_;
56              
57 1   0     5 $path //= $ENV{HISTFILE} // "$ENV{HOME}/.bash_history";
      33        
58              
59 1 50       93 open my($fh), "<", $path or die "Can't open bash history file '$path': $!";
60 1         4 my $res = [];
61             _doit('parse_bash_history_file',
62             sub {
63 5     5   14 push @$res, [$main::TS, $_];
64 5         6 $main::PRINT = 0;
65             },
66 1         8 $fh,
67             );
68 1         21 $res;
69             }
70              
71             1;
72             # ABSTRACT: Utility to read bash history file entries
73              
74             __END__