lib/DB/Hooks/HistoryTerminal.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 16 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 170 CC 14 SCAR 26.5
CC
14
Cov
0%
on_call
42.0
on_return
30.0
SetHistory
6.0
CRAP 14.1
1 package DB::Hooks::HistoryTerminal;
2  
3 use strict;
4 use warnings;
5 use Term::ReadLine::Tiny;
6 use File::Slurper 'read_lines';
7 use open qw( :encoding(UTF-8) :std );
8  
9 # We can not track DB::Commands loading if 'use' it before DB::Hooks
10 use DB::Commands qw/ set_command get_command run /;
11  
12 # Commands should be available immediately. This is important when
13 # we are debugging at CT: when DB is in process of loading
14 BEGIN { set_command next_command => \&readline; }
15  
16 my $fn = $ENV{HOME} . '/.dbhooks_history';
17  
18 if ( !( -e $fn ) ) {
19 CleanHistory();
20 }
21  
22 my $invitation = "\n\e[2;96mDBG>\e[0m";
23 my $counter = 0;
24 my $term = Term::ReadLine::Tiny->new();
25  
26 #Write in file
27 sub SetHistory {
28 my $i = 0;
29 my @content = $term->{history}->@*;
30  
31 open my $fh, '>', $fn or die "Can't open > $fn: $!";
32  
33 while ( $i <= scalar(@content) ) {
34  
35 if ( !defined( $content[$i] ) ) {
36 last;
37 }
38  
39 print $fh $content[$i], "\n";
40 $i++;
41  
42 }
43  
44 close $fh or warn "Close failed: $!";
45 }
46  
47 #Read from file
48 sub GetHistory {
49 my @content = read_lines($fn);
50  
51 push( $term->{history}->@*, @content );
52 }
53  
54 #Clean all history from file
55 sub CleanHistory {
56  
57 open FILE, '>', $fn or die "Can't open > $fn: $!";
58  
59 print FILE "";
60  
61 close FILE or warn "Close failed: $!";
62 }
63  
64 # #View all command in history
65 # sub HistoryMenu{
66 # # my ( $history ) = @_;
67 # # $prim_command = $buffer ne "" ? $buffer : $prim_command;
68  
69 # # print "\nHistory:\n";
70 # # foreach my $n ( @$history ) {
71 # # print $n, "\n";
72 # # }
73  
74 # # $prim_command = $prim_command ? $prim_command : "";
75  
76 # # return $prim_command;
77 # # }
78  
79 my $last_command;
80 my $command;
81 GetHistory();
82 BEGIN { $last_command = '' }
83  
84 sub readline {
85  
86 $command = $term->readline($invitation);
87 chomp $command;
88  
89 return $last_command unless length $command;
90  
91 return $last_command = $command;
92 }
93  
94 sub on_load {
95 my ($file) = @_;
96  
97 return unless ( DB::state('TraceLoad') // 0 ) & 1;
98  
99 DB::say "\e[32mLoaded\e[0m: \e[1;33m$file\e[0m";
100 }
101  
102 sub on_goto {
103 my ($sub) = @_;
104  
105 return unless ( DB::state('TraceGoto') // 0 ) & 1;
106  
107 DB::say "Goto \e[32m$sub\e[0m";
108 }
109  
110 sub on_trace {
111 my ( $source, $file, $line ) = @_;
112 $source =~ s/^\s+//;
113  
114 DB::say "\e[2;37m$file:$line\e[0m", "t: \e[1;33m$source\e[0m";
115 }
116  
117 sub on_call {
118 if ( DB::state('TraceStack') ) {
119 my $stack = DB::state('stack');
120 DB::state( stack => $stack = [] ) unless $stack;
121 push @$stack, $_[1];
122 }
123  
124 return unless ( DB::state('TraceCall') // 0 ) & 1;
125  
126 my ( $sub, $context, @args ) = @_;
127 $context = $context ? '@' : defined $context ? '$' : ';';
128 $sub = "\e[1;32m$sub\e[0m";
129 @args = DB::vis_undef(@args);
130  
131 local $" = ', ';
132 DB::say "$context $sub( \e[32m@args\e[0m )";
133 }
134  
135 sub on_return {
136 if ( DB::state('TraceStack') ) {
137 my $stack = DB::state('stack');
138 pop @$stack;
139 }
140  
141 return unless ( DB::state('TraceReturn') // 0 ) & 1;
142  
143 my ( $sub, $context, @values ) = @_;
144  
145 $context = $context ? '@' : defined $context ? '$' : ';';
146 $sub = "\e[32m$sub\e[0m";
147 @values = DB::vis_undef(@values);
148  
149 local $" = ', ';
150 DB::say "$context( \e[32m@values\e[0m ) <-- $sub";
151 }
152  
153 sub on_binteract {
154 run('l 0');
155 }
156  
157 sub on_interact { get_command('interact')->(@_) }
158  
159 # Subscribe subs to events
160 # on_interact (some debugger commands) does not work without on_call/on_return
161 # NOTICE: subscribe in the given order
162 use DB::Utils qw/ trace call return interact load goto binteract /;
163  
164 END { SetHistory(); }
165  
166 1;