| 1 | package App::Tk::Deparse; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use 5.008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | |||
| 6 | 1 | ▶ | use Browser::Open qw(open_browser open_browser_cmd); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Path::Tiny qw(path); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Capture::Tiny qw(capture); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 0 | ▶ | use Tk; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use Tk::Dialog; | ||
| 12 | use Tk::HyperText; | ||
| 13 | use Tk::BrowseEntry; | ||
| 14 | |||
| 15 | our $VERSION = '0.02'; | ||
| 16 | |||
| 17 | # TODO: make fonts more readable | ||
| 18 | # TODO: Clear the output when we change the input (or maybe rerun the deparse process?) | ||
| 19 | # TODO: use nice temporary filename as we can see the name of the file with the -l flags | ||
| 20 | # TODO: Save window size upon exit; restore window size upon start | ||
| 21 | # TODO: If there is a syntax error in the code B::Deparse will fail. We sould display this. | ||
| 22 | |||
| 23 | |||
| 24 | my $sample = q{ | ||
| 25 | # Paste your code in the top window and click the Deparse button to see what B::Deparse thinks about it | ||
| 26 | for (my $j=0, $j<3, ++$j) { | ||
| 27 | print $j; | ||
| 28 | } | ||
| 29 | |||
| 30 | my $pi = 3.14; # -d changes this to a string | ||
| 31 | my $answer = "42"; # single quote or no quote? | ||
| 32 | |||
| 33 | my @planets = ('Mercury', 'Venus', 'Earth', 'Mars', 'Jupiter', 'Saturn'); | ||
| 34 | |||
| 35 | my %h = ( | ||
| 36 | name => "Foo Bar", | ||
| 37 | age => 42, | ||
| 38 | fruits => qw(Apple Banana Peach), | ||
| 39 | ); | ||
| 40 | |||
| 41 | |||
| 42 | my $gentlemen = "Some names"; | ||
| 43 | my $me = 'Perl'; | ||
| 44 | |||
| 45 | print "Hello, $pi, @ladies, \u$gentlemen\E, \u\L$me!"; | ||
| 46 | |||
| 47 | # Trying to show -P without success | ||
| 48 | sub foo (\@) { 1 } foo @x | ||
| 49 | }; | ||
| 50 | |||
| 51 | my $s = q{ | ||
| 52 | }; | ||
| 53 | |||
| 54 | # Removed l for now showing the temporary filename does not do much good. | ||
| 55 | my @flags = ('d', 'p', 'q', 'P'); | ||
| 56 | |||
| 57 | sub new { | ||
| 58 | my ($class) = @_; | ||
| 59 | my $self = bless {}, $class; | ||
| 60 | |||
| 61 | $self->{top} = MainWindow->new( | ||
| 62 | -title => 'B::Deparse', | ||
| 63 | ); | ||
| 64 | $self->create_menu; | ||
| 65 | $self->create_app; | ||
| 66 | $self->{incode}->insert("0.0", $sample); | ||
| 67 | $self->deparse; | ||
| 68 | |||
| 69 | return $self; | ||
| 70 | } | ||
| 71 | |||
| 72 | sub create_menu { | ||
| 73 | my ($self) = @_; | ||
| 74 | |||
| 75 | my $main_menu = $self->{top}->Menu(); | ||
| 76 | |||
| 77 | my $file_menu = $main_menu->cascade(-label => 'File', -underline => 0); | ||
| 78 | #$file_menu->command(-label => 'Open Perl File', -command => sub { $self->show_open(); }, -underline => 0); | ||
| 79 | $file_menu->command(-label => 'Quit (Ctrl-q)', -command => sub { $self->exit_app(); }, -underline => 0); | ||
| 80 | |||
| 81 | my $about_menu = $main_menu->cascade(-label => 'Help', -underline => 0); | ||
| 82 | $about_menu->command(-label => 'About', -command => sub { $self->show_about; }, -underline => 0); | ||
| 83 | |||
| 84 | $self->{top}->configure(-menu => $main_menu); | ||
| 85 | } | ||
| 86 | |||
| 87 | sub show_about { | ||
| 88 | my ($self) = @_; | ||
| 89 | |||
| 90 | my $dialog = $self->{top}->DialogBox( | ||
| 91 | -title => 'About App::Deparse::Tk', | ||
| 92 | -popover => $self->{top}, | ||
| 93 | -buttons => ['OK'], | ||
| 94 | ); | ||
| 95 | |||
| 96 | my $html = $dialog->HyperText(); | ||
| 97 | $html->pack; | ||
| 98 | $html->setHandler (Resource => \&onResource); | ||
| 99 | $html->loadString(qq{<html> | ||
| 100 | <head> | ||
| 101 | <title>About App::Deparse::Tk</title> | ||
| 102 | </head> | ||
| 103 | <body> | ||
| 104 | Version: $VERSION<br> | ||
| 105 | <a href="https://metacpan.org/pod/Tk">Perl Tk</a>: $Tk::VERSION<br> | ||
| 106 | <a href="https://metacpan.org/pod/B::Deparse">B::Deparse</a><br> | ||
| 107 | Perl $]<br> | ||
| 108 | <p> | ||
| 109 | Create by Gabor Szabo<br> | ||
| 110 | Source code on <a href="https://github.com/szabgab/App-Tk-Deparse">GitHub</a><br> | ||
| 111 | Thanks to my <a href="https://www.patreon.com/szabgab">Patreon</a> supporters<br> | ||
| 112 | |||
| 113 | <h2>Supporters</h2> | ||
| 114 | <ul> | ||
| 115 | <li><a href="https://www.activestate.com/">ActiveState</a></li> | ||
| 116 | <li><a href="https://www.chatterjee.net/">Anirvan Chatterjee</a></li> | ||
| 117 | <li>Brian Gaboury</li> | ||
| 118 | <li><a href="https://www.linkedin.com/in/chan-wilson-b867b3/">Chan Wilson</a></li> | ||
| 119 | <li><a href="https://www.linkedin.com/in/fins0ck/">Csaba Gaspar</a></li> | ||
| 120 | <li><a href="https://www.linkedin.com/in/dihnen/">David Ihnen</a></li> | ||
| 121 | <li><a href="https://www.preshweb.co.uk/about/">David Precious</a></li> | ||
| 122 | <li>Frank Kropp</li> | ||
| 123 | <li>John Andersen</li> | ||
| 124 | <li>Magnus Enger</li> | ||
| 125 | <li><a href="https://www.linkedin.com/in/mjgardner/">Mark Gardner</a></li> | ||
| 126 | <li>Markus Hechenberger</li> | ||
| 127 | <li>Matthew Mitchell</li> | ||
| 128 | <li>Matthew Persico</li> | ||
| 129 | <li>Meir Guttman</li> | ||
| 130 | <li>Mike Small</li> | ||
| 131 | <li><a href="https://www.linkedin.com/in/n8dgr8/">Nathan Schlehlein</a></li> | ||
| 132 | <li><a href="https://www.linkedin.com/in/pfmabry/">Paul Mabry</a></li> | ||
| 133 | <li><a href="https://perl.careers/">Perl Careers</a></li> | ||
| 134 | <li>Richard Leach</li> | ||
| 135 | <li>Robert Coursen</li> | ||
| 136 | <li><a href="https://www.linkedin.com/in/shajiindia/">Shaji Kalidasan</a></li> | ||
| 137 | <li><a href="https://www.linkedin.com/in/shanta-mcbain-7b644437/">Shanta McBain</a></li> | ||
| 138 | <li><a href="https://www.linkedin.com/in/slobodanmiskovic/">Slobodan Mišković</a></li> | ||
| 139 | <li><a href="https://www.linkedin.com/in/stephen-jarjoura-a684401/">Stephen A. Jarjoura</a></li> | ||
| 140 | <li>Tony Edwardson</li> | ||
| 141 | <li><a href="https://www.linkedin.com/in/tori-hunter-00009639/">Tori Hunter</a></li> | ||
| 142 | <li><a href="https://bruck.co.il/">Uri Bruck</a></li> | ||
| 143 | <li>Victor Moral</li> | ||
| 144 | <li>Wolfgang Odendahl</li> | ||
| 145 | <li>Yes2Crypto</li> | ||
| 146 | <li>... + many others</li> | ||
| 147 | </ul> | ||
| 148 | |||
| 149 | </body> | ||
| 150 | </html> | ||
| 151 | }); | ||
| 152 | |||
| 153 | $dialog->Show; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub onResource { | ||
| 157 | my ($html, %info) = @_; | ||
| 158 | my $url = $info{href}; | ||
| 159 | #print $url, "\n"; | ||
| 160 | #open_browser($url); # https://rt.cpan.org/Public/Bug/Display.html?id=133315 | ||
| 161 | #print "done\n"; | ||
| 162 | my $cmd = open_browser_cmd($url); | ||
| 163 | # TODO: verify that the URL is well formatted before passing it to system | ||
| 164 | if ($^O eq 'MSWin32') { | ||
| 165 | system("$cmd $url"); | ||
| 166 | } else { | ||
| 167 | system("$cmd $url &"); | ||
| 168 | } | ||
| 169 | } | ||
| 170 | |||
| 171 | sub changed { | ||
| 172 | my ($self, $event) = @_; | ||
| 173 | # TODO can we delay this and only run the deparse process if there were no changes for some time (e.g. 1 sec) | ||
| 174 | $self->deparse; | ||
| 175 | #print("changed\n"); | ||
| 176 | } | ||
| 177 | |||
| 178 | sub create_app { | ||
| 179 | my ($self) = @_; | ||
| 180 | $self->{incode} = $self->{top}->Text( | ||
| 181 | -state => 'normal', | ||
| 182 | -font => ['fixed', 12], | ||
| 183 | -bg => 'white', | ||
| 184 | ); | ||
| 185 | $self->{incode}->bindtags([$self->{incode}, 'Tk::Text', $self->{top}, 'all']); | ||
| 186 | #$self->{incode}->bind('<<Modified>>' => sub { I could not get this working | ||
| 187 | $self->{incode}->bind('<Any-KeyPress>' => sub { $self->changed(shift); }); | ||
| 188 | $self->{incode}->pack(-fill => 'both', -expand => 1); | ||
| 189 | |||
| 190 | $self->{flags} = $self->{top}->Frame(); | ||
| 191 | $self->{flags}->pack(-side => 'top'); | ||
| 192 | |||
| 193 | for my $flag (@flags) { | ||
| 194 | $self->{"${flag}_flag"} = 0; | ||
| 195 | |||
| 196 | $self->{"${flag}_flag_checkbox"} = $self->{flags}->Checkbutton( | ||
| 197 | -text => "-$flag", | ||
| 198 | -variable => \$self->{"${flag}_flag"}, | ||
| 199 | -font => ['fixed', 10], | ||
| 200 | -command => sub { $self->deparse }, | ||
| 201 | ); | ||
| 202 | $self->{"${flag}_flag_checkbox"}->pack(-side => 'left'); | ||
| 203 | } | ||
| 204 | # $self->{"s_flag"} = ''; | ||
| 205 | # my @s_values = qw(C i T); | ||
| 206 | # $self->{"s_flag_widget"} = $self->{flags}->BrowseEntry( | ||
| 207 | # -label => "-s", | ||
| 208 | # -width => 3, | ||
| 209 | # -variable => \$self->{"s_flag"}, | ||
| 210 | # ); | ||
| 211 | # for my $s (@s_values) { | ||
| 212 | # $self->{"s_flag_widget"}->insert("end", $s); | ||
| 213 | # } | ||
| 214 | # $self->{"s_flag_widget"}->pack; | ||
| 215 | |||
| 216 | $self->{outcode} = $self->{top}->Text( | ||
| 217 | -state => 'disabled', | ||
| 218 | -font => ['fixed', 12], | ||
| 219 | ); | ||
| 220 | $self->{outcode}->pack(-fill => 'both', -expand => 1); | ||
| 221 | |||
| 222 | |||
| 223 | $self->{deparse} = $self->{top}->Button( | ||
| 224 | -text => 'Deparse', | ||
| 225 | -command => sub { $self->deparse }, | ||
| 226 | ); | ||
| 227 | $self->{deparse}->pack() | ||
| 228 | |||
| 229 | } | ||
| 230 | |||
| 231 | sub deparse { | ||
| 232 | my ($self) = @_; | ||
| 233 | |||
| 234 | my $code = $self->{incode}->get("0.0", 'end'); | ||
| 235 | my $temp = Path::Tiny->tempfile; | ||
| 236 | path($temp)->spew($code); | ||
| 237 | |||
| 238 | my $cmd = 'perl -MO=Deparse'; | ||
| 239 | for my $flag (@flags) { | ||
| 240 | if ($self->{"${flag}_flag"}) { | ||
| 241 | $cmd .= ",-$flag"; | ||
| 242 | } | ||
| 243 | } | ||
| 244 | if ($self->{s_flag}) { | ||
| 245 | $cmd .= ",-s$self->{s_flag}" | ||
| 246 | } | ||
| 247 | |||
| 248 | my ($stdout, $stderr, $exit) = capture { system("$cmd $temp"); }; | ||
| 249 | $self->{outcode}->configure('state' => 'normal'); | ||
| 250 | $self->{outcode}->delete("0.0", 'end'); | ||
| 251 | if ($exit) { | ||
| 252 | $self->{outcode}->configure('fg' => 'red'); | ||
| 253 | $self->{outcode}->insert("0.0", $stderr); | ||
| 254 | } else { | ||
| 255 | $self->{outcode}->configure('fg' => 'black'); | ||
| 256 | $self->{outcode}->insert("0.0", $stdout); | ||
| 257 | } | ||
| 258 | $self->{outcode}->configure('state' => 'disabled'); | ||
| 259 | } | ||
| 260 | |||
| 261 | sub run { | ||
| 262 | my ($self) = @_; | ||
| 263 | MainLoop; | ||
| 264 | } | ||
| 265 | |||
| 266 | sub exit_app { | ||
| 267 | my ($self) = @_; | ||
| 268 | |||
| 269 | # TODO: Save flags when exiting | ||
| 270 | # TODO: Save content of the input window when exiting | ||
| 271 | # TODO: Save current window size so we can start the same size next time. | ||
| 272 | # my %config = ( | ||
| 273 | # 'geometry' => $self->{top}->geometry, | ||
| 274 | # ); | ||
| 275 | # path($config_file)->spew(encode_json(\%config)); | ||
| 276 | |||
| 277 | exit; | ||
| 278 | } | ||
| 279 | |||
| 280 | 1; | ||
| 281 | |||
| 282 | =head1 NAME | ||
| 283 | |||
| 284 | App::Tk::Deparse - Tk based GUI to experiment with B::Deparse | ||
| 285 | |||
| 286 | =head1 SYNOPSIS | ||
| 287 | |||
| 288 | perl-deparse | ||
| 289 | |||
| 290 | =head1 DESCRIPTION | ||
| 291 | |||
| 292 | This is a GUI program. There are some videos on L<Perl Maven Tk|https://perlmaven.com/tk> explaining | ||
| 293 | how does this work and how was this built. | ||
| 294 | |||
| 295 | When the application exits we save the current window size in the config file and next time we use that size to open the application. | ||
| 296 | |||
| 297 | ~/.perl-tk-deparse.json | ||
| 298 | |||
| 299 | =head1 SEE ALSO | ||
| 300 | |||
| 301 | L<B::Deparse> | ||
| 302 | |||
| 303 | L<Tk> | ||
| 304 | |||
| 305 | L<Tk::HyperText> | ||
| 306 | |||
| 307 | |||
| 308 | =head1 COPYRIGHT AND LICENSE | ||
| 309 | |||
| 310 | Copyright (C) 2020 by L<Gabor Szabo|https://szabgab.com/> | ||
| 311 | |||
| 312 | This library is free software; you can redistribute it and/or modify | ||
| 313 | it under the same terms as Perl itself, either Perl version 5.26.1 or, | ||
| 314 | at your option, any later version of Perl 5 you may have available. | ||
| 315 | |||
| 316 | =cut | ||
| 317 | |||