| 1 | package DBIx::dbMan::Extension::CmdClipboard; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use base 'DBIx::dbMan::Extension'; | ||
| 5 | |||
| 6 | our $VERSION = '0.05'; | ||
| 7 | |||
| 8 | 1; | ||
| 9 | |||
| 10 | sub IDENTIFICATION { return "000001-000064-000005"; } | ||
| 11 | |||
| 12 | sub preference { return 2000; } | ||
| 13 | |||
| 14 | sub known_actions { return [ qw/COMMAND/ ]; } | ||
| 15 | |||
| 16 | sub init { | ||
| 17 | my $obj = shift; | ||
| 18 | $obj->{prompt_num} = $obj->{-interface}->register_prompt(500); | ||
| 19 | } | ||
| 20 | |||
| 21 | sub done { | ||
| 22 | my $obj = shift; | ||
| 23 | $obj->{-interface}->deregister_prompt($obj->{prompt_num}); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub menu { | ||
| 27 | my $obj = shift; | ||
| 28 | |||
| 29 | my $clip = $obj->{-mempool}->get('clipboard'); | ||
| 30 | if (defined $clip and ref $clip eq 'HASH' and keys %$clip) { | ||
| 31 | return ( { label => 'Output', submenu => [ | ||
| 32 | { label => 'Clipboard', submenu => [ | ||
| 33 | { label => 'Show clipboard', | ||
| 34 | action => { action => 'COMMAND', | ||
| 35 | cmd => 'show clipboard' } }, | ||
| 36 | { label => 'Clear clipboard', | ||
| 37 | action => { action => 'COMMAND', cmd => '\\clear' } } | ||
| 38 | ] } ] } ); | ||
| 39 | } else { | ||
| 40 | return (); | ||
| 41 | } | ||
| 42 | } | ||
| 43 | |||
| 44 | sub handle_action { | ||
| 45 | my ($obj,%action) = @_; | ||
| 46 | |||
| 47 | $action{processed} = 1; | ||
| 48 | if ($action{action} eq 'COMMAND') { | ||
| 49 | if ($action{cmd} =~ s/^\\copy\s+//i) { | ||
| 50 | $obj->{-mempool}->set('clipboard',{}); | ||
| 51 | $obj->{-interface}->prompt($obj->{prompt_num},''); | ||
| 52 | $action{copy_to_clipboard} = 1; | ||
| 53 | $action{clipboard_prompt_num} = $obj->{prompt_num}; | ||
| 54 | delete $action{processed}; | ||
| 55 | } elsif ($action{cmd} =~ s/^\\unioncopy\s+//i) { | ||
| 56 | $action{copy_to_clipboard} = 1; | ||
| 57 | $action{union_clipboard} = 1; | ||
| 58 | $action{clipboard_prompt_num} = $obj->{prompt_num}; | ||
| 59 | delete $action{processed}; | ||
| 60 | } elsif ($action{cmd} =~ /^\\clear$/i) { | ||
| 61 | $obj->{-mempool}->set('clipboard',{}); | ||
| 62 | $obj->{-interface}->prompt($obj->{prompt_num},''); | ||
| 63 | $action{action} = 'OUTPUT'; | ||
| 64 | $action{output} = "Clipboard cleared.\n"; | ||
| 65 | $obj->{-interface}->rebuild_menu(); | ||
| 66 | } elsif ($action{cmd} =~ /^\\paste(\s+(.*))?$/) { | ||
| 67 | unless ($1) { | ||
| 68 | $action{action} = 'OUTPUT'; | ||
| 69 | $action{output} = "You must specify insert-like command after \\paste.\n"; | ||
| 70 | } else { | ||
| 71 | my $sql = $2; | ||
| 72 | my $clip = $obj->{-mempool}->get('clipboard'); | ||
| 73 | if (exists $clip->{-result}) { | ||
| 74 | for (@{$clip->{-result}}) { | ||
| 75 | my $newaction = { action => 'SQL', sql => $sql, type => 'do', placeholders => $_ }; | ||
| 76 | $obj->{-interface}->add_to_actionlist($newaction); | ||
| 77 | } | ||
| 78 | $action{action} = 'OUTPUT'; | ||
| 79 | $action{output} = "Pasting from clipboard inserted into actionlist buffer.\n"; | ||
| 80 | } else { | ||
| 81 | $action{action} = 'OUTPUT'; | ||
| 82 | $action{output} = "No data in clipboard.\n"; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | } elsif ($action{cmd} =~ /^show\s+clipboard$/i) { | ||
| 86 | my $clip = $obj->{-mempool}->get('clipboard'); | ||
| 87 | if (exists $clip->{-result}) { | ||
| 88 | $action{action} = 'SQL_RESULT'; | ||
| 89 | $action{result} = $clip->{-result}; | ||
| 90 | $action{fieldnames} = $clip->{-fieldnames}; | ||
| 91 | $action{fieldtypes} = $clip->{-fieldtypes}; | ||
| 92 | $action{type} = 'select'; | ||
| 93 | } else { | ||
| 94 | $action{action} = 'OUTPUT'; | ||
| 95 | $action{output} = "No data in clipboard.\n"; | ||
| 96 | } | ||
| 97 | delete $action{processed}; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | |||
| 101 | return %action; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub cmdhelp { | ||
| 105 | return [ | ||
| 106 | '\copy <command>' => 'Copy output of <command> to clipboard (<command> must be select-like command)', | ||
| 107 | '\unioncopy <command>' => 'Appending output of <command> to clipboard (<command> must be select-like command)', | ||
| 108 | '\paste <command>' => 'Paste block from clipboard into insert-like <command> with bind variables', | ||
| 109 | '\clear' => 'Clear contents of clipboard', | ||
| 110 | 'SHOW CLIPBOARD' => 'Show contents of clipboard' | ||
| 111 | ]; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | sub restart_complete { | ||
| 116 | my ($obj,$text,$line,$start) = @_; | ||
| 117 | my %action = (action => 'LINE_COMPLETE', text => $text, line => $line, | ||
| 118 | start => $start); | ||
| 119 | do { | ||
| 120 | %action = $obj->{-core}->handle_action(%action); | ||
| 121 | } until ($action{processed}); | ||
| 122 | return @{$action{list}} if ref $action{list} eq 'ARRAY'; | ||
| 123 | return $action{list} if $action{list}; | ||
| 124 | return (); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub cmdcomplete { | ||
| 128 | my ($obj,$text,$line,$start) = @_; | ||
| 129 | return $obj->restart_complete($text,$1,$start-(length($line)-length($1))) if $line =~ /^\s*\\(?:copy|unioncopy|paste)\s*\(.+?\)\s+(.*)$/i; | ||
| 130 | return ('CLIPBOARD') if $line =~ /^\s*SHOW\s+/i; | ||
| 131 | return ('\copy','\unioncopy','\paste','SHOW','\clear') if $line =~ /^\s*$/i; | ||
| 132 | return ('copy','unioncopy','paste','clear') if $line =~ /^\s*\\[A-Z]*$/i; | ||
| 133 | return (); | ||
| 134 | } |