| 1 | package DBIx::dbMan::Extension::EditLine; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | |||
| 6 | our $VERSION = '0.06'; | ||
| 7 | |||
| 8 | 1; | ||
| 9 | |||
| 10 | sub IDENTIFICATION { return "000001-000069-000006"; } | ||
| 11 | |||
| 12 | sub preference { return 0; } | ||
| 13 | |||
| 14 | sub known_actions { return [ qw/EDIT_LINE/ ]; } | ||
| 15 | |||
| 16 | sub menu { | ||
| 17 | my $obj = shift; | ||
| 18 | |||
| 19 | return ( { label => 'Input', submenu => [ | ||
| 20 | { label => 'Edit current line in editor', | ||
| 21 | action => { action => 'EDIT_LINE', get_what => 1 } } | ||
| 22 | ] } ); | ||
| 23 | } | ||
| 24 | |||
| 25 | sub handle_action { | ||
| 26 | my ($obj,%action) = @_; | ||
| 27 | |||
| 28 | $action{processed} = 1; | ||
| 29 | if ($action{action} eq 'EDIT_LINE') { | ||
| 30 | $action{what} = $obj->{-interface}->current_line() if $action{get_what}; | ||
| 31 | my $text = $action{what} || ''; | ||
| 32 | my $editor = $ENV{DBMAN_EDITOR} || $ENV{EDITOR} || 'vi'; | ||
| 33 | my $filename = "/tmp/dbman.edit_line.$$.sql"; | ||
| 34 | if (open F,">$filename") { | ||
| 35 | if ( $obj->{ -interface }->is_utf8 ) { | ||
| 36 | binmode F, ':utf8'; | ||
| 37 | } | ||
| 38 | print F $text; | ||
| 39 | close F; | ||
| 40 | $text = ''; | ||
| 41 | my $modi = -M $filename; | ||
| 42 | $obj->{-interface}->go_away(); | ||
| 43 | system "$editor $filename"; | ||
| 44 | $obj->{-interface}->come_back(); | ||
| 45 | if (-M $filename ne $modi and open F,$filename) { | ||
| 46 | if ( $obj->{ -interface }->is_utf8 ) { | ||
| 47 | binmode F, ':utf8'; | ||
| 48 | } | ||
| 49 | $text = join '',<F>; | ||
| 50 | close F; | ||
| 51 | } | ||
| 52 | unlink $filename if -e $filename; | ||
| 53 | } else { $text = ''; } | ||
| 54 | $action{action} = 'OUTPUT'; | ||
| 55 | if ($text) { | ||
| 56 | $text =~ s/\n$//gs; | ||
| 57 | $text =~ s/^\n//gs; | ||
| 58 | $action{output} = "\nI execute next long command:\n".$text."\n"; | ||
| 59 | $text =~ s/--.*?\n/ /gs; | ||
| 60 | $text =~ s/\n/ /gs; | ||
| 61 | $text =~ s/\t+/ /gs; | ||
| 62 | $text =~ s/\s{2,}/ /gs; | ||
| 63 | $obj->{-interface}->history_add($text); | ||
| 64 | $obj->{-interface}->add_to_actionlist({ action => 'COMMAND', cmd => $text }); | ||
| 65 | } else { | ||
| 66 | $action{output} = "\nI needn't execute entered long command.\n"; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | |||
| 70 | return %action; | ||
| 71 | } |