Shell/History.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Shell/Command.pm ↑ Summary Shell/Rc.pm »
1 #
2 # $Id$
3 #
4 # shell::history Brik
5 #
6 package Metabrik::Shell::History;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(command) ],
16 attributes => {
17 history_file => [ qw(file) ],
18 },
19 commands => {
20 load => [ ],
21 write => [ ],
22 get => [ ],
23 get_one => [ qw(integer) ],
24 get_range => [ qw(integer_first..integer_last) ],
25 show => [ ],
26 execute => [ qw(integer|integer_first..integer_last) ],
27 },
28 };
29 }
30  
31 sub brik_use_properties {
32 my $self = shift;
33  
34 return {
35 attributes_default => {
36 history_file => defined($self->global) && $self->global->homedir.'/.metabrik_history'
37 || defined($ENV{HOME}) && $ENV{HOME}.'/.metabrik_history'
38 || '/tmp/.metabrik_history',
39 },
40 };
41 }
42  
43 sub load {
44 my $self = shift;
45  
46 if (! defined($self->shell)) {
47 return $self->log->error("load: no core::shell Brik");
48 }
49  
50 my $shell = $self->shell;
51 my $history_file = $self->history_file;
52  
53 if ($shell->term->can('ReadHistory')) {
54 if (! -f $history_file) {
55 return $self->log->error("load: can't find history file [$history_file]");
56 }
57  
58 $shell->term->ReadHistory($history_file)
59 or return $self->log->error("load: can't ReadHistory file [$history_file]: $!");
60  
61 $self->log->debug("load: success");
62 }
63 else {
64 $self->log->warning("load: cannot ReadHistory");
65 }
66  
67 return 1;
68 }
69  
70 sub write {
71 my $self = shift;
72  
73 if (! defined($self->shell)) {
74 return $self->log->error("write: no core::shell Brik");
75 }
76  
77 my $shell = $self->shell;
78 my $history_file = $self->history_file;
79  
80 if ($shell->term->can('WriteHistory')) {
81 $shell->term->WriteHistory($history_file)
82 or return $self->log->error("load: can't WriteHistory file [$history_file]: $!");
83 $self->log->debug("write: success");
84 }
85 else {
86 $self->log->warning("load: cannot WriteHistory");
87 }
88  
89 return 1;
90 }
91  
92 sub get {
93 my $self = shift;
94  
95 if (! defined($self->shell)) {
96 return $self->log->error("get: no core::shell Brik");
97 }
98  
99 my $shell = $self->shell;
100  
101 my @history = ();
102 if ($shell->term->can('GetHistory')) {
103 @history = $shell->term->GetHistory;
104  
105 $self->log->debug("get: success");
106 }
107 else {
108 $self->log->warning("load: cannot GetHistory");
109 }
110  
111 return \@history;
112 }
113  
114 sub get_one {
115 my $self = shift;
116 my ($number) = @_;
117  
118 if (! defined($self->shell)) {
119 return $self->log->error("get_one: no core::shell Brik");
120 }
121  
122 $self->brik_help_run_undef_arg('get_one', $number) or return;
123 if ($number !~ /^\d+$/) {
124 return $self->log->error($self->brik_help_run('get_one'));
125 }
126  
127 my $shell = $self->shell;
128  
129 my $history = '';
130 my @history = ();
131 if ($shell->term->can('GetHistory')) {
132 @history = $shell->term->GetHistory;
133 $history = $history[$number];
134  
135 $self->log->debug("get_one: success");
136 }
137 else {
138 $self->log->warning("load: cannot GetHistory");
139 }
140  
141 return $history;
142 }
143  
144 sub get_range {
145 my $self = shift;
146 my ($range) = @_;
147  
148 if (! defined($self->shell)) {
149 return $self->log->error("get_range: no core::shell Brik");
150 }
151  
152 $self->brik_help_run_undef_arg('get_range', $range) or return;
153 if ($range !~ /^\d+\.\.\d+$/) {
154 return $self->log->error($self->brik_help_run('get_range'));
155 }
156  
157 my $shell = $self->shell;
158  
159 my @history = ();
160 if ($shell->term->can('GetHistory')) {
161 @history = $shell->term->GetHistory;
162 @history = @history[eval($range)];
163  
164 $self->log->debug("get_range: success");
165 }
166 else {
167 $self->log->warning("load: cannot GetHistory");
168 }
169  
170 return \@history;
171 }
172  
173 sub show {
174 my $self = shift;
175  
176 my $history = $self->get;
177  
178 my $count = 0;
179 for (@$history) {
180 print "$_ # ! $count\n";
181 $count++;
182 }
183  
184 return $count - 1;
185 }
186  
187 sub execute {
188 my $self = shift;
189 my ($numbers) = @_;
190  
191 if (! defined($self->shell)) {
192 return $self->log->error("execute: no core::shell Brik");
193 }
194  
195 $self->brik_help_run_undef_arg('execute', $numbers) or return;
196  
197 # We want to execute some history command(s)
198 my $lines = [];
199 if ($numbers =~ /^\d+$/) {
200 $lines = [ $self->get_one($numbers) ];
201 }
202 elsif ($numbers =~ /^\d+\.\.\d+$/) {
203 $lines = $self->get_range($numbers);
204 }
205  
206 my $shell = $self->shell;
207  
208 $shell->cmdloop($lines);
209  
210 return 1;
211 }
212  
213 1;
214  
215 __END__
« Shell/Command.pm ↑ Summary Shell/Rc.pm »