| 1 | package App::Codit::Plugins::Console; | ||
| 2 | |||
| 3 | =head1 NAME | ||
| 4 | |||
| 5 | App::Codit::Plugins::Console - plugin for App::Codit | ||
| 6 | |||
| 7 | =cut | ||
| 8 | |||
| 9 | use strict; | ||
| 10 | use warnings; | ||
| 11 | use vars qw( $VERSION ); | ||
| 12 | $VERSION = '0.21'; | ||
| 13 | |||
| 14 | use Config; | ||
| 15 | my $mswin = $Config{'osname'} eq 'MSWin32'; | ||
| 16 | |||
| 17 | |||
| 18 | use base qw( Tk::AppWindow::BaseClasses::Plugin ); | ||
| 19 | |||
| 20 | require Tk::HList; | ||
| 21 | require Tk::Menu; | ||
| 22 | require Tk::LabFrame; | ||
| 23 | require Tk::NoteBook; | ||
| 24 | require Tk::Terminal; | ||
| 25 | |||
| 26 | =head1 DESCRIPTION | ||
| 27 | |||
| 28 | Test your code and run system commands. | ||
| 29 | |||
| 30 | Will not load on Windows. | ||
| 31 | |||
| 32 | =head1 DETAILS | ||
| 33 | |||
| 34 | The Console plugin allows you to run system commands and tests inside Codit. | ||
| 35 | It works a bit as a standard command console. If a command produces errors, | ||
| 36 | the output is scanned for document names and line numbers. Clickable links | ||
| 37 | are created that bring you directly to the place where the error occured. | ||
| 38 | |||
| 39 | The command console has three keybindings: | ||
| 40 | |||
| 41 | =over 4 | ||
| 42 | |||
| 43 | =item B<CTRL+U> | ||
| 44 | |||
| 45 | Toggle buffering on or off. | ||
| 46 | |||
| 47 | =item B<CTRL-W> | ||
| 48 | |||
| 49 | Clear the screen | ||
| 50 | |||
| 51 | =item B<CTRL+Z> | ||
| 52 | |||
| 53 | Kill the currently running process. | ||
| 54 | |||
| 55 | =back | ||
| 56 | |||
| 57 | This plugin does not work and cannot load on Windows. | ||
| 58 | |||
| 59 | =cut | ||
| 60 | |||
| 61 | sub new { | ||
| 62 | my $class = shift; | ||
| 63 | my $self = $class->SUPER::new(@_); | ||
| 64 | return undef unless defined $self; | ||
| 65 | return undef if $mswin; #will not load on windows. | ||
| 66 | |||
| 67 | my $page = $self->ToolBottomPageAdd('Console', 'utilities-terminal', undef, 'Execute system commands', 250); | ||
| 68 | |||
| 69 | my @pad = (-padx => 2, -pady => 2); | ||
| 70 | |||
| 71 | my $workdir = ''; | ||
| 72 | $self->{WORKDIR} = \$workdir; | ||
| 73 | $self->{UPDATEBLOCK} = 0; | ||
| 74 | |||
| 75 | my $folder = $self->configGet('-configfolder'); | ||
| 76 | my $hist = "$folder/console_history"; | ||
| 77 | my $text = $page->Scrolled('Terminal', | ||
| 78 | -width => 8, | ||
| 79 | -height => 8, | ||
| 80 | -historyfile => $hist, | ||
| 81 | -scrollbars => 'oe', | ||
| 82 | -linkcall => ['linkSelect', $self], | ||
| 83 | -linkreg => qr/[^\s]+\sline\s\d+/, | ||
| 84 | )->pack(@pad, -expand => 1, -fill => 'both'); | ||
| 85 | $text->bind('<Button-3>', [$self, 'popMenu']); | ||
| 86 | $text->bind('<Control-Button-1>', [$text, 'focus']); | ||
| 87 | $self->{TXT} = $text; | ||
| 88 | $workdir = $text->cget('-workdir'); | ||
| 89 | my $sb = $self->sidebars; | ||
| 90 | $sb->pageSelectCall('Console', sub { $text->focus }); | ||
| 91 | $sb->pageUnselectCall('Console', sub { | ||
| 92 | my $w = $self->mdi->docWidget; | ||
| 93 | $w->focus if defined $w; | ||
| 94 | }); | ||
| 95 | |||
| 96 | |||
| 97 | return $self; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _txt { return $_[0]->{TXT} } | ||
| 101 | |||
| 102 | sub dirGet { | ||
| 103 | return $_[0]->_txt->workdir | ||
| 104 | } | ||
| 105 | |||
| 106 | sub dirSet { # TODO | ||
| 107 | my ($self, $dir) = @_; | ||
| 108 | if ((defined $dir) and (-d $dir)) { | ||
| 109 | $self->_txt->launch("cd $dir"); | ||
| 110 | } | ||
| 111 | } | ||
| 112 | |||
| 113 | sub docWidget { | ||
| 114 | my $self = shift; | ||
| 115 | my $mdi = $self->mdi; | ||
| 116 | my $name = $mdi->docSelected; | ||
| 117 | return undef unless defined $name; | ||
| 118 | my $doc = $mdi->docGet($name); | ||
| 119 | return undef unless defined $doc; | ||
| 120 | return $doc->CWidg; | ||
| 121 | } | ||
| 122 | |||
| 123 | sub linkSelect { | ||
| 124 | my ($self, $text) = @_; | ||
| 125 | if ($text =~ /^([^\s]+)\s+line\s+(\d+)/) { | ||
| 126 | my $file = $1; | ||
| 127 | my $line = $2; | ||
| 128 | $file =~ s/blib\///; | ||
| 129 | my $folder = $self->{TXT}->cget('-workdir'); | ||
| 130 | my $test = "$folder/$file"; | ||
| 131 | $file = $test if -e $test; | ||
| 132 | if (-e $file) { | ||
| 133 | my $mdi = $self->extGet('CoditMDI'); | ||
| 134 | $self->cmdExecute('doc_open', $file) unless $mdi->docExists($file); | ||
| 135 | $self->cmdExecute('doc_select', $file); | ||
| 136 | my $widg = $mdi->docGet($file)->CWidg; | ||
| 137 | $widg->focus; | ||
| 138 | $widg->goTo("$line.0"); | ||
| 139 | $widg->see("$line.0"); | ||
| 140 | } | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | sub popMenu { | ||
| 145 | my $self = shift; | ||
| 146 | |||
| 147 | #refreshing commands | ||
| 148 | my $txt = $self->_txt; | ||
| 149 | my $dir = $txt->cget('-workdir'); | ||
| 150 | my $maketype = ''; | ||
| 151 | my @menuitems = (); | ||
| 152 | # my $iconsize = $self->configGet('-menuiconsize'); | ||
| 153 | my $iconsize = 16; | ||
| 154 | if (-e "$dir/Makefile.PL") { | ||
| 155 | $maketype = 'make'; | ||
| 156 | for ( | ||
| 157 | 'perl Makefile.PL', | ||
| 158 | 'make', | ||
| 159 | 'make clean', | ||
| 160 | 'make dist', | ||
| 161 | 'make disttest', | ||
| 162 | 'make manifest', | ||
| 163 | 'make realclean', | ||
| 164 | 'make test', | ||
| 165 | ) { | ||
| 166 | my $cmd = $_; | ||
| 167 | push @menuitems, ['command', $cmd, | ||
| 168 | -command => ['launch', $txt, $cmd], | ||
| 169 | # -image => $self->getArt('application-x-executable', $iconsize), | ||
| 170 | -label => $cmd, | ||
| 171 | ]; | ||
| 172 | } | ||
| 173 | } | ||
| 174 | if (-e "$dir/Build.PL") { | ||
| 175 | $maketype = './Build'; | ||
| 176 | for ( | ||
| 177 | 'perl Build.PL', | ||
| 178 | './Build', | ||
| 179 | './Build clean', | ||
| 180 | './Build dist', | ||
| 181 | './Build disttest', | ||
| 182 | './Build manifest', | ||
| 183 | './Build realclean', | ||
| 184 | './Build test', | ||
| 185 | ) { | ||
| 186 | my $cmd = $_; | ||
| 187 | push @menuitems, ['command', $cmd, | ||
| 188 | -command => ['launch', $txt, $cmd], | ||
| 189 | # -image => $self->getArt('application-x-executable', $iconsize), | ||
| 190 | -label => $cmd, | ||
| 191 | ]; | ||
| 192 | } | ||
| 193 | } | ||
| 194 | |||
| 195 | #refreshing tests | ||
| 196 | my $tdir = "$dir/t"; | ||
| 197 | my @tests = (); | ||
| 198 | if ((-e $tdir) and (-d $tdir)) { | ||
| 199 | if (opendir my $dh, $tdir) { | ||
| 200 | while (my $entry = readdir $dh) { | ||
| 201 | next unless $entry =~ /\.t$/; | ||
| 202 | push @tests, $entry | ||
| 203 | } | ||
| 204 | closedir $dh; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | push @menuitems, ['separator', '-'] if @tests and @menuitems; | ||
| 208 | for (sort @tests) { | ||
| 209 | my $cmd = $_; | ||
| 210 | push @menuitems, ['command', $cmd, | ||
| 211 | -command => ['launch', $txt, "$maketype; perl -Mblib t/$cmd"], | ||
| 212 | # -image => $self->getArt('application-x-executable-script', $iconsize), | ||
| 213 | -label => $cmd, | ||
| 214 | ]; | ||
| 215 | } | ||
| 216 | my ($x, $y) = $self->pointerxy; | ||
| 217 | if (@menuitems) { | ||
| 218 | my $menu = $self->Menu( | ||
| 219 | -menuitems => \@menuitems, | ||
| 220 | -tearoff => 0, | ||
| 221 | ); | ||
| 222 | $menu->bind('<Leave>', sub { | ||
| 223 | $menu->unpost; | ||
| 224 | $menu->destroy; | ||
| 225 | }); | ||
| 226 | $menu->post($x - 2, $y - 2); | ||
| 227 | } | ||
| 228 | } | ||
| 229 | |||
| 230 | sub Unload { | ||
| 231 | my $self = shift; | ||
| 232 | $self->ToolBottomPageRemove('Console'); | ||
| 233 | return $self->SUPER::Unload | ||
| 234 | } | ||
| 235 | |||
| 236 | =head1 LICENSE | ||
| 237 | |||
| 238 | Same as Perl. | ||
| 239 | |||
| 240 | =head1 AUTHOR | ||
| 241 | |||
| 242 | Hans Jeuken (hanje at cpan dot org) | ||
| 243 | |||
| 244 | =head1 TODO | ||
| 245 | |||
| 246 | =over 4 | ||
| 247 | |||
| 248 | =back | ||
| 249 | |||
| 250 | =head1 BUGS AND CAVEATS | ||
| 251 | |||
| 252 | If you find any bugs, please report them here L<https://github.com/haje61/App-Codit/issues>. | ||
| 253 | |||
| 254 | =head1 SEE ALSO | ||
| 255 | |||
| 256 | =over 4 | ||
| 257 | |||
| 258 | =back | ||
| 259 | |||
| 260 | =cut | ||
| 261 | |||
| 262 | |||
| 263 | |||
| 264 | |||
| 265 | 1; | ||
| 266 | |||
| 267 |