dbMan/Interface.pm untested

filter: Statementstmt 0.0% 0 / 140 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 0.0% 0 / 39 Podpod 0.0% 0 / 35 totaltotal 0.0% 0 / 245 CC 15 SCAR 16.9
CC
15
Cov
0%
get_action
12.0
get_prompt
12.0
loop
12.0
CRAP 5.4
1 package DBIx::dbMan::Interface;
2  
3 use strict;
4 use DBIx::dbMan::History;
5 use Term::ANSIColor qw/colorstrip/;
6  
7 our $VERSION = '0.18';
8  
9 1;
10  
11 sub new {
12 my $class = shift;
13 my $obj = bless { @_ }, $class;
14 $obj->{prompt_num} = 0;
15 $obj->{actionlist} = [];
16 $obj->init();
17 return $obj;
18 }
19  
20 sub init {
21 my $obj = shift;
22 $obj->prompt($obj->register_prompt(99999999),'SQL:');
23
24 $obj->{history} = new DBIx::dbMan::History
25 -config => $obj->{-config}, -interface => $obj;
26 }
27  
28 sub print {
29 my $obj = shift;
30  
31 my $s = join '', @_;
32  
33 if ( $obj->is_utf8 ) {
34 binmode STDOUT, ':utf8';
35 }
36 else {
37 $s = $obj->{ -lang }->str( $s );
38 }
39  
40 print $s;
41 }
42  
43 sub trace {
44 my $obj = shift;
45 print STDERR join '',@_;
46 }
47  
48 sub hello {
49 my $obj = shift;
50 $obj->print("This is dbMan, Version $main::DBIx::dbMan::VERSION.\n\n");
51 }
52  
53 sub goodbye {
54 my $obj = shift;
55 $obj->print("Bye.\n");
56 }
57  
58 sub get_action {
59 my $obj = shift;
60 my %action = qw/action NONE/;
61  
62 if (@{$obj->{actionlist}}) {
63 my $action = shift @{$obj->{actionlist}};
64 %action = %$action;
65 } else {
66 my $command = $obj->get_command();
67 $command =~ s/\n+$//s;
68  
69 if ($command) {
70 $action{action} = 'COMMAND';
71 $action{flags} = 'real';
72 $action{cmd} = $command;
73 } else {
74 $action{action} = 'IDLE';
75 }
76 }
77  
78 return %action;
79 }
80  
81 sub prompt {
82 my ($obj,$num,$prompt) = @_;
83  
84 $obj->{prompt}->[$num] = $prompt;
85 }
86  
87 sub get_prompt {
88 my $obj = shift;
89 my %params = @_;
90  
91 my $prompt = '';
92 for (sort {
93 ($obj->{prompt_priority_list}->[$a] == $obj->{prompt_priority_list}->[$b])
94 ? ($b <=> $a)
95 : ($obj->{prompt_priority_list}->[$a] <=> $obj->{prompt_priority_list}->[$b])
96 } 1..$obj->{prompt_num}) {
97  
98 next unless $obj->{ prompt }->[ $_ ];
99  
100 $prompt .= $obj->{prompt}->[$_] . ' ';
101 }
102 return $prompt;
103 }
104  
105 sub get_command {
106 my $obj = shift;
107 $obj->print($obj->get_prompt);
108 my $command = <>;
109 return $command;
110 }
111  
112 sub error {
113 my $obj = shift;
114 $obj->print("ERROR: ",join '',@_,"\n");
115 }
116  
117 sub get_password {
118 my $obj = shift;
119 system 'stty -echo';
120 $obj->print(shift || 'Password: ');
121 my $pass = <>; $pass =~ s/\n$//;
122 system 'stty echo';
123 print "\n";
124 return $pass;
125 }
126  
127 sub render_size {
128 my $obj = shift;
129 return 79;
130 }
131  
132 sub register_prompt {
133 my ($obj,$priority) = @_;
134 $priority = 0 unless $priority;
135 $obj->{prompt_priority_list}->[++$obj->{prompt_num}] = $priority;
136 return $obj->{prompt_num};
137 }
138  
139 sub deregister_prompt {
140 my ($obj,$prompt_id) = @_;
141 return unless defined $prompt_id;
142 splice @{$obj->{prompt_priority_list}},$prompt_id,1;
143 splice @{$obj->{prompt}},$prompt_id,1;
144 --$obj->{prompt_num};
145 }
146  
147 sub add_to_actionlist {
148 my $obj = shift;
149 my $action = shift;
150 push @{$obj->{actionlist}},$action;
151 }
152  
153 sub filenames_complete {
154 my $obj = shift;
155 my $pattern = shift;
156  
157 my @files = (<$pattern*>);
158 foreach (@files) {
159 $_ .= '/' if -d _;
160 }
161 return @files;
162 }
163  
164 sub loop {
165 my $obj = shift;
166 my %action = ();
167  
168 do {
169 eval {
170 %action = $obj->get_action();
171 do {
172 %action = $obj->{-core}->handle_action(%action);
173 } until ($action{processed});
174 };
175 if ( $@ =~ /^Catched signal INT/ ) {
176 $obj->error( "Interrupted by user." );
177 }
178 elsif ( $@ ) {
179 die $@;
180 }
181 } until ($action{action} eq 'QUIT');
182 }
183  
184 sub history_clear {
185 my $obj = shift;
186 $obj->{history}->clear();
187 }
188  
189 sub history_add {
190 my $obj = shift;
191 $obj->{history}->add(@_);
192 }
193  
194 sub rebuild_menu {
195 # nothing to do, special purpose for descendant
196 }
197  
198 sub bind_key {
199 # we can't do anything, it's for descendant
200 }
201  
202 sub get_key {
203 # we can't do anything, it's for descendant
204 }
205  
206 sub can_pager {
207 return 1;
208 }
209  
210 sub clear_screen {
211 my $obj = shift;
212  
213 eval {
214 use Term::Screen;
215  
216 my $scr = new Term::Screen;
217 die "no" unless $scr;
218 $scr->clrscr();
219 };
220 if ($@) { # fallback
221 my $oldpath = $ENV{PATH};
222 $ENV{PATH} = '';
223 system '/usr/bin/clear';
224 $ENV{PATH} = $oldpath;
225 }
226 }
227  
228 sub go_away {
229  
230 }
231  
232 sub come_back {
233  
234 }
235  
236 sub gather_complete {
237 my ($obj,$text,$line,$start) = @_;
238 my %action = (action => 'LINE_COMPLETE',
239 text => $text, line => $line, start => $start);
240 do {
241 %action = $obj->{-core}->handle_action(%action);
242 } until ($action{processed});
243 return @{$action{list}} if ref $action{list} eq 'ARRAY';
244 return $action{list} if $action{list};
245 return ();
246 }
247  
248 sub status {
249  
250 }
251  
252 sub nostatus {
253  
254 }
255  
256 sub print_prompt {
257 my $obj = shift;
258  
259 $obj->print("\n".join('',@_)."\n");
260 }
261  
262 sub gui {
263 return 0;
264 }
265  
266 sub current_line {
267 my $obj = shift;
268  
269 return '';
270 }
271  
272 sub infobox {
273 my $obj = shift;
274  
275 $obj->print( @_ );
276 }
277  
278 sub is_utf8 {
279 my $obj = shift;
280  
281 return 0; # we don't know
282 }