dbMan/Interface/curses.pm untested

filter: Statementstmt 0.0% 0 / 385 Branchbran 0.0% 0 / 106 Conditioncond 0.0% 0 / 40 MC/DCmcdc 0.0% 0 / 23 Subroutinesub 0.0% 0 / 45 Podpod 0.0% 0 / 37 totaltotal 0.0% 0 / 636 CC 54 SCAR 45.9
CC
54
Cov
0%
search
380.0
completation
132.0
init
56.0
CRAP 98.4
1 package DBIx::dbMan::Interface::curses;
2  
3 use strict;
4 use DBIx::dbMan::History;
5 use Curses;
6 use Curses::UI;
7 use Curses::UI::Common;
8 use base 'DBIx::dbMan::Interface';
9  
10 our $VERSION = '0.02';
11  
12 1;
13  
14 sub init {
15 my $obj = shift;
16  
17 $obj->SUPER::init(@_);
18  
19 $obj->{want_color} = 1;
20 $obj->{want_color} = 0 if $obj->{-config}->color() =~ /^(0|no|false|off)$/;
21 $obj->{want_compat} = 0;
22 $obj->{want_compat} = 1 if $obj->{-config}->compat_mode() =~ /^(1|yes|true|on)$/;
23  
24 $obj->{ui} = new Curses::UI -color_support => $obj->{want_color},
25 -clear_on_exit => 1, -mouse_support => 1,
26 -compat => $obj->{want_compat};
27  
28 my @colors = ();
29 if ($obj->{want_color}) {
30 push @colors, -bg => $obj->{-config}->menu_bg if $obj->{-config}->menu_bg;
31 push @colors, -fg => $obj->{-config}->menu_fg if $obj->{-config}->menu_fg;
32 }
33 $obj->{menu} = $obj->{ui}->add('menu', 'Menubar', -menu => [],
34 -menuhandler => sub { $obj->menu_action(@_); }, @colors);
35  
36 my $sqlheight = $obj->{-config}->sql_window_height || 5;
37  
38 $obj->{sqlinput} = $obj->{ui}->add('sqlinput', 'Window',
39 -border => 1, -y => -1, -height => $sqlheight);
40 $obj->{area_window} = $obj->{ui}->add('area_window', 'Window',
41 -border => 0, -y => 1, -padbottom => $sqlheight);
42  
43 $obj->{sqleditor} = $obj->{sqlinput}->add('editor','TextEditor',
44 -vscrollbar => 1, -wrapping => 1);
45 $obj->{area} = $obj->{area_window}->add('area', 'TextViewer',
46 -text => '', -wrapping => 0, -vscrollbar => 1, -hscrollbar => 0);
47  
48 $obj->{ui}->set_binding(sub { $obj->{menu}->focus(); }, KEY_F(10));
49  
50 $obj->{sqleditor}->set_binding(sub { $obj->internal_loop(); }, KEY_ENTER());
51 $obj->{sqleditor}->set_binding(sub { $obj->next_history(); }, KEY_DOWN());
52 $obj->{sqleditor}->set_binding(sub { $obj->prev_history(); }, KEY_UP());
53 $obj->{sqleditor}->set_binding(sub { $obj->completation(); }, "\cI");
54 $obj->{sqleditor}->set_binding(sub { $obj->search('start'); }, "\cR");
55 $obj->{sqleditor}->set_binding(sub { $obj->page_down(); }, KEY_NPAGE());
56 $obj->{sqleditor}->set_binding(sub { $obj->page_up(); }, KEY_PPAGE());
57
58 $obj->refresh_ui();
59
60 $obj->horizontal_scrollbar(
61 ($obj->{-config}->horizontal_scrollbar =~ /^(yes|on|true|1|y|)$/)?1:0);
62  
63 $obj->{history}->load_and_store;
64 }
65  
66 sub search {
67 my $obj = shift;
68 my $mode = shift;
69  
70 if ($mode eq 'start') {
71 $obj->{default_bindings}->{escape} = $obj->{sqleditor}->{-bindings}->{CUI_ESCAPE()};
72 $obj->{default_bindings}->{default} = $obj->{sqleditor}->{-bindings}->{''};
73 $obj->{default_bindings}->{left} = $obj->{sqleditor}->{-bindings}->{KEY_LEFT()};
74 $obj->{default_bindings}->{right} = $obj->{sqleditor}->{-bindings}->{KEY_RIGHT()};
75 $obj->{default_bindings}->{to_start} = $obj->{sqleditor}->{-bindings}->{"\cA"};
76 $obj->{default_bindings}->{to_end} = $obj->{sqleditor}->{-bindings}->{"\cE"};
77  
78 $obj->{sqleditor}->set_binding(sub { $obj->search('next'); }, "\cR");
79 $obj->{sqleditor}->set_binding(sub { $obj->search('stop'); }, CUI_ESCAPE());
80 $obj->{sqleditor}->set_binding(sub { $obj->search('search',@_); }, '');
81 $obj->{sqleditor}->set_binding(sub { $obj->search('search',@_); }, KEY_BACKSPACE());
82 $obj->{sqleditor}->set_binding(sub { $obj->search('stop',@_); }, KEY_LEFT());
83 $obj->{sqleditor}->set_binding(sub { $obj->search('stop',@_); }, KEY_RIGHT());
84 $obj->{sqleditor}->set_binding(sub { $obj->search('stop',@_); }, "\cE");
85 $obj->{sqleditor}->set_binding(sub { $obj->search('stop',@_); }, "\cA");
86  
87 $obj->{search_pattern} = '';
88 $obj->refresh_ui;
89 } elsif ($mode eq 'next') {
90 if ($obj->{search_pattern}) {
91 my $line = $obj->{history}->reverse_search($obj->{search_pattern},1);
92 if ($line) {
93 $obj->{sqleditor}->text($line);
94 $obj->{sqleditor}->{-pos} = length $1 if $line =~ /^(.*)$obj->{search_pattern}/i;
95 $obj->{sqleditor}->draw();
96 } else {
97 $obj->{ui}->dobeep();
98 }
99 }
100 } elsif ($mode eq 'stop') {
101 my $origobj = shift; my $key = shift;
102  
103 delete $obj->{search_pattern};
104 $obj->refresh_ui;
105  
106 $obj->{sqleditor}->set_binding(sub { $obj->search('start'); }, "\cR");
107 $obj->{sqleditor}->{-bindings}->{CUI_ESCAPE()} = $obj->{default_bindings}->{escape} if exists $obj->{default_bindings}->{escape};
108 $obj->{sqleditor}->{-bindings}->{''} = $obj->{default_bindings}->{default} if exists $obj->{default_bindings}->{default};
109 $obj->{sqleditor}->{-bindings}->{KEY_LEFT()} = $obj->{default_bindings}->{left} if exists $obj->{default_bindings}->{left};
110 $obj->{sqleditor}->{-bindings}->{KEY_RIGHT()} = $obj->{default_bindings}->{right} if exists $obj->{default_bindings}->{right};
111 $obj->{sqleditor}->{-bindings}->{"\cE"} = $obj->{default_bindings}->{to_end} if exists $obj->{default_bindings}->{to_end};
112 $obj->{sqleditor}->{-bindings}->{"\cA"} = $obj->{default_bindings}->{to_start} if exists $obj->{default_bindings}->{to_start};
113  
114 $origobj->process_bindings($key) if $key;
115 } elsif ($mode eq 'search') {
116 shift; my $key = shift;
117  
118 if ($key eq KEY_BACKSPACE()) {
119 if ($obj->{search_pattern}) {
120 $obj->{search_pattern} =~ s/.$//;
121 $key = '';
122 $obj->refresh_ui;
123 } else {
124 $obj->{ui}->dobeep();
125 }
126 } else {
127 my $line = $obj->{history}->reverse_search($obj->{search_pattern}.$key);
128 if ($line) {
129 $obj->{search_pattern} .= $key;
130 $obj->refresh_ui;
131 $obj->{sqleditor}->text($line);
132 $obj->{sqleditor}->{-pos} = length $1 if $line =~ /^(.*)$obj->{search_pattern}/i;
133 $obj->{sqleditor}->draw();
134 } else {
135 $obj->{ui}->dobeep();
136 }
137 }
138 }
139 }
140  
141 sub page_up {
142 my $obj = shift;
143  
144 $obj->search('stop');
145 $obj->{area}->cursor_pageup();
146 $obj->{area}->draw();
147 }
148  
149 sub page_down {
150 my $obj = shift;
151  
152 $obj->search('stop');
153 $obj->{area}->cursor_pagedown();
154 $obj->{area}->draw();
155 }
156  
157 sub completation {
158 my $obj = shift;
159  
160 $obj->search('stop');
161 my $line = $obj->current_line();
162 my $text = $line;
163 $text = $1 if $line =~ /(\S*)$/;
164 my $start = 0;
165 $start = length($line)-length($1) if $line =~ /\s+(\S*)$/;
166  
167 my @exprs = grep !/^$/,$obj->gather_complete($text,$line,$start);
168 if (@exprs == 1) {
169 $line = substr($line,0,$start).$exprs[0].' ';
170 $obj->{sqleditor}->text($line);
171 $obj->{sqleditor}->cursor_to_end();
172 $obj->{sqleditor}->draw();
173 } elsif (@exprs) {
174 my $maxlength = 0;
175 my $prefix = $exprs[0];
176 for my $expr (@exprs) {
177 $maxlength = length $expr if length $expr > $maxlength;
178 if ($prefix) {
179 my @prefix = split //,$prefix;
180 my @expr = split //,$expr;
181 $prefix = '';
182 for (0..((@prefix < @expr)?@prefix-1:@expr-1)) {
183 if ($prefix[$_] eq $expr[$_]) {
184 $prefix .= $prefix[$_];
185 } else {
186 last;
187 }
188 }
189 }
190 }
191 $maxlength += 2;
192 use integer;
193 my $cols = $obj->render_size / $maxlength;
194 my $rows = @exprs / $cols;
195 ++$rows if $cols * $rows < @exprs;
196 no integer;
197 my @showlist = ();
198 for my $col (1..$cols) {
199 for my $row (1..$rows) {
200 $showlist[$col][$row] = shift(@exprs)|| '';
201 }
202 }
203 my $output = "Complete to:\n";
204 for my $row (1..$rows) {
205 for my $col (1..$cols) {
206 $output .= sprintf "%*s",-$maxlength,$showlist[$col][$row];
207 }
208 $output .= "\n";
209 }
210 $obj->print($output);
211  
212 if ($prefix) {
213 $line = substr($line,0,$start).$prefix;
214 $obj->{sqleditor}->text($line);
215 $obj->{sqleditor}->cursor_to_end();
216 $obj->{sqleditor}->draw();
217 }
218 } else {
219 $obj->{ui}->dobeep();
220 }
221 }
222  
223 sub next_history {
224 my $obj = shift;
225  
226 $obj->search('stop');
227 $obj->{sqleditor}->text($obj->{history}->next);
228 $obj->{sqleditor}->cursor_to_end();
229 $obj->{sqleditor}->draw();
230 }
231  
232 sub prev_history {
233 my $obj = shift;
234  
235 $obj->search('stop');
236 $obj->{sqleditor}->text($obj->{history}->prev);
237 $obj->{sqleditor}->cursor_to_end();
238 $obj->{sqleditor}->draw();
239 }
240  
241 sub get_command {
242 my $obj = shift;
243  
244 my $what = $obj->current_line();
245 if ($what) {
246 $what =~ s/\n/ /gs;
247 $obj->{sqleditor}->text('');
248 $obj->print($obj->get_prompt.$what."\n");
249 $obj->{history}->add($what);
250 }
251 return $what;
252 }
253  
254 sub internal_loop {
255 my $obj = shift;
256  
257 $obj->search('stop');
258  
259 my %action = ();
260  
261 my $idle;
262 do {
263 $idle = 0;
264 %action = $obj->get_action();
265 ++$idle if $action{action} eq 'IDLE';
266 do {
267 %action = $obj->{-core}->handle_action(%action);
268 } until ($action{processed});
269 } until (($idle and $action{action} eq 'NONE') or $action{action} eq 'QUIT');
270  
271 $obj->nostatus;
272 $obj->refresh_ui();
273  
274 die if $action{action} eq 'QUIT';
275 }
276  
277 sub goodbye {
278 my $obj = shift;
279  
280 $obj->SUPER::goodbye();
281 sleep 1;
282 }
283  
284 sub loop {
285 my $obj = shift;
286  
287 $obj->{ui}->draw();
288  
289 $obj->internal_loop();
290  
291 eval {
292 $obj->{ui}->mainloop();
293 };
294 }
295  
296 sub print {
297 my $obj = shift;
298  
299 $obj->{area}->text($obj->{area}->get().$obj->{-lang}->str(@_));
300 $obj->{area}->cursor_to_end();
301 $obj->{area}->draw();
302 }
303  
304 sub refresh_ui {
305 my $obj = shift;
306  
307 my $prompt = $obj->get_prompt; $prompt =~ s/\s$//;
308 $prompt = "'reverse-i-search: $obj->{search_pattern}' ".$prompt if exists $obj->{search_pattern};
309 $obj->{sqlinput}->title($prompt);
310 $obj->{sqlinput}->draw();
311  
312 $obj->{sqleditor}->focus();
313 $obj->{sqlinput}->focus();
314 }
315  
316 sub can_pager {
317 return 0;
318 }
319  
320 sub clear_screen {
321 my $obj = shift;
322  
323 $obj->{area}->text('');
324 $obj->{area}->cursor_to_end();
325 $obj->{area}->draw();
326 }
327  
328 sub go_away {
329 my $obj = shift;
330  
331 $obj->{ui}->leave_curses();
332 }
333  
334 sub come_back {
335 my $obj = shift;
336  
337 $obj->{ui}->reset_curses();
338 $obj->{ui}->draw();
339 $obj->refresh_ui();
340 }
341  
342 sub menu_action {
343 my $obj = shift;
344 shift; # menulistbox object - not needed
345 my $action = shift;
346  
347 $action->{gui} = 1; # we prefer gui version if exists
348  
349 $obj->add_to_actionlist($action);
350 $obj->internal_loop();
351 }
352  
353 sub ignore_trail {
354 my $what = shift;
355 $what =~ s/^[ *]+//;
356 return $what;
357 }
358  
359 sub create_submenu {
360 my $obj = shift;
361 my $menuref = shift;
362  
363 my @submenu = ();
364 for (sort { (($b->{preference} || 0) <=> ($a->{preference} || 0)) ||
365 (uc ignore_trail($a->{label} || '-') cmp
366 uc ignore_trail($b->{label} || '-')) } @$menuref) {
367 my @params = ();
368 if (exists $_->{submenu}) {
369 push @params, -submenu => $obj->create_submenu($_->{submenu});
370 } elsif (exists $_->{action}) {
371 push @params, -value => $_->{action};
372 }
373 if (exists $_->{separator}) {
374 push @submenu, { -label => '-' };
375 } else {
376 push @submenu, { -label => ' '.$_->{label}, @params };
377 }
378 }
379 my $maxlength = 0;
380 for (@submenu) {
381 $maxlength = length $_->{-label} if length $_->{-label} > $maxlength;
382 }
383 ++$maxlength;
384 for (@submenu) {
385 $_->{-label} = '-' x $maxlength if $_->{-label} eq '-';
386 $_->{-label} = $_->{-label} . (' ' x ($maxlength-length($_->{-label})));
387 }
388  
389 return \@submenu;
390 }
391  
392 sub insert_menu {
393 my ($obj,$menu,$newmenu) = @_;
394  
395 for my $item (@$newmenu) {
396 if (exists $item->{label}) {
397 my @arr = grep { $item->{label} eq $_->{label} } @$menu;
398 if (@arr) {
399 for (@arr) {
400 if (exists $_->{submenu}) {
401 $obj->insert_menu($_->{submenu},$item->{submenu});
402 } else {
403 push @$menu,$item;
404 }
405 }
406 } else {
407 push @$menu,$item;
408 }
409 } else {
410 push @$menu,$item;
411 }
412 }
413 }
414  
415 sub rebuild_menu {
416 my $obj = shift;
417  
418 my %menu = ();
419 for my $ext (sort { $b->preference <=> $a->preference; }
420 @{$obj->{-core}->{extensions}}) {
421 my @submenu = $ext->menu();
422 for (@submenu) {
423 $_->{label} =~ s/_//g;
424 my $current = $menu{$_->{label}}->{preference};
425 if (exists($_->{preference}) and
426 (not defined($current) or $_->{preference} > $current)) {
427 $menu{$_->{label}}->{preference} = $_->{preference};
428 }
429 $menu{$_->{label}}->{submenu} = [] unless exists $menu{$_->{label}}->{submenu};
430 $obj->insert_menu($menu{$_->{label}}->{submenu},$_->{submenu});
431 }
432 }
433  
434 my @menu = ();
435 for my $mainitem (sort {
436 (($menu{$b}->{preference} || 0) <=> ($menu{$a}->{preference} || 0)) ||
437 (uc $a cmp uc $b) } keys %menu) {
438 push @menu, { -label => $mainitem,
439 -submenu => $obj->create_submenu($menu{$mainitem}->{submenu}) };
440 }
441  
442 $obj->{menu}->{-menu} = \@menu;
443 $obj->{menu}->draw();
444 }
445  
446 sub render_size {
447 my $obj = shift;
448  
449 return $obj->{area}->canvaswidth();
450 }
451  
452 sub horizontal_scrollbar {
453 my $obj = shift;
454 my $onoff = shift;
455  
456 return (not $obj->{area}->{-wrapping}) unless defined $onoff;
457  
458 $obj->{area}->{-wrapping} = $onoff?0:1;
459 $obj->{area}->cursor_to_end();
460 $obj->{area}->draw();
461 }
462  
463 sub get_key {
464 my $obj = shift;
465 my $key;
466 1 while ($key = $obj->{ui}->get_key(5)) == -1;
467 return $key;
468 }
469  
470 sub macro {
471 my ($obj,$text) = @_;
472  
473 $obj->search('stop');
474  
475 my $cr = 0;
476 ++$cr if $text =~ s/\\n$//;
477  
478 $obj->{sqleditor}->text($obj->{sqleditor}->current_line().$text);
479 $obj->{sqleditor}->cursor_to_end();
480 $obj->{sqleditor}->draw();
481  
482 $obj->internal_loop() if $cr;
483 }
484  
485 sub bind_key {
486 my ($obj,$key,$text) = @_;
487  
488 $obj->{sqleditor}->set_binding(sub { $obj->macro($text); }, $key);
489 }
490  
491 sub status {
492 my ($obj,$info) = @_;
493  
494 $obj->{ui}->status($info);
495 }
496  
497 sub nostatus {
498 my $obj = shift;
499  
500 $obj->{ui}->nostatus;
501 }
502  
503 sub print_prompt {
504 my $obj = shift;
505  
506 $obj->status(join '',@_);
507 }
508  
509 sub infobox {
510 my ($obj,$info) = @_;
511  
512 my $dialog = $obj->{ui}->add('infobox','Dialog::Basic',
513 -message => $info, -title => 'Information');
514  
515 $dialog->getobj('message')->{-border} = 0;
516 $dialog->getobj('message')->{-vscrollbar} = 0;
517 $dialog->getobj('message')->{-wrapping} = 1;
518  
519 $dialog->modalfocus;
520  
521 $obj->{ui}->delete('infobox');
522 }
523  
524 sub hello { # splash
525 my $obj = shift;
526  
527 $obj->status("This is dbMan, Version $main::DBIx::dbMan::VERSION.\nCurses Edition.");
528 sleep 1;
529
530 $obj->print("This is dbMan, Version $main::DBIx::dbMan::VERSION. Curses Edition.\n\n");
531 }
532  
533 sub gui {
534 return 1;
535 }
536  
537 sub is_curses {
538 return 1;
539 }
540  
541 sub ask_value {
542 my $obj = shift;
543 my %params = @_;
544  
545 my $dialog = $obj->{ui}->add('dialog','Window',
546 -border => 1, -ipad => 1, -centered => 1,
547 -title => $params{-title} || 'Question',
548 -height => 8, -width => 50);
549  
550 $dialog->add('label1', 'Label',
551 -text => $params{-question} || 'Enter value',
552 -x => 0, -y => 0);
553 my $e_answer = $dialog->add('e_answer', 'TextEntry',
554 -x => 0, -y => 1, -sbborder => 1);
555 my $btns = $dialog->add('buttons', 'Buttonbox', -y => -1,
556 -buttonalignment => 'right', -buttons => [
557 { -label => '< '.($params{-button} || 'Enter').' >', -value => 1 },
558 { -label => '< Cancel >', -value => 0 } ]);
559 $btns->set_routine('press-button',
560 sub { shift->parent->loose_focus(); });
561  
562 $e_answer->focus();
563  
564 $dialog->modalfocus();
565  
566 my $val = '';
567 $val = $e_answer->get() if $btns->get();
568  
569 $obj->{ui}->delete('dialog');
570  
571 return $val;
572 }
573  
574 sub trace {
575 my $obj = shift;
576 $obj->print('TRACE: ',@_);
577 }
578  
579 sub current_line {
580 my $obj = shift;
581  
582 return $obj->{sqleditor}->get();
583 }