blib/lib/App/PerlTidy/Tk.pm

filter: Statementstmt 94.7% 36 / 38 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 96.0% 49 / 51 CC 1 SCAR 0.2
CC
1
Cov
95%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 package App::PerlTidy::Tk;
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 Cwd qw(getcwd);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Cpanel::JSON::XS qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Data::Dumper qw(Dumper);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use File::HomeDir ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use File::Spec ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Path::Tiny qw(path);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Getopt::Long qw(GetOptions);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Perl::Tidy;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶use Tk;
Subroutine BEGIN: called CC=1 SCAR=2.6
17 use Tk::Dialog;
18 use Tk::FileSelect;
19 use Tk::HyperText;
20 use Tk::Table;
21  
22 our $VERSION = '0.03';
23  
24 my $zoom = 3;
25 my %skip = map { $_ => 1 } qw(nocheck-syntax perl-syntax-check-flags);
26 my $home = File::HomeDir->my_home;
27 my $config_file = File::Spec->catfile($home, '.perltidy-tk.json');
28 my @options = ('indent-columns', 'paren-tightness', 'brace-tightness', 'block-brace-tightness');
29  
30 sub usage {
31 die "Usage: $0 [--help] [--perl somefile.pl]\n";
32 }
33  
34 sub new {
35 my ($class) = @_;
36 my $self = bless {}, $class;
37  
38 my $perlfile;
39 my $help;
40 GetOptions(
41 'perl=s' => \$perlfile,
42 'help' => \$help,
43 ) or usage();
44 usage() if $help;
45  
46 my $config = {};
47 if (-e $config_file) {
48 $config = decode_json(path($config_file)->slurp_utf8);
49 }
50  
51 $self->{autotidy} = 0;
52  
53 $self->load_default_configuration;
54  
55 $self->{top} = MainWindow->new();
56 if (exists $config->{geometry}) {
57 $self->{top}->geometry($config->{geometry});
58 }
59  
60 $self->{top}->bind("<Control-Shift-plus>", sub { $self->zoom($zoom) });
61 $self->{top}->bind("<Control-minus>", sub { $self->zoom(-$zoom) });
62 $self->{top}->bind("<Control-q>", sub { $self->exit_app(); });
63  
64 $self->create_menu;
65 $self->create_text_widget;
66 $self->create_config_panel;
67  
68 if ($perlfile) {
69 $self->load_perl_file($perlfile);
70 }
71  
72 return $self;
73 }
74  
75 sub run {
76 my ($self) = @_;
77 MainLoop;
78 }
79  
80 sub load_default_configuration {
81 my ($self) = @_;
82  
83 my ($option_string, $defaults, $expansion, $category, $option_range) = Perl::Tidy::generate_options();
84 #print Dumper $option_range;
85 $option_range->{'indent-columns'} ||= [1, 8];
86 $self->{range} = $option_range;
87 #$self->{defaults} = $defaults;
88 $self->{config} = {}; # options that have a value
89 $self->{flags} = {}; # options that only have presence
90 $self->{widgets} = {};
91 #print Dumper $option_string;
92 for my $def (sort @$defaults) {
93 my ($name, $value) = split /=/, $def;
94 #print "$name\n";
95 next if $skip{$name};
96 if (defined $value) {
97 #print " $value\n";
98 $self->{config}{$name} = $value;
99 } else {
100 $self->{flags}{$name} = 1;
101 }
102 }
103 }
104  
105  
106 sub create_menu {
107 my ($self) = @_;
108  
109 my $main_menu = $self->{top}->Menu();
110  
111 my $file_menu = $main_menu->cascade(-label => 'File', -underline => 0);
112 $file_menu->command(-label => 'Open Perl File', -command => sub { $self->show_open(); }, -underline => 0);
113 #$file_menu->command(-label => 'Load Config', -command => sub { $self->load_config(); }, -underline => 0);
114 $file_menu->command(-label => 'Save Config', -command => sub { $self->save_config(); }, -underline => 0);
115 $file_menu->command(-label => 'Quit (Ctrl-q)', -command => sub { $self->exit_app(); }, -underline => 0);
116  
117 my $action_menu = $main_menu->cascade(-label => 'Action', -underline => 0);
118 $action_menu->command(-label => 'Tidy', -command => sub { $self->run_tidy; });
119 $action_menu->command(-label => 'Zoom in (Ctrl-Shift-+)', -command => sub { $self->zoom($zoom); });
120 $action_menu->command(-label => 'Zoom Out (Ctrl--)', -command => sub { $self->zoom(-$zoom); });
121 $action_menu->checkbutton(-label => 'Autotidy', -variable => \$self->{autotidy});
122  
123 my $about_menu = $main_menu->cascade(-label => 'Help', -underline => 0);
124 $about_menu->command(-label => 'About', -command => sub { $self->show_about; }, -underline => 0);
125  
126 $self->{top}->configure(-menu => $main_menu);
127 }
128  
129 sub save_config {
130 my ($self) = @_;
131  
132 my $start_dir = getcwd();
133 my $file_selector = $self->{top}->FileSelect(-directory => $start_dir);
134 my $filename = $file_selector->Show;
135 if (-e $filename) {
136 my $dialog = $self->{top}->Dialog(
137 -title => 'Overwrite?',
138 -text => "The file $filename already exists. Overwrite?",
139 -popover => $self->{top},
140 -buttons => ['Yes', 'No'],
141 );
142 my $res = $dialog->Show;
143 return if $res ne 'Yes';
144 }
145 my $rc = $self->get_rc();
146 if (open my $fh, '>', $filename) {
147 my $localtime = scalar localtime;
148 print $fh "# Saved by App::PerlTidy::Tk on $localtime\n\n";
149 print $fh $rc;
150 } else {
151 my $dialog = $self->{top}->Dialog(
152 -title => 'Error',
153 -text => "Could not write to file. $!",
154 -popover => $self->{top},
155 -buttons => ['OK'],
156 );
157 }
158 }
159  
160 sub load_config {
161 my ($self) = @_;
162 }
163  
164 sub zoom {
165 my ($self, $number) = @_;
166 my $font_info = $self->{text}->configure('-font');
167 #print "${$font_info->[4]}\n"; # 'fixed 20';
168 my ($font, $size) = split / /, ${$font_info->[4]};
169 $size += $number;
170 $self->{text}->configure(-font => ['fixed', $size]);
171 }
172  
173 sub create_config_panel {
174 my ($self) = @_;
175  
176 $self->{table} = $self->{top}->Table(-columns => 2, -rows => 1, -fixedrows => 1, -scrollbars => '');
177 $self->{table}->pack(-expand=> 1, -fill => 'both');
178  
179 my $row = -1;
180  
181 #my $name = 'line-up-parentheses';
182 #print $self->{flags}{$name}, "\n";
183 #my $cb = $self->{top}->Checkbutton(
184 # -text => $name,
185 # -variable => \$self->{flags}{$name},
186 # -font => ['fixed', 10]
187 #);
188 #$cb->pack(-side => 'left');
189  
190 for my $name (@options) {
191 $row++;
192 my $label = $self->{table}->Label(
193 -text => $name,
194 );
195 $self->{table}->put($row, 0, $label);
196  
197 my $cb = $self->{table}->Optionmenu(
198 -variable => \$self->{config}{$name},
199 -options => [$self->{range}{$name}[0] .. $self->{range}{$name}[1]],
200 -command => sub { $self->config_changed },
201 );
202 $self->{table}->put($row, 1, $cb);
203 $self->{widgets}{$name} = $cb;
204 }
205 }
206  
207 sub config_changed {
208 my ($self) = @_;
209  
210 if ($self->{autotidy}) {
211 $self->run_tidy;
212 }
213 }
214  
215  
216 sub update_config {
217 my ($self) = @_;
218 #my $name = 'indent-columns';
219 #$self->{config}{$name} = $self->{widgets}{$name}->get;
220 }
221  
222 sub create_text_widget {
223 my ($self) = @_;
224  
225 $self->{text} = $self->{top}->Text(
226 -state => 'normal',
227 -font => ['fixed', 12],
228 );
229 $self->{text}->pack(-fill => 'both', -expand => 1);
230 }
231  
232 sub show_open {
233 my ($self) = @_;
234  
235 my $start_dir = getcwd();
236 my $file_selector = $self->{top}->FileSelect(-directory => $start_dir);
237 my $filename = $file_selector->Show;
238 $self->load_perl_file($filename);
239 }
240  
241 sub load_perl_file {
242 my ($self, $filename) = @_;
243  
244 if ($filename and -f $filename) {
245 if (open my $fh, '<', $filename) {
246 local $/ = undef;
247 my $content = <$fh>;
248 $self->{text}->delete("0.0", 'end');
249 $self->{text}->insert("0.0", $content);
250 } else {
251 print "TODO: Report error $! for '$filename'\n";
252 }
253 }
254 }
255  
256 sub get_rc {
257 my ($self) = @_;
258  
259 $self->update_config;
260  
261 my $rc = '';
262 while (my ($name, $value) = each %{$self->{config}}) {
263 $rc .= "--$name=$value\n";
264 }
265 for my $name (keys %{$self->{flags}}) {
266 $rc .= "--$name\n";
267 }
268 #print $rc;
269 return $rc;
270 }
271  
272 sub run_tidy {
273 my ($self) = @_;
274 #print Dumper \%skip;
275 #
276 my $rc = $self->get_rc();
277  
278 #for my $field (sort keys %config) {
279 # if (defined $config{$field}) {
280 # $rc .= "$field=$config{$field}\n";
281 # } else {
282 # $rc .= "$field\n";
283 # }
284 #}
285  
286 my $code = $self->{text}->get("0.0", 'end');
287 my $clean;
288 my $stderr;
289  
290 my $error = Perl::Tidy::perltidy(
291 source => \$code,
292 destination => \$clean,
293 stderr => \$stderr,
294 perltidyrc => \$rc,
295 );
296 $self->{text}->delete("0.0", 'end');
297 $self->{text}->insert("0.0", $clean);
298 }
299  
300 sub show_about {
301 my ($self) = @_;
302  
303 my $dialog = $self->{top}->DialogBox(
304 -title => 'About App::PerlTidy::Tk',
305 -popover => $self->{top},
306 -buttons => ['OK'],
307 );
308  
309 my $html = $dialog->HyperText();
310 $html->pack;
311 $html->setHandler (Resource => \&onResource);
312 $html->loadString(qq{<html>
313 <head>
314 <title>About App::PerlTidy::Tk</title>
315 </head>
316 <body>
317 Version: $VERSION<br>
318 &nbsp;<a href="https://metacpan.org/pod/Tk">Perl Tk</a>: $Tk::VERSION<br>
319 <a href="https://metacpan.org/pod/Perl::Tidy">Perl::Tidy</a>: $Perl::Tidy::VERSION<br>
320 Perl $]<br>
321 <p>
322 Create by Gabor Szabo<br>
323 Source code on <a href="https://github.com/szabgab/App-PerlTidy-Tk">GitHub</a><br>
324 Thanks to my <a href="https://www.patreon.com/szabgab">Patreon</a> supporters<br>
325  
326 <h2>Supporters</h2>
327 <ul>
328 <li><a href="https://www.activestate.com/">ActiveState</a></li>
329 <li><a href="https://www.chatterjee.net/">Anirvan Chatterjee</a></li>
330 <li>Brian Gaboury</li>
331 <li><a href="https://www.linkedin.com/in/chan-wilson-b867b3/">Chan Wilson</a></li>
332 <li><a href="https://www.linkedin.com/in/fins0ck/">Csaba Gaspar</a></li>
333 <li><a href="https://www.linkedin.com/in/dihnen/">David Ihnen</a></li>
334 <li><a href="https://www.preshweb.co.uk/about/">David Precious</a></li>
335 <li>Frank Kropp</li>
336 <li>John Andersen</li>
337 <li>Magnus Enger</li>
338 <li><a href="https://www.linkedin.com/in/mjgardner/">Mark Gardner</a></li>
339 <li>Markus Hechenberger</li>
340 <li>Matthew Mitchell</li>
341 <li>Matthew Persico</li>
342 <li>Meir Guttman</li>
343 <li>Mike Small</li>
344 <li><a href="https://www.linkedin.com/in/n8dgr8/">Nathan Schlehlein</a></li>
345 <li><a href="https://www.linkedin.com/in/pfmabry/">Paul Mabry</a></li>
346 <li><a href="https://perl.careers/">Perl Careers</a></li>
347 <li>Richard Leach</li>
348 <li>Robert Coursen</li>
349 <li><a href="https://www.linkedin.com/in/shajiindia/">Shaji Kalidasan</a></li>
350 <li><a href="https://www.linkedin.com/in/shanta-mcbain-7b644437/">Shanta McBain</a></li>
351 <li><a href="https://www.linkedin.com/in/slobodanmiskovic/">Slobodan Mišković</a></li>
352 <li><a href="https://www.linkedin.com/in/stephen-jarjoura-a684401/">Stephen A. Jarjoura</a></li>
353 <li>Tony Edwardson</li>
354 <li><a href="https://www.linkedin.com/in/tori-hunter-00009639/">Tori Hunter</a></li>
355 <li><a href="https://bruck.co.il/">Uri Bruck</a></li>
356 <li>Victor Moral</li>
357 <li>Wolfgang Odendahl</li>
358 <li>Yes2Crypto</li>
359 <li>... + many others</li>
360 </ul>
361  
362 </body>
363 </html>
364 });
365  
366 $dialog->Show;
367 }
368  
369 sub onResource {
370 my ($html, %info) = @_;
371 my $url = $info{href};
372 #print $url, "\n";
373 #open_browser($url); # https://rt.cpan.org/Public/Bug/Display.html?id=133315
374 #print "done\n";
375 my $cmd = open_browser_cmd($url);
376 # TODO: verify that the URL is well formatted before passing it to system
377 if ($^O eq 'MSWin32') {
378 system("$cmd $url");
379 } else {
380 system("$cmd $url &");
381 }
382 }
383  
384 sub exit_app {
385 my ($self) = @_;
386  
387 # Save current window size so we can start the same size next time.
388 my %config = (
389 'geometry' => $self->{top}->geometry,
390 );
391 path($config_file)->spew(encode_json(\%config));
392  
393 print("TODO: save changes before exit? Same when user click on x\n");
394 exit;
395 }
396  
397 1;
398  
399 =head1 NAME
400  
401 App::PerlTidy::Tk - Tk based GUI to experiment with PerlTidy configuration options
402  
403 =head1 SYNOPSIS
404  
405 perltidy-tk
406 perltidy-tk --perl path/to/some.pl
407  
408 =head1 DESCRIPTION
409  
410 This is a GUI program. There are some videos on L<Perl Maven Tk|https://perlmaven.com/tk> explaining
411 how does this work and how was this built.
412  
413 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.
414  
415 ~/.perltidy-tk.json
416  
417 =head1 SEE ALSO
418  
419 L<Perl::Tidy>
420  
421 L<Tk>
422  
423 L<Tk::HyperText>
424  
425  
426 =head1 COPYRIGHT AND LICENSE
427  
428 Copyright (C) 2020 by L<Gabor Szabo|https://szabgab.com/>
429  
430 This library is free software; you can redistribute it and/or modify
431 it under the same terms as Perl itself, either Perl version 5.26.1 or,
432 at your option, any later version of Perl 5 you may have available.
433  
434 =cut
435  
↑ Summary